sandbox-stack.spec.ts 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /** The unchanged sandbox-local → bash-sandbox → subprocess stack over the Worker Node layer. */
  2. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  3. import { Context } from '@deepseek-ai/cordis'
  4. import { SandboxBashExecutor } from '@deepseek-ai/dsh-bash-sandbox'
  5. import LocalSandboxProvider from '@deepseek-ai/dsh-sandbox-local'
  6. import { SandboxPolicyService } from '@deepseek-ai/dsh-sandbox-policy'
  7. import LocalSubprocessRuntime from '@deepseek-ai/dsh-subprocess-local'
  8. import { MemoryVfs } from '../../src/storage/memory.ts'
  9. import { setActiveVfs } from '../../src/storage/active.ts'
  10. import { processAlive, signalProcess } from '../../src/node/process-table.ts'
  11. vi.mock('node:child_process', async () => await import('../../src/node/builtin_modules/implemented/child_process.ts'))
  12. const WORKSPACE = '/dsh/workspace'
  13. const OUTSIDE = '/dsh/home'
  14. let vfs: MemoryVfs
  15. const contexts: Context[] = []
  16. beforeEach(() => {
  17. vfs = new MemoryVfs()
  18. setActiveVfs(vfs)
  19. vfs.mkdirSync(WORKSPACE, { recursive: true })
  20. vfs.mkdirSync(OUTSIDE, { recursive: true })
  21. vfs.mkdirSync('/dsh/tmp', { recursive: true })
  22. vi.spyOn(process, 'kill').mockImplementation((pid: number, signal?: string | number): true => {
  23. if (signal === 0) {
  24. if (processAlive(pid)) return true
  25. const error = new Error('kill ESRCH') as NodeJS.ErrnoException
  26. error.code = 'ESRCH'
  27. throw error
  28. }
  29. signalProcess(pid, (signal ?? 'SIGTERM') as NodeJS.Signals)
  30. return true
  31. })
  32. })
  33. afterEach(async () => {
  34. await Promise.all(contexts.splice(0).map(async (ctx) => { await ctx.fiber.dispose() }))
  35. vi.restoreAllMocks()
  36. })
  37. /** Boot the production providers while only their platform primitives are replaced. */
  38. async function setup(mode: 'read-only' | 'workspace-write' | 'danger-full-access'): Promise<SandboxBashExecutor> {
  39. const ctx = new Context()
  40. contexts.push(ctx)
  41. await ctx.plugin(LocalSandboxProvider)
  42. await ctx.plugin(SandboxPolicyService, { mode, workspaceRoot: WORKSPACE })
  43. await ctx.plugin(LocalSubprocessRuntime)
  44. await ctx.plugin(SandboxBashExecutor, { cwd: WORKSPACE })
  45. return ctx.shell as SandboxBashExecutor
  46. }
  47. describe('Worker Landlock through the production sandbox stack', () => {
  48. it('allows workspace and temp writes while classifying an outside write as denied', async () => {
  49. const bash = await setup('workspace-write')
  50. const allowed = await bash.run(bash.resolve({
  51. command: `echo workspace > ${WORKSPACE}/allowed.txt; echo temp > /tmp/allowed.txt`,
  52. }))
  53. expect(allowed.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement: 'full' })
  54. expect(vfs.readFileSync(`${WORKSPACE}/allowed.txt`, 'utf8')).toBe('workspace\n')
  55. expect(vfs.readFileSync('/dsh/tmp/allowed.txt', 'utf8')).toBe('temp\n')
  56. const denied = await bash.run(bash.resolve({ command: `echo denied > ${OUTSIDE}/denied.txt` }))
  57. expect(denied.exitCode).toBe(1)
  58. expect(denied.sandbox).toEqual({ mode: 'workspace-write', denied: true, enforcement: 'full' })
  59. expect(vfs.existsSync(`${OUTSIDE}/denied.txt`)).toBe(false)
  60. })
  61. it('keeps read-only confined and danger-full-access unwrapped', async () => {
  62. const readOnly = await setup('read-only')
  63. const strict = await readOnly.run(readOnly.resolve({
  64. command: `echo discarded > /dev/null; echo denied > ${WORKSPACE}/strict.txt`,
  65. }))
  66. expect(strict.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' })
  67. expect(vfs.existsSync(`${WORKSPACE}/strict.txt`)).toBe(false)
  68. const unrestricted = await setup('danger-full-access')
  69. const result = await unrestricted.run(unrestricted.resolve({ command: `echo allowed > ${OUTSIDE}/full.txt` }))
  70. expect(result.sandbox).toEqual({ mode: 'danger-full-access', denied: false })
  71. expect(vfs.readFileSync(`${OUTSIDE}/full.txt`, 'utf8')).toBe('allowed\n')
  72. })
  73. it('does not leak a concurrent command policy into another process', async () => {
  74. const bash = await setup('read-only')
  75. const strict = bash.run(bash.resolve({
  76. command: `sleep 0.02; echo denied > ${WORKSPACE}/strict.txt`,
  77. }))
  78. const writable = bash.run(bash.resolve({
  79. command: `echo allowed > ${WORKSPACE}/writable.txt`,
  80. sandboxPolicy: { mode: 'workspace-write', workspaceRoot: WORKSPACE },
  81. }))
  82. const [strictResult, writableResult] = await Promise.all([strict, writable])
  83. expect(strictResult.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' })
  84. expect(writableResult.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement: 'full' })
  85. expect(vfs.existsSync(`${WORKSPACE}/strict.txt`)).toBe(false)
  86. expect(vfs.readFileSync(`${WORKSPACE}/writable.txt`, 'utf8')).toBe('allowed\n')
  87. })
  88. })