// GitRemote.redactText: credential masking over a remote held as text or free text holding remotes. The examples // pin each rule; the property pins the contract that no URL password survives, wherever the URL sits in the text. import { assert, describe, it } from "@effect/vitest"; import { Arbitrary, Schema } from "effect"; import { GitRemote } from "../src/GitRemote.js"; const redact = GitRemote.redactText; describe("GitRemote.redactText", () => { it("masks a URL's userinfo and query, dropping the password, as redact does", () => { assert.strictEqual(redact("https://u:s3cret@github.com/o/r.git"), "https://@github.com/o/r.git"); assert.strictEqual(redact("https://ghp_tok@github.com/o/r.git"), "https://@github.com/o/r.git"); assert.strictEqual(redact("https://h/o/r?token=abc#frag"), "https://h/o/r?"); assert.strictEqual(redact("persistent::https://u:pw@h/r"), "persistent::https://@h/r"); }); it("masks every URL inside free text and keeps the prose and its delimiters", () => { assert.strictEqual( redact("fetch https://u:s3cret@github.com/o/r.git?token=abc failed"), "fetch https://@github.com/o/r.git? failed", ); assert.strictEqual( redact("fatal: unable to access 'https://x:tok@h/r/': 403\n\tand \"https://y@h/s\""), "fatal: unable to access 'https://@h/r/': 403\n\tand \"https://@h/s\"", ); }); it("masks a mistyped credential URL that does not parse as a URL", () => { assert.strictEqual(redact("https:/u:tok@h/r"), "https:/@h/r"); assert.strictEqual(redact("see //u:tok@h/r"), "see //@h/r"); assert.strictEqual(redact("https:://u:tok3n@github.com/o/r"), "https:://@github.com/o/r"); }); it("leaves text with no credential shape untouched", () => { for (const text of [ "", "nothing to see", "https://github.com/o/r.git", "git@github.com:o/r.git", "what? why?", "cloned /srv/r.git into ./work", ]) { assert.strictEqual(redact(text), text, text); } }); it("over-masks a URL glued to punctuation rather than leak it", () => { assert.strictEqual(redact("(https://u:pw@h/r)"), "://@h/r)"); }); const SECRET = "Zq9Secret"; const filler = Arbitrary.array( Arbitrary.schema(Schema.Literals(["a", " ", "\t", "\n", "'", '"', "`", ":", "/", "@", "?", "=", "."])), { maxLength: 12 }, ).pipe(Arbitrary.map((chars) => chars.join(""))); const url = Arbitrary.schema( Schema.Literals([ `https://user:${SECRET}@github.com/o/r.git`, `https://${SECRET}@h/r`, `ssh://git:${SECRET}@h:22/r`, `https://h/r?token=${SECRET}`, `zz::https://u:${SECRET}@h/r`, `https:/u:${SECRET}@h/r`, `//u:${SECRET}@h/r`, ]), ); it.prop( "no credential survives, whatever text surrounds the URL", [filler, url, filler], ([before, middle, after]) => { // A delimiter on each side keeps the URL one token, which is the documented contract. return !redact(`${before} ${middle} ${after}`).includes(SECRET); }, ); });