seatbelt.e2e.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 { seatbeltProfileArgs } from '@deepseek-ai/dsh-sandbox-local/src/profiles.ts'
  11. import { SandboxBashExecutor } from '@deepseek-ai/dsh-bash-sandbox'
  12. /**
  13. * Keyless macOS integration of the real provider and executor through public run/start paths.
  14. * Linux rungs are forced off so Seatbelt is selected. The tests check world effects and stamped
  15. * facts, including EPERM classification through the wrap-carried dialect; backend-only
  16. * confinement is covered by `@deepseek-ai/dsh-sandbox-local`. Skips off macOS or when
  17. * `sandbox-exec` rejects the profile.
  18. */
  19. const probe = spawnSync('sandbox-exec', [...seatbeltProfileArgs({ mode: 'read-only', workspaceRoot: '/' }), '--', 'true'], { timeout: 5_000, stdio: 'ignore' })
  20. const seatbeltUsable = 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-seatbelt-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. ;(ctx.sandbox as LocalSandboxProvider).internals = { probeBwrap: () => false, probeLandlock: () => 'unusable' }
  37. await ctx.plugin(SandboxPolicyService, { mode, workspaceRoot: workspace })
  38. await ctx.plugin(SandboxBashExecutor, { cwd: workspace, timeoutMs: 30_000 })
  39. return ctx.bash as SandboxBashExecutor
  40. }
  41. describe.skipIf(!seatbeltUsable)('bash-sandbox: real Seatbelt confinement through ctx.bash', () => {
  42. it('read-only denies a write — the file must NOT exist, and EPERM text classifies as a denial', async () => {
  43. const workdir = await tempDir(homedir())
  44. const bash = await sandboxedBash(workdir, 'read-only')
  45. const result = await bash.run(bash.resolve({ command: `echo hi > ${workdir}/denied.txt` }))
  46. expect(result.exitCode).not.toBe(0)
  47. expect(result.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' })
  48. expect(existsSync(join(workdir, 'denied.txt'))).toBe(false)
  49. })
  50. it('workspace-write lands a write inside the workspace root and still denies one beside it', async () => {
  51. // HOME-based dirs on purpose: workspace-write grants /tmp and the
  52. // per-user temp dir wholesale, so only paths outside both prove the
  53. // workspace-root boundary.
  54. const workdir = await tempDir(homedir())
  55. const outside = await tempDir(homedir())
  56. const bash = await sandboxedBash(workdir, 'workspace-write')
  57. const inside = await bash.run(bash.resolve({ command: `printf seatbelt-ok > ${workdir}/allowed.txt` }))
  58. expect(inside.exitCode).toBe(0)
  59. expect(inside.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement: 'full' })
  60. expect(readFileSync(join(workdir, 'allowed.txt'), 'utf8')).toBe('seatbelt-ok')
  61. const denied = await bash.run(bash.resolve({ command: `echo hi > ${outside}/denied.txt` }))
  62. expect(denied.exitCode).not.toBe(0)
  63. expect(denied.sandbox).toEqual({ mode: 'workspace-write', denied: true, enforcement: 'full' })
  64. expect(existsSync(join(outside, 'denied.txt'))).toBe(false)
  65. })
  66. it('classifies a background denial once the task settles', async () => {
  67. const workdir = await tempDir(homedir())
  68. const bash = await sandboxedBash(workdir, 'read-only')
  69. const task = bash.start(bash.resolve({ command: `echo hi > ${workdir}/bg-denied.txt` }))
  70. await task.done
  71. expect(task.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' })
  72. expect(existsSync(join(workdir, 'bg-denied.txt'))).toBe(false)
  73. })
  74. it('an approved escalated retry — the spec-level workspace-write override — lands the exact write read-only denied', async () => {
  75. const workdir = await tempDir(homedir())
  76. const bash = await sandboxedBash(workdir, 'read-only')
  77. const command = `printf escalated > ${workdir}/escalated.txt`
  78. const strict = await bash.run(bash.resolve({ command }))
  79. expect(strict.exitCode).not.toBe(0)
  80. expect(strict.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' })
  81. expect(existsSync(join(workdir, 'escalated.txt'))).toBe(false)
  82. const retried = await bash.run(bash.resolve({ command, sandboxPolicy: { mode: 'workspace-write', workspaceRoot: workdir } }))
  83. expect(retried.exitCode).toBe(0)
  84. expect(retried.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement: 'full' })
  85. expect(readFileSync(join(workdir, 'escalated.txt'), 'utf8')).toBe('escalated')
  86. })
  87. })