// The displayers' engine and compiler (demo epic D4): identity at the // defaults, the path / host / alias / e-mail rules, fail-closed broken rules, // the regex bound, and secret steps kept off the client. // // Run with: node_modules/.bin/tsx --test common/lib/privacy/display.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { DEFAULT_PRIVACY_SETTINGS, sanitizePrivacy, privacyActive, type PrivacySettings } from "../privacySettings"; import { compilePrivacy, literalFragments, pythonReplacement, riskyRegex, type PrivacyCompileInput } from "./compile"; import { applyPrivacy, displayersOf, IDENTITY, PRIVACY_REGEX_MAX_INPUT, publicRules } from "./display"; const HOME = "/home/someone"; const base = (privacy: Partial, extra: Partial = {}): PrivacyCompileInput => ({ privacy: { ...DEFAULT_PRIVACY_SETTINGS, ...privacy }, homeDir: HOME, hostnames: ["boxen"], locations: [{ id: "platter", label: "Platter", root: "/run/media/someone/1234-ABCD/archive", volume: { uuid: "1234-ABCD", mountpoint: "/run/media/someone/1234-ABCD" } }], ...extra, }); const show = (privacy: Partial, s: string, extra: Partial = {}) => applyPrivacy(compilePrivacy(base(privacy, extra)).rules, s); const CORPUS = [ `${HOME}/Projects/x/out.mp4`, "held by pid 12 on boxen since 13:31", "write to someone@example.com", "/run/media/someone/1234-ABCD/archive/chan/media", "", "plain words", ]; test("defaults: no rules, and every displayer returns its input itself", () => { assert.equal(privacyActive(DEFAULT_PRIVACY_SETTINGS), false); assert.deepEqual(sanitizePrivacy(undefined), DEFAULT_PRIVACY_SETTINGS); // Present but wrong is read STRICT, never as off (review round 1, S3). assert.deepEqual(sanitizePrivacy({ displayPaths: "nope", hideHost: "yes", aliases: [1, "", "a==>b", "x\ny"] }), { ...DEFAULT_PRIVACY_SETTINGS, displayPaths: "masked", hideHost: true, aliases: ["a==>b", "xy"], }); const { rules, problems } = compilePrivacy(base({})); assert.equal(rules, null); assert.deepEqual(problems, []); const d = displayersOf(rules); assert.equal(d, IDENTITY); for (const s of CORPUS) { assert.equal(applyPrivacy(null, s), s); assert.equal(d.displayPath(s), s); assert.equal(d.displayText(s), s); } }); test("displayPaths home and masked; a longer name is not the home dir", () => { assert.equal(show({ displayPaths: "home" }, `${HOME}/a`), "~/a"); assert.equal(show({ displayPaths: "home" }, HOME), "~"); assert.equal(show({ displayPaths: "home" }, `${HOME}x/a`), `${HOME}x/a`); assert.equal(show({ displayPaths: "home" }, `${HOME}.bak`), `${HOME}.bak`); assert.equal(show({ displayPaths: "masked" }, `see ${HOME}/a and ${HOME}`), "see /home/user/a and /home/user"); // A trailing slash on the home dir is the same home dir. assert.equal(show({ displayPaths: "home" }, `${HOME}/a`, { homeDir: `${HOME}/` }), "~/a"); // A home of "/" (a container) rewrites nothing. assert.equal(show({ displayPaths: "masked" }, "/srv/a", { homeDir: "/" }), "/srv/a"); }); test("masked: a location root becomes its label (longest first), mounts and volume ids are hidden", () => { const m = (s: string) => show({ displayPaths: "masked" }, s); assert.equal(m("Media relocated to /run/media/someone/1234-ABCD/archive/chan/media."), "Media relocated to [Platter]/chan/media."); assert.equal(m("/run/media/someone/1234-ABCD/other"), "[Platter]/other"); assert.equal(m("/media/someone/USBSTICK/x"), "/media/drive/x"); assert.equal(m("/mnt/0f3c2a1b-9d2e-4c11-8a77-0123456789ab/x"), "/media/drive/x"); assert.equal(m("volume 1234-ABCD (ext4)"), "volume [volume] (ext4)"); assert.equal(m("job 1234-ABCDE"), "job 1234-ABCDE"); // "home" mode labels no root; the user's name in a media mount is `user`. assert.equal(show({ displayPaths: "home" }, "/run/media/someone/1234-ABCD/archive/x"), "/run/media/user/1234-ABCD/archive/x"); }); test("hideHost: whole word, any case, never inside another word", () => { const h = (s: string, names = ["boxen"]) => show({ hideHost: true }, s, { hostnames: names }); assert.equal(h("held on boxen since 13:31"), "held on host since 13:31"); assert.equal(h("boxen.lan:3050"), "host.lan:3050"); assert.equal(h("an oxen boxenly unboxen boxen-2 Boxen"), "an oxen boxenly unboxen boxen-2 host"); // A hostname that is a common word is only replaced as that whole word. assert.equal(h("the desk is a desktop; desk.", ["desk"]), "the host is a desktop; host."); // The short form of a dotted name, and ARCHILYZER_HOST_ID-style extras. assert.equal(h("on boxen and on boxen.example.org", ["boxen.example.org"]), "on host and on host"); assert.equal(h("localhost:3001", ["localhost"]), "localhost:3001"); }); test("aliases in the source mirror's four forms", () => { const a = (aliases: string[], s: string) => show({ aliases }, s); assert.equal(a(["secret-site==>demo-site"], "a secret-site b"), "a demo-site b"); assert.equal(a(["literal:a.b==>c$1"], "xa.byaxb"), "xc$1yaxb"); assert.equal(a(["regex:proj-(\\d+)==>project \\1"], "proj-42 proj-x"), "project 42 proj-x"); assert.equal(a(["regex:(?Psec)ret==>\\g"], "secret"), "sec"); assert.equal(a(["regex:(?i)ALPHA==>b"], "alpha Alpha"), "b b"); assert.equal(a(["glob:priv*te==>p"], "a private b"), "a p b"); assert.equal(a(["glob:p*e==>X"], "pie and pine"), "X"); assert.equal(a(["glob:file?.txt==>f"], "file1.txt fileXX.txt"), "f fileXX.txt"); assert.equal(a(["gone"], "is gone"), "is ***REMOVED***"); assert.equal(a(["# comment", " ", "x==>y"], "x"), "y"); assert.equal(pythonReplacement("a\\1$b\\g<2>\\n"), "a$1$$b$2\n"); }); test("a broken or unbounded regex fails CLOSED: reported by number, its fragments hidden", () => { const { rules, problems } = compilePrivacy(base({ aliases: ["ok==>fine", "regex:jeral(yzer-private==>x", "regex:(a+)+b==>c", "glob:x*y*z*w==>q"] })); assert.deepEqual( problems.map((p) => [p.source, p.line]), [ ["aliases", 2], ["aliases", 3], ["aliases", 4], ], ); for (const p of problems) assert.ok(!/jeral|yzer|private/.test(p.what), "a problem never quotes the rule"); // Rule 2's fragments are hidden; rule 3 has none, so every line it would run // on is hidden whole (review round 2, M-C: a fallback is never a no-op). assert.equal(applyPrivacy(rules, "ok jeralyzer-private"), "[hidden]"); // Rule 2's certain fragments: "jeral" leads, "yzer-private" does not — a // line holding a non-leading one is hidden whole (review round 3, M2). const two = compilePrivacy(base({ aliases: ["ok==>fine", "regex:jeral(yzer-private==>x"] })); assert.equal(applyPrivacy(two.rules, "ok jeralyzer-private"), "[hidden]"); assert.equal(applyPrivacy(two.rules, "ok jeral then"), "fine [hidden]", "from a leading fragment to the line's end"); assert.ok(riskyRegex("(a+)+")); assert.ok(riskyRegex("(a|aa)*")); assert.ok(riskyRegex("((ab)*c)+")); assert.ok(riskyRegex("a*b*c*")); assert.ok(!riskyRegex("proj-(\\d+)")); assert.ok(!riskyRegex("(?:ab)?c+[x*+]")); assert.ok(!riskyRegex("(a|b)?x")); assert.deepEqual(literalFragments("jeral(yzer-private", "regex"), ["jeral", "yzer-private"]); assert.deepEqual(literalFragments("abcd?ef", "regex"), ["abc"]); }); test("an operator regex sees at most PRIVACY_REGEX_MAX_INPUT characters at a time", () => { const rules = compilePrivacy(base({ aliases: ["regex:b(\\w)==>B\\1"] })).rules; const long = "a".repeat(PRIVACY_REGEX_MAX_INPUT * 2 + 10) + "bc"; const t0 = Date.now(); assert.ok(applyPrivacy(rules, long).endsWith("aBc")); assert.ok(Date.now() - t0 < 1000); assert.equal(applyPrivacy(rules, "x bq\ny bz"), "x Bq\ny Bz"); }); test("maskEmail", () => { assert.equal(show({ maskEmail: true }, "mail a.b+c@mail.example.co.uk or x@y"), "mail [email] or x@y"); }); test("useSourceRules: the files' rules are secret steps the client never gets", () => { const input = base( { useSourceRules: true, displayPaths: "home" }, { scrubText: "\uFEFFMARKER-SCRUB==>alias\r\n# c\nregex:QQQ(ZZZ==>z\n", denyText: "MARKER-DENY\ni:marker-ci\n" }, ); const { rules, problems } = compilePrivacy(input); assert.equal(applyPrivacy(rules, "MARKER-SCRUB MARKER-DENY Marker-CI"), "alias [hidden] [hidden]"); assert.equal(applyPrivacy(rules, "x QQQZZZ"), "[hidden]", "the broken scrub rule's fallback: a line with a non-leading fragment, whole"); assert.deepEqual(problems.map((p) => `${p.source}:${p.line}`), ["scrub:3"]); assert.ok(!JSON.stringify(problems).includes("QQQ")); assert.ok(!JSON.stringify(publicRules(rules)).includes("QQQ")); const client = publicRules(rules); assert.ok(client); assert.ok(!JSON.stringify(client).includes("MARKER"), "no secret text reaches the client rules"); assert.ok(!JSON.stringify(client).toLowerCase().includes("marker")); assert.equal(applyPrivacy(client, `${HOME}/x`), "~/x"); // Off: the files' text is not even compiled. const off = compilePrivacy({ ...input, privacy: { ...input.privacy!, useSourceRules: false } }); assert.ok(!JSON.stringify(off).includes("MARKER")); });