feat(sandbox): route host-shell surfaces into microVM via shell wrapper

This commit is contained in:
Chris Scott committed 2026-08-14 18:26:21 -04:00
1 parent aa3fb48a62
commit ae117d6a78
57 files changed
+711 -784

No files matched your search

+2 -37
View File
@@ -206,6 +206,7 @@ describe('OpenCodeClient', () => {
it('honours an explicit host override instead of OPENCODE_HOST', async () => {
const originalFetch = globalThis.fetch
const originalHost = ENV.OPENCODE.HOST
Object.defineProperty(ENV.OPENCODE, 'HOST', { value: '192.168.1.10', configurable: true, writable: true })
let capturedUrl: URL | undefined
const fetchFn = async (input: URL | Request | string) => {
@@ -220,7 +221,7 @@ describe('OpenCodeClient', () => {
expect(capturedUrl?.origin).toBe('http://127.0.0.1:5551')
} finally {
Object.defineProperty(ENV.OPENCODE, 'HOST', { value: '127.0.0.1', configurable: true, writable: true })
Object.defineProperty(ENV.OPENCODE, 'HOST', { value: originalHost, configurable: true, writable: true })
Object.defineProperty(globalThis, 'fetch', { value: originalFetch, configurable: true, writable: true })
}
})
@@ -462,40 +463,4 @@ describe('OpenCodeClient', () => {
expect(result).toBe(false)
})
})
describe('startMcpAuth', () => {
it('should build path with encoded serverName and directory param', async () => {
const mockResponse = new Response(JSON.stringify({}), { status: 200 })
let capturedUrl: URL | undefined
const fetchFn = async (input: URL | Request | string) => {
capturedUrl = input instanceof URL ? input : new URL(input.toString())
return mockResponse
}
const client = new FetchOpenCodeClient({ baseUrl, basicAuth: '', fetchFn: fetchFn as unknown as typeof fetch })
const result = await client.startMcpAuth('foo bar', '/dir')
expect(capturedUrl?.pathname).toBe('/mcp/foo%20bar/auth')
expect(capturedUrl?.searchParams.get('directory')).toBe('/dir')
expect(result).toBeInstanceOf(Response)
})
})
describe('authenticateMcp', () => {
it('should build path with auth/authenticate suffix', async () => {
const mockResponse = new Response(JSON.stringify({}), { status: 200 })
let capturedUrl: URL | undefined
const fetchFn = async (input: URL | Request | string) => {
capturedUrl = input instanceof URL ? input : new URL(input.toString())
return mockResponse
}
const client = new FetchOpenCodeClient({ baseUrl, basicAuth: '', fetchFn: fetchFn as unknown as typeof fetch })
const result = await client.authenticateMcp('name', undefined)
expect(capturedUrl?.pathname).toBe('/mcp/name/auth/authenticate')
expect(capturedUrl?.searchParams.has('directory')).toBe(false)
expect(result).toBeInstanceOf(Response)
})
})
})
@@ -71,14 +71,6 @@ function createStubClient(
async deleteProviderAuth(_providerId: string) {
throw new Error('not used')
},
async startMcpAuth(_serverName: string, _directory?: string) {
throw new Error('not used')
},
async authenticateMcp(_serverName: string, _directory?: string) {
throw new Error('not used')
},
}
}
@@ -254,12 +246,6 @@ describe('patchConfigWithRecovery', () => {
async deleteProviderAuth(providerId: string) {
throw new Error('not used')
},
async startMcpAuth(serverName: string, directory?: string) {
throw new Error('not used')
},
async authenticateMcp(serverName: string, directory?: string) {
throw new Error('not used')
},
}
const config = { command: { review: 'test' } }
@@ -8,7 +8,6 @@ import {
isSandboxConfigMutation,
isSandboxMcpAdd,
isSandboxAuthWrite,
SANDBOX_BLOCKED_REASON_PREFIX,
SANDBOX_CONFIG_MUTATION_REASON_PREFIX,
} from '../../../src/services/opencode/proxy-policy'
@@ -19,17 +18,12 @@ describe('sandbox proxy policy', () => {
expect(decideSandboxProxyBlock(false, 'POST', '/session/s1/command')).toEqual({ blocked: false })
})
it('blocks the session shell endpoint when enforced', () => {
const decision = decideSandboxProxyBlock(true, 'POST', '/session/ses_1/shell')
expect(decision.blocked).toBe(true)
if (decision.blocked) {
expect(decision.reason).toContain(SANDBOX_BLOCKED_REASON_PREFIX)
expect(decision.reason).toContain('host process')
}
it('forwards the session shell endpoint when enforced', () => {
expect(decideSandboxProxyBlock(true, 'POST', '/session/ses_1/shell')).toEqual({ blocked: false })
})
it('blocks custom slash command execution when enforced', () => {
expect(decideSandboxProxyBlock(true, 'POST', '/session/ses_1/command').blocked).toBe(true)
it('forwards custom slash command execution when enforced', () => {
expect(decideSandboxProxyBlock(true, 'POST', '/session/ses_1/command')).toEqual({ blocked: false })
})
it('blocks PTY creation and connection when enforced', () => {
@@ -41,8 +35,8 @@ describe('sandbox proxy policy', () => {
it('treats /api-prefixed execution routes identically to unprefixed routes when enforced', () => {
expect(decideSandboxProxyBlock(true, 'POST', '/api/pty').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'GET', '/api/pty/p1/connect').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/api/session/ses_1/shell').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/api/session/ses_1/command').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/api/session/ses_1/shell')).toEqual({ blocked: false })
expect(decideSandboxProxyBlock(true, 'POST', '/api/session/ses_1/command')).toEqual({ blocked: false })
expect(decideSandboxProxyBlock(true, 'POST', '/api/p%74y').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'GET', '/api/session')).toEqual({ blocked: false })
expect(decideSandboxProxyBlock(true, 'GET', '/api/pty/p1')).toEqual({ blocked: false })
@@ -51,7 +45,7 @@ describe('sandbox proxy policy', () => {
it('fails closed on encoded separators inside the /api prefix when enforced', () => {
expect(decideSandboxProxyBlock(true, 'POST', '/api%2Fpty').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/api%2Fsession/ses_1/shell').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/api%2Fpty/p1/connect').blocked).toBe(true)
})
it('classifies /api-prefixed config, mcp, and auth writes identically when enforced', () => {
@@ -80,21 +74,21 @@ describe('sandbox proxy policy', () => {
})
it('blocks percent-encoded spellings of blocked routes when enforced', () => {
expect(decideSandboxProxyBlock(true, 'POST', '/session/ses_1/%73hell').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/session/ses_1/%73%68%65%6c%6c').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/session/ses_1/%63ommand').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/%70ty').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/pty/%70%31/connect').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/p%74y/p1/connect').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/p%74y').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'GET', '/p%74y/ses_1/connect').blocked).toBe(true)
})
it('fails closed on encoded separators, double-encoded, control, and malformed paths when enforced', () => {
expect(decideSandboxProxyBlock(true, 'POST', '/session/ses_1/shell%2F..').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/session/ses_1%2Fshell').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/session/ses_1/shell%5Cx').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/session/ses_1/%2573hell').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/session/ses_1/shell%00').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/session/ses_1/shell%zz').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/session/ses_1/%c0%ae').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/pty%2F..').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/pty%2Fp1').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/pty%5Cx').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/%2570ty').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/pty%00').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/pty%zz').blocked).toBe(true)
expect(decideSandboxProxyBlock(true, 'POST', '/p%c0%ae').blocked).toBe(true)
})
it('passes safely encoded non-execution paths through when enforced', () => {
@@ -0,0 +1,76 @@
import { describe, it, expect, afterEach } from 'vitest'
import { ENV } from '@opencode-manager/shared/config/env'
import { getOpenCodeUpstreamBaseUrl, resolveEffectiveServerHost } from '../../../src/services/opencode/upstream'
const originalHost = ENV.OPENCODE.HOST
function setHost(value: string): void {
Object.defineProperty(ENV.OPENCODE, 'HOST', { value, configurable: true, writable: true })
}
describe('getOpenCodeUpstreamBaseUrl', () => {
afterEach(() => {
setHost(originalHost)
})
it('leaves an IPv4 host unchanged', () => {
setHost('127.0.0.1')
expect(getOpenCodeUpstreamBaseUrl(false)).toBe(`http://127.0.0.1:${ENV.OPENCODE.PORT}`)
})
it('leaves a plain hostname unchanged', () => {
setHost('opencode.internal')
expect(getOpenCodeUpstreamBaseUrl(false)).toBe(`http://opencode.internal:${ENV.OPENCODE.PORT}`)
})
it('brackets a bare IPv6 host', () => {
setHost('::1')
expect(getOpenCodeUpstreamBaseUrl(false)).toBe(`http://[::1]:${ENV.OPENCODE.PORT}`)
})
it('does not double-bracket an already bracketed IPv6 host', () => {
setHost('[::1]')
expect(getOpenCodeUpstreamBaseUrl(false)).toBe(`http://[::1]:${ENV.OPENCODE.PORT}`)
})
it('normalizes a wildcard bind to loopback', () => {
setHost('0.0.0.0')
expect(getOpenCodeUpstreamBaseUrl(false)).toBe(`http://127.0.0.1:${ENV.OPENCODE.PORT}`)
})
it('forces loopback for a non-loopback host while enforcement is active', () => {
setHost('192.168.1.10')
expect(getOpenCodeUpstreamBaseUrl(true)).toBe(`http://127.0.0.1:${ENV.OPENCODE.PORT}`)
expect(getOpenCodeUpstreamBaseUrl(false)).toBe(`http://192.168.1.10:${ENV.OPENCODE.PORT}`)
})
it('keeps an IPv6 loopback host while enforcement is active', () => {
setHost('::1')
expect(getOpenCodeUpstreamBaseUrl(true)).toBe(`http://[::1]:${ENV.OPENCODE.PORT}`)
})
it('honours an explicit host override over the configured host', () => {
setHost('192.168.1.10')
expect(getOpenCodeUpstreamBaseUrl(false, '::1')).toBe(`http://[::1]:${ENV.OPENCODE.PORT}`)
expect(getOpenCodeUpstreamBaseUrl(false, () => '10.0.0.5')).toBe(`http://10.0.0.5:${ENV.OPENCODE.PORT}`)
})
})
describe('resolveEffectiveServerHost', () => {
afterEach(() => {
setHost(originalHost)
})
it('treats every loopback spelling as already bound to loopback', () => {
for (const host of ['localhost', '127.0.0.1', '::1', '::ffff:127.0.0.1', '127.1.2.3', '[::1]']) {
setHost(host)
expect(resolveEffectiveServerHost(true)).toBe(host)
}
})
it('rebinds a non-loopback host to loopback while enforcement is active', () => {
setHost('0.0.0.0')
expect(resolveEffectiveServerHost(true)).toBe('127.0.0.1')
expect(resolveEffectiveServerHost(false)).toBe('0.0.0.0')
})
})