|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { mkdtemp, rm, symlink, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { |
| 6 | + commandReferencesSensitivePath, |
| 7 | + isSensitiveShellToken, |
| 8 | +} from "./secret-guard-plugin.js"; |
| 9 | +import { isAutoAllowedShellCommand } from "../permission/classify.js"; |
| 10 | +import { autoShellRuleForCall } from "../permission/auto-shell-policy.js"; |
| 11 | + |
| 12 | +/** |
| 13 | + * CL-7790: shell token matching ignores symlinks. A benign-named symlink |
| 14 | + * into a secret file (notes.txt -> .env) must not auto-allow a content dump |
| 15 | + * (`cat notes.txt`), identically to the direct name (`cat .env`). Pure |
| 16 | + * name-listing (`ls notes.txt`) still lists freely — dumping contents is the |
| 17 | + * threat, listing a name is not (dump-vs-list distinction, CL-5420). |
| 18 | + */ |
| 19 | + |
| 20 | +async function withFixture<T>( |
| 21 | + run: (paths: { cwd: string }) => Promise<T>, |
| 22 | +): Promise<T> { |
| 23 | + const cwd = await mkdtemp(join(tmpdir(), "cl7790-shell-symlink-")); |
| 24 | + try { |
| 25 | + await writeFile(join(cwd, ".env"), "SECRET=fixture-env\n"); |
| 26 | + await writeFile(join(cwd, "README.md"), "# fixture\n"); |
| 27 | + await symlink(join(cwd, ".env"), join(cwd, "notes.txt")); |
| 28 | + return await run({ cwd }); |
| 29 | + } finally { |
| 30 | + await rm(cwd, { recursive: true, force: true }); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +const shellCall = (command: string) => ({ |
| 35 | + id: "c", |
| 36 | + name: "run_shell", |
| 37 | + arguments: { command }, |
| 38 | +}); |
| 39 | + |
| 40 | +describe("CL-7790 shell tokens resolve symlinks before the secret denylist", () => { |
| 41 | + test("cat through a benign-named symlink does not auto-allow", async () => { |
| 42 | + await withFixture(async ({ cwd }) => { |
| 43 | + expect(isAutoAllowedShellCommand("cat notes.txt", cwd)).toBe(false); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + test("cat of the direct secret name still does not auto-allow", async () => { |
| 48 | + await withFixture(async ({ cwd }) => { |
| 49 | + expect(isAutoAllowedShellCommand("cat .env", cwd)).toBe(false); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + test("pure listing of the symlink still lists freely", async () => { |
| 54 | + await withFixture(async ({ cwd }) => { |
| 55 | + expect(isAutoAllowedShellCommand("ls notes.txt", cwd)).toBe(true); |
| 56 | + expect(isAutoAllowedShellCommand("ls -la", cwd)).toBe(true); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + test("pure listing of the direct secret name still asks (CL-5420)", async () => { |
| 61 | + await withFixture(async ({ cwd }) => { |
| 62 | + expect(isAutoAllowedShellCommand("ls .env", cwd)).toBe(false); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + test("plugin flags the symlinked dump but not the listing", async () => { |
| 67 | + await withFixture(async ({ cwd }) => { |
| 68 | + expect(commandReferencesSensitivePath("cat notes.txt", cwd)).toBe( |
| 69 | + "notes.txt", |
| 70 | + ); |
| 71 | + expect( |
| 72 | + commandReferencesSensitivePath("ls notes.txt", cwd), |
| 73 | + ).toBeUndefined(); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + test("shared helper matches the resolved target, not just the lexical name", async () => { |
| 78 | + await withFixture(async ({ cwd }) => { |
| 79 | + expect(isSensitiveShellToken("notes.txt", cwd)).toBe(true); |
| 80 | + // The listing leg never resolves: names are not contents. |
| 81 | + expect(isSensitiveShellToken("notes.txt", cwd, false)).toBe(false); |
| 82 | + expect(isSensitiveShellToken("README.md", cwd)).toBe(false); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + test("auto mode asks for the symlinked dump, not the listing", async () => { |
| 87 | + await withFixture(async ({ cwd }) => { |
| 88 | + expect( |
| 89 | + autoShellRuleForCall(shellCall("cat notes.txt"), () => false, cwd) |
| 90 | + ?.name, |
| 91 | + ).toBe("sensitive-path"); |
| 92 | + expect( |
| 93 | + autoShellRuleForCall(shellCall("ls notes.txt"), () => false, cwd), |
| 94 | + ).toBeUndefined(); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments