|
5 | 5 | deriveCodexAccountInstallationId, |
6 | 6 | isolateCodexPoolAccountHeaders, |
7 | 7 | isolateCodexPoolClientMetadata, |
| 8 | + rewriteTurnMetadataInstallationId, |
8 | 9 | } from "../../src/codex/forward-transport-headers"; |
| 10 | +import { CodexInstallationIdUnavailableError } from "../../src/codex/installation-id"; |
9 | 11 | import type { OcxParsedRequest, OcxProviderConfig } from "../../src/types"; |
10 | 12 | import { withTestTranslatorBudget } from "../helpers/translator-budget"; |
11 | 13 |
|
@@ -226,6 +228,64 @@ describe("Codex metadata integrity", () => { |
226 | 228 | isolateCodexPoolAccountHeaders(copyHeaders, accountA.chatgptAccountId); |
227 | 229 | expect(copyHeaders.get("x-codex-installation-id")).toBe(idA); |
228 | 230 | }); |
| 231 | + test("pooled turn metadata preserves non-ASCII values in header-safe JSON", () => { |
| 232 | + const raw = String.raw`{"installation_id":"caller-installation","workspaces":["/tmp/\ubcd1\uc6d0/\ud83d\ude80"],"extra":{"note":"\ud55c\uae00 \ud83d\ude00"}}`; |
| 233 | + const incoming = new Headers({ "x-codex-turn-metadata": raw }); |
| 234 | + const headers = headersForCodexAuthContext(incoming, poolAuthContext); |
| 235 | + const rewritten = headers.get("x-codex-turn-metadata")!; |
| 236 | + expect(rewritten).toMatch(/^[\x00-\x7f]*$/); |
| 237 | + expect(JSON.parse(rewritten)).toEqual({ |
| 238 | + ...JSON.parse(raw), |
| 239 | + installation_id: deriveCodexAccountInstallationId(accountA.chatgptAccountId, "caller-installation"), |
| 240 | + }); |
| 241 | + const body = { client_metadata: { "x-codex-turn-metadata": raw } }; |
| 242 | + expect((isolateCodexPoolClientMetadata(body, accountA.chatgptAccountId) as typeof body) |
| 243 | + .client_metadata["x-codex-turn-metadata"]).toBe(rewritten); |
| 244 | + isolateCodexPoolAccountHeaders(headers, accountA.chatgptAccountId); |
| 245 | + expect(headers.get("x-codex-turn-metadata")).toBe(rewritten); |
| 246 | + }); |
| 247 | + test("pooled turn metadata escapes DEL so the header stays a valid field value", () => { |
| 248 | + const raw = String.raw`{"installation_id":"caller-installation","workspaces":["/tmp/a\u007fb/\ubcd1\u0001"]}`; |
| 249 | + const headers = headersForCodexAuthContext(new Headers({ "x-codex-turn-metadata": raw }), poolAuthContext); |
| 250 | + const rewritten = headers.get("x-codex-turn-metadata")!; |
| 251 | + expect(rewritten).toMatch(/^[\x20-\x7e\t]*$/); |
| 252 | + expect(JSON.parse(rewritten).workspaces).toEqual(["/tmp/a\u007fb/\ubcd1\u0001"]); |
| 253 | + expect(JSON.parse(rewritten).installation_id) |
| 254 | + .toBe(deriveCodexAccountInstallationId(accountA.chatgptAccountId, "caller-installation")); |
| 255 | + }); |
| 256 | + test("escaped turn metadata is accepted unchanged on re-entry and capped at 96 KiB", () => { |
| 257 | + const account = accountA.chatgptAccountId; |
| 258 | + const raw = JSON.stringify({ installation_id: "caller-installation", pad: "\u00e9".repeat(6000) }); |
| 259 | + const once = rewriteTurnMetadataInstallationId(raw, account); |
| 260 | + expect(once.length).toBeGreaterThan(32 * 1024); |
| 261 | + expect(rewriteTurnMetadataInstallationId(once, account)).toBe(once); |
| 262 | + const headers = headersForCodexAuthContext(new Headers({ "x-codex-turn-metadata": raw }), poolAuthContext); |
| 263 | + expect(headersForCodexAuthContext(headers, poolAuthContext).get("x-codex-turn-metadata")).toBe(once); |
| 264 | + // DEL expands 6x: 20,000 of them fit the 32 KiB canonical bound but exceed the escaped cap. |
| 265 | + const del = JSON.stringify({ installation_id: "caller-installation", pad: "\u007f".repeat(20_000) }); |
| 266 | + expect(() => rewriteTurnMetadataInstallationId(del, account)).toThrow(CodexInstallationIdUnavailableError); |
| 267 | + // Non-JSON values pass through unchanged, so they keep the raw 32 KiB bound. |
| 268 | + expect(() => rewriteTurnMetadataInstallationId("x".repeat(32 * 1024 + 1), account)).toThrow(CodexInstallationIdUnavailableError); |
| 269 | + }); |
| 270 | + for (const value of ["漢字", "a\u007fb"]) test(`equal installation IDs escape ${JSON.stringify(value)}`, () => { |
| 271 | + const account = accountA.chatgptAccountId; |
| 272 | + const installation_id = deriveCodexAccountInstallationId(account, "caller"); |
| 273 | + const raw = JSON.stringify({ installation_id, value }); |
| 274 | + const once = rewriteTurnMetadataInstallationId(raw, account); |
| 275 | + expect(once).toMatch(/^[\x20-\x7e]*$/); |
| 276 | + expect(JSON.parse(once)).toEqual({ installation_id, value }); |
| 277 | + expect(rewriteTurnMetadataInstallationId(once, account)).toBe(once); |
| 278 | + }); |
| 279 | + test("equal installation IDs use canonical and escaped size bounds", () => { |
| 280 | + const account = accountA.chatgptAccountId; |
| 281 | + const installation_id = deriveCodexAccountInstallationId(account, "caller"); |
| 282 | + const padded = ` ${" ".repeat(90_000)}${JSON.stringify({ installation_id })}`; |
| 283 | + expect(rewriteTurnMetadataInstallationId(padded, account)).toBe(JSON.stringify({ installation_id })); |
| 284 | + const oversized = JSON.stringify({ installation_id, pad: "a".repeat(32 * 1024) }); |
| 285 | + expect(() => rewriteTurnMetadataInstallationId(oversized, account)).toThrow(CodexInstallationIdUnavailableError); |
| 286 | + const del = JSON.stringify({ installation_id, pad: "\u007f".repeat(20_000) }); |
| 287 | + expect(() => rewriteTurnMetadataInstallationId(del, account)).toThrow(CodexInstallationIdUnavailableError); |
| 288 | + }); |
229 | 289 | test("Responses preserves caller User-Agent as a fallback in key and forward modes", async () => { |
230 | 290 | for (const provider of [ |
231 | 291 | { |
|
0 commit comments