fix(sandbox): guard enable path and harden UI rendering performance (#341)

* fix(sandbox): guard enable path and harden UI rendering performance

* chore(deps): allow esbuild postinstall in pnpm onlyBuiltDependencies

* fix(sandbox): align availability reporting and address PR review feedback

* fix(sandbox): block planning without process attestation
This commit is contained in:
Chris Scott authored and GitHub committed 2026-08-27 08:58:23 -04:00
1 parent ba8e8f4b29
commit d63198ec8a
19 files changed
+501 -82

No files matched your search

@@ -1,4 +1,4 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { Database } from 'bun:sqlite'
import { migrate } from '../../src/db/migration-runner'
import { allMigrations } from '../../src/db/migrations'
@@ -275,8 +275,14 @@ describe('CredentialProvider', () => {
const repo = createGithubRepo()
setRepoSandboxGitCredentials(db, repo.id, false)
expect(provider.isSandboxGitCredentialsAllowed({ cwd: repo.fullPath })).toBe(false)
expect(provider.getSandboxGitEnv({ cwd: repo.fullPath })).toEqual({})
const getSettingsSpy = vi.spyOn(SettingsService.prototype, 'getSettings')
try {
expect(provider.isSandboxGitCredentialsAllowed({ cwd: repo.fullPath })).toBe(false)
expect(provider.getSandboxGitEnv({ cwd: repo.fullPath })).toEqual({})
expect(getSettingsSpy).not.toHaveBeenCalled()
} finally {
getSettingsSpy.mockRestore()
}
})
it('lets a per-repo override grant credentials while the global toggle is off', () => {
@@ -296,5 +302,18 @@ describe('CredentialProvider', () => {
expect(env.GIT_CONFIG_COUNT).toBe('1')
expect(env.GIT_CONFIG_KEY_0).toBe('http.https://github.com/.extraheader')
})
it('resolves settings once per getSandboxGitEnv invocation', () => {
settingsService.updateSettings({ sandbox: { enabled: true, gitCredentials: true } })
const repo = createGithubRepo()
const getSettingsSpy = vi.spyOn(SettingsService.prototype, 'getSettings')
try {
provider.getSandboxGitEnv({ cwd: repo.fullPath })
expect(getSettingsSpy).toHaveBeenCalledTimes(1)
} finally {
getSettingsSpy.mockRestore()
}
})
})
})
+37 -3
View File
@@ -12,6 +12,7 @@ import { SandboxRuntimeService, resetSandboxRuntimeState, stopWorkspaceSandboxOn
import { executeCommand } from '../../../src/utils/process'
import { detectSandboxCapability } from '../../../src/services/sandbox/capability'
import { logger } from '../../../src/utils/logger'
import { forceProcessAttestation } from '../../../src/services/opencode/process-identity'
vi.mock('../../../src/utils/process', () => ({
executeCommand: vi.fn(),
@@ -78,7 +79,7 @@ describe('SandboxRuntimeService', () => {
function enableEnforcement(): void {
settingsService.updateSettings({ sandbox: { enabled: true } })
mockDetectSandboxCapability.mockReturnValue({ available: true, msbVersion: 'msb 0.6.8' })
mockDetectSandboxCapability.mockReturnValue({ available: true, msbVersion: 'msb 0.6.15' })
}
function memoryMib(): number {
@@ -980,7 +981,7 @@ describe('SandboxRuntimeService', () => {
expect(mockExecuteCommand.mock.calls.filter((call) => call[0].includes('run'))).toHaveLength(0)
})
it('accepts the full image-resolved v0.6.8 config shape without recreating the sandbox', async () => {
it('accepts the full image-resolved v0.6.15 config shape without recreating the sandbox', async () => {
enableEnforcement()
const resolvedConfig = realInspectConfig({
image: {
@@ -1903,6 +1904,23 @@ describe('SandboxRuntimeService', () => {
expect(mockExecuteCommand).not.toHaveBeenCalled()
})
it('blocks an enforced request when process identity cannot be attested', async () => {
mockDetectSandboxCapability.mockReturnValue({ available: true, msbVersion: 'msb 0.6.15' })
forceProcessAttestation(false)
try {
const plan = await service.planShell(repoADir, true)
expect(plan).toEqual({
mode: 'blocked',
reason: 'process identity attestation is unavailable on this platform (Linux /proc is required)',
})
expect(mockExecuteCommand).not.toHaveBeenCalled()
} finally {
forceProcessAttestation(null)
}
})
it('uses a directory created after boot without recreating the sandbox', async () => {
enableEnforcement()
mockExecuteCommand.mockImplementation(async (args: string[]) => {
@@ -1940,9 +1958,25 @@ describe('SandboxRuntimeService', () => {
expect(service.getStatus()).toEqual({ available: false, enabled: true, reason: '/dev/kvm is not available' })
})
it('reports sandboxing unavailable when process identity cannot be attested', () => {
mockDetectSandboxCapability.mockReturnValue({ available: true, msbVersion: 'msb 0.6.15' })
forceProcessAttestation(false)
try {
expect(service.getStatus()).toEqual({
available: false,
enabled: false,
reason: 'process identity attestation is unavailable on this platform (Linux /proc is required)',
msbVersion: 'msb 0.6.15',
})
} finally {
forceProcessAttestation(null)
}
})
it('fails closed when the capability becomes unavailable after the toggle was enabled', async () => {
settingsService.updateSettings({ sandbox: { enabled: true } })
mockDetectSandboxCapability.mockReturnValue({ available: true, msbVersion: 'msb 0.6.8' })
mockDetectSandboxCapability.mockReturnValue({ available: true, msbVersion: 'msb 0.6.15' })
mockExecuteCommand.mockImplementation(async (args: string[]) => {
if (args.includes('inspect')) return { exitCode: 0, stdout: runningInspectOutput(realInspectConfig()), stderr: '' }
return { exitCode: 0, stdout: '[]', stderr: '' }
@@ -33,6 +33,12 @@ function writeArgvCapturingFakeMsb(msbPath: string, captureFile: string): void {
)
}
function sandboxShellEnv(overrides: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
const env = { ...process.env }
for (const name of SANDBOX_FORWARDED_ENV_NAMES) 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-'))
@@ -54,7 +60,7 @@ describe('sandbox shell shim', () => {
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: { ...process.env, [SANDBOX_SHELL_ENV_WORKDIR]: directory },
env: sandboxShellEnv({ [SANDBOX_SHELL_ENV_WORKDIR]: directory }),
})
expect(result.status).toBe(0)
expect(result.stdout).toBe("it's a test\nLINE2\nafter-newline\n")
@@ -107,14 +113,13 @@ describe('sandbox shell shim', () => {
const extraheaderValue = 'AUTHORIZATION: basic eDphYmMgZGVm'
const result = spawnSync(shimPath, ['-c', 'echo -e sentinel'], {
encoding: 'utf8',
env: {
...process.env,
env: sandboxShellEnv({
[SANDBOX_SHELL_ENV_WORKDIR]: directory,
GIT_CONFIG_COUNT: '1',
GIT_CONFIG_KEY_0: 'http.https://github.com/.extraheader',
GIT_CONFIG_VALUE_0: extraheaderValue,
OCM_INTERNAL_TOKEN: 'must-not-be-forwarded',
},
}),
})
expect(result.status).toBe(0)
@@ -168,7 +173,7 @@ describe('sandbox shell shim', () => {
const command = 'echo hostile-ok'
const result = spawnSync(shimPath, ['-c', command], {
encoding: 'utf8',
env: { ...process.env, [SANDBOX_SHELL_ENV_WORKDIR]: directory },
env: sandboxShellEnv({ [SANDBOX_SHELL_ENV_WORKDIR]: directory }),
})
expect(result.status).toBe(0)
expect(result.stdout).toBe('hostile-ok\n')