Skip to content

Commit f848b49

Browse files
authored
Merge pull request #1861 from lidge-jun/codex/1686-admission-threading
fix(auth): serve env_key bearer admission on Direct by substituting stored main auth
2 parents e2ef24a + acfedae commit f848b49

5 files changed

Lines changed: 207 additions & 9 deletions

File tree

‎src/server/chat-completions.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
import { responseWithDeferredRequestLog } from "./relay";
3838
import { handleResponses } from "./responses";
3939
import type { AdmissionLease } from "../lib/admission";
40+
import type { DataPlaneAdmission } from "./auth-cors";
4041
import { tryClaimNativeMainProfileForTurn } from "../codex/native-main-admission";
4142
import {
4243
createTranslatorBudget,
@@ -64,7 +65,7 @@ export async function handleChatCompletions(
6465
req: Request,
6566
config: OcxConfig,
6667
logCtx: RequestLogContext,
67-
logIds?: { requestId: string; start: number; turnAdmissionLease?: AdmissionLease },
68+
logIds?: { requestId: string; start: number; turnAdmissionLease?: AdmissionLease; admission?: DataPlaneAdmission },
6869
): Promise<Response> {
6970
const translatorBudget = createTranslatorBudget();
7071
try {
@@ -83,7 +84,7 @@ async function handleChatCompletionsWithBudget(
8384
config: OcxConfig,
8485
logCtx: RequestLogContext,
8586
translatorBudget: TranslatorBudget,
86-
logIds?: { requestId: string; start: number; turnAdmissionLease?: AdmissionLease },
87+
logIds?: { requestId: string; start: number; turnAdmissionLease?: AdmissionLease; admission?: DataPlaneAdmission },
8788
): Promise<Response> {
8889
let chatBody: Rec;
8990
try {
@@ -251,6 +252,9 @@ async function handleChatCompletionsWithBudget(
251252
};
252253
const upstream = await handleResponses(internalReq, config, logCtx, {
253254
...(logIds?.turnAdmissionLease ? { turnAdmissionLease: logIds.turnAdmissionLease } : {}),
255+
// #1686: the Chat surface translates its body and replays here, so the admission fact has
256+
// to ride along or a bearer-admitted Chat caller would still be refused by Direct.
257+
...(logIds?.admission ? { admission: logIds.admission } : {}),
254258
abortSignal: req.signal,
255259
// Body is Responses-shaped by now, but the client spoke Chat Completions.
256260
inboundWire: "chat",

‎src/server/index.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server<W
10921092
return runAdmittedHttpTurn(req, policy, async turnAdmissionLease => {
10931093
let response: Response;
10941094
try {
1095-
response = await handleResponsesCompact(req, config, logCtx, turnAdmissionLease);
1095+
response = await handleResponsesCompact(req, config, logCtx, turnAdmissionLease, admission);
10961096
} catch {
10971097
response = formatErrorResponse(500, "server_error", "Unexpected compact request failure");
10981098
}
@@ -1214,6 +1214,7 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server<W
12141214
return runAdmittedHttpTurn(req, policy, async turnAdmissionLease => {
12151215
const response = await handleResponses(req, config, logCtx, {
12161216
turnAdmissionLease,
1217+
admission,
12171218
onRequestBodyRead: () => disableResponsesRequestTimeout(req, requestServer),
12181219
abortSignal: req.signal,
12191220
onFirstOutput: () => recordFirstOutput(logCtx, start),
@@ -1302,7 +1303,7 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server<W
13021303
inboundProtocol: "chat",
13031304
};
13041305
return runAdmittedHttpTurn(req, policy, async turnAdmissionLease => withCors(
1305-
await handleChatCompletions(req, config, logCtx, { requestId, start, turnAdmissionLease }),
1306+
await handleChatCompletions(req, config, logCtx, { requestId, start, turnAdmissionLease, admission }),
13061307
req,
13071308
config,
13081309
));
@@ -1569,6 +1570,7 @@ export function startServer(port?: number, deps: StartServerDeps = {}): Server<W
15691570
try {
15701571
let terminalRecorder: ((status: ResponsesTerminalStatus, httpStatusOverride?: number) => void) | undefined;
15711572
const response = await handleResponses(req, config, logCtx, {
1573+
...(wsAdmission ? { admission: wsAdmission } : {}),
15721574
forceEmptyResponseId: true,
15731575
inboundTransport: "websocket",
15741576
abortSignal: turnAbort.signal,

‎src/server/responses/compact.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ import {
5151
CodexPoolAuthenticationError,
5252
CodexThreadAffinityExpiredError,
5353
headersForCodexAuthContext,
54+
materializeCodexUpstreamAuth,
55+
CodexMainSubstitutionUnavailableError,
5456
isCodexAuthContextUsable,
5557
resolveCodexAuthContext,
5658
codexProbeLeaseId,
@@ -81,6 +83,7 @@ import {
8183
type UpstreamHostAdmissionLease,
8284
} from "../../codex/upstream-host-health";
8385
import { ForwardAdmissionCredentialError, validateForwardAdmissionCredential } from "../auth-cors";
86+
import type { DataPlaneAdmission } from "../auth-cors";
8487
import { listOpenAiForwardSidecarCandidates, resolveFirstUsableOpenAiSidecar, type ResolvedOpenAiForwardSidecar } from "../../providers/openai-sidecar";
8588
import { CODEX_FORWARD_BASE_URL, isCanonicalOpenAiForwardProvider, supportsNativeResponsesCompactEndpoint } from "../../providers/openai-tiers";
8689
import { slugsEquivalent } from "../../providers/slug-codec";
@@ -269,6 +272,7 @@ export async function handleResponsesCompact(
269272
config: OcxConfig,
270273
logCtx: RequestLogContext,
271274
turnAdmissionLease?: AdmissionLease,
275+
admission?: DataPlaneAdmission,
272276
): Promise<Response> {
273277
let body: unknown;
274278
try {
@@ -316,7 +320,10 @@ export async function handleResponsesCompact(
316320
logCtx.resolvedModel = route.modelId;
317321
}
318322

319-
if (route.codexAccountMode === "direct") {
323+
// #1686: a bearer-presented admission secret is one of ours, so the stored main credential
324+
// is substituted below instead of the caller bearer being forwarded.
325+
const substituteMainCredential = admission?.source === "bearer";
326+
if (route.codexAccountMode === "direct" && !substituteMainCredential) {
320327
try { validateForwardAdmissionCredential(req.headers, config); }
321328
catch (err) {
322329
if (err instanceof ForwardAdmissionCredentialError) return formatErrorResponse(401, "authentication_error", err.message);
@@ -364,7 +371,7 @@ export async function handleResponsesCompact(
364371
beginCodexAccountSelection: codexAccountSelectionForTurn(turnAdmissionLease),
365372
});
366373
logCtx.accountLogLabel = codexAuthContextLogLabel(authCtx, config);
367-
const selected = headersForCodexAuthContext(req.headers, authCtx);
374+
const selected = materializeCodexUpstreamAuth(req.headers, authCtx, { substituteMainCredential });
368375
compactProvider = applyCodexAuthContextToProvider(route.provider, authCtx, route.codexAccountMode);
369376
for (const name of FORWARD_HEADERS) {
370377
const value = selected.get(name);
@@ -671,7 +678,7 @@ export async function handleResponsesCompact(
671678
headers: internalHeaders,
672679
body: JSON.stringify(internalBody),
673680
});
674-
const response = await handleResponses(internalReq, config, logCtx, { abortSignal: req.signal, turnAdmissionLease });
681+
const response = await handleResponses(internalReq, config, logCtx, { abortSignal: req.signal, turnAdmissionLease, ...(admission ? { admission } : {}) });
675682
if (!response.ok) return response;
676683
let json: { output?: unknown[]; status?: unknown; error?: unknown };
677684
try {

‎src/server/responses/core.ts‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ import {
9191
CodexPoolAuthenticationError,
9292
CodexThreadAffinityExpiredError,
9393
headersForCodexAuthContext,
94+
materializeCodexUpstreamAuth,
95+
CodexMainSubstitutionUnavailableError,
9496
isCodexAuthContextUsable,
9597
resolveCodexAuthContext,
9698
codexProbeLeaseId,
@@ -114,6 +116,7 @@ import {
114116
prepareSameTarget429Wait,
115117
} from "../../lib/upstream-retry";
116118
import { ForwardAdmissionCredentialError, validateForwardAdmissionCredential } from "../auth-cors";
119+
import type { DataPlaneAdmission } from "../auth-cors";
117120
import { createTranslatorBudget, isTranslatorBudgetExceededError, type TranslatorBudget } from "../../lib/translator-budget";
118121
import { listOpenAiForwardSidecarCandidates, resolveFirstUsableOpenAiSidecar, type ResolvedOpenAiForwardSidecar } from "../../providers/openai-sidecar";
119122
import { isCanonicalOpenAiForwardProvider } from "../../providers/openai-tiers";
@@ -755,6 +758,15 @@ export interface ConsumedComboFailure {
755758

756759
export interface HandleResponsesOptions {
757760
turnAdmissionLease?: AdmissionLease;
761+
/**
762+
* How the caller proved data-plane admission (#1686).
763+
*
764+
* A bearer-presented admission secret is one of OUR OWN secrets, so a Direct turn must
765+
* SUBSTITUTE the stored main credential rather than forward it. Without this fact at the
766+
* decision point, Direct cannot tell an admission bearer from the user own ChatGPT bearer,
767+
* which is why it refused the whole env_key flow instead of serving it.
768+
*/
769+
admission?: DataPlaneAdmission;
758770
/** Called at most once after the complete client body is read and accepted for dispatch. */
759771
onRequestBodyRead?: () => void;
760772
forceEmptyResponseId?: boolean;
@@ -988,7 +1000,14 @@ async function resolveResponsesCodexAuth(
9881000
options: HandleResponsesOptions,
9891001
): Promise<ResponsesAuthResolution> {
9901002
try {
991-
if (route.codexAccountMode === "direct") validateForwardAdmissionCredential(req.headers, config);
1003+
// #1686: a caller that proved admission with a BEARER presented one of our own secrets.
1004+
// Refusing it here is what made the codex-cli `env_key` contract unusable against Direct.
1005+
// Admitting it is only safe because the stored main credential is substituted below, so
1006+
// the admission secret still never leaves this process.
1007+
const substituteMainCredential = options.admission?.source === "bearer";
1008+
if (route.codexAccountMode === "direct" && !substituteMainCredential) {
1009+
validateForwardAdmissionCredential(req.headers, config);
1010+
}
9921011
let authCtx: CodexAuthContext;
9931012
if (route.codexAccountMode) {
9941013
authCtx = await resolveCodexAuthContext(req.headers, config, route.codexAccountMode, {
@@ -1011,7 +1030,7 @@ async function resolveResponsesCodexAuth(
10111030
return {
10121031
ok: true,
10131032
authCtx,
1014-
headers: headersForCodexAuthContext(req.headers, authCtx),
1033+
headers: materializeCodexUpstreamAuth(req.headers, authCtx, { substituteMainCredential }),
10151034
};
10161035
} catch (err) {
10171036
if (err instanceof CodexAccountCooldownError) {
@@ -1045,6 +1064,13 @@ async function resolveResponsesCodexAuth(
10451064
if (err instanceof ForwardAdmissionCredentialError) {
10461065
return { ok: false, response: formatErrorResponse(401, "authentication_error", err.message) };
10471066
}
1067+
if (err instanceof CodexMainSubstitutionUnavailableError) {
1068+
// Fail BEFORE any upstream I/O. The alternative is forwarding the admission secret.
1069+
return {
1070+
ok: false,
1071+
response: formatErrorResponse(401, "authentication_error", "No usable Codex main credential to serve this request"),
1072+
};
1073+
}
10481074
throw err;
10491075
}
10501076
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import { saveConfig } from "../src/config";
6+
import { startServer } from "../src/server";
7+
import type { OcxConfig } from "../src/types";
8+
9+
/**
10+
* #1686 end to end: a Codex client injected with `env_key` presents the proxy admission
11+
* secret as `Authorization: Bearer`. Direct must ADMIT that request and substitute the
12+
* stored main credential; the admission secret must never appear upstream.
13+
*
14+
* The unit matrix in tests/codex-auth-context.test.ts proves the materializer in isolation.
15+
* It cannot prove the HTTP handler passes the presentation source down to it, which is the
16+
* half that was missing: the source was resolved at the door and dropped one frame later.
17+
*/
18+
19+
const originalFetch = globalThis.fetch;
20+
const previousOcxHome = process.env.OPENCODEX_HOME;
21+
const previousCodexHome = process.env.CODEX_HOME;
22+
const previousDataToken = process.env.OPENCODEX_API_AUTH_TOKEN;
23+
24+
let ocxHome = "";
25+
let codexHome = "";
26+
let upstreamAuth: Array<string | null> = [];
27+
28+
const ADMISSION_SECRET = "ocx_data_envkeysecret";
29+
30+
/** A JWT whose `exp` is far in the future, so the stored main token reads as live. */
31+
function liveJwt(): string {
32+
const payload = Buffer.from(JSON.stringify({ exp: Math.floor(Date.now() / 1000) + 86_400 })).toString("base64url");
33+
return `header.${payload}.signature`;
34+
}
35+
36+
function directConfig(): OcxConfig {
37+
return {
38+
port: 0,
39+
// Remote bind, so admission is actually required rather than loopback-waived.
40+
hostname: "0.0.0.0",
41+
defaultProvider: "openai",
42+
openaiProviderTierVersion: 2,
43+
providers: {
44+
openai: {
45+
adapter: "openai-responses",
46+
baseUrl: "https://chatgpt.com/backend-api/codex",
47+
authMode: "forward",
48+
codexAccountMode: "direct",
49+
defaultModel: "gpt-5.6-luna",
50+
},
51+
},
52+
apiKeys: [
53+
{ id: "env-key", name: "env_key", key: ADMISSION_SECRET, createdAt: "2026-08-16T00:00:00.000Z" },
54+
],
55+
} as OcxConfig;
56+
}
57+
58+
function writeStoredMain(accessToken: string): void {
59+
writeFileSync(
60+
join(codexHome, "auth.json"),
61+
JSON.stringify({ tokens: { access_token: accessToken, account_id: "stored_main_acc" } }),
62+
);
63+
}
64+
65+
beforeEach(() => {
66+
ocxHome = mkdtempSync(join(tmpdir(), "ocx-1686-home-"));
67+
codexHome = mkdtempSync(join(tmpdir(), "ocx-1686-codex-"));
68+
process.env.OPENCODEX_HOME = ocxHome;
69+
process.env.CODEX_HOME = codexHome;
70+
delete process.env.OPENCODEX_API_AUTH_TOKEN;
71+
upstreamAuth = [];
72+
globalThis.fetch = (async (input, init) => {
73+
const raw = input instanceof Request ? input.url : String(input);
74+
const url = new URL(raw);
75+
if (url.hostname === "chatgpt.com" || url.hostname === "api.openai.com") {
76+
const headers = new Headers(input instanceof Request ? input.headers : init?.headers);
77+
upstreamAuth.push(headers.get("authorization"));
78+
return Response.json({ id: "resp_1686", object: "response", status: "completed", output: [] });
79+
}
80+
return originalFetch(input, init);
81+
}) as typeof fetch;
82+
});
83+
84+
afterEach(() => {
85+
globalThis.fetch = originalFetch;
86+
if (previousOcxHome === undefined) delete process.env.OPENCODEX_HOME;
87+
else process.env.OPENCODEX_HOME = previousOcxHome;
88+
if (previousCodexHome === undefined) delete process.env.CODEX_HOME;
89+
else process.env.CODEX_HOME = previousCodexHome;
90+
if (previousDataToken === undefined) delete process.env.OPENCODEX_API_AUTH_TOKEN;
91+
else process.env.OPENCODEX_API_AUTH_TOKEN = previousDataToken;
92+
if (ocxHome) rmSync(ocxHome, { recursive: true, force: true });
93+
if (codexHome) rmSync(codexHome, { recursive: true, force: true });
94+
ocxHome = "";
95+
codexHome = "";
96+
});
97+
98+
async function postResponses(url: string | URL, authorization: string): Promise<Response> {
99+
return originalFetch(new URL("/v1/responses", url), {
100+
method: "POST",
101+
headers: { "content-type": "application/json", authorization },
102+
body: JSON.stringify({ model: "gpt-5.6-luna", input: "hi", stream: false }),
103+
});
104+
}
105+
106+
describe("#1686 env_key bearer admission reaches Direct with substitution", () => {
107+
test("an admission bearer is served and the stored main credential goes upstream", async () => {
108+
saveConfig(directConfig());
109+
const stored = liveJwt();
110+
writeStoredMain(stored);
111+
112+
const server = startServer(0);
113+
try {
114+
const response = await postResponses(server.url, `Bearer ${ADMISSION_SECRET}`);
115+
116+
// Before this change the same request answered 401: admission accepted the bearer at the
117+
// door, then Direct refused it because it could not tell it from a user's own credential.
118+
expect(response.status).toBe(200);
119+
expect(upstreamAuth).toEqual([`Bearer ${stored}`]);
120+
// The proof that matters: our own secret never reached the wire.
121+
expect(upstreamAuth.join("|")).not.toContain(ADMISSION_SECRET);
122+
} finally {
123+
await server.stop(true);
124+
}
125+
});
126+
127+
test("substitution fails closed before any upstream I/O when no main credential is stored", async () => {
128+
saveConfig(directConfig());
129+
writeFileSync(join(codexHome, "auth.json"), JSON.stringify({ tokens: {} }));
130+
131+
const server = startServer(0);
132+
try {
133+
const response = await postResponses(server.url, `Bearer ${ADMISSION_SECRET}`);
134+
135+
expect(response.status).toBe(401);
136+
// Falling through would have forwarded the admission secret, which is the leak the
137+
// forward guard exists to prevent. Nothing may reach an upstream on this path.
138+
expect(upstreamAuth).toHaveLength(0);
139+
} finally {
140+
await server.stop(true);
141+
}
142+
});
143+
144+
test("a foreign bearer is still Codex Direct passthrough, not admission", async () => {
145+
saveConfig(directConfig());
146+
writeStoredMain(liveJwt());
147+
148+
const server = startServer(0);
149+
try {
150+
// A real ChatGPT credential is NOT one of our secrets, so it must not be admitted as one.
151+
const response = await postResponses(server.url, "Bearer sk-user-chatgpt-token");
152+
expect(response.status).toBe(401);
153+
expect(upstreamAuth).toHaveLength(0);
154+
} finally {
155+
await server.stop(true);
156+
}
157+
});
158+
});
159+

0 commit comments

Comments
 (0)