Files
opencode-manager/backend/test/services/sandbox/shell-wrapper.test.ts
T
Chris Scott 0215de9d92 fix(sandbox): bound lifecycle queue and harden install/backup paths
Bound the OpenCode supervisor lifecycle queue to one running plus one
pending operation so repeated restarts cannot stack unbounded HTTP
requests, while exempting stop() so shutdown is never dropped.

Reject enforcement backups that carry no recoverable removed sections,
matching the existing fail-closed handling of unparseable backups.

Install OpenCode through a private mktemp staging directory instead of
the predictable /tmp/opencode path.

Use named path/os imports in the files added by this branch.
2026-08-22 00:14:19 -04:00

78 lines
3.0 KiB
TypeScript

import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { promises as fs } from 'fs'
import { join } from 'path'
import { tmpdir } from 'os'
import { spawnSync } from 'child_process'
import { getWorkspacePath } from '@opencode-manager/shared/config/env'
import {
SANDBOX_SHELL_FILENAME,
getSandboxShellDir,
getSandboxShellPath,
getEnforcedSandboxShellPath,
installSandboxShell,
} from '../../../src/services/sandbox/shell-wrapper'
import { SANDBOX_UNAVAILABLE_PREFIX } from '../../../src/services/sandbox/command'
const FORBIDDEN_SHELL_BASENAMES = ['bash', 'zsh', 'sh', 'dash', 'ksh', 'fish', 'nu', 'pwsh', 'powershell', 'cmd']
describe('sandbox shell wrapper', () => {
let configHome: string
beforeEach(async () => {
configHome = await fs.mkdtemp(join(tmpdir(), 'ocm-shell-wrapper-'))
})
afterEach(async () => {
await fs.rm(configHome, { recursive: true, force: true })
})
it('derives the sandbox shell directory from the config home', () => {
expect(getSandboxShellDir(configHome)).toBe(join(configHome, 'ocm'))
})
it('derives the sandbox shell path from the config home', () => {
expect(getSandboxShellPath(configHome)).toBe(join(configHome, 'ocm', SANDBOX_SHELL_FILENAME))
})
it('derives the enforced sandbox shell path from the workspace config home', () => {
expect(getEnforcedSandboxShellPath()).toBe(join(getWorkspacePath(), '.config', 'ocm', SANDBOX_SHELL_FILENAME))
})
it('uses a sandbox shell basename outside the rc-file sourcing basenames so OpenCode never injects shell config', () => {
expect(FORBIDDEN_SHELL_BASENAMES).not.toContain(SANDBOX_SHELL_FILENAME)
})
it('installs an executable wrapper whose first line is a shebang targeting the runtime binary', async () => {
await installSandboxShell(configHome)
const wrapperPath = getSandboxShellPath(configHome)
const stat = await fs.stat(wrapperPath)
expect(stat.mode & 0o777).toBe(0o700)
const source = await fs.readFile(wrapperPath, 'utf-8')
const firstLine = source.split('\n')[0]!
expect(firstLine.startsWith('#!')).toBe(true)
expect(firstLine).toContain(process.execPath)
})
it('writes a wrapper that plans commands through the internal sandbox endpoint with the enforcement token', async () => {
await installSandboxShell(configHome)
const source = await fs.readFile(getSandboxShellPath(configHome), 'utf-8')
expect(source).toContain('/sandbox/command')
expect(source).toContain('OCM_INTERNAL_TOKEN')
expect(source).toContain(SANDBOX_UNAVAILABLE_PREFIX)
})
it('fail-closes to stderr with exit 1 when the sandbox is unavailable', async () => {
await installSandboxShell(configHome)
const result = spawnSync(getSandboxShellPath(configHome), [], { encoding: 'utf8' })
expect(result.status).toBe(1)
expect(result.stdout).toBe('')
expect(result.stderr).toContain(SANDBOX_UNAVAILABLE_PREFIX)
expect(result.stderr).toContain('interactive shell sessions are not available while sandbox enforcement is on')
})
})