mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
553 lines
23 KiB
TypeScript
553 lines
23 KiB
TypeScript
import { describe, expect, it, vi, afterEach } from 'vitest'
|
|
import { spawnSync } from 'child_process'
|
|
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import path from 'node:path'
|
|
import { getReposPath } from '@opencode-manager/shared/config/env'
|
|
import { sessionSandboxName } from '../../../src/services/sandbox/command'
|
|
import {
|
|
SANDBOX_MAX_FORWARDED_GIT_CONFIGS,
|
|
SANDBOX_SHELL_ENV_FORWARD,
|
|
SANDBOX_SHELL_ENV_NAME,
|
|
SANDBOX_SHELL_ENV_WORKDIR,
|
|
} from '../../../src/services/sandbox/shell-shim'
|
|
|
|
const SANDBOX_NAME = sessionSandboxName('ses_shim_test')
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
function writeArgvCapturingFakeMsb(msbPath: string, captureFile: string): void {
|
|
writeFileSync(
|
|
msbPath,
|
|
[
|
|
'#!/bin/sh',
|
|
`printf '%s\\0' "$0" "$@" > "${captureFile}"`,
|
|
'payload=""',
|
|
'prev=""',
|
|
'for arg in "$@"; do',
|
|
' if [ "$prev" = "-c" ]; then payload="$arg"; fi',
|
|
' prev="$arg"',
|
|
'done',
|
|
'sh -c "$payload"',
|
|
].join('\n'),
|
|
{ mode: 0o755 },
|
|
)
|
|
}
|
|
|
|
function sandboxShellEnv(overrides: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
|
|
const env = { ...process.env }
|
|
const inherited = [
|
|
'GIT_TERMINAL_PROMPT',
|
|
'GIT_CONFIG_COUNT',
|
|
...Array.from({ length: SANDBOX_MAX_FORWARDED_GIT_CONFIGS }, (_, index) => [
|
|
`GIT_CONFIG_KEY_${index}`,
|
|
`GIT_CONFIG_VALUE_${index}`,
|
|
]).flat(),
|
|
'GIT_AUTHOR_NAME',
|
|
'GIT_AUTHOR_EMAIL',
|
|
'GIT_COMMITTER_NAME',
|
|
'GIT_COMMITTER_EMAIL',
|
|
'GH_TOKEN',
|
|
'GITHUB_TOKEN',
|
|
SANDBOX_SHELL_ENV_FORWARD,
|
|
]
|
|
for (const name of inherited) delete env[name]
|
|
return { ...env, ...overrides }
|
|
}
|
|
|
|
describe('sandbox shell shim', () => {
|
|
it('execs msb through the shim with the working directory and a byte-for-byte guest payload', async () => {
|
|
const fakeBin = mkdtempSync(path.join(tmpdir(), 'ocm-shim-msb-'))
|
|
const configHome = mkdtempSync(path.join(tmpdir(), 'ocm-shim-config-'))
|
|
const msbPath = path.join(fakeBin, 'msb')
|
|
const captureFile = path.join(fakeBin, 'argv.txt')
|
|
writeArgvCapturingFakeMsb(msbPath, captureFile)
|
|
const originalMsbPath = process.env.MSB_PATH
|
|
process.env.MSB_PATH = msbPath
|
|
try {
|
|
vi.resetModules()
|
|
const shimMod = await import('../../../src/services/sandbox/shell-shim')
|
|
const commandMod = await import('../../../src/services/sandbox/command')
|
|
commandMod.overrideSandboxExecutableTrustValidator(() => true)
|
|
const { ENV } = await import('@opencode-manager/shared/config/env')
|
|
const shimPath = await shimMod.ensureSandboxShellShim(configHome)
|
|
|
|
const directory = path.join(getReposPath(), 'foo')
|
|
const command = 'echo "it\'s a test" && echo line2 | tr a-z A-Z\necho after-newline'
|
|
const result = spawnSync(shimPath, ['-c', command], {
|
|
encoding: 'utf8',
|
|
env: sandboxShellEnv({ [SANDBOX_SHELL_ENV_WORKDIR]: directory, [SANDBOX_SHELL_ENV_NAME]: SANDBOX_NAME }),
|
|
})
|
|
expect(result.status).toBe(0)
|
|
expect(result.stdout).toBe("it's a test\nLINE2\nafter-newline\n")
|
|
|
|
const argv = readFileSync(captureFile, 'utf8').split('\0').filter((element) => element !== '')
|
|
expect(argv).toEqual([
|
|
msbPath,
|
|
'exec',
|
|
SANDBOX_NAME,
|
|
'--no-tty',
|
|
'-q',
|
|
'-u',
|
|
commandMod.resolveSandboxExecUser(),
|
|
'-w',
|
|
directory,
|
|
'--timeout',
|
|
`${Math.floor(ENV.SANDBOX.EXEC_TIMEOUT_MS / 1000)}s`,
|
|
'--',
|
|
'sh',
|
|
'-c',
|
|
command,
|
|
])
|
|
} finally {
|
|
if (originalMsbPath === undefined) {
|
|
delete process.env.MSB_PATH
|
|
} else {
|
|
process.env.MSB_PATH = originalMsbPath
|
|
}
|
|
rmSync(fakeBin, { recursive: true, force: true })
|
|
rmSync(configHome, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('forwards exactly the plan-approved names as -e pairs, ahead of the command', async () => {
|
|
const fakeBin = mkdtempSync(path.join(tmpdir(), 'ocm-shim-msb-'))
|
|
const configHome = mkdtempSync(path.join(tmpdir(), 'ocm-shim-config-'))
|
|
const msbPath = path.join(fakeBin, 'msb')
|
|
const captureFile = path.join(fakeBin, 'argv.txt')
|
|
writeArgvCapturingFakeMsb(msbPath, captureFile)
|
|
const originalMsbPath = process.env.MSB_PATH
|
|
process.env.MSB_PATH = msbPath
|
|
try {
|
|
vi.resetModules()
|
|
const shimMod = await import('../../../src/services/sandbox/shell-shim')
|
|
const commandMod = await import('../../../src/services/sandbox/command')
|
|
commandMod.overrideSandboxExecutableTrustValidator(() => true)
|
|
const shimPath = await shimMod.ensureSandboxShellShim(configHome)
|
|
|
|
const directory = path.join(getReposPath(), 'foo')
|
|
const extraheaderValue = 'AUTHORIZATION: basic eDphYmMgZGVm'
|
|
const result = spawnSync(shimPath, ['-c', 'echo -e sentinel'], {
|
|
encoding: 'utf8',
|
|
env: sandboxShellEnv({
|
|
[SANDBOX_SHELL_ENV_WORKDIR]: directory,
|
|
[SANDBOX_SHELL_ENV_NAME]: SANDBOX_NAME,
|
|
[SANDBOX_SHELL_ENV_FORWARD]: 'GIT_CONFIG_COUNT GIT_CONFIG_KEY_0 GIT_CONFIG_VALUE_0',
|
|
GIT_CONFIG_COUNT: '1',
|
|
GIT_CONFIG_KEY_0: 'http.https://github.com/.extraheader',
|
|
GIT_CONFIG_VALUE_0: extraheaderValue,
|
|
GH_TOKEN: 'inherited-must-not-be-forwarded',
|
|
GIT_AUTHOR_NAME: 'Inherited Author',
|
|
OCM_INTERNAL_TOKEN: 'must-not-be-forwarded',
|
|
}),
|
|
})
|
|
expect(result.status).toBe(0)
|
|
|
|
const argv = readFileSync(captureFile, 'utf8').split('\0').filter((element) => element !== '')
|
|
|
|
expect(argv).toContain('-e')
|
|
expect(argv).toContain('GIT_CONFIG_COUNT=1')
|
|
expect(argv).toContain('GIT_CONFIG_KEY_0=http.https://github.com/.extraheader')
|
|
expect(argv).toContain(`GIT_CONFIG_VALUE_0=${extraheaderValue}`)
|
|
|
|
const separatorIndex = argv.indexOf('--')
|
|
expect(argv.slice(separatorIndex)).toEqual(['--', 'sh', '-c', 'echo -e sentinel'])
|
|
expect(argv.slice(0, separatorIndex).filter((element) => element === '-e')).toHaveLength(3)
|
|
|
|
expect(argv.some((element) => element.startsWith('GH_TOKEN='))).toBe(false)
|
|
expect(argv.some((element) => element.startsWith('GIT_AUTHOR_NAME='))).toBe(false)
|
|
expect(argv.some((element) => element.startsWith('OCM_INTERNAL_TOKEN='))).toBe(false)
|
|
} finally {
|
|
if (originalMsbPath === undefined) {
|
|
delete process.env.MSB_PATH
|
|
} else {
|
|
process.env.MSB_PATH = originalMsbPath
|
|
}
|
|
rmSync(fakeBin, { recursive: true, force: true })
|
|
rmSync(configHome, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('forwards nothing but the plan-approved names when the spawn env carries git and GitHub credentials', async () => {
|
|
const fakeBin = mkdtempSync(path.join(tmpdir(), 'ocm-shim-msb-'))
|
|
const configHome = mkdtempSync(path.join(tmpdir(), 'ocm-shim-config-'))
|
|
const msbPath = path.join(fakeBin, 'msb')
|
|
const captureFile = path.join(fakeBin, 'argv.txt')
|
|
writeArgvCapturingFakeMsb(msbPath, captureFile)
|
|
const originalMsbPath = process.env.MSB_PATH
|
|
process.env.MSB_PATH = msbPath
|
|
try {
|
|
vi.resetModules()
|
|
const shimMod = await import('../../../src/services/sandbox/shell-shim')
|
|
const commandMod = await import('../../../src/services/sandbox/command')
|
|
commandMod.overrideSandboxExecutableTrustValidator(() => true)
|
|
const shimPath = await shimMod.ensureSandboxShellShim(configHome)
|
|
|
|
const directory = path.join(getReposPath(), 'foo')
|
|
const result = spawnSync(shimPath, ['-c', 'echo sentinel'], {
|
|
encoding: 'utf8',
|
|
env: sandboxShellEnv({
|
|
[SANDBOX_SHELL_ENV_WORKDIR]: directory,
|
|
[SANDBOX_SHELL_ENV_NAME]: SANDBOX_NAME,
|
|
GH_TOKEN: 'inherited-gh-token',
|
|
GITHUB_TOKEN: 'inherited-github-token',
|
|
GIT_CONFIG_COUNT: '1',
|
|
GIT_CONFIG_KEY_0: 'http.https://github.com/.extraheader',
|
|
GIT_CONFIG_VALUE_0: 'AUTHORIZATION: basic inherited',
|
|
GIT_AUTHOR_NAME: 'Inherited Author',
|
|
GIT_COMMITTER_EMAIL: 'inherited@example.com',
|
|
}),
|
|
})
|
|
expect(result.status).toBe(0)
|
|
|
|
const argv = readFileSync(captureFile, 'utf8').split('\0').filter((element) => element !== '')
|
|
|
|
expect(argv.filter((element) => element === '-e')).toHaveLength(0)
|
|
expect(argv).not.toContain('GH_TOKEN=inherited-gh-token')
|
|
expect(argv).not.toContain('GITHUB_TOKEN=inherited-github-token')
|
|
expect(argv).not.toContain('GIT_CONFIG_COUNT=1')
|
|
} finally {
|
|
if (originalMsbPath === undefined) {
|
|
delete process.env.MSB_PATH
|
|
} else {
|
|
process.env.MSB_PATH = originalMsbPath
|
|
}
|
|
rmSync(fakeBin, { recursive: true, force: true })
|
|
rmSync(configHome, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('skips reserved and invalid forward names without executing anything', async () => {
|
|
const fakeBin = mkdtempSync(path.join(tmpdir(), 'ocm-shim-names-'))
|
|
const configHome = mkdtempSync(path.join(tmpdir(), 'ocm-shim-config-'))
|
|
const msbPath = path.join(fakeBin, 'msb')
|
|
const captureFile = path.join(fakeBin, 'argv.txt')
|
|
writeArgvCapturingFakeMsb(msbPath, captureFile)
|
|
const originalMsbPath = process.env.MSB_PATH
|
|
process.env.MSB_PATH = msbPath
|
|
try {
|
|
vi.resetModules()
|
|
const shimMod = await import('../../../src/services/sandbox/shell-shim')
|
|
const commandMod = await import('../../../src/services/sandbox/command')
|
|
commandMod.overrideSandboxExecutableTrustValidator(() => true)
|
|
const shimPath = await shimMod.ensureSandboxShellShim(configHome)
|
|
|
|
const directory = path.join(getReposPath(), 'foo')
|
|
const marker = path.join(fakeBin, 'injected.marker')
|
|
const substitutionName = `$(touch ${marker})`
|
|
const result = spawnSync(shimPath, ['-c', 'echo invalid-sentinel'], {
|
|
encoding: 'utf8',
|
|
env: sandboxShellEnv({
|
|
[SANDBOX_SHELL_ENV_WORKDIR]: directory,
|
|
[SANDBOX_SHELL_ENV_NAME]: SANDBOX_NAME,
|
|
[SANDBOX_SHELL_ENV_FORWARD]: `1BAD A-B ${substitutionName} OCM_INTERNAL_TOKEN MSB_SECRET GOOD_ONE`,
|
|
'1BAD': 'leading-digit',
|
|
'A-B': 'dash',
|
|
[substitutionName]: 'substitution',
|
|
OCM_INTERNAL_TOKEN: 'reserved-ocm',
|
|
MSB_SECRET: 'reserved-msb',
|
|
GOOD_ONE: 'forwarded',
|
|
}),
|
|
})
|
|
expect(result.status).toBe(0)
|
|
expect(result.stdout).toBe('invalid-sentinel\n')
|
|
expect(existsSync(marker)).toBe(false)
|
|
|
|
const argv = readFileSync(captureFile, 'utf8').split('\0').filter((element) => element !== '')
|
|
expect(argv.filter((element) => element === '-e')).toHaveLength(1)
|
|
expect(argv).toContain('GOOD_ONE=forwarded')
|
|
expect(argv.some((element) => element.startsWith('OCM_INTERNAL_TOKEN='))).toBe(false)
|
|
expect(argv.some((element) => element.startsWith('MSB_SECRET='))).toBe(false)
|
|
expect(argv.some((element) => element.startsWith('1BAD='))).toBe(false)
|
|
expect(argv.some((element) => element.startsWith('A-B='))).toBe(false)
|
|
} finally {
|
|
if (originalMsbPath === undefined) {
|
|
delete process.env.MSB_PATH
|
|
} else {
|
|
process.env.MSB_PATH = originalMsbPath
|
|
}
|
|
rmSync(fakeBin, { recursive: true, force: true })
|
|
rmSync(configHome, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('passes forwarded values with spaces, quotes, dollars, and newlines verbatim as single arguments', async () => {
|
|
const fakeBin = mkdtempSync(path.join(tmpdir(), 'ocm-shim-values-'))
|
|
const configHome = mkdtempSync(path.join(tmpdir(), 'ocm-shim-config-'))
|
|
const msbPath = path.join(fakeBin, 'msb')
|
|
const captureFile = path.join(fakeBin, 'argv.txt')
|
|
writeArgvCapturingFakeMsb(msbPath, captureFile)
|
|
const originalMsbPath = process.env.MSB_PATH
|
|
process.env.MSB_PATH = msbPath
|
|
try {
|
|
vi.resetModules()
|
|
const shimMod = await import('../../../src/services/sandbox/shell-shim')
|
|
const commandMod = await import('../../../src/services/sandbox/command')
|
|
commandMod.overrideSandboxExecutableTrustValidator(() => true)
|
|
const shimPath = await shimMod.ensureSandboxShellShim(configHome)
|
|
|
|
const directory = path.join(getReposPath(), 'foo')
|
|
const plain = 'a b c'
|
|
const quoted = 'it\'s "quoted"'
|
|
const dollar = '$HOME ${not_expanded} `backtick`'
|
|
const multiline = 'line1\nline2'
|
|
const result = spawnSync(shimPath, ['-c', 'echo verbatim-sentinel'], {
|
|
encoding: 'utf8',
|
|
env: sandboxShellEnv({
|
|
[SANDBOX_SHELL_ENV_WORKDIR]: directory,
|
|
[SANDBOX_SHELL_ENV_NAME]: SANDBOX_NAME,
|
|
[SANDBOX_SHELL_ENV_FORWARD]: 'PLAIN QUOTED DOLLAR MULTILINE',
|
|
PLAIN: plain,
|
|
QUOTED: quoted,
|
|
DOLLAR: dollar,
|
|
MULTILINE: multiline,
|
|
}),
|
|
})
|
|
expect(result.status).toBe(0)
|
|
expect(result.stdout).toBe('verbatim-sentinel\n')
|
|
|
|
const argv = readFileSync(captureFile, 'utf8').split('\0').filter((element) => element !== '')
|
|
expect(argv.filter((element) => element === '-e')).toHaveLength(4)
|
|
expect(argv).toContain(`PLAIN=${plain}`)
|
|
expect(argv).toContain(`QUOTED=${quoted}`)
|
|
expect(argv).toContain(`DOLLAR=${dollar}`)
|
|
expect(argv).toContain(`MULTILINE=${multiline}`)
|
|
} finally {
|
|
if (originalMsbPath === undefined) {
|
|
delete process.env.MSB_PATH
|
|
} else {
|
|
process.env.MSB_PATH = originalMsbPath
|
|
}
|
|
rmSync(fakeBin, { recursive: true, force: true })
|
|
rmSync(configHome, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('passes MSB_PATH and the resolved exec identity to msb as single literal arguments', async () => {
|
|
const fakeBin = mkdtempSync(path.join(tmpdir(), 'ocm-shim-hostile-'))
|
|
const configHome = mkdtempSync(path.join(tmpdir(), 'ocm-shim-config-'))
|
|
const captureFile = path.join(fakeBin, 'argv.txt')
|
|
const msbPath = path.join(fakeBin, 'my msb')
|
|
const hostileUser = 'node; echo hacked'
|
|
writeArgvCapturingFakeMsb(msbPath, captureFile)
|
|
const originalMsbPath = process.env.MSB_PATH
|
|
const originalExecUser = process.env.SANDBOX_EXEC_USER
|
|
process.env.MSB_PATH = msbPath
|
|
process.env.SANDBOX_EXEC_USER = hostileUser
|
|
try {
|
|
vi.resetModules()
|
|
const shimMod = await import('../../../src/services/sandbox/shell-shim')
|
|
const commandMod = await import('../../../src/services/sandbox/command')
|
|
commandMod.overrideSandboxExecutableTrustValidator(() => true)
|
|
const shimPath = await shimMod.ensureSandboxShellShim(configHome)
|
|
|
|
const directory = path.join(getReposPath(), 'foo')
|
|
const command = 'echo hostile-ok'
|
|
const result = spawnSync(shimPath, ['-c', command], {
|
|
encoding: 'utf8',
|
|
env: sandboxShellEnv({ [SANDBOX_SHELL_ENV_WORKDIR]: directory, [SANDBOX_SHELL_ENV_NAME]: SANDBOX_NAME }),
|
|
})
|
|
expect(result.status).toBe(0)
|
|
expect(result.stdout).toBe('hostile-ok\n')
|
|
|
|
const resolvedUser = commandMod.resolveSandboxExecUser()
|
|
expect(resolvedUser).toMatch(/^\d+:\d+$/)
|
|
|
|
const argv = readFileSync(captureFile, 'utf8').split('\0').filter((element) => element !== '')
|
|
expect(argv[0]).toBe(msbPath)
|
|
expect(argv[argv.indexOf('-u') + 1]).toBe(resolvedUser)
|
|
expect(argv[argv.indexOf('-w') + 1]).toBe(directory)
|
|
expect(argv[argv.indexOf('-c') + 1]).toBe(command)
|
|
expect(argv.join(' ')).not.toContain(hostileUser)
|
|
} finally {
|
|
if (originalMsbPath === undefined) {
|
|
delete process.env.MSB_PATH
|
|
} else {
|
|
process.env.MSB_PATH = originalMsbPath
|
|
}
|
|
if (originalExecUser === undefined) {
|
|
delete process.env.SANDBOX_EXEC_USER
|
|
} else {
|
|
process.env.SANDBOX_EXEC_USER = originalExecUser
|
|
}
|
|
rmSync(fakeBin, { recursive: true, force: true })
|
|
rmSync(configHome, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('refuses to run the payload and never invokes msb when the workdir env is unset', async () => {
|
|
const fakeBin = mkdtempSync(path.join(tmpdir(), 'ocm-shim-failclosed-'))
|
|
const configHome = mkdtempSync(path.join(tmpdir(), 'ocm-shim-config-'))
|
|
const msbCaptureFile = path.join(fakeBin, 'msb-invoked.txt')
|
|
const payloadFile = path.join(fakeBin, 'payload-ran.txt')
|
|
const msbPath = path.join(fakeBin, 'msb')
|
|
writeFileSync(msbPath, ['#!/bin/sh', `printf 'invoked' > "${msbCaptureFile}"`].join('\n'), { mode: 0o755 })
|
|
const originalMsbPath = process.env.MSB_PATH
|
|
process.env.MSB_PATH = msbPath
|
|
try {
|
|
vi.resetModules()
|
|
const shimMod = await import('../../../src/services/sandbox/shell-shim')
|
|
const commandMod = await import('../../../src/services/sandbox/command')
|
|
commandMod.overrideSandboxExecutableTrustValidator(() => true)
|
|
const shimPath = await shimMod.ensureSandboxShellShim(configHome)
|
|
|
|
const env = { ...process.env }
|
|
delete env[SANDBOX_SHELL_ENV_WORKDIR]
|
|
const result = spawnSync(shimPath, ['-c', `touch ${payloadFile}`], { encoding: 'utf8', env })
|
|
expect(result.status).toBe(126)
|
|
expect(result.stderr).toContain('ocm-sandbox-shell: sandbox working directory missing; refusing to run on the host')
|
|
expect(existsSync(payloadFile)).toBe(false)
|
|
expect(existsSync(msbCaptureFile)).toBe(false)
|
|
} finally {
|
|
if (originalMsbPath === undefined) {
|
|
delete process.env.MSB_PATH
|
|
} else {
|
|
process.env.MSB_PATH = originalMsbPath
|
|
}
|
|
rmSync(fakeBin, { recursive: true, force: true })
|
|
rmSync(configHome, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('refuses to run the payload and never invokes msb when the sandbox name env is unset or invalid', async () => {
|
|
const fakeBin = mkdtempSync(path.join(tmpdir(), 'ocm-shim-name-'))
|
|
const configHome = mkdtempSync(path.join(tmpdir(), 'ocm-shim-config-'))
|
|
const msbCaptureFile = path.join(fakeBin, 'msb-invoked.txt')
|
|
const payloadFile = path.join(fakeBin, 'payload-ran.txt')
|
|
const msbPath = path.join(fakeBin, 'msb')
|
|
writeFileSync(msbPath, ['#!/bin/sh', `printf 'invoked' > "${msbCaptureFile}"`].join('\n'), { mode: 0o755 })
|
|
const originalMsbPath = process.env.MSB_PATH
|
|
process.env.MSB_PATH = msbPath
|
|
try {
|
|
vi.resetModules()
|
|
const shimMod = await import('../../../src/services/sandbox/shell-shim')
|
|
const commandMod = await import('../../../src/services/sandbox/command')
|
|
commandMod.overrideSandboxExecutableTrustValidator(() => true)
|
|
const shimPath = await shimMod.ensureSandboxShellShim(configHome)
|
|
|
|
const directory = path.join(getReposPath(), 'foo')
|
|
for (const name of [
|
|
undefined,
|
|
'',
|
|
'ocm-workspace',
|
|
'ocm-session-short',
|
|
`ocm-session-${'a'.repeat(11)}`,
|
|
`ocm-session-${'a'.repeat(13)}`,
|
|
`ocm-session-${'g'.repeat(12)}`,
|
|
`ocm-session-${'A'.repeat(12)}`,
|
|
`ocm-session-${'a'.repeat(12)}extra`,
|
|
]) {
|
|
const env: NodeJS.ProcessEnv = {
|
|
...process.env,
|
|
[SANDBOX_SHELL_ENV_WORKDIR]: directory,
|
|
}
|
|
if (name === undefined) {
|
|
delete env[SANDBOX_SHELL_ENV_NAME]
|
|
} else {
|
|
env[SANDBOX_SHELL_ENV_NAME] = name
|
|
}
|
|
const result = spawnSync(shimPath, ['-c', `touch ${payloadFile}`], { encoding: 'utf8', env })
|
|
expect(result.status).toBe(126)
|
|
expect(result.stderr).toContain('ocm-sandbox-shell: sandbox name missing or invalid; refusing to run on the host')
|
|
expect(existsSync(payloadFile)).toBe(false)
|
|
expect(existsSync(msbCaptureFile)).toBe(false)
|
|
}
|
|
} finally {
|
|
if (originalMsbPath === undefined) {
|
|
delete process.env.MSB_PATH
|
|
} else {
|
|
process.env.MSB_PATH = originalMsbPath
|
|
}
|
|
rmSync(fakeBin, { recursive: true, force: true })
|
|
rmSync(configHome, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('execs the sandbox named by OCM_SANDBOX_NAME', async () => {
|
|
const fakeBin = mkdtempSync(path.join(tmpdir(), 'ocm-shim-name-'))
|
|
const configHome = mkdtempSync(path.join(tmpdir(), 'ocm-shim-config-'))
|
|
const msbPath = path.join(fakeBin, 'msb')
|
|
const captureFile = path.join(fakeBin, 'argv.txt')
|
|
writeArgvCapturingFakeMsb(msbPath, captureFile)
|
|
const originalMsbPath = process.env.MSB_PATH
|
|
process.env.MSB_PATH = msbPath
|
|
try {
|
|
vi.resetModules()
|
|
const shimMod = await import('../../../src/services/sandbox/shell-shim')
|
|
const commandMod = await import('../../../src/services/sandbox/command')
|
|
commandMod.overrideSandboxExecutableTrustValidator(() => true)
|
|
const shimPath = await shimMod.ensureSandboxShellShim(configHome)
|
|
|
|
const directory = path.join(getReposPath(), 'foo')
|
|
const otherName = commandMod.sessionSandboxName('ses_other')
|
|
const result = spawnSync(shimPath, ['-c', 'echo ok'], {
|
|
encoding: 'utf8',
|
|
env: sandboxShellEnv({ [SANDBOX_SHELL_ENV_WORKDIR]: directory, [SANDBOX_SHELL_ENV_NAME]: otherName }),
|
|
})
|
|
expect(result.status).toBe(0)
|
|
|
|
const argv = readFileSync(captureFile, 'utf8').split('\0').filter((element) => element !== '')
|
|
expect(argv[argv.indexOf('exec') + 1]).toBe(otherName)
|
|
} finally {
|
|
if (originalMsbPath === undefined) {
|
|
delete process.env.MSB_PATH
|
|
} else {
|
|
process.env.MSB_PATH = originalMsbPath
|
|
}
|
|
rmSync(fakeBin, { recursive: true, force: true })
|
|
rmSync(configHome, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('writes an executable regular shim at sandboxShellShimPath and is idempotent', async () => {
|
|
const configHome = mkdtempSync(path.join(tmpdir(), 'ocm-shim-config-'))
|
|
try {
|
|
vi.resetModules()
|
|
const shimMod = await import('../../../src/services/sandbox/shell-shim')
|
|
|
|
const expected = shimMod.sandboxShellShimPath(configHome)
|
|
const first = await shimMod.ensureSandboxShellShim(configHome)
|
|
expect(first).toBe(expected)
|
|
const stat = statSync(expected)
|
|
expect(stat.isFile()).toBe(true)
|
|
expect(stat.mode & 0o100).not.toBe(0)
|
|
const content = readFileSync(expected, 'utf8')
|
|
expect(content).toContain('#!/bin/sh')
|
|
|
|
const second = await shimMod.ensureSandboxShellShim(configHome)
|
|
expect(second).toBe(expected)
|
|
expect(readFileSync(expected, 'utf8')).toBe(content)
|
|
} finally {
|
|
rmSync(configHome, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('refuses to install the shim when its path falls inside a sandbox mount root', async () => {
|
|
const tmp = mkdtempSync(path.join(tmpdir(), 'ocm-shim-guard-'))
|
|
const originalWorkspacePath = process.env.WORKSPACE_PATH
|
|
try {
|
|
const repos = path.join(tmp, 'workspace', 'repos')
|
|
mkdirSync(repos, { recursive: true })
|
|
|
|
process.env.WORKSPACE_PATH = path.join(tmp, 'workspace')
|
|
vi.resetModules()
|
|
const shimMod = await import('../../../src/services/sandbox/shell-shim')
|
|
|
|
const configHome = path.join(repos, 'config')
|
|
await expect(shimMod.ensureSandboxShellShim(configHome)).rejects.toThrow(
|
|
'refusing to install the sandbox shell shim',
|
|
)
|
|
expect(existsSync(shimMod.sandboxShellShimPath(configHome))).toBe(false)
|
|
} finally {
|
|
if (originalWorkspacePath === undefined) {
|
|
delete process.env.WORKSPACE_PATH
|
|
} else {
|
|
process.env.WORKSPACE_PATH = originalWorkspacePath
|
|
}
|
|
rmSync(tmp, { recursive: true, force: true })
|
|
}
|
|
})
|
|
})
|