Skip to content

Commit 3da2c2b

Browse files
committed
fix(codex): emit modern env_key admission line on non-loopback inject
codex-cli 0.146+ documents env_key as the bearer auth form for custom model providers: it sends Authorization: Bearer $VAR and hard-errors on a missing variable instead of silently omitting the header. The server side already admits and substitutes this bearer (#1686 chain), so the injector was the stale half. Loopback Design B still emits no auth line; the #2061 orphan sub-table cleanup keeps handling historic env_http_headers configs. Closes #2073
1 parent a0eb776 commit 3da2c2b

14 files changed

Lines changed: 86 additions & 15 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 010 — #2073: injector env_key contract (codex-cli 0.146+)
2+
3+
## Contract facts (verified against openai/codex rust-v0.146.0 source, wp5 research lane)
4+
5+
- env_key reads the named env var and sends Authorization: Bearer <value>
6+
(model-provider-info/src/lib.rs#L263-281). Missing/empty var = HARD error
7+
(CodexErr::EnvVar), never an empty bearer.
8+
- env_key + requires_openai_auth = true is valid; env_key WINS for wire auth
9+
(first-party scoped auth disabled), requires_openai_auth keeps login/account
10+
UX (provider.rs#L153-174, auth.rs#L162-178).
11+
- env_http_headers is still honored in 0.146 (not removed), but bearer auth is
12+
the documented modern form.
13+
14+
## Server-side prerequisite (already landed)
15+
16+
#1686 chain (22d5492b2, acfedae0a, f848b4997): /v1/responses admits our
17+
admission secret via Authorization: Bearer and SUBSTITUTES stored main auth
18+
upstream (materializeCodexUpstreamAuth). So an env_key client works end to end
19+
on dev today. The injector is the only stale half (this issue).
20+
21+
## Change
22+
23+
src/codex/inject.ts buildProviderTableBlock (legacy/non-loopback mode only —
24+
loopback Design B emits no auth line at all):
25+
26+
- BEFORE: env_http_headers = { "x-opencodex-api-key" = "OPENCODEX_API_AUTH_TOKEN" }
27+
- AFTER: env_key = "OPENCODEX_API_AUTH_TOKEN"
28+
29+
requires_openai_auth = true stays (login UX; env_key wins wire auth).
30+
Sub-table strip logic from #2061 (env_http_headers orphan cleanup) STAYS — it
31+
cleans historic configs regenerated by the app. stripExistingModelProvider
32+
already removes the whole marker-owned block, so old env_http_headers lines
33+
from prior injections are replaced on next inject (idempotent).
34+
35+
## Behavior deltas (accepted)
36+
37+
- Missing OPENCODEX_API_AUTH_TOKEN in the codex process env: was silent header
38+
omission -> 401; now a clear codex-side hard error. Better diagnosability;
39+
the unauthenticated-loopback-listener case already suppresses the auth line
40+
entirely (shouldInjectApiAuthHeader), so the known no-var environment never
41+
sees env_key.
42+
- The client's own ChatGPT bearer is no longer forwarded on this path (env_key
43+
replaces Authorization); the proxy substitutes stored main auth — exactly the
44+
#1686 design. Hand-configured env_http_headers remains honored by the runtime
45+
for users who want the legacy form.
46+
47+
## Tests
48+
49+
- tests/codex-inject.test.ts:44,194 — flip to env_key expectation + add a
50+
regression that env_http_headers is NOT emitted.
51+
- tests/loopback-listener-admission.test.ts:199 / integration:617 — extend the
52+
not-contains to env_key (loopback emits no auth line).
53+
- Keep #2061 sub-table strip tests untouched.
54+
55+
## Docs
56+
57+
- structure/02_config-and-codex-home.md provider-block sample: update line.
58+
- docs-site reference mentions of env_http_headers (if any) updated.
59+
60+
Verifier: bun test tests/codex-inject.test.ts tests/loopback-listener-admission.test.ts tests/loopback-listener-integration.test.ts + bun x tsc --noEmit.
61+

‎docs-site/src/content/docs/fr/guides/codex-integration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ name = "OpenCodex Proxy"
125125
base_url = "http://your-host:10100/v1"
126126
wire_api = "responses"
127127
requires_openai_auth = true
128-
env_http_headers = { "x-opencodex-api-key" = "OPENCODEX_API_AUTH_TOKEN" }
128+
env_key = "OPENCODEX_API_AUTH_TOKEN"
129129
# supports_websockets = true # only when config.websockets is true
130130
```
131131

‎docs-site/src/content/docs/guides/codex-integration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ name = "OpenCodex Proxy"
121121
base_url = "http://your-host:10100/v1"
122122
wire_api = "responses"
123123
requires_openai_auth = true
124-
env_http_headers = { "x-opencodex-api-key" = "OPENCODEX_API_AUTH_TOKEN" }
124+
env_key = "OPENCODEX_API_AUTH_TOKEN"
125125
# supports_websockets = true # only when config.websockets is true
126126
```
127127

‎docs-site/src/content/docs/ja/guides/codex-integration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ name = "OpenCodex Proxy"
8787
base_url = "http://your-host:10100/v1"
8888
wire_api = "responses"
8989
requires_openai_auth = true
90-
env_http_headers = { "x-opencodex-api-key" = "OPENCODEX_API_AUTH_TOKEN" }
90+
env_key = "OPENCODEX_API_AUTH_TOKEN"
9191
# supports_websockets = true # only when config.websockets is true
9292
```
9393

‎docs-site/src/content/docs/ko/guides/codex-integration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ name = "OpenCodex Proxy"
7979
base_url = "http://your-host:10100/v1"
8080
wire_api = "responses"
8181
requires_openai_auth = true
82-
env_http_headers = { "x-opencodex-api-key" = "OPENCODEX_API_AUTH_TOKEN" }
82+
env_key = "OPENCODEX_API_AUTH_TOKEN"
8383
# supports_websockets = true # only when config.websockets is true
8484
```
8585

‎docs-site/src/content/docs/ru/guides/codex-integration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ name = "OpenCodex Proxy"
127127
base_url = "http://your-host:10100/v1"
128128
wire_api = "responses"
129129
requires_openai_auth = true
130-
env_http_headers = { "x-opencodex-api-key" = "OPENCODEX_API_AUTH_TOKEN" }
130+
env_key = "OPENCODEX_API_AUTH_TOKEN"
131131
# supports_websockets = true # only when config.websockets is true
132132
```
133133

‎docs-site/src/content/docs/tr/guides/codex-integration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ name = "OpenCodex Proxy"
140140
base_url = "http://your-host:10100/v1"
141141
wire_api = "responses"
142142
requires_openai_auth = true
143-
env_http_headers = { "x-opencodex-api-key" = "OPENCODEX_API_AUTH_TOKEN" }
143+
env_key = "OPENCODEX_API_AUTH_TOKEN"
144144
# supports_websockets = true # yalnızca config.websockets true olduğunda
145145
```
146146

‎docs-site/src/content/docs/zh-cn/guides/codex-integration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ name = "OpenCodex Proxy"
115115
base_url = "http://your-host:10100/v1"
116116
wire_api = "responses"
117117
requires_openai_auth = true
118-
env_http_headers = { "x-opencodex-api-key" = "OPENCODEX_API_AUTH_TOKEN" }
118+
env_key = "OPENCODEX_API_AUTH_TOKEN"
119119
# supports_websockets = true # only when config.websockets is true
120120
```
121121

‎docs-site/src/content/docs/zh-tw/guides/codex-integration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ name = "OpenCodex Proxy"
113113
base_url = "http://your-host:10100/v1"
114114
wire_api = "responses"
115115
requires_openai_auth = true
116-
env_http_headers = { "x-opencodex-api-key" = "OPENCODEX_API_AUTH_TOKEN" }
116+
env_key = "OPENCODEX_API_AUTH_TOKEN"
117117
# supports_websockets = true # 僅當 config.websockets 為 true
118118
```
119119

‎src/codex/inject.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,13 @@ export function buildProviderTableBlock(
227227
"requires_openai_auth = true",
228228
];
229229
if (includeApiAuthHeader) {
230-
lines.push(
231-
'env_http_headers = { "x-opencodex-api-key" = "OPENCODEX_API_AUTH_TOKEN" }',
232-
);
230+
// codex-cli 0.146+ contract (#2073): env_key sends Authorization: Bearer $VAR and
231+
// hard-errors on a missing/empty variable instead of silently omitting auth. It
232+
// coexists with requires_openai_auth (env_key wins wire auth; the flag keeps the
233+
// login/account UX), and the server substitutes stored main auth for our admission
234+
// bearer (#1686), so the modern form is strictly better than the legacy
235+
// env_http_headers table this line used to emit.
236+
lines.push('env_key = "OPENCODEX_API_AUTH_TOKEN"');
233237
}
234238
if (supportsWebsockets) lines.push("supports_websockets = true");
235239
return lines.join("\n") + "\n";

0 commit comments

Comments
 (0)