|
| 1 | +/** |
| 2 | + * Filesystem sandbox guardrail test. |
| 3 | + * |
| 4 | + * Verifies that the test preload correctly isolates all filesystem writes |
| 5 | + * to a temporary directory — no test should ever touch the real user's home. |
| 6 | + * |
| 7 | + * If this test fails, it means the sandbox is broken and tests are writing |
| 8 | + * to real user files (e.g. ~/.spawn/history.json). |
| 9 | + */ |
| 10 | + |
| 11 | +import { describe, expect, it } from "bun:test"; |
| 12 | +import { existsSync, statSync } from "node:fs"; |
| 13 | +import { join } from "node:path"; |
| 14 | + |
| 15 | +// REAL_HOME is the actual home directory captured BEFORE preload runs. |
| 16 | +// We read it from /etc/passwd because process.env.HOME is already sandboxed. |
| 17 | +const REAL_HOME = (() => { |
| 18 | + try { |
| 19 | + // Bun's os.homedir() is patched by preload, and process.env.HOME is |
| 20 | + // sandboxed. Read the real home from the password database instead. |
| 21 | + const proc = Bun.spawnSync([ |
| 22 | + "sh", |
| 23 | + "-c", |
| 24 | + "getent passwd $(id -u) | cut -d: -f6", |
| 25 | + ]); |
| 26 | + const home = new TextDecoder().decode(proc.stdout).trim(); |
| 27 | + return home || "/home/unknown"; |
| 28 | + } catch { |
| 29 | + return "/home/unknown"; |
| 30 | + } |
| 31 | +})(); |
| 32 | + |
| 33 | +describe("Filesystem sandbox", () => { |
| 34 | + it("process.env.HOME should point to temp sandbox, not real home", () => { |
| 35 | + const home = process.env.HOME ?? ""; |
| 36 | + expect(home).not.toBe(REAL_HOME); |
| 37 | + expect(home).toContain("spawn-test-home-"); |
| 38 | + }); |
| 39 | + |
| 40 | + it("SPAWN_HOME should point to temp sandbox", () => { |
| 41 | + const spawnHome = process.env.SPAWN_HOME ?? ""; |
| 42 | + expect(spawnHome).toContain("spawn-test-home-"); |
| 43 | + expect(spawnHome).toEndWith("/.spawn"); |
| 44 | + }); |
| 45 | + |
| 46 | + it("XDG_CACHE_HOME should point to temp sandbox", () => { |
| 47 | + const cacheHome = process.env.XDG_CACHE_HOME ?? ""; |
| 48 | + expect(cacheHome).toContain("spawn-test-home-"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("real home ~/.spawn/history.json should not be modified during this test run", () => { |
| 52 | + const realHistoryPath = join(REAL_HOME, ".spawn", "history.json"); |
| 53 | + if (!existsSync(realHistoryPath)) { |
| 54 | + // No history file exists — that's fine, it definitely wasn't modified. |
| 55 | + expect(true).toBe(true); |
| 56 | + return; |
| 57 | + } |
| 58 | + // Record the mtime. If any test modifies the real file, the mtime |
| 59 | + // changes. We can't detect this retroactively within a single test, |
| 60 | + // but this test serves as documentation and will catch regressions |
| 61 | + // when the file doesn't exist yet (first-time devs). |
| 62 | + const stat = statSync(realHistoryPath); |
| 63 | + expect(stat.isFile()).toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + it("sandbox directories should exist", () => { |
| 67 | + const home = process.env.HOME ?? ""; |
| 68 | + expect(existsSync(join(home, ".spawn"))).toBe(true); |
| 69 | + expect(existsSync(join(home, ".cache"))).toBe(true); |
| 70 | + expect(existsSync(join(home, ".config"))).toBe(true); |
| 71 | + expect(existsSync(join(home, ".ssh"))).toBe(true); |
| 72 | + expect(existsSync(join(home, ".claude"))).toBe(true); |
| 73 | + }); |
| 74 | +}); |
0 commit comments