seatbelt.e2e.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { spawnSync } from 'node:child_process'
  2. import { existsSync, readFileSync } from 'node:fs'
  3. import { mkdtemp, rm, writeFile } 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. import LocalSubprocessService from '@deepseek-ai/dsh-subprocess-local'
  13. /**
  14. * Keyless macOS integration of the real provider and executor through public run/start paths.
  15. * Linux rungs are forced off so Seatbelt is selected. The tests check world effects and stamped
  16. * facts, including EPERM classification through the wrap-carried dialect; backend-only
  17. * confinement is covered by `@deepseek-ai/dsh-sandbox-local`. Skips off macOS or when
  18. * `sandbox-exec` rejects the profile.
  19. */
  20. const probe = spawnSync('sandbox-exec', [...seatbeltProfileArgs({ mode: 'read-only', workspaceRoot: '/' }), '--', 'true'], { timeout: 5_000, stdio: 'ignore' })
  21. const seatbeltUsable = 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-seatbelt-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. ;(ctx.sandbox as LocalSandboxProvider).internals = { probeBwrap: () => false, probeLandlock: () => 'unusable' }
  38. await ctx.plugin(SandboxPolicyService, { mode, workspaceRoot: workspace })
  39. await ctx.plugin(LocalSubprocessService)
  40. await ctx.plugin(SandboxBashExecutor, { cwd: workspace, timeoutMs: 30_000 })
  41. return ctx.bash as SandboxBashExecutor
  42. }
  43. describe.skipIf(!seatbeltUsable)('bash-sandbox: real Seatbelt confinement through ctx.bash', () => {
  44. it('read-only denies a write — the file must NOT exist, and EPERM text classifies as a denial', async () => {
  45. const workdir = await tempDir(homedir())
  46. const bash = await sandboxedBash(workdir, 'read-only')
  47. const result = await bash.run(bash.resolve({ command: `echo hi > ${workdir}/denied.txt` }))
  48. expect(result.exitCode).not.toBe(0)
  49. expect(result.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' })
  50. expect(existsSync(join(workdir, 'denied.txt'))).toBe(false)
  51. })
  52. it('workspace-write lands a write inside the workspace root and still denies one beside it', async () => {
  53. // HOME-based dirs on purpose: workspace-write grants /tmp and the
  54. // per-user temp dir wholesale, so only paths outside both prove the
  55. // workspace-root boundary.
  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 seatbelt-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('seatbelt-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('evaluates BASH_ENV only after Seatbelt confines the inner Bash', async () => {
  69. const workdir = await tempDir(homedir())
  70. const outside = await tempDir(homedir())
  71. const hook = join(workdir, 'bash-env-hook.sh')
  72. const insideProbe = join(workdir, 'hook-ran.txt')
  73. const outsideProbe = join(outside, 'escaped.txt')
  74. await writeFile(hook, [
  75. 'printf hook > "$DSH_BASH_ENV_INSIDE"',
  76. 'printf escaped > "$DSH_BASH_ENV_OUTSIDE"',
  77. '',
  78. ].join('\n'))
  79. const bash = await sandboxedBash(workdir, 'workspace-write')
  80. await bash.run(bash.resolve({
  81. command: 'true',
  82. env: { BASH_ENV: hook },
  83. dshEnv: {
  84. DSH_BASH_ENV_INSIDE: insideProbe,
  85. DSH_BASH_ENV_OUTSIDE: outsideProbe,
  86. },
  87. }))
  88. expect(readFileSync(insideProbe, 'utf8')).toBe('hook')
  89. expect(existsSync(outsideProbe)).toBe(false)
  90. })
  91. it('classifies a background denial once the task settles', async () => {
  92. const workdir = await tempDir(homedir())
  93. const bash = await sandboxedBash(workdir, 'read-only')
  94. const task = bash.start(bash.resolve({ command: `echo hi > ${workdir}/bg-denied.txt` }))
  95. await task.done
  96. expect(task.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' })
  97. expect(existsSync(join(workdir, 'bg-denied.txt'))).toBe(false)
  98. })
  99. it('an approved escalated retry — the spec-level workspace-write override — lands the exact write read-only denied', async () => {
  100. const workdir = await tempDir(homedir())
  101. const bash = await sandboxedBash(workdir, 'read-only')
  102. const command = `printf escalated > ${workdir}/escalated.txt`
  103. const strict = await bash.run(bash.resolve({ command }))
  104. expect(strict.exitCode).not.toBe(0)
  105. expect(strict.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: 'full' })
  106. expect(existsSync(join(workdir, 'escalated.txt'))).toBe(false)
  107. const retried = await bash.run(bash.resolve({ command, sandboxPolicy: { mode: 'workspace-write', workspaceRoot: workdir } }))
  108. expect(retried.exitCode).toBe(0)
  109. expect(retried.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement: 'full' })
  110. expect(readFileSync(join(workdir, 'escalated.txt'), 'utf8')).toBe('escalated')
  111. })
  112. })