seatbelt.e2e.ts 6.2 KB

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