landlock.e2e.ts 4.9 KB

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