Skip to content

Commit fc6bc25

Browse files
committed
fix(proxy): preserve Retry-After and redact tenant URLs in debug
Normalized empty-body passthrough errors keep a validated Retry-After with application/json. openai-chat diagnostics log only the host so Cloudflare account paths never appear.
1 parent fc3870a commit fc6bc25

3 files changed

Lines changed: 205 additions & 68 deletions

File tree

‎src/adapters/openai-chat.ts‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { AdapterEvent, OcxAssistantMessage, OcxContentPart, OcxMessage, Ocx
33
import { isAllowedToolChoice, modelInList, namespacedToolName, resolveToolChoiceWireName, toolAllowedByChoice } from "../types";
44
import { mapReasoningEffort, modelRecordValue } from "../reasoning-effort";
55
import { debugProviderDiagnostic } from "../lib/debug";
6+
import { isDebugEnabled } from "../lib/debug-settings";
67
import { redactSecretString } from "../lib/redact";
78
import { contentPartsToText } from "./image";
89
import { neutralizeIdentity } from "./identity";
@@ -589,17 +590,24 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
589590
if (hasCredential) headers["Authorization"] = `Bearer ${provider.apiKey}`;
590591
if (provider.headers) Object.assign(headers, provider.headers);
591592

592-
debugProviderDiagnostic("openai-chat", "request", {
593-
url,
594-
model: body.model,
595-
stream: parsed.stream,
596-
messageCount: Array.isArray(messages) ? messages.length : 0,
597-
toolCount: Array.isArray(tools) ? tools.length : 0,
598-
hasCredential,
599-
bodyBytes: new TextEncoder().encode(JSON.stringify(body)).length,
600-
});
593+
const bodyJson = JSON.stringify(body);
594+
// Never log pathname/query — tenant-scoped hosts (e.g. Cloudflare
595+
// /accounts/<account_id>/ai/v1) would otherwise leak account identifiers (#452).
596+
if (isDebugEnabled()) {
597+
let host = "upstream";
598+
try { host = new URL(url).host; } catch { /* keep fallback */ }
599+
debugProviderDiagnostic("openai-chat", "request", {
600+
host,
601+
model: body.model,
602+
stream: parsed.stream,
603+
messageCount: Array.isArray(messages) ? messages.length : 0,
604+
toolCount: Array.isArray(tools) ? tools.length : 0,
605+
hasCredential,
606+
bodyBytes: new TextEncoder().encode(bodyJson).length,
607+
});
608+
}
601609

602-
return { url, method: "POST", headers, body: JSON.stringify(body) };
610+
return { url, method: "POST", headers, body: bodyJson };
603611
},
604612

