feat(sandbox): add KVM-backed agent sandboxing via microsandbox

This commit is contained in:
Chris Scott committed 2026-08-13 16:14:16 -04:00
1 parent 1d477bdee4
commit aa3fb48a62
80 files changed
+17304 -204

No files matched your search

@@ -49,6 +49,18 @@ describe('entrypoint library wiring', () => {
expect(warnIndex).toBeLessThan(chownIndex)
})
it('grants node access to /dev/kvm before dropping privileges', () => {
const entrypoint = read(entrypointPath)
expect(entrypoint).toMatch(/^grant_kvm_access\(\) \{/m)
const alignIndex = entrypoint.indexOf('if ! align_container_user node; then')
const grantCallIndex = entrypoint.indexOf('if ! grant_kvm_access; then')
const runuserIndex = entrypoint.indexOf('exec runuser -u node')
expect(grantCallIndex, 'entrypoint must call grant_kvm_access').toBeGreaterThan(-1)
expect(grantCallIndex).toBeGreaterThan(alignIndex)
expect(runuserIndex).toBeGreaterThan(grantCallIndex)
expect(entrypoint.slice(grantCallIndex, grantCallIndex + 60)).toMatch(/exit 1/)
})
it('does not re-chown /app when ids change', () => {
const entrypoint = read(entrypointPath)
@@ -66,6 +78,55 @@ describe('entrypoint library wiring', () => {
})
})
describe('microsandbox runtime install', () => {
const dockerfile = read(dockerfilePath)
it('declares MICROSANDBOX_VERSION next to the other tool args', () => {
expect(dockerfile).toMatch(/ARG MICROSANDBOX_VERSION=0\.6\.8/)
})
it('resolves the release URL from MICROSANDBOX_VERSION, not only the log message', () => {
const microsandboxRun = dockerfile.slice(dockerfile.indexOf('Installing microsandbox='), dockerfile.indexOf('msb --version'))
expect(microsandboxRun).toMatch(/releases\/download\/\$\{MSB_VERSION\}/)
expect(microsandboxRun).toMatch(/MSB_VERSION="v\$\{MICROSANDBOX_VERSION\}"/)
})
it('pins a tested version and avoids unauthenticated GitHub API lookups', () => {
const microsandboxRun = dockerfile.slice(dockerfile.indexOf('Installing microsandbox='), dockerfile.indexOf('msb --version'))
expect(microsandboxRun).not.toMatch(/releases\/latest\/download/)
expect(microsandboxRun).not.toContain('install.microsandbox.dev')
expect(microsandboxRun).not.toMatch(/api\.github\.com/)
})
it('passes the same MICROSANDBOX_VERSION from the docker-build workflow', () => {
const workflow = read(join(repoRoot, '.github/workflows/docker-build.yml'))
expect(workflow).toContain('MICROSANDBOX_VERSION=0.6.8')
expect(workflow).toContain('MICROSANDBOX_VERSION=${{ steps.versions.outputs.microsandbox }}')
})
it('downloads the arch-specific bundle and verifies its checksum', () => {
const microsandboxRun = dockerfile.slice(dockerfile.indexOf('Installing microsandbox='), dockerfile.indexOf('msb --version'))
expect(microsandboxRun).toMatch(/MSB_BUNDLE="microsandbox-linux-\$\{MSB_TARGET\}\.tar\.gz"/)
expect(microsandboxRun).toMatch(/MSB_TARGET="x86_64"/)
expect(microsandboxRun).toMatch(/MSB_TARGET="aarch64"/)
expect(microsandboxRun).toMatch(/checksums\.sha256/)
expect(microsandboxRun).toMatch(/sha256sum -c --quiet/)
})
it('installs msb and libkrunfw under /opt/microsandbox with the runtime symlinks', () => {
expect(dockerfile).toContain('/opt/microsandbox/bin/msb')
expect(dockerfile).toContain('/usr/local/bin/msb')
expect(dockerfile).toContain('/opt/microsandbox/lib/libkrunfw.so')
expect(dockerfile).toMatch(/chmod -R a\+rX \/opt\/microsandbox/)
expect(dockerfile).toMatch(/msb --version/)
})
it('keeps the state directory writable by the node user', () => {
expect(dockerfile).toMatch(/mkdir -p \/workspace \/app\/data \/home\/node\/\.cache \/home\/node\/\.opencode \/home\/node\/\.microsandbox/)
expect(dockerfile).toMatch(/chown -R node:node \/workspace \/app\/data \/home\/node/)
})
})
describe('workspace ownership configuration', () => {
it('exposes PUID and PGID environment defaults in docker-compose.yml', () => {
const compose = read(composePath)
@@ -124,6 +185,28 @@ describe('workspace ownership configuration', () => {
})
})
describe('sandbox compose overlay', () => {
const overlayPath = join(repoRoot, 'docker-compose.sandbox.yml')
const overlay = read(overlayPath)
it('defaults SANDBOX_EXEC_USER from PUID so the guest identity tracks the workspace owner', () => {
expect(overlay).toContain('- SANDBOX_EXEC_USER=${SANDBOX_EXEC_USER:-${PUID:-1000}}')
})
it('keeps the base compose free of KVM and privileged flags', () => {
const compose = read(composePath)
expect(compose).not.toContain('privileged')
expect(compose).not.toContain('/dev/kvm')
})
it('grants KVM and persists microsandbox state only in the overlay', () => {
expect(overlay).toContain('privileged: true')
expect(overlay).toContain('"/dev/kvm:/dev/kvm"')
expect(overlay).toContain('microsandbox-data:/home/node/.microsandbox')
expect(overlay).toMatch(/^volumes:\n(?:.*\n)*?\s+microsandbox-data:/m)
})
})
describe('named-volume migration recipe', () => {
const runMigrationShell = (src: string, dst: string) => {
const scriptDir = mkdtempSync(join(tmpdir(), 'migrate-script-'))