mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
66 lines
2.5 KiB
TypeScript
66 lines
2.5 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|
import { promises as fs } from 'fs'
|
|
import path from 'path'
|
|
import os from 'os'
|
|
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(path.join(os.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(path.join(configHome, 'ocm'))
|
|
})
|
|
|
|
it('derives the sandbox shell path from the config home', () => {
|
|
expect(getSandboxShellPath(configHome)).toBe(path.join(configHome, 'ocm', SANDBOX_SHELL_FILENAME))
|
|
})
|
|
|
|
it('derives the enforced sandbox shell path from the workspace config home', () => {
|
|
expect(getEnforcedSandboxShellPath()).toBe(path.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)
|
|
})
|
|
})
|