605613
async *parseStream(response: Response): AsyncGenerator<AdapterEvent> {

‎src/server/responses/passthrough-error.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
import { formatErrorResponse } from "../../bridge";
2+
import { parseRetryAfterMs } from "../../combos";
3+
4+
function sanitizedRetryAfter(value: string | null | undefined, now: number): string | undefined {
5+
const trimmed = value?.trim();
6+
if (!trimmed || trimmed.length > 128) return undefined;
7+
return parseRetryAfterMs(trimmed, now) !== undefined ? trimmed : undefined;
8+
}
29

310
/**
411
* Passthrough adapters historically relayed upstream non-2xx bodies verbatim.
@@ -8,16 +15,24 @@ import { formatErrorResponse } from "../../bridge";
815
* Non-empty bodies (including ChatGPT `{detail: ...}` account-model 400s and
916
* HTML/text errors) must keep their original bytes and headers so pool-retry
1017
* activation and client diagnostics stay honest.
18+
*
19+
* Normalized (empty-body) responses force `Content-Type: application/json` and
20+
* preserve a validated `Retry-After` so Responses clients and the chat-completions
21+
* / Claude bridges that copy that header keep correct backoff.
1122
*/
1223
export function formatPassthroughUpstreamError(
1324
status: number,
1425
bodyText: string,
1526
options?: {
1627
statusText?: string;
1728
headers?: Headers;
29+
now?: number;
1830
},
1931
): Response {
2032
const trimmed = bodyText.trim();
33+
const now = options?.now ?? Date.now();
34+
const retryAfter = sanitizedRetryAfter(options?.headers?.get("retry-after"), now);
35+
2136
if (trimmed) {
2237
return new Response(bodyText, {
2338
status,
@@ -26,9 +41,13 @@ export function formatPassthroughUpstreamError(
2641
});
2742
}
2843

29-
return formatErrorResponse(
44+
const response = formatErrorResponse(
3045
status,
3146
"upstream_error",
3247
`Provider error ${status}: (empty body)`,
3348
);
49+
const headers = new Headers(response.headers);
50+
headers.set("Content-Type", "application/json");
51+
if (retryAfter !== undefined) headers.set("Retry-After", retryAfter);
52+
return new Response(response.body, { status: response.status, headers });
3453
}

‎tests/issue-452-empty-503.test.ts‎

Lines changed: 167 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ describe("formatPassthroughUpstreamError (#452)", () => {
7272
expect(json.error?.message?.toLowerCase()).not.toBe("unknown error");
7373
});
7474

75+
test("empty body preserves validated Retry-After and forces application/json", async () => {
76+
const headers = new Headers({ "retry-after": "12", "x-other": "drop-me" });
77+
const response = formatPassthroughUpstreamError(429, "", { headers });
78+
expect(response.status).toBe(429);
79+
expect(response.headers.get("content-type")).toContain("application/json");
80+
expect(response.headers.get("retry-after")).toBe("12");
81+
expect(response.headers.get("x-other")).toBeNull();
82+
});
83+
84+
test("empty body drops invalid Retry-After values", async () => {
85+
for (const bad of ["", "nope", "-1", "0", "1e6", "not-a-delay"]) {
86+
const headers = new Headers({ "retry-after": bad });
87+
const response = formatPassthroughUpstreamError(503, "", { headers, now: Date.now() });
88+
expect(response.headers.get("retry-after")).toBeNull();
89+
}
90+
});
91+
7592
test("JSON with error.message is preserved for Codex", async () => {
7693
const body = JSON.stringify({ error: { message: "no healthy upstream", type: "server_error" } });
7794
const response = formatPassthroughUpstreamError(503, body);
@@ -90,70 +107,134 @@ describe("formatPassthroughUpstreamError (#452)", () => {
90107
});
91108
});
92109

110+
async function withPoolPassthrough(
111+
reply: (request: Request) => Response | Promise<Response>,
112+
run: (serverUrl: string) => Promise<void>,
113+
): Promise<void> {
114+
if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true, force: true });
115+
mkdirSync(TEST_DIR, { recursive: true });
116+
process.env.OPENCODEX_HOME = TEST_DIR;
117+
delete process.env.OPENCODEX_API_AUTH_TOKEN;
118+
clearCodexUpstreamHealth();
119+
clearThreadAccountMap();
120+
clearAccountQuota();
121+
clearAccountNeedsReauth("pool-a");
122+
123+
const upstream = Bun.serve({
124+
port: 0,
125+
fetch(request) {
126+
return reply(request);
127+
},
128+
});
129+
redirectCanonicalCodexTo(upstream.url.toString());
130+
131+
saveConfig({
132+
port: 0,
133+
defaultProvider: "openai",
134+
openaiProviderTierVersion: 2,
135+
providers: {
136+
openai: {
137+
adapter: "openai-responses",
138+
baseUrl: "https://chatgpt.com/backend-api/codex",
139+
authMode: "forward",
140+
codexAccountMode: "pool",
141+
},
142+
},
143+
codexAccounts: [
144+
{ id: "main", email: "[email protected]", isMain: true },
145+
{ id: "pool-a", email: "[email protected]", isMain: false, chatgptAccountId: "acct-pool-a" },
146+
],
147+
activeCodexAccountId: "pool-a",
148+
} as OcxConfig);
149+
saveCodexAccountCredential("pool-a", {
150+
accessToken: "pool-a-token",
151+
refreshToken: "pool-a-refresh",
152+
expiresAt: Date.now() + 10 * 60_000,
153+
chatgptAccountId: "acct-pool-a",
154+
});
155+
updateAccountQuota("pool-a", 10);
156+
157+
const server = startServer(0);
158+
try {
159+
await run(server.url.toString());
160+
} finally {
161+
await server.stop(true);
162+
await upstream.stop(true);
163+
}
164+
}
165+
93166
describe("passthrough empty 503 (#452)", () => {
94167
test("ChatGPT passthrough empty-body 503 becomes JSON Codex can parse", async () => {
95-
if (existsSync(TEST_DIR)) rmSync(TEST_DIR, { recursive: true, force: true });
96-
mkdirSync(TEST_DIR, { recursive: true });
97-
process.env.OPENCODEX_HOME = TEST_DIR;
98-
delete process.env.OPENCODEX_API_AUTH_TOKEN;
99-
clearCodexUpstreamHealth();
100-
clearThreadAccountMap();
101-
clearAccountQuota();
102-
clearAccountNeedsReauth("pool-a");
103-
104-
const upstream = Bun.serve({
105-
port: 0,
106-
fetch() {
107-
// Codex shows "Unknown error" when the body is empty — the historical passthrough bug.
108-
return new Response(null, { status: 503 });
168+
await withPoolPassthrough(
169+
() => new Response(null, { status: 503 }),
170+
async (serverUrl) => {
171+
const response = await originalGlobalFetch(new URL("/v1/responses", serverUrl), {
172+
method: "POST",
173+
headers: { "content-type": "application/json", authorization: "Bearer inbound-token" },
174+
body: JSON.stringify({ model: "gpt-5.6-sol", input: "hi", stream: false }),
175+
});
176+
expect(response.status).toBe(503);
177+
const text = await response.text();
178+
expect(text.trim().length).toBeGreaterThan(0);
179+
const json = JSON.parse(text) as { error?: { message?: string } };
180+
expect(typeof json.error?.message).toBe("string");
181+
expect(json.error!.message!.trim().length).toBeGreaterThan(0);
182+
expect(json.error!.message!.toLowerCase()).not.toBe("unknown error");
109183
},
110-
});
111-
redirectCanonicalCodexTo(upstream.url.toString());
184+
);
185+
});
112186

113-
saveConfig({
114-
port: 0,
115-
defaultProvider: "openai",
116-
openaiProviderTierVersion: 2,
117-
providers: {
118-
openai: {
119-
adapter: "openai-responses",
120-
baseUrl: "https://chatgpt.com/backend-api/codex",
121-
authMode: "forward",
122-
codexAccountMode: "pool",
187+
test("direct /v1/responses preserves Retry-After on empty-body 429 and 503", async () => {
188+
for (const status of [429, 503] as const) {
189+
await withPoolPassthrough(
190+
() => new Response(null, { status, headers: { "Retry-After": "1" } }),
191+
async (serverUrl) => {
192+
const response = await originalGlobalFetch(new URL("/v1/responses", serverUrl), {
193+
method: "POST",
194+
headers: { "content-type": "application/json", authorization: "Bearer inbound-token" },
195+
body: JSON.stringify({ model: "gpt-5.6-sol", input: "hi", stream: false }),
196+
});
197+
expect(response.status).toBe(status);
198+
expect(response.headers.get("content-type")).toContain("application/json");
199+
expect(response.headers.get("retry-after")).toBe("1");
123200
},
201+
);
202+
}
203+
});
204+
205+
test("direct /v1/responses drops invalid Retry-After on empty-body 503", async () => {
206+
await withPoolPassthrough(
207+
() => new Response(null, { status: 503, headers: { "Retry-After": "not-a-delay" } }),
208+
async (serverUrl) => {
209+
const response = await originalGlobalFetch(new URL("/v1/responses", serverUrl), {
210+
method: "POST",
211+
headers: { "content-type": "application/json", authorization: "Bearer inbound-token" },
212+
body: JSON.stringify({ model: "gpt-5.6-sol", input: "hi", stream: false }),
213+
});
214+
expect(response.status).toBe(503);
215+
expect(response.headers.get("retry-after")).toBeNull();
124216
},
125-
codexAccounts: [
126-
{ id: "main", email: "[email protected]", isMain: true },
127-
{ id: "pool-a", email: "[email protected]", isMain: false, chatgptAccountId: "acct-pool-a" },
128-
],
129-
activeCodexAccountId: "pool-a",
130-
} as OcxConfig);
131-
saveCodexAccountCredential("pool-a", {
132-
accessToken: "pool-a-token",
133-
refreshToken: "pool-a-refresh",
134-
expiresAt: Date.now() + 10 * 60_000,
135-
chatgptAccountId: "acct-pool-a",
136-
});
137-
updateAccountQuota("pool-a", 10);
217+
);
218+
});
138219

139-
const server = startServer(0);
140-
try {
141-
const response = await originalGlobalFetch(new URL("/v1/responses", server.url), {
142-
method: "POST",
143-
headers: { "content-type": "application/json", authorization: "Bearer inbound-token" },
144-
body: JSON.stringify({ model: "gpt-5.6-sol", input: "hi", stream: false }),
145-
});
146-
expect(response.status).toBe(503);
147-
const text = await response.text();
148-
expect(text.trim().length).toBeGreaterThan(0);
149-
const json = JSON.parse(text) as { error?: { message?: string } };
150-
expect(typeof json.error?.message).toBe("string");
151-
expect(json.error!.message!.trim().length).toBeGreaterThan(0);
152-
expect(json.error!.message!.toLowerCase()).not.toBe("unknown error");
153-
} finally {
154-
await server.stop(true);
155-
await upstream.stop(true);
156-
}
220+
test("/v1/chat/completions forwards Retry-After from empty-body upstream 429", async () => {
221+
await withPoolPassthrough(
222+
() => new Response(null, { status: 429, headers: { "Retry-After": "3" } }),
223+
async (serverUrl) => {
224+
const response = await originalGlobalFetch(new URL("/v1/chat/completions", serverUrl), {
225+
method: "POST",
226+
headers: { "content-type": "application/json", authorization: "Bearer inbound-token" },
227+
body: JSON.stringify({
228+
model: "gpt-5.6-sol",
229+
messages: [{ role: "user", content: "hi" }],
230+
stream: false,
231+
}),
232+
});
233+
expect(response.status).toBe(429);
234+
expect(response.headers.get("content-type")).toContain("application/json");
235+
expect(response.headers.get("retry-after")).toBe("3");
236+
},
237+
);
157238
});
158239
});
159240

@@ -217,6 +298,35 @@ describe("openai-chat provider debug (#452)", () => {
217298
adapter.buildRequest(parsed);
218299
const lines = getDebugLogEntries().map(e => e.line);
219300
expect(lines.some(line => line.includes("[ocx:openai-chat:request]"))).toBe(true);
301+
expect(lines.join("\n")).toContain('"host":"api.xiaomimimo.com"');
220302
expect(lines.join("\n")).not.toContain("sk-secret-xiaomi-key");
303+
expect(lines.join("\n")).not.toContain("/v1/chat/completions");
304+
});
305+
306+
test("tenant-scoped baseUrl logs host only — account id never appears", () => {
307+
process.env.OCX_DEBUG = "1";
308+
resetDebugLogBufferForTests();
309+
const accountId = "cf-account-abc123secret";
310+
const adapter = createOpenAIChatAdapter({
311+
adapter: "openai-chat",
312+
baseUrl: `https://api.cloudflare.com/client/v4/accounts/${accountId}/ai/v1`,
313+
apiKey: "cf-key-should-not-appear",
314+
});
315+
const parsed = {
316+
modelId: "@cf/meta/llama-3.3-70b-instruct-fp8-fast",
317+
stream: false,
318+
context: {
319+
messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }],
320+
},
321+
options: {},
322+
} as unknown as OcxParsedRequest;
323+
adapter.buildRequest(parsed);
324+
const joined = getDebugLogEntries().map(e => e.line).join("\n");
325+
expect(joined).toContain("[ocx:openai-chat:request]");
326+
expect(joined).toContain('"host":"api.cloudflare.com"');
327+
expect(joined).not.toContain(accountId);
328+
expect(joined).not.toContain("/accounts/");
329+
expect(joined).not.toContain("cf-key-should-not-appear");
330+
expect(joined).not.toContain("/ai/v1");
221331
});
222332
});

0 commit comments

Comments
 (0)