diff --git a/cloud/apps/sandbox-control/src/operator.ts b/cloud/apps/sandbox-control/src/operator.ts index 1cbd3ede154..eccdabd0d41 100644 --- a/cloud/apps/sandbox-control/src/operator.ts +++ b/cloud/apps/sandbox-control/src/operator.ts @@ -104,6 +104,11 @@ export async function handleOperator( console.log(JSON.stringify({ event: 'provider_policy_scrub_test', sandboxId: row.id, outcome: typeof outcome === 'string' ? outcome : 'failed' })) return Response.json({ sandboxId: row.id, outcome }) } + // resume-raw and echo-probe act on a workspace, so only on throwaway probe accounts (dev:egress-…), never a + // real dev user's workspace. + if ((policyAudit[2] === 'resume-raw' || policyAudit[2] === 'echo-probe') && !row.account_id.startsWith('dev:egress-')) { + return Response.json({ error: 'not_a_probe_account' }, { status: 403 }) + } if (policyAudit[2] === 'resume-raw' && request.method === 'POST') { // Probe 2 only: resume the provider session from its stored record with NO policy write of ours, so a probe // can see which value the resumed session swaps. The row is left as it was (dev throwaways only). @@ -126,13 +131,13 @@ export async function handleOperator( const url = body.data.host === 'httpbin.org' ? 'https://httpbin.org/headers' : 'https://postman-echo.com/headers' const result = await life.provider.runCommand(state.session.id, { command: 'bash', - args: ['-c', 'curl -s --max-time 20 -o /tmp/orca-echo-probe -w "%{http_code}" -H "$1: $2" "$3"; echo; grep -c -- "$4" /tmp/orca-echo-probe || true; rm -f /tmp/orca-echo-probe', 'echo-probe', body.data.header, body.data.placeholder, url, marker], + // Straight into grep -c: the echoed request (whatever the firewall swapped in) never touches the VM's disk. + args: ['-c', 'curl -s --max-time 20 -H "$1: $2" "$3" | grep -c -- "$4" || true', 'echo-probe', body.data.header, body.data.placeholder, url, marker], timeoutMs: 30_000 }) - const [status, count] = result.stdout.trim().split('\n') - const markerFound = Number(count ?? '0') > 0 + const markerFound = Number(result.stdout.trim().split('\n').at(-1) ?? '0') > 0 console.log(JSON.stringify({ event: 'provider_echo_probe', sandboxId: row.id, markerFound })) - return Response.json({ sandboxId: row.id, status: Number(status) || null, markerFound }) + return Response.json({ sandboxId: row.id, markerFound }) } if (policyAudit[2] === 'provider-policy-audit' && request.method === 'GET') { const marker = new URL(request.url).searchParams.get('marker') ?? '' diff --git a/cloud/apps/sandbox-control/test/provider-policy-audit.test.ts b/cloud/apps/sandbox-control/test/provider-policy-audit.test.ts index c5d656b33ce..31605afd677 100644 --- a/cloud/apps/sandbox-control/test/provider-policy-audit.test.ts +++ b/cloud/apps/sandbox-control/test/provider-policy-audit.test.ts @@ -6,7 +6,7 @@ import { createHarness } from './harness' const OP = 'o'.repeat(40) const MARKER = 'orcaprobebogus-0123456789' -async function setup(env: Record) { +async function setup(env: Record, account = 'acme') { const h = await createHarness({ SECRETS_KEK: Buffer.alloc(32, 7).toString('base64'), EGRESS_PROXY_URL: 'https://sandbox.test/v1/egress/proxy', @@ -15,12 +15,12 @@ async function setup(env: Record) { EGRESS_SWAP_MODE: 'firewall', ...env }) - const mia = await h.token('mia', 'acme', 'sandbox', {}) + const mia = await h.token('mia', account, 'sandbox', {}) await h.call('PUT', '/v1/secrets/personal/ANTHROPIC_API_KEY', { body: { value: `sk-ant-api03-${MARKER}` }, bearer: mia }) const created = await h.call('POST', '/v1/sandboxes', { bearer: mia, body: { workspaceId: 'ws-1', machineSize: 'standard' } }) const id: string = created.body.sandbox.id const box = () => [...h.vercel.boxes.values()].find((b) => b.params.tags.orcaSandbox === id)! - return { h, id, box } + return { h, id, box, mia } } describe('provider-policy-audit (dev only)', () => { @@ -68,24 +68,39 @@ describe('policyFacts', () => { }) }) -describe('resume-raw and echo-probe (dev only)', () => { - it('echo-probe answers only whether the marker came back; placeholders only', async () => { +describe('resume-raw and echo-probe (dev only, probe accounts only)', () => { + const probeAccount = async (h: Awaited>, id: string) => + h.env.SANDBOX_DB.prepare("UPDATE sandboxes SET account_id = 'dev:egress-probe-1' WHERE id = ?").bind(id).run() + + it('echo-probe answers only whether the marker came back; placeholders only; nothing written in the VM', async () => { const { h, id } = await setup({ SANDBOX_ENV: 'dev' }) - h.vercel.commandHandler = (params) => - params.command === 'bash' && params.args?.[1]?.includes('orca-echo-probe') ? { exitCode: 0, stdout: '200\n1\n', stderr: '' } : undefined + await probeAccount(h, id) + const scripts: string[] = [] + h.vercel.commandHandler = (params) => { + if (params.command === 'bash' && params.args?.[1]?.includes('grep -c')) { + scripts.push(params.args[1]!) + return { exitCode: 0, stdout: '1\n', stderr: '' } + } + return undefined + } const placeholder = `orca-ph-anthropic-api-key-${id}` const ok = await h.call('POST', `/v1/internal/sandboxes/${id}/echo-probe?marker=${MARKER}`, { bearer: OP, body: { host: 'httpbin.org', header: 'x-api-key', placeholder } }) - expect(ok.body).toEqual({ sandboxId: id, status: 200, markerFound: true }) - const value = await h.call('POST', `/v1/internal/sandboxes/${id}/echo-probe?marker=${MARKER}`, { bearer: OP, body: { host: 'httpbin.org', header: 'x-api-key', placeholder: 'sk-ant-real' } }) - expect(value.status).toBe(400) - const elsewhere = await h.call('POST', `/v1/internal/sandboxes/${id}/echo-probe?marker=${MARKER}`, { bearer: OP, body: { host: 'evil.example', header: 'x-api-key', placeholder } }) - expect(elsewhere.status).toBe(400) + expect(ok.body).toEqual({ sandboxId: id, markerFound: true }) + expect(scripts[0]).not.toMatch(/-o |> ?\/|tmp/) + expect((await h.call('POST', `/v1/internal/sandboxes/${id}/echo-probe?marker=${MARKER}`, { bearer: OP, body: { host: 'httpbin.org', header: 'x-api-key', placeholder: 'sk-ant-real' } })).status).toBe(400) + expect((await h.call('POST', `/v1/internal/sandboxes/${id}/echo-probe?marker=${MARKER}`, { bearer: OP, body: { host: 'evil.example', header: 'x-api-key', placeholder } })).status).toBe(400) + }) + + it('refuses a real dev user’s workspace', async () => { + const { h, id } = await setup({ SANDBOX_ENV: 'dev' }) + expect((await h.call('POST', `/v1/internal/sandboxes/${id}/resume-raw`, { bearer: OP })).status).toBe(403) + expect((await h.call('POST', `/v1/internal/sandboxes/${id}/echo-probe?marker=${MARKER}`, { bearer: OP, body: { host: 'httpbin.org', header: 'x-api-key', placeholder: `orca-ph-x-${id}` } })).status).toBe(403) }) it('resume-raw resumes the provider session without writing a policy', async () => { - const { h, id, box } = await setup({ SANDBOX_ENV: 'dev' }) - const mia = await h.token('mia', 'acme', 'sandbox', {}) + const { h, id, box, mia } = await setup({ SANDBOX_ENV: 'dev' }) expect((await h.call('POST', `/v1/sandboxes/${id}/suspend`, { bearer: mia })).status).toBe(200) + await probeAccount(h, id) const before = JSON.stringify(box().policy) const res = await h.call('POST', `/v1/internal/sandboxes/${id}/resume-raw`, { bearer: OP }) expect(res.body.sandboxId).toBe(id)