bwrap.e2e.ts 5.1 KB

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