// settings.privacy — what the two apps' pages SHOW of the machine they run on // (demo epic D4, lib/privacy/). Display only: nothing stored, logged to disk, // sent to a job or written to a record changes; a "copy path" button still // copies the real path. Pure: the type, its defaults, the sanitizer // settingsSchema.ts runs it through and the per-field docs SETTINGS.md renders. // // EVERY DEFAULT IS TODAY'S BEHAVIOUR. With the block absent or at its defaults // lib/privacy/compile.ts compiles NO rules, every displayer returns its input // unchanged and no provider is rendered — the pages are byte-identical. import type { FieldDocs } from "./fieldDocs"; export type DisplayPathsMode = "full" | "home" | "masked"; export const DISPLAY_PATHS_MODES: readonly DisplayPathsMode[] = ["full", "home", "masked"]; export type PrivacySettings = { displayPaths: DisplayPathsMode; hideHost: boolean; hideUser: boolean; hidePrivateSites: boolean; aliases: string[]; maskEmail: boolean; useSourceRules: boolean; }; export const DEFAULT_PRIVACY_SETTINGS: PrivacySettings = { displayPaths: "full", hideHost: false, hideUser: false, hidePrivateSites: false, aliases: [], maskEmail: false, useSourceRules: false, }; // An alias line longer than this is dropped by the sanitizer: an operator's // display rule is a name and its stand-in, not a document. export const PRIVACY_ALIAS_MAX_CHARS = 1000; export const PRIVACY_ALIASES_MAX = 200; export const PRIVACY_SETTINGS_FIELD_DOCS: FieldDocs = { displayPaths: "How a page shows a local path. `\"full\"` (default): as it is. `\"home\"`: the home directory as `~`, and the " + "user's name in a removable-media mount (`/run/media//…`, `/media//…`) as `user`. `\"masked\"`: the " + "home directory as `/home/user`, a storage location's root (and its volume's mountpoint) as `[]`, any " + "other removable-media mount (`/media//`, `/run/media/…`, `/mnt/`) as `/media/drive`, and a " + "configured volume's UUID, in any case, as `[volume]`. Either mode also catches the path JSON-escaped " + "(`\\/home\\/u`) and percent-encoded (`%2Fhome%2Fu`), and a `file://` URL keeps its shape (`file:///home/user/…`); " + "a path ends at punctuation (`/home/u.` is the home dir, `/home/u.bak` is not). Display only: copy buttons copy " + "the real path. While any key is on, the editor's and umtool's pages carry the rules their browser applies — " + "the values they hide included — in the page's script payload (see `useSourceRules`).", hideHost: "Show this machine's hostname (os.hostname(), its short form and ARCHILYZER_HOST_ID; three characters or more) " + "as the word `host` wherever a page draws it as text. Matched whole-word, in any case, so a hostname inside " + "another word is left alone (a hostname that is itself an ordinary word is replaced where it stands alone); " + "links and other attributes a page needs to work are never rewritten. Default false.", hideUser: "Show this account's user name (three characters or more) as the word `user` wherever a page draws it as a " + "whole word, in any case — a log line's `by: … (name)`, a path segment under `/run/media/`. A name inside " + "another word is left alone. Default false.", hidePrivateSites: "Leave the sites marked private (site.json `audience: \"private\"`) out of the lists and pickers a page DRAWS: " + "the editor's site-scope picker, /channels' Focus bar and a channel's site memberships (a hidden membership is " + "still saved as it was), umtool's home, /projects, /work and its research source pickers (a project is hidden " + "when it belongs to a private site's article). A private site that is the current choice is shown as \"A " + "private site\". Only what is drawn: every lookup, default and allow-list keeps the private sites, so a case " + "on a private site's source keeps reading it. Not access control: a private site's own pages still open by " + "URL. An article's own `audience` is a different notion and hides nothing. If the sites cannot be read, umtool " + "hides every site's pieces (fail closed). Default false.", aliases: "Display rules in the SOURCE MIRROR's scrub format (common/lib/scrubRuleLine.ts): one rule per entry — " + "`lhs==>rhs` (literal), `literal:lhs==>rhs`, `regex:pattern==>rhs` (`\\1` in rhs is group 1) or " + "`glob:pattern==>rhs`; with no `==>` the match shows as `***REMOVED***`. Applied to displayed text after the " + "path and host rules. A `regex:` or `glob:` rule must be cheap to run on every render: at most ONE unbounded " + "or large quantifier (`*`, `+`, `{n,}`, `{n,m}` with m > 64, lazy forms too; it then repeats at most 256 times, " + "and where the pattern could match past that the rest of the line is hidden), no backreference, no lookahead " + "or lookbehind, no repeated group holding a quantifier or an alternation, at most four optional groups in a " + "row, quantifier ranges and alternation branches multiplying to at most 1,024 and a match of at most 2,048 " + "characters — so a glob has at most one `*`. A rule that breaks this, or does not compile, is not applied: it " + "runs in FALLBACK — on each line holding one of its certain literal fragments (three characters or more that " + "every match contains), the text from that fragment to the line's end (the whole line, where the fragment " + "does not start the match) is shown as `[hidden]`; with no such fragment, every non-empty line is — and it is " + "reported by its number in the server log and in `archilyzer privacy audit`. A rule that takes over 50 ms on " + "one 4,096-character window is disabled into the same fallback for the rest of the process. One display call " + "spends at most 150 ms on all operator rules together: past that, the text a rule has not read is shown as " + "`[hidden]`, one per line (the rule stays on; the built-in path, host, user and e-mail rules always run on all " + "of it). Rules run per line; a line over 4,096 characters is read in overlapping windows. Set with " + "`pnpm ops settings --json '{\"patch\":{\"privacy\":{\"aliases\":[\"old==>new\"]}}}'`. Default [].", maskEmail: "Show every e-mail address a page draws as text as `[email]` (mailto links keep working). Default false.", useSourceRules: "Also apply the source mirror's private rule files (SOURCE_SCRUB_FILE, default " + "`~/.config/archilyzer/source-scrub.txt`, as aliases; SOURCE_DENYLIST_FILE's literals shown as `[hidden]`). " + "Read by the SERVER only: their rules are applied where a page is rendered on the server and are never sent " + "to a browser, a response, a log or an error (a client-rendered panel does not get them). A missing file is " + "a server-log warning naming its path and error code, and nothing else — `archilyzer privacy audit` is the " + "check that the rules took. Default false. AFTER TURNING PRIVACY ON, RELOAD THE OPEN PAGES: a client-rendered " + "panel on a page already open stays unmasked until it is loaded again (a change while on, or turning it off, " + "reaches them on the next navigation). NOTE, for every rule but these: the browser applies the rules " + "itself, so the page it receives CARRIES the values they hide (the home dir, the hostname, the user name, " + "the volume UUIDs and roots, each alias's left side) in its script payload — view-source shows them; the " + "audit reads only what a page draws, not its scripts.", }; export function isDisplayPathsMode(v: unknown): v is DisplayPathsMode { return v === "full" || v === "home" || v === "masked"; } // FAIL CLOSED (review round 1, S3). A block that is ABSENT is today's // behaviour; one that is PRESENT but is not an object, or a key with a value // it cannot take, is read as the STRICTEST valid value — never silently as // "off": `"displayPaths": "Masked"` is "masked", `"hideHost": "yes"` is true, // `"privacy": "on"` turns every hiding key on. privacyProblems() names what // was wrong, by key, for the server's one warning. export const STRICT_PRIVACY_SETTINGS: PrivacySettings = { displayPaths: "masked", hideHost: true, hideUser: true, hidePrivateSites: true, aliases: [], maskEmail: true, useSourceRules: true, }; const BOOLEAN_KEYS = ["hideHost", "hideUser", "hidePrivateSites", "maskEmail", "useSourceRules"] as const; function cleanAliases(v: unknown): string[] { return Array.isArray(v) ? v .filter((a): a is string => typeof a === "string") .map((a) => a.replace(/\r?\n/g, "")) .filter((a) => a.trim() !== "" && a.length <= PRIVACY_ALIAS_MAX_CHARS) .slice(0, PRIVACY_ALIASES_MAX) : []; } export function sanitizePrivacy(value: unknown): PrivacySettings { if (value === undefined) return { ...DEFAULT_PRIVACY_SETTINGS, aliases: [] }; if (!value || typeof value !== "object" || Array.isArray(value)) return { ...STRICT_PRIVACY_SETTINGS, aliases: [] }; const r = value as Record; const mode = (v: unknown): DisplayPathsMode => { if (v === undefined) return DEFAULT_PRIVACY_SETTINGS.displayPaths; return isDisplayPathsMode(v) ? v : "masked"; }; const bool = (k: (typeof BOOLEAN_KEYS)[number]) => (r[k] === undefined ? DEFAULT_PRIVACY_SETTINGS[k] : r[k] !== false); return { displayPaths: mode(r.displayPaths), hideHost: bool("hideHost"), hideUser: bool("hideUser"), hidePrivateSites: bool("hidePrivateSites"), aliases: cleanAliases(r.aliases), maskEmail: bool("maskEmail"), useSourceRules: bool("useSourceRules"), }; } /** What a present `privacy` block got wrong, by key name — never by value. */ export function privacyProblems(value: unknown): string[] { if (value === undefined) return []; if (!value || typeof value !== "object" || Array.isArray(value)) return ["the block is not an object: every hiding key is read as on"]; const r = value as Record; const out: string[] = []; if (r.displayPaths !== undefined && !isDisplayPathsMode(r.displayPaths)) out.push("displayPaths is not \"full\", \"home\" or \"masked\": read as \"masked\""); for (const k of BOOLEAN_KEYS) if (r[k] !== undefined && typeof r[k] !== "boolean") out.push(`${k} is not true or false: read as true`); if (r.aliases !== undefined) { if (!Array.isArray(r.aliases)) out.push("aliases is not a list: read as []"); else if (cleanAliases(r.aliases).length !== r.aliases.length) out.push("aliases has entries that are not rules (not text, empty, or too long): dropped"); } // Unknown keys by COUNT: a key's name is the operator's text. const known = new Set(["displayPaths", "aliases", ...BOOLEAN_KEYS]); const unknown = Object.keys(r).filter((k) => !known.has(k)).length; if (unknown) out.push(`${unknown} key${unknown === 1 ? " is" : "s are"} not a privacy key: ignored`); return out; } /** Is anything on? False is today's behaviour exactly. */ export function privacyActive(p: PrivacySettings | null | undefined): boolean { if (!p) return false; return ( p.displayPaths !== "full" || p.hideHost || p.hideUser || p.hidePrivateSites || p.aliases.length > 0 || p.maskEmail || p.useSourceRules ); }