bwrap.e2e.ts 4.8 KB

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