From dca419d3fdb3e95b39730acb40b1f7ac1af6cb4e Mon Sep 17 00:00:00 2001 From: Neil Date: Sun, 15 Nov 2026 00:09:55 -0800 Subject: [PATCH] fix(cloud): CODEX-EGRESS-BLOCKED say so when chatgpt.com refuses the egress proxy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit chatgpt.com answers every request from Cloudflare Workers' egress IPs with a 403 "Unable to load site" HTML page, whatever the token (verified from a throwaway Worker; the same request from elsewhere gets a normal 401). Codex via the Orca account goes through the Worker egress proxy, so it got that page on /responses and /wham/usage: "Working…" without an answer, and /status had no limits. The proxy now recognises a provider edge's own refusal (403, text/html, server: cloudflare) on routes that define an answer for it, logs egress_upstream_blocked, and answers Codex in ChatGPT's error shape so Codex prints a plain message. The block says nothing about the credential: no status hook, so no refresh and no revoke. This doesn't make Codex work; routing chatgpt.com around the Worker is a separate decision. --- .../sandbox-control/src/account-providers.ts | 2 ++ .../sandbox-control/src/codex-credentials.ts | 10 ++++++ .../apps/sandbox-control/src/egress-proxy.ts | 24 ++++++++++++++ cloud/apps/sandbox-control/src/index.ts | 1 + .../sandbox-control/test/codex-broker.test.ts | 31 +++++++++++++++++++ 5 files changed, 68 insertions(+) diff --git a/cloud/apps/sandbox-control/src/account-providers.ts b/cloud/apps/sandbox-control/src/account-providers.ts index 771a2a59ffa..aa110528382 100644 --- a/cloud/apps/sandbox-control/src/account-providers.ts +++ b/cloud/apps/sandbox-control/src/account-providers.ts @@ -41,6 +41,8 @@ export type AccountProvider = { * normal sign-in message instead of a proxy error. */ signedOut?: { status: number; contentType: string; body: string } + /** The provider's edge refused the egress proxy itself (CODEX-EGRESS-BLOCKED): what the agent shows. */ + upstreamBlocked?: { status: number; contentType: string; body: string } } /** Anthropic's token counter: authenticated like Messages, free, and nothing is generated. */ diff --git a/cloud/apps/sandbox-control/src/codex-credentials.ts b/cloud/apps/sandbox-control/src/codex-credentials.ts index 06fcc044918..7065a8c8793 100644 --- a/cloud/apps/sandbox-control/src/codex-credentials.ts +++ b/cloud/apps/sandbox-control/src/codex-credentials.ts @@ -186,6 +186,16 @@ export const codexAccount: AccountProvider = { detail: 'Orca Cloud can’t use your ChatGPT sign-in here. Check it in Orca: Settings → Agent accounts.' }) }, + // CODEX-EGRESS-BLOCKED: chatgpt.com answers Cloudflare Workers' egress with a 403 "Unable to load + // site" page, whatever the token. Same shape as signedOut, so Codex prints it and stops. + upstreamBlocked: { + status: 403, + contentType: 'application/json', + body: JSON.stringify({ + detail: + 'Orca Cloud can’t reach ChatGPT with your Orca account right now: ChatGPT refused Orca’s network, not your sign-in. Use another agent for now.' + }) + }, credential: async (db, keyring, userId, now) => { const value = await readUsableAgentCredential(db, keyring, userId, 'codex', now) const tokens = value ? parseStoredCodex(value) : null diff --git a/cloud/apps/sandbox-control/src/egress-proxy.ts b/cloud/apps/sandbox-control/src/egress-proxy.ts index b99bde5f4a6..90bb86093f0 100644 --- a/cloud/apps/sandbox-control/src/egress-proxy.ts +++ b/cloud/apps/sandbox-control/src/egress-proxy.ts @@ -51,6 +51,21 @@ export type ProxiedRoute = { keepResponseFields?: readonly string[] /** Names the swapped credential in the proxy's log line (never its value). */ label?: string + /** + * CODEX-EGRESS-BLOCKED: what the client gets when the provider's own edge refused this proxy (a + * Cloudflare 403 HTML page for Workers' egress IPs) instead of answering the request. The raw page + * is never passed on: the client shows this message rather than an HTML dump or a retry loop. + */ + upstreamBlocked?: { status: number; contentType: string; body: string } +} + +/** A provider's edge refused the proxy itself: Cloudflare's 403 HTML page, not an API answer. */ +export function edgeBlocked(upstream: Response): boolean { + return ( + upstream.status === 403 && + (upstream.headers.get('content-type') ?? '').toLowerCase().startsWith('text/html') && + (upstream.headers.get('server') ?? '').toLowerCase() === 'cloudflare' + ) } export type EgressProxyDeps = { @@ -176,6 +191,15 @@ export async function handleEgressProxy(request: Request, deps: EgressProxyDeps) }) // Names only: which sandbox, host and credential were swapped, and what the provider answered. secretLog('egress_swap', { sandboxId: row.id, host, credential: route.label ?? route.header, status: upstream.status }, [credential]) + if (route.upstreamBlocked && edgeBlocked(upstream)) { + // The request never reached the provider: no status hook (it says nothing about the credential). + secretLog('egress_upstream_blocked', { sandboxId: row.id, host, credential: route.label ?? route.header }) + await upstream.body?.cancel().catch(() => undefined) + return new Response(route.upstreamBlocked.body, { + status: route.upstreamBlocked.status, + headers: { 'content-type': route.upstreamBlocked.contentType } + }) + } route.onUpstreamStatus?.(upstream.status, { method: request.method, path }) if (route.keepResponseFields) { return onlyFields(upstream, route.keepResponseFields) diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index d59ad114e81..87f6182fcc1 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -415,6 +415,7 @@ function egressRoutes(env: Env, deps: Deps): (row: SandboxRow, host: string) => placeholders: [provider.format(provider.placeholder(row))], label: `account:${agent}`, ...(provider.pathAllowed ? { pathAllowed: provider.pathAllowed } : {}), + ...(provider.upstreamBlocked ? { upstreamBlocked: provider.upstreamBlocked } : {}), credential: async () => { // The agent sees the provider's own "sign in again" answer, not a proxy error. const signedOut = (reason: string) => ({ deny: reason, ...(provider.signedOut ? { answer: provider.signedOut } : {}) }) diff --git a/cloud/apps/sandbox-control/test/codex-broker.test.ts b/cloud/apps/sandbox-control/test/codex-broker.test.ts index 06c8323de73..b6d925cfbf7 100644 --- a/cloud/apps/sandbox-control/test/codex-broker.test.ts +++ b/cloud/apps/sandbox-control/test/codex-broker.test.ts @@ -73,6 +73,8 @@ async function setup(overrides: Record = {}) { const upstream: { url: string; headers: Headers }[] = [] const tokenCalls: Record[] = [] const upstreamStatus = { value: 200 } + // CODEX-EGRESS-BLOCKED: chatgpt.com's edge answering Workers' egress (what dev saw, 2026-11-15). + const edgeBlock = { on: false } let tokenReply: () => Response = () => Response.json({ access_token: ACCESS_2, refresh_token: REFRESH_2, id_token: ID }) const h = await createHarness( @@ -82,6 +84,12 @@ async function setup(overrides: Record = {}) { credentials: { authorizationFor: async () => null }, egressFetch: async (input, init) => { upstream.push({ url: String(input), headers: new Headers(init?.headers) }) + if (edgeBlock.on) { + return new Response('Unable to load site', { + status: 403, + headers: { 'content-type': 'text/html; charset=UTF-8', server: 'cloudflare' } + }) + } return new Response('{}', { status: upstreamStatus.value }) }, codexTokenFetch: async (input, init) => { @@ -144,6 +152,7 @@ async function setup(overrides: Record = {}) { proxied, upstream, upstreamStatus, + edgeBlock, tokenCalls, signIn, refreshDeps: async () => ({ ...deps(), keyring: await deps().keyring }), @@ -460,3 +469,25 @@ describe('Codex lease refresher', () => { expect(await view()).toBe('none') }) }) + +describe('CODEX-EGRESS-BLOCKED: chatgpt.com refuses the proxy itself', () => { + it('answers with a message Codex prints, never the HTML page, and leaves the login alone', async () => { + const { h, as, signIn, proxied, upstream, edgeBlock, tokenCalls } = await setup() + const mia = await as('mia', 'acme', 'member') + await signIn(mia) + const created = await h.call('POST', '/v1/sandboxes', { body: { workspaceId: 'ws-1', machineSize: 'standard' }, bearer: mia }) + const id: string = created.body.sandbox.id + const auth = { authorization: `Bearer ${codexPlaceholder({ id })}` } + edgeBlock.on = true + const refreshesBefore = tokenCalls.length + const answer = await proxied(id, 'chatgpt.com', auth) + expect(upstream.at(-1)!.url).toBe('https://chatgpt.com/backend-api/codex/responses') + expect(answer.status).toBe(403) + expect(answer.body).not.toContain('