bwrap.e2e.ts 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { spawnSync } from 'node:child_process'
  2. import { existsSync, readFileSync } from 'node:fs'
  3. import { mkdtemp, rm } from 'node:fs/promises'
  4. import { homedir } from 'node:os'
  5. import { join } from 'node:path'
  6. import { afterEach, describe, expect, it } from 'vitest'
  7. import { Context } from 'cordis'
  8. import { LocalSandboxProvider } from '@deepseek-ai/dsh-sandbox-local'
  9. import { bwrapProfileArgs } from '@deepseek-ai/dsh-sandbox-local/src/profiles.ts'
  10. import { SandboxBashExecutor } from '@deepseek-ai/dsh-bash-sandbox'
  11. /**
  12. * Keyless integration of the real provider and executor through public run/start paths. With
  13. * no rung forced, a passing bwrap probe selects the ladder's first rung. The tests check world
  14. * effects and stamped facts, including EROFS classification through the wrap-carried dialect;
  15. * backend-only confinement is covered by `@deepseek-ai/dsh-sandbox-local`.
  16. *
  17. * Skips when bwrap or unprivileged user namespaces are unavailable. HOME-based paths are
  18. * intentional because bwrap replaces `/tmp`, which cannot prove the workspace-root boundary.
  19. */
  20. const probe = spawnSync('bwrap', [...bwrapProfileArgs({ mode: 'read-only', workspaceRoot: '/' }), '--', 'true'], { timeout: 5_000, stdio: 'ignore' })
  21. const bwrapUsable = probe.status === 0
  22. let ctx: Context | undefined
  23. const tempDirs: string[] = []
  24. afterEach(async () => {
  25. await ctx?.fiber.dispose()
  26. ctx = undefined
  27. await Promise.all(tempDirs.splice(0).map(dir => rm(dir, { recursive: true, force: true })))
  28. })
  29. async function tempDir(base: string): Promise<string> {
  30. const dir = await mkdtemp(join(base, 'dsh-bwrap-e2e-'))
  31. tempDirs.push(dir)
  32. return dir
  33. }
  34. async function sandboxedBash(workspace: string, mode: 'read-only' | 'workspace-write'): Promise<SandboxBashExecutor> {
  35. ctx = new Context()
  36. await ctx.plugin(LocalSandboxProvider, {})
  37. await ctx.plugin(SandboxBashExecutor, { mode, cwd: workspace, workspaceRoot: workspace, timeoutMs: 30_000 })
  38. return ctx.bash as SandboxBashExecutor
  39. }
  40. describe.skipIf(!bwrapUsable)('bash-sandbox: real bwrap confinement through ctx.bash', () => {
  41. it('read-only denies a write — the file must NOT exist, and EROFS text classifies as a denial', async () => {
  42. const workdir = await tempDir(homedir())
  43. const bash = await sandboxedBash(workdir, 'read-only')
  44. const result = await bash.run(bash.resolve({ command: `echo hi > ${workdir}/denied.txt` }))
  45. expect(result.exitCode).not.toBe(0)
  46. expect(result.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' })
  47. expect(existsSync(join(workdir, 'denied.txt'))).toBe(false)
  48. })
  49. it('workspace-write lands a write inside the workspace root and still denies one beside it', async () => {
  50. const workdir = await tempDir(homedir())
  51. const outside = await tempDir(homedir())
  52. const bash = await sandboxedBash(workdir, 'workspace-write')
  53. const inside = await bash.run(bash.resolve({ command: `printf bwrap-ok > ${workdir}/allowed.txt` }))
  54. expect(inside.exitCode).toBe(0)
  55. expect(inside.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement: 'full' })
  56. expect(readFileSync(join(workdir, 'allowed.txt'), 'utf8')).toBe('bwrap-ok')
  57. const denied = await bash.run(bash.resolve({ command: `echo hi > ${outside}/denied.txt` }))
  58. expect(denied.exitCode).not.toBe(0)
  59. expect(denied.sandbox).toEqual({ mode: 'workspace-write', denied: true, enforcement: 'full' })
  60. expect(existsSync(join(outside, 'denied.txt'))).toBe(false)
  61. })
  62. it('classifies a background denial once the task settles', async () => {
  63. const workdir = await tempDir(homedir())
  64. const bash = await sandboxedBash(workdir, 'read-only')
  65. const task = bash.start(bash.resolve({ command: `echo hi > ${workdir}/bg-denied.txt` }))
  66. await task.done
  67. expect(task.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' })
  68. expect(existsSync(join(workdir, 'bg-denied.txt'))).toBe(false)
  69. })
  70. it('an approved escalated retry — the spec-level workspace-write override — lands the exact write read-only denied', async () => {
  71. const workdir = await tempDir(homedir())
  72. const bash = await sandboxedBash(workdir, 'read-only')
  73. const command = `printf escalated > ${workdir}/escalated.txt`
  74. const strict = await bash.run(bash.resolve({ command }))
  75. expect(strict.exitCode).not.toBe(0)
  76. expect(strict.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' })
  77. expect(existsSync(join(workdir, 'escalated.txt'))).toBe(false)
  78. const retried = await bash.run(bash.resolve({ command, sandboxMode: 'workspace-write' }))
  79. expect(retried.exitCode).toBe(0)
  80. expect(retried.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement: 'full' })
  81. expect(readFileSync(join(workdir, 'escalated.txt'), 'utf8')).toBe('escalated')
  82. })
  83. })