diff --git a/src/runner/proxy.ts b/src/runner/proxy.ts index 647dc17..7a4ca16 100644 --- a/src/runner/proxy.ts +++ b/src/runner/proxy.ts @@ -41,6 +41,30 @@ async function responseFailure(upstream: Response): Promise { return providerFailure(errorBody, upstream.status); } +/** Only for an absent Content-Type on a successful Responses request. + * Inspect a bounded prefix on a clone; keep the original stream untouched. + * The full stream still goes through safeEvents and must reach response.completed. + */ +async function hasEventStreamPrefix(upstream: Response): Promise { + const reader = upstream.clone().body?.getReader(); + if (!reader) return false; + const parts: Uint8Array[] = []; + let size = 0; + try { + while (size < 64) { + const next = await reader.read(); + if (next.done) break; + if (!next.value.length) continue; + const count = Math.min(next.value.length, 64 - size); + parts.push(next.value.subarray(0, count)); + size += count; + if (size >= 6) break; + } + return /^(?:event:|data:)/.test(Buffer.concat(parts).toString('utf8')); + } catch { return false; } + finally { void reader.cancel().catch(() => {}); } +} + export async function inferenceProxy(request: IncomingMessage, response: ServerResponse, token: string, fetcher: typeof fetch = fetch, enforceSelection: (body: Record) => void = () => {}, report: (failure: ExecutionFailure) => void = () => {}): Promise { const disconnected = new AbortController(); let stage: 'validation' | 'transport' | 'stream' = 'validation'; @@ -79,7 +103,13 @@ export async function inferenceProxy(request: IncomingMessage, response: ServerR body: JSON.stringify(body), redirect: 'error', signal: AbortSignal.any([AbortSignal.timeout(20 * 60_000), disconnected.signal]) }); const metadata = { upstream_status: upstream.status, content_type_category: contentTypeCategory(upstream.headers.get('content-type')), body_present: upstream.body !== null }; - if (!upstream.ok || !upstream.body || metadata.content_type_category !== 'event-stream') { + // A documented Codex SSE success can omit Content-Type. In that one case, +// require an SSE-looking prefix before handing the unmodified stream to +// safeEvents, which enforces bounded frames and terminal response.completed. +// Never accept JSON/HTML/other explicit media types as streaming responses. + const headerlessSse = upstream.ok && upstream.body && metadata.content_type_category === 'missing' + ? await hasEventStreamPrefix(upstream) : false; + if (!upstream.ok || !upstream.body || (metadata.content_type_category !== 'event-stream' && !headerlessSse)) { report({ ...await responseFailure(upstream), ...metadata, code: upstream.ok ? 'provider_stream_content_type_invalid' : 'provider_http_failed' }); response.writeHead(upstream.ok ? 403 : upstream.status, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' }); response.end(upstream.ok ? '{"error":"inference_proxy_denied_or_unavailable"}' : '{"error":{"message":"OpenAI inference unavailable; check authentication, entitlement or usage allowance.","type":"codexbot_inference_error"}}'); diff --git a/tests/proxy.test.ts b/tests/proxy.test.ts index d49371b..f79bde8 100644 --- a/tests/proxy.test.ts +++ b/tests/proxy.test.ts @@ -64,6 +64,7 @@ await test('upstream 2xx requires a real SSE body; only classified response meta const cases: { status: number; type: string | null; body: string | null; category: string; approvedCode?: boolean }[] = [ { status: 200, type: 'text/event-stream', body: 'data: {"type":"response.completed"}\n\n', category: 'event-stream' }, { status: 201, type: 'Text/Event-Stream; charset=utf-8', body: 'data: {"type":"response.completed"}\n\n', category: 'event-stream' }, + { status: 200, type: null, body: 'event: response.created\ndata: {"type":"response.created"}\n\nevent: response.completed\ndata: {"type":"response.completed"}\n\n', category: 'missing' }, { status: 200, type: 'application/json; charset=utf-8', body: json, category: 'json', approvedCode: true }, { status: 202, type: 'application/problem+json', body: json, category: 'json', approvedCode: true }, { status: 200, type: 'application/json', body: JSON.stringify({ status: 'completed', output: [{ type: 'message', content: [{ type: 'output_text', text: secret }] }] }), category: 'json' }, @@ -91,7 +92,7 @@ await test('upstream 2xx requires a real SSE body; only classified response meta server.listen(0, '127.0.0.1'); await once(server, 'listening'); const address = server.address(); assert.ok(address && typeof address !== 'string'); try { const result = await fetch(`http://127.0.0.1:${address.port}/v1/responses`, { method: 'POST', body: JSON.stringify({ model: 'fixture', store: false, stream: true, input: [] }) }); - const output = await result.text(), valid = input.category === 'event-stream' && input.body !== null; + const output = await result.text(), valid = (input.category === 'event-stream' || (input.category === 'missing' && input.body?.startsWith('event:'))) && input.body !== null; assert.equal(result.status, valid ? input.status : 403); if (valid) { assert.match(output, /response.completed/); assert.deepEqual(diagnostics, []); } else assert.deepEqual(diagnostics, [{ component: 'provider', code: 'provider_stream_content_type_invalid', upstream_status: input.status,