Files
orca/cloud/apps/sandbox-control/test/codex-firewall-paths.test.ts
T
Neil 1376ac6e92 feat(cloud): CODEX-FIREWALL-SWAP Codex's hot calls swap at the sandbox firewall
chatgpt.com answers the sandbox's own egress (probed on a throwaway sandbox: a bogus token gets
ChatGPT's JSON 401, and the firewall's header transform, path and method matchers all work), so the
Codex account token no longer needs the Worker + Cloud Run relay hop for Codex's hot calls.

- Policy compiler: claims scoped by path (exact/prefix, query ignored) and method; transforms can
  set extra headers; path-only forwards (no placeholder header).
- With CODEX_EGRESS_SWAP=firewall (dev on), the owner's Codex token is swapped at the firewall for
  POST /backend-api/codex/responses(/compact), GET /backend-api/codex/models and GET
  /backend-api/wham/usage, with chatgpt-account-id. Exact paths only: a prefix rule was walked out of
  live with an encoded dot segment that the upstream normalises. Every other placeholder call still
  goes through the egress proxy, whose pinning rejects that; then the 403.
- Same eligibility as the proxy (owner's own account, not shared, provider on, token usable); none:
  proxy route only, which answers the signed-out cases. Every policy write path carries the swap
  (boot, create, git refresh, env refresh, egress hosts via rewriteLivePolicy); a wake rewrites it.
- Central refresh (already single-flight per person, compare-and-swap) now pushes a refreshed token
  to the person's running sandboxes' policies, including refreshes the egress 401 path starts.
- Codex refreshing its placeholder login (POST auth.openai.com/oauth/token) is forwarded by path to
  the proxy, which answers with the placeholder again (never a real token) and refreshes centrally.
- VM: the placeholder auth.json's last_refresh is 2099-01-01 (Codex never refreshes it itself);
  the orca_chatgpt provider (supports_websockets = false) is also written to /etc/codex/config.toml
  (best effort, passwordless sudo); [features] plugins = false while the Orca account applies,
  unless the person's own config has a [features] table (CODEX-PS-CALLS: ~170 catalog calls per
  session start).
- Off by default: CODEX_EGRESS_SWAP unset keeps every call on the proxy (and the relay).
2026-11-15 02:34:31 -08:00

46 lines
2.3 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { CODEX_FIREWALL_PATHS, codexPathAllowed } from '../src/codex-paths'
import { compileNetworkPolicy } from '../src/network-policy'
describe('CODEX-FIREWALL-SWAP paths', () => {
it('every firewall path is one the proxy allows too, and none is a prefix', () => {
for (const { exact, methods } of CODEX_FIREWALL_PATHS) {
expect(exact.startsWith('/backend-api/')).toBe(true)
for (const method of methods) {
expect(codexPathAllowed(method, exact)).toBe(true)
}
}
expect(CODEX_FIREWALL_PATHS.every((path) => !('startsWith' in path))).toBe(true)
})
})
describe('compileNetworkPolicy: path-scoped claims and path forwards', () => {
it('one claim per placeholder and path, extra headers alongside the swap, then the forward, then the 403', () => {
const policy = compileNetworkPolicy({
mode: 'open',
credentials: [
{
host: 'chatgpt.com',
header: 'Authorization',
placeholders: ['Bearer PH'],
paths: [{ exact: '/a', methods: ['POST'] }, { startsWith: '/b/' }],
via: 'transform',
value: 'Bearer REAL',
extraHeaders: { 'chatgpt-account-id': 'acct' }
},
{ host: 'chatgpt.com', header: 'authorization', placeholders: ['Bearer PH'], via: 'forward', forwardURL: 'https://p.example/x' }
],
pathForwards: [{ host: 'auth.openai.com', path: { exact: '/oauth/token', methods: ['POST'] }, forwardURL: 'https://p.example/x' }]
})
const ph = [{ key: { exact: 'authorization' }, value: { exact: 'Bearer PH' } }]
expect(policy.allow['chatgpt.com']).toEqual([
{ match: { headers: ph, path: { exact: '/a' }, method: ['POST'] }, transform: [{ headers: { 'chatgpt-account-id': 'acct', authorization: 'Bearer REAL' } }] },
{ match: { headers: ph, path: { startsWith: '/b/' } }, transform: [{ headers: { 'chatgpt-account-id': 'acct', authorization: 'Bearer REAL' } }] },
{ match: { headers: ph }, forwardURL: 'https://p.example/x' },
{ response: { statusCode: 403, body: 'orca-egress-denied', contentType: 'text/plain' } }
])
// A path forward adds no 403: the rest of auth.openai.com is untouched.
expect(policy.allow['auth.openai.com']).toEqual([{ match: { path: { exact: '/oauth/token' }, method: ['POST'] }, forwardURL: 'https://p.example/x' }])
})
})