|
| 1 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 5 | +import { resolveInsideCwd, validateForOverwrite } from "./file-ops.js"; |
| 6 | +import { FileStateCache } from "./file-state-cache.js"; |
| 7 | +import { validateShellCommand } from "./shell-validator.js"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Tests for the unrestricted opt-outs. Each one verifies BOTH that the |
| 11 | + * restriction holds by default AND that the env flag relaxes it, so a |
| 12 | + * future refactor that accidentally drops the gate is caught. |
| 13 | + */ |
| 14 | + |
| 15 | +describe("CODEBASE_NO_PROJECT_ROOT", () => { |
| 16 | + let saved: string | undefined; |
| 17 | + let cwd: string; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + saved = process.env.CODEBASE_NO_PROJECT_ROOT; |
| 21 | + delete process.env.CODEBASE_NO_PROJECT_ROOT; |
| 22 | + cwd = mkdtempSync(join(tmpdir(), "codebase-unrestricted-")); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + rmSync(cwd, { recursive: true, force: true }); |
| 27 | + if (saved === undefined) delete process.env.CODEBASE_NO_PROJECT_ROOT; |
| 28 | + else process.env.CODEBASE_NO_PROJECT_ROOT = saved; |
| 29 | + }); |
| 30 | + |
| 31 | + it("default: rejects /etc/passwd as outside the project root", () => { |
| 32 | + expect(() => resolveInsideCwd(cwd, "/etc/passwd")).toThrow(/outside/i); |
| 33 | + }); |
| 34 | + |
| 35 | + it("default: rejects ../escape paths", () => { |
| 36 | + expect(() => resolveInsideCwd(cwd, "../../../etc/passwd")).toThrow(/outside/i); |
| 37 | + }); |
| 38 | + |
| 39 | + it("unrestricted=1: lets /etc/passwd through verbatim", () => { |
| 40 | + process.env.CODEBASE_NO_PROJECT_ROOT = "1"; |
| 41 | + expect(resolveInsideCwd(cwd, "/etc/passwd")).toBe("/etc/passwd"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("unrestricted=1: lets ../escape resolve normally", () => { |
| 45 | + process.env.CODEBASE_NO_PROJECT_ROOT = "1"; |
| 46 | + const out = resolveInsideCwd(cwd, "../sibling/file"); |
| 47 | + expect(out).not.toContain(cwd); |
| 48 | + expect(out.endsWith("/sibling/file")).toBe(true); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe("CODEBASE_NO_VALIDATOR", () => { |
| 53 | + let saved: string | undefined; |
| 54 | + |
| 55 | + beforeEach(() => { |
| 56 | + saved = process.env.CODEBASE_NO_VALIDATOR; |
| 57 | + delete process.env.CODEBASE_NO_VALIDATOR; |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + if (saved === undefined) delete process.env.CODEBASE_NO_VALIDATOR; |
| 62 | + else process.env.CODEBASE_NO_VALIDATOR = saved; |
| 63 | + }); |
| 64 | + |
| 65 | + it("default: blocks `rm -rf /`", () => { |
| 66 | + expect(validateShellCommand("rm -rf /").verdict).toBe("block"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("default: warns on `sudo apt update`", () => { |
| 70 | + expect(validateShellCommand("sudo apt update").verdict).toBe("warn"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("unrestricted=1: allows `rm -rf /`", () => { |
| 74 | + process.env.CODEBASE_NO_VALIDATOR = "1"; |
| 75 | + expect(validateShellCommand("rm -rf /").verdict).toBe("allow"); |
| 76 | + }); |
| 77 | + |
| 78 | + it("unrestricted=1: allows `sudo apt update` without warn", () => { |
| 79 | + process.env.CODEBASE_NO_VALIDATOR = "1"; |
| 80 | + expect(validateShellCommand("sudo apt update").verdict).toBe("allow"); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe("CODEBASE_NO_READ_BEFORE_WRITE", () => { |
| 85 | + let saved: string | undefined; |
| 86 | + let cache: FileStateCache; |
| 87 | + |
| 88 | + beforeEach(() => { |
| 89 | + saved = process.env.CODEBASE_NO_READ_BEFORE_WRITE; |
| 90 | + delete process.env.CODEBASE_NO_READ_BEFORE_WRITE; |
| 91 | + cache = new FileStateCache(); |
| 92 | + }); |
| 93 | + |
| 94 | + afterEach(() => { |
| 95 | + if (saved === undefined) delete process.env.CODEBASE_NO_READ_BEFORE_WRITE; |
| 96 | + else process.env.CODEBASE_NO_READ_BEFORE_WRITE = saved; |
| 97 | + }); |
| 98 | + |
| 99 | + it("default: throws FileNotReadFirstError when the file wasn't read", () => { |
| 100 | + expect(() => validateForOverwrite("/tmp/never-read.txt", cache)).toThrow(); |
| 101 | + }); |
| 102 | + |
| 103 | + it("unrestricted=1: returns a synthetic snapshot without throwing", () => { |
| 104 | + process.env.CODEBASE_NO_READ_BEFORE_WRITE = "1"; |
| 105 | + const snap = validateForOverwrite("/tmp/never-read.txt", cache); |
| 106 | + expect(snap.path).toBe("/tmp/never-read.txt"); |
| 107 | + expect(snap.isPartialView).toBe(false); |
| 108 | + expect(snap.size).toBe(0); |
| 109 | + }); |
| 110 | +}); |
0 commit comments