landlock.e2e.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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, tmpdir } from 'node:os'
  5. import { join } from 'node:path'
  6. import { afterEach, describe, expect, it } from 'vitest'
  7. import { Context } from 'cordis'
  8. import { launcherPath } from 'node-addon-landlock-run'
  9. import { LocalSandboxProvider } from '@deepseek-ai/dsh-sandbox-local'
  10. import { SandboxPolicyService } from '@deepseek-ai/dsh-sandbox-policy'
  11. import { SandboxBashExecutor } from '@deepseek-ai/dsh-bash-sandbox'
  12. /**
  13. * KEYLESS consumer-integration proof: the REAL `LocalSandboxProvider` (bwrap
  14. * rung forced off, so the npm-distributed `landlock-run` confines) underneath the
  15. * REAL `SandboxBashExecutor`, driven through the executor's public run/start
  16. * paths. Verifies the WORLD (files exist or don't) plus the stamped result
  17. * facts; the backend-only confinement proofs live with
  18. * `@deepseek-ai/dsh-sandbox-local`.
  19. *
  20. * Self-skips when the running kernel does not enforce Landlock; the
  21. * launcher binary itself arrives with `pnpm install` (`node-addon-landlock-run`).
  22. */
  23. const probe = spawnSync(launcherPath(), ['--probe'], { timeout: 5_000, encoding: 'utf8' })
  24. const landlockUsable = probe.status === 0
  25. /** The kernel's enforcement level from the probe report — stamped facts below must match it. */
  26. const enforcement = /partially enforced/.test(probe.stdout ?? '') ? 'partial' : 'full'
  27. let ctx: Context | undefined
  28. const tempDirs: string[] = []
  29. afterEach(async () => {
  30. await ctx?.fiber.dispose()
  31. ctx = undefined
  32. await Promise.all(tempDirs.splice(0).map(dir => rm(dir, { recursive: true, force: true })))
  33. })
  34. async function tempDir(base: string): Promise<string> {
  35. const dir = await mkdtemp(join(base, 'dsh-landlock-e2e-'))
  36. tempDirs.push(dir)
  37. return dir
  38. }
  39. async function sandboxedBash(workspace: string, mode: 'read-only' | 'workspace-write'): Promise<SandboxBashExecutor> {
  40. ctx = new Context()
  41. await ctx.plugin(LocalSandboxProvider, {})
  42. ;(ctx.sandbox as LocalSandboxProvider).internals = { probeBwrap: () => false }
  43. await ctx.plugin(SandboxPolicyService, { mode, workspaceRoot: workspace })
  44. await ctx.plugin(SandboxBashExecutor, { cwd: workspace, timeoutMs: 30_000 })
  45. return ctx.bash as SandboxBashExecutor
  46. }
  47. describe.skipIf(!landlockUsable)('bash-sandbox: real Landlock confinement through ctx.bash', () => {
  48. it('read-only denies a write — the file must NOT exist, the result carries denial + enforcement facts', async () => {
  49. const workdir = await tempDir(tmpdir())
  50. const bash = await sandboxedBash(workdir, 'read-only')
  51. const result = await bash.run(bash.resolve({ command: `echo hi > ${workdir}/denied.txt` }))
  52. expect(result.exitCode).not.toBe(0)
  53. expect(result.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement })
  54. expect(existsSync(join(workdir, 'denied.txt'))).toBe(false)
  55. })
  56. it('workspace-write lands a write inside the workspace root and still denies one beside it', async () => {
  57. const workdir = await tempDir(homedir())
  58. const outside = await tempDir(homedir())
  59. const bash = await sandboxedBash(workdir, 'workspace-write')
  60. const inside = await bash.run(bash.resolve({ command: `printf landlock-ok > ${workdir}/allowed.txt` }))
  61. expect(inside.exitCode).toBe(0)
  62. expect(inside.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement })
  63. expect(readFileSync(join(workdir, 'allowed.txt'), 'utf8')).toBe('landlock-ok')
  64. const denied = await bash.run(bash.resolve({ command: `echo hi > ${outside}/denied.txt` }))
  65. expect(denied.exitCode).not.toBe(0)
  66. expect(denied.sandbox).toEqual({ mode: 'workspace-write', denied: true, enforcement })
  67. expect(existsSync(join(outside, 'denied.txt'))).toBe(false)
  68. })
  69. it('classifies a background denial once the task settles', async () => {
  70. const workdir = await tempDir(homedir())
  71. const bash = await sandboxedBash(workdir, 'read-only')
  72. const task = bash.start(bash.resolve({ command: `echo hi > ${workdir}/bg-denied.txt` }))
  73. await task.done
  74. expect(task.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement })
  75. expect(existsSync(join(workdir, 'bg-denied.txt'))).toBe(false)
  76. })
  77. it('an approved escalated retry — the spec-level workspace-write override — lands the exact write read-only denied', async () => {
  78. const workdir = await tempDir(homedir())
  79. const bash = await sandboxedBash(workdir, 'read-only')
  80. const command = `printf escalated > ${workdir}/escalated.txt`
  81. const strict = await bash.run(bash.resolve({ command }))
  82. expect(strict.exitCode).not.toBe(0)
  83. expect(strict.sandbox).toEqual({ mode: 'read-only', denied: true, enforcement: enforcement })
  84. expect(existsSync(join(workdir, 'escalated.txt'))).toBe(false)
  85. const retried = await bash.run(bash.resolve({ command, sandboxMode: 'workspace-write' }))
  86. expect(retried.exitCode).toBe(0)
  87. expect(retried.sandbox).toEqual({ mode: 'workspace-write', denied: false, enforcement: enforcement })
  88. expect(readFileSync(join(workdir, 'escalated.txt'), 'utf8')).toBe('escalated')
  89. })
  90. })