|
| 1 | +import { copyFileSync, readFileSync, symlinkSync, unlinkSync, writeFileSync } from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { afterEach, describe, expect, it } from "vitest"; |
| 4 | +import { |
| 5 | + commitArgs, |
| 6 | + createContentGuardFixture, |
| 7 | + literals, |
| 8 | + rulePath, |
| 9 | + ruleSetting, |
| 10 | + run, |
| 11 | + runFailure, |
| 12 | + stageContent as stage, |
| 13 | + writeExecutable, |
| 14 | +} from "./git-hooks-pre-commit.test-support.js"; |
| 15 | +import { cleanupTempDirs } from "./helpers/temp-dir.js"; |
| 16 | + |
| 17 | +const tempDirs: string[] = []; |
| 18 | +const fixture = () => createContentGuardFixture(tempDirs); |
| 19 | +const failureLine = "[pre-commit] FAILED (exit 23)\n"; |
| 20 | + |
| 21 | +afterEach(() => cleanupTempDirs(tempDirs)); |
| 22 | + |
| 23 | +function expectBlocked(output: string, name: string): void { |
| 24 | + expect(output).toContain("Blocked staged content"); |
| 25 | + expect(output).toContain(JSON.stringify(name)); |
| 26 | + expect(output).not.toContain(literals[0]); |
| 27 | + expect(output).toContain("[pre-commit] FAILED (exit 1)\n"); |
| 28 | +} |
| 29 | + |
| 30 | +// Only the external formatter is simulated; it emits the working-tree input completely. |
| 31 | +function diagnosticFormatter(dir: string, stream: number, exitCode: number): void { |
| 32 | + writeExecutable( |
| 33 | + path.join(dir, "node_modules/.bin"), |
| 34 | + "oxfmt", |
| 35 | + `#!/usr/bin/env node |
| 36 | +const fs = require("node:fs"); |
| 37 | +const payload = fs.readFileSync("payload.ts"); |
| 38 | +let written = 0; |
| 39 | +while (written < payload.length) written += fs.writeSync(${stream}, payload, written, payload.length - written); |
| 40 | +process.exitCode = ${exitCode}; |
| 41 | +`, |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +describe("pre-commit Git path identity", () => { |
| 46 | + it.each(["staged", "restaged"])("blocks the only BOM-prefixed path when %s", (mode) => { |
| 47 | + const dir = fixture(); |
| 48 | + const name = "\uFEFFpayload.txt"; |
| 49 | + stage(dir, name, mode === "staged" ? literals[0] : "clean\n"); |
| 50 | + writeFileSync(path.join(dir, name), literals[0]); |
| 51 | + const result = runFailure(dir, "git", commitArgs); |
| 52 | + expectBlocked(result.stderr, name); |
| 53 | + expect(runFailure(dir, "git", ["rev-parse", "--verify", "HEAD"]).status).not.toBe(0); |
| 54 | + }); |
| 55 | + |
| 56 | + it("accepts a benign BOM path without searching its unchanged non-BOM counterpart", () => { |
| 57 | + const dir = fixture(); |
| 58 | + stage(dir, "payload.txt", literals[0]); |
| 59 | + run(dir, "git", ["commit", "-qm", "historical fixture"]); |
| 60 | + const name = "\uFEFFpayload.txt"; |
| 61 | + stage(dir, name, "clean\n"); |
| 62 | + run(dir, "git", commitArgs); |
| 63 | + expect(run(dir, "git", ["show", `HEAD:${name}`])).toBe("clean"); |
| 64 | + expect(run(dir, "git", ["show", "HEAD:payload.txt"])).toBe(literals[0]); |
| 65 | + }); |
| 66 | + |
| 67 | + it.each([rulePath, "\uFEFFprivate-rules.txt"])( |
| 68 | + "consumes a rule-file BOM but preserves the configured path: %s", |
| 69 | + (name) => { |
| 70 | + const dir = fixture(); |
| 71 | + writeFileSync(path.join(dir, name), `\uFEFF${literals[0]}\n`); |
| 72 | + run(dir, "git", ["config", "--local", ruleSetting, name]); |
| 73 | + stage(dir, "payload.txt", literals[0]); |
| 74 | + expectBlocked(runFailure(dir, "git", commitArgs).stderr, "payload.txt"); |
| 75 | + }, |
| 76 | + ); |
| 77 | + |
| 78 | + it.each(["--glob-pathspecs", "--icase-pathspecs", "--noglob-pathspecs", "--literal-pathspecs"])( |
| 79 | + "keeps enumerated filenames literal under %s without private rules", |
| 80 | + (flag) => { |
| 81 | + const dir = fixture(); |
| 82 | + run(dir, "git", ["config", "--local", "--unset", ruleSetting]); |
| 83 | + unlinkSync(path.join(dir, rulePath)); |
| 84 | + const name = "chosen[1].txt"; |
| 85 | + stage(dir, name, "staged\n"); |
| 86 | + stage(dir, ":(exclude)clean.txt", "literal colon\n"); |
| 87 | + writeFileSync(path.join(dir, name), "restaged\n"); |
| 88 | + expect( |
| 89 | + run(dir, "bash", ["-c", 'exec git "$@" 2>&1', "hook-proof", flag, ...commitArgs]), |
| 90 | + ).toBe(""); |
| 91 | + expect(run(dir, "git", ["show", `HEAD:${name}`])).toBe("restaged"); |
| 92 | + expect(run(dir, "git", ["show", "HEAD::(exclude)clean.txt"])).toBe("literal colon"); |
| 93 | + }, |
| 94 | + ); |
| 95 | + |
| 96 | + it.each( |
| 97 | + ["--glob-pathspecs", "--icase-pathspecs"].flatMap((flag) => |
| 98 | + ["only", "alternate"].map((index) => ({ flag, index })), |
| 99 | + ), |
| 100 | + )("preserves $index commit index authority under $flag", ({ flag, index }) => { |
| 101 | + const dir = fixture(); |
| 102 | + const name = "chosen[1].txt"; |
| 103 | + stage(dir, name, "original\n"); |
| 104 | + stage(dir, "excluded.txt", "original excluded\n"); |
| 105 | + run(dir, "git", ["commit", "-qm", "initial fixture"]); |
| 106 | + const alternateIndex = path.join(dir, ".git/selected-index"); |
| 107 | + copyFileSync(path.join(dir, ".git/index"), alternateIndex); |
| 108 | + stage(dir, "excluded.txt", literals[0]); |
| 109 | + const env = |
| 110 | + index === "alternate" |
| 111 | + ? { GIT_INDEX_FILE: alternateIndex, GIT_DIR: path.join(dir, ".git"), GIT_WORK_TREE: dir } |
| 112 | + : undefined; |
| 113 | + const select = flag === "--glob-pathspecs" ? "chosen*.txt" : "CHOSEN[1].TXT"; |
| 114 | + const args = [flag, ...commitArgs, ...(index === "only" ? ["--only", "--", select] : [])]; |
| 115 | + writeFileSync(path.join(dir, name), "selected staged\n"); |
| 116 | + run(dir, "git", ["--literal-pathspecs", "add", "--", name], env); |
| 117 | + writeFileSync(path.join(dir, name), "selected restaged\n"); |
| 118 | + run(dir, "git", args, env); |
| 119 | + expect(run(dir, "git", ["show", `HEAD:${name}`])).toBe("selected restaged"); |
| 120 | + expect(run(dir, "git", ["show", "HEAD:excluded.txt"])).toBe("original excluded"); |
| 121 | + expect(run(dir, "git", ["show", ":excluded.txt"])).toBe(literals[0]); |
| 122 | + expect(readFileSync(path.join(dir, "excluded.txt"), "utf8")).toBe(literals[0]); |
| 123 | + const head = run(dir, "git", ["rev-parse", "HEAD"]); |
| 124 | + writeFileSync(path.join(dir, name), literals[0]); |
| 125 | + run(dir, "git", ["--literal-pathspecs", "add", "--", name], env); |
| 126 | + expectBlocked(runFailure(dir, "git", args, env).stderr, name); |
| 127 | + expect(run(dir, "git", ["rev-parse", "HEAD"])).toBe(head); |
| 128 | + expect(run(dir, "git", ["show", ":excluded.txt"])).toBe(literals[0]); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +describe("pre-commit formatter capture", () => { |
| 133 | + it.each([1, 2])("discards incomplete overflow captures from stream %s", (stream) => { |
| 134 | + const dir = fixture(); |
| 135 | + const token = "SYNTHETIC_CAPTURE_".padEnd(128, "x"); |
| 136 | + writeFileSync(path.join(dir, rulePath), `${token}\n`); |
| 137 | + stage(dir, "payload.ts", "clean\n"); |
| 138 | + const cap = 16 * 1024 * 1024; |
| 139 | + const payload = ".".repeat(cap - 131168) + token.repeat(2048) + "ACTIONABLE_TAIL\n"; |
| 140 | + writeFileSync(path.join(dir, "payload.ts"), payload); |
| 141 | + diagnosticFormatter(dir, stream, 23); |
| 142 | + const result = runFailure(dir, "git", commitArgs); |
| 143 | + const output = result.stdout + result.stderr; |
| 144 | + expect(output).toContain("Formatter could not complete"); |
| 145 | + // No padding, redacted capture, or cut literal may be replayed on either stream. |
| 146 | + expect(output.length).toBeLessThan(300); |
| 147 | + expect(output).not.toMatch(/\.{2}|SYNTHETIC|x{2}|REDACTED|ACTIONABLE_TAIL/); |
| 148 | + expect(output).toContain("[pre-commit] FAILED (exit 1)\n"); |
| 149 | + expect(runFailure(dir, "git", ["rev-parse", "--verify", "HEAD"]).status).not.toBe(0); |
| 150 | + }); |
| 151 | + |
| 152 | + it.each([1, 2])("preserves and redacts complete below-cap output on stream %s", (stream) => { |
| 153 | + const dir = fixture(); |
| 154 | + stage(dir, "payload.ts", "clean\n"); |
| 155 | + const payload = ".".repeat(256) + `${literals[0]}\n`.repeat(2048) + "ACTIONABLE_TAIL\n"; |
| 156 | + writeFileSync(path.join(dir, "payload.ts"), payload); |
| 157 | + diagnosticFormatter(dir, stream, 23); |
| 158 | + const result = runFailure(dir, "bash", ["git-hooks/pre-commit"]); |
| 159 | + const output = result.stdout + result.stderr; |
| 160 | + expect(result.status).toBe(23); |
| 161 | + expect(output.startsWith(payload.replaceAll(literals[0], "[REDACTED]"))).toBe(true); |
| 162 | + expect(output).not.toContain(literals[0]); |
| 163 | + expect(output.endsWith(failureLine)).toBe(true); |
| 164 | + // Formatter failure must abort before restaging or the second scan. |
| 165 | + expect(run(dir, "git", ["show", ":payload.ts"])).toBe("clean"); |
| 166 | + }); |
| 167 | + |
| 168 | + it("discards captures when the formatter shell is terminated by a signal", () => { |
| 169 | + const dir = fixture(); |
| 170 | + stage(dir, "payload.ts", "clean\n"); |
| 171 | + writeExecutable( |
| 172 | + path.join(dir, "node_modules/.bin"), |
| 173 | + "oxfmt", |
| 174 | + `#!/usr/bin/env bash |
| 175 | +printf 'INCOMPLETE_STDOUT' |
| 176 | +printf 'INCOMPLETE_STDERR' >&2 |
| 177 | +kill -TERM "$PPID" |
| 178 | +`, |
| 179 | + ); |
| 180 | + const result = runFailure(dir, "git", commitArgs); |
| 181 | + expect(result.stdout + result.stderr).not.toContain("INCOMPLETE"); |
| 182 | + expect(result.stderr).toContain("Formatter could not complete"); |
| 183 | + expect(result.stderr).toContain("FAILED (exit 1)"); |
| 184 | + }); |
| 185 | + |
| 186 | + it("fails safely when the formatter shell cannot be spawned", () => { |
| 187 | + const dir = fixture(); |
| 188 | + stage(dir, "payload.txt", "clean\n"); |
| 189 | + const bin = path.join(dir, "bin"); |
| 190 | + symlinkSync(process.execPath, path.join(bin, "node")); |
| 191 | + symlinkSync(run(dir, "which", ["git"]), path.join(bin, "git")); |
| 192 | + const result = runFailure(dir, "/bin/bash", ["git-hooks/pre-commit"], { PATH: bin }); |
| 193 | + expect(result.status).toBe(1); |
| 194 | + expect(result.stdout).toBe(""); |
| 195 | + expect(result.stderr).toContain("Formatter could not complete"); |
| 196 | + expect(result.stderr).toContain("FAILED (exit 1)"); |
| 197 | + }); |
| 198 | + |
| 199 | + it.each([true, false].flatMap((configured) => [1, 2].map((stream) => ({ configured, stream }))))( |
| 200 | + "drains piped diagnostics: rules=$configured stream=$stream", |
| 201 | + ({ configured, stream }) => { |
| 202 | + const dir = fixture(); |
| 203 | + if (!configured) { |
| 204 | + run(dir, "git", ["config", "--local", "--unset", ruleSetting]); |
| 205 | + unlinkSync(path.join(dir, rulePath)); |
| 206 | + } |
| 207 | + stage(dir, "payload.ts", "clean\n"); |
| 208 | + const payload = "public diagnostic context\n".repeat(4000) + "ACTIONABLE_FINAL_DETAIL\n"; |
| 209 | + writeFileSync(path.join(dir, "payload.ts"), payload); |
| 210 | + diagnosticFormatter(dir, stream, 23); |
| 211 | + for (const [cmd, args] of [ |
| 212 | + ["bash", ["git-hooks/pre-commit"]], |
| 213 | + ["git", commitArgs], |
| 214 | + ] as const) { |
| 215 | + const result = runFailure(dir, cmd, [...args]); |
| 216 | + const output = result.stdout + result.stderr; |
| 217 | + expect(result.status).toBe(cmd === "git" ? 1 : 23); |
| 218 | + expect(output.startsWith(payload)).toBe(true); |
| 219 | + expect(output.endsWith(failureLine)).toBe(true); |
| 220 | + expect(output.match(/\[pre-commit\] FAILED/g)).toHaveLength(1); |
| 221 | + } |
| 222 | + diagnosticFormatter(dir, stream, 0); |
| 223 | + expect(run(dir, "bash", ["-c", 'exec git "$@" 2>&1', "hook-proof", ...commitArgs])).toBe( |
| 224 | + payload.trim(), |
| 225 | + ); |
| 226 | + expect(run(dir, "git", ["show", "HEAD:payload.ts"])).toBe(payload.trim()); |
| 227 | + }, |
| 228 | + ); |
| 229 | +}); |
0 commit comments