mirror of
https://github.com/stablyai/orca.git
synced 2026-11-17 19:34:29 +00:00
fix(cloud): provider-policy audit counts injection/forward/response rules; dev resume-raw and echo-probe
egress-docs' probe 2 re-run through #803 found transformRuleCount always 0: Vercel's redacted shapes name header swaps injectionRules (session) next to forwardRules and responseRules. The audit now reports each kind separately (count + header names), so a scrubbed policy is checkable: 0 injection rules, response rules only. Two more dev-only operator routes so the probe needs no personal Vercel login: - POST …/resume-raw: resume the provider session from its stored record with no policy write of ours (the row is left as it was; dev throwaways only). - POST …/echo-probe?marker=orcaprobebogus…: one fixed request from inside the VM to httpbin.org or postman-echo.com carrying a placeholder header (placeholders only, validated); the answer is whether the echo contained the marker. No body comes back or is logged.
This commit is contained in:
@@ -93,7 +93,7 @@ export async function handleOperator(
|
||||
}
|
||||
// Dev only (probe 2, egress-docs): redacted facts about what Vercel holds for one sandbox, read with the
|
||||
// Worker's own token. Never a value or a body; anywhere but dev, these routes don't exist.
|
||||
const policyAudit = /^\/v1\/internal\/sandboxes\/([A-Za-z0-9_]+)\/(provider-policy-audit|provider-policy-scrub-test)$/.exec(path)
|
||||
const policyAudit = /^\/v1\/internal\/sandboxes\/([A-Za-z0-9_]+)\/(provider-policy-audit|provider-policy-scrub-test|resume-raw|echo-probe)$/.exec(path)
|
||||
if (policyAudit && life.config.environment === 'dev') {
|
||||
const row = await getSandbox(life.db, policyAudit[1]!)
|
||||
if (!row) {
|
||||
@@ -104,6 +104,36 @@ 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 })
|
||||
}
|
||||
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).
|
||||
const state = await life.provider.getSandbox(row.provider_name, { resume: true })
|
||||
console.log(JSON.stringify({ event: 'provider_resume_raw', sandboxId: row.id, sessionStatus: state?.session.status ?? null }))
|
||||
return Response.json({ sandboxId: row.id, sessionStatus: state?.session.status ?? null })
|
||||
}
|
||||
if (policyAudit[2] === 'echo-probe' && request.method === 'POST') {
|
||||
// Probe 2 only: one fixed request from inside the VM to an echo host, carrying a placeholder header; the
|
||||
// answer is whether the echoed request contained the marker (i.e. what the firewall swapped in). No body back.
|
||||
const body = EchoProbeBody.safeParse(await deps.readJson())
|
||||
const marker = new URL(request.url).searchParams.get('marker') ?? ''
|
||||
if (!body.success || marker.length < 12 || !marker.startsWith('orcaprobebogus')) {
|
||||
return Response.json({ error: 'invalid_request' }, { status: 400 })
|
||||
}
|
||||
const state = await life.provider.getSandbox(row.provider_name, { resume: false })
|
||||
if (!state || state.session.status !== 'running') {
|
||||
return Response.json({ error: 'not_running' }, { status: 409 })
|
||||
}
|
||||
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],
|
||||
timeoutMs: 30_000
|
||||
})
|
||||
const [status, count] = result.stdout.trim().split('\n')
|
||||
const markerFound = Number(count ?? '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 })
|
||||
}
|
||||
if (policyAudit[2] === 'provider-policy-audit' && request.method === 'GET') {
|
||||
const marker = new URL(request.url).searchParams.get('marker') ?? ''
|
||||
if (marker.length < 12 || !marker.startsWith('orcaprobebogus')) {
|
||||
@@ -250,8 +280,24 @@ export async function handleOperator(
|
||||
|
||||
const EgressModeBody = z.object({ mode: z.enum(['proxy', 'firewall']).nullable() })
|
||||
|
||||
/** Redacted facts about a raw provider body: rule counts and header NAMES only, plus a marker substring test. */
|
||||
export function policyFacts(body: string, marker: string): { hasNetworkPolicy: boolean; transformRuleCount: number; headerNames: string[]; markerFound: boolean } {
|
||||
/** Rule kinds in Vercel's redacted policy shapes (probe 2): injection = a header swap, forward, response. */
|
||||
const RULE_KINDS: Record<string, 'injection' | 'forward' | 'response'> = {
|
||||
injectionrules: 'injection',
|
||||
transformrules: 'injection',
|
||||
forwardrules: 'forward',
|
||||
responserules: 'response'
|
||||
}
|
||||
|
||||
type RuleFacts = { count: number; headerNames: string[] }
|
||||
|
||||
/**
|
||||
* Redacted facts about a raw provider body: per rule kind, a count and header NAMES only, plus a marker
|
||||
* substring test. A scrubbed policy reads as 0 injection rules and some response rules.
|
||||
*/
|
||||
export function policyFacts(
|
||||
body: string,
|
||||
marker: string
|
||||
): { hasNetworkPolicy: boolean; injectionRules: RuleFacts; forwardRules: RuleFacts; responseRules: RuleFacts; markerFound: boolean } {
|
||||
let parsed: unknown = null
|
||||
try {
|
||||
parsed = JSON.parse(body)
|
||||
@@ -259,14 +305,20 @@ export function policyFacts(body: string, marker: string): { hasNetworkPolicy: b
|
||||
// Facts from the substring test only.
|
||||
}
|
||||
let hasNetworkPolicy = false
|
||||
let transformRuleCount = 0
|
||||
const headerNames = new Set<string>()
|
||||
const walk = (value: unknown, key: string): void => {
|
||||
if (Array.isArray(value)) {
|
||||
if (/transform/i.test(key)) {
|
||||
transformRuleCount += value.length
|
||||
const kinds = { injection: { count: 0, names: new Set<string>() }, forward: { count: 0, names: new Set<string>() }, response: { count: 0, names: new Set<string>() } }
|
||||
const collectNames = (rule: unknown, into: Set<string>): void => {
|
||||
const names = (rule as { headerNames?: unknown } | null)?.headerNames
|
||||
if (Array.isArray(names)) {
|
||||
for (const header of names) {
|
||||
if (typeof header === 'string' && /^[A-Za-z0-9-]{1,64}$/.test(header)) {
|
||||
into.add(header.toLowerCase())
|
||||
}
|
||||
}
|
||||
value.forEach((item) => walk(item, key))
|
||||
}
|
||||
}
|
||||
const walk = (value: unknown): void => {
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach(walk)
|
||||
return
|
||||
}
|
||||
if (value && typeof value === 'object') {
|
||||
@@ -274,17 +326,23 @@ export function policyFacts(body: string, marker: string): { hasNetworkPolicy: b
|
||||
if (/^networkpolicy$/i.test(name)) {
|
||||
hasNetworkPolicy = true
|
||||
}
|
||||
if (/^headerNames$/i.test(name) && Array.isArray(child)) {
|
||||
for (const header of child) {
|
||||
if (typeof header === 'string' && /^[A-Za-z0-9-]{1,64}$/.test(header)) {
|
||||
headerNames.add(header.toLowerCase())
|
||||
}
|
||||
}
|
||||
const kind = RULE_KINDS[name.toLowerCase()]
|
||||
if (kind && Array.isArray(child)) {
|
||||
kinds[kind].count += child.length
|
||||
child.forEach((rule) => collectNames(rule, kinds[kind].names))
|
||||
}
|
||||
walk(child, name)
|
||||
walk(child)
|
||||
}
|
||||
}
|
||||
}
|
||||
walk(parsed, '')
|
||||
return { hasNetworkPolicy, transformRuleCount, headerNames: [...headerNames].sort(), markerFound: body.includes(marker) }
|
||||
walk(parsed)
|
||||
const facts = (kind: keyof typeof kinds): RuleFacts => ({ count: kinds[kind].count, headerNames: [...kinds[kind].names].sort() })
|
||||
return { hasNetworkPolicy, injectionRules: facts('injection'), forwardRules: facts('forward'), responseRules: facts('response'), markerFound: body.includes(marker) }
|
||||
}
|
||||
|
||||
const EchoProbeBody = z.object({
|
||||
host: z.enum(['httpbin.org', 'postman-echo.com']),
|
||||
header: z.string().regex(/^[A-Za-z0-9-]{1,64}$/),
|
||||
// A placeholder (ours are orca-ph-…, or Bearer orca-ph-…): never a value worth echoing.
|
||||
placeholder: z.string().regex(/^(Bearer |token )?orca-ph-(?:[a-z0-9-]{1,80}-)?sbx_[a-f0-9]{24}$/)
|
||||
})
|
||||
|
||||
@@ -53,8 +53,42 @@ describe('provider-policy-audit (dev only)', () => {
|
||||
})
|
||||
|
||||
describe('policyFacts', () => {
|
||||
it('counts transform rules and keeps header names only', () => {
|
||||
const body = JSON.stringify({ session: { networkPolicy: { transformRules: [{ domain: 'a.com', headerNames: ['Authorization', 'x-api-key'] }, { domain: 'b.com', headerNames: ['bad header'] }] } } })
|
||||
expect(policyFacts(body, MARKER)).toEqual({ hasNetworkPolicy: true, transformRuleCount: 2, headerNames: ['authorization', 'x-api-key'], markerFound: false })
|
||||
it('counts injection, forward and response rules separately, with header names per kind only', () => {
|
||||
const live = JSON.stringify({ session: { networkPolicy: { injectionRules: [{ domain: 'a.com', headerNames: ['Authorization', 'x-api-key'] }], forwardRules: [{ domain: 'b.com', headerNames: ['bad header'] }], responseRules: [{ domain: 'a.com', statusCode: 403 }] } } })
|
||||
expect(policyFacts(live, MARKER)).toEqual({
|
||||
hasNetworkPolicy: true,
|
||||
injectionRules: { count: 1, headerNames: ['authorization', 'x-api-key'] },
|
||||
forwardRules: { count: 1, headerNames: [] },
|
||||
responseRules: { count: 1, headerNames: [] },
|
||||
markerFound: false
|
||||
})
|
||||
// A scrubbed policy reads as no injection rules, response rules only.
|
||||
const scrubbed = JSON.stringify({ session: { networkPolicy: { responseRules: [{ domain: 'a.com', statusCode: 403 }, { domain: 'a.com', statusCode: 403 }] } } })
|
||||
expect(policyFacts(scrubbed, MARKER)).toMatchObject({ injectionRules: { count: 0 }, responseRules: { count: 2 } })
|
||||
})
|
||||
})
|
||||
|
||||
describe('resume-raw and echo-probe (dev only)', () => {
|
||||
it('echo-probe answers only whether the marker came back; placeholders only', 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
|
||||
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)
|
||||
})
|
||||
|
||||
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', {})
|
||||
expect((await h.call('POST', `/v1/sandboxes/${id}/suspend`, { bearer: mia })).status).toBe(200)
|
||||
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)
|
||||
expect(JSON.stringify(box().policy)).toBe(before)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user