landlock.e2e.ts 5.2 KB

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