bwrap.e2e.ts 4.7 KB

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