// `archilyzer privacy audit` — the privacy audit (lib/privacy/audit.ts) over a // running editor and/or umtool, against a denylist the caller names. // // archilyzer privacy audit --editor --umtool --denylist // [--scrub ] [--settings ] [--max-per-route N] [--strict] [--json] // [--allow-unreached [,…]] [--url [,…]] // // The literals are the denylist's lines (`i:` = any case) plus, with --scrub, // every scrub rule's literal left side — the source mirror's two formats, read // by its own parsers. `--settings` (default: the settings file this checkout // reads) is compiled the way the apps compile it, so a privacy rule that does // not compile is reported by its number. Exit 0 clean and complete, 1 hits, // 2 usage, 3 incomplete (a page did not load or answered ≥ 400, a route was // not reached and not excused by --allow-unreached, or no page was audited). // // It runs against the URLs it is given and nothing else. The report names a // literal by its line and length, never by its characters. import { readFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { getPaths } from "../lib/paths"; import { settingsFromFile } from "../lib/settings"; import { parseDenylist, dedupeLiterals, type Literal } from "../publish/sourceAudit"; import { parseScrubRules } from "../publish/source"; import { compilePrivacy, problemLine } from "../lib/privacy/compile"; import { hostnames, tildify } from "../lib/privacy/privacy-server"; import { auditExitCode, formatPrivacyReport, privacyReportJson, runPrivacyAudit, type AuditBrowser, type AuditTarget } from "../publish/privacyAudit"; import { importPlaywright } from "../social/playwrightRuntime"; export type PrivacyAuditCliOpts = { editor?: string; umtool?: string; denylist?: string; scrub?: string; settings?: string; maxPerRoute?: number; strict?: boolean; json?: boolean; allowUnreached?: string[]; seedPaths?: string[]; log?: (line: string) => void; }; /** The literal list from the two files (either may be absent). */ export function auditLiterals(opts: { denylist?: string; scrub?: string }): Literal[] { const list: Literal[] = []; if (opts.denylist) list.push(...parseDenylist(readFileSync(opts.denylist, "utf8"))); if (opts.scrub) { // "/" as the home dir: the built-in home rule is the apps' displayPaths // business, and a denylist that wants the home dir names it. const scrub = parseScrubRules(readFileSync(opts.scrub, "utf8"), "/"); list.push(...scrub.denied.map((d) => ({ bytes: Buffer.from(d.text, "utf8"), ci: false, from: d.from }))); } return dedupeLiterals(list); } /** What the target's settings.privacy would report about its own rules. */ export function ruleProblemsOf(settingsFile: string): string[] { try { const s = settingsFromFile(settingsFile); const paths = getPaths(); const read = (f: string) => { try { return readFileSync(f, "utf8"); } catch { return null; } }; const { problems } = compilePrivacy({ privacy: s.privacy, homeDir: os.homedir(), hostnames: hostnames(), locations: s.storage.locations, scrubText: s.privacy.useSourceRules ? read(paths.sourceScrubFile) : null, denyText: s.privacy.useSourceRules ? read(paths.sourceDenylistFile) : null, }); return problems.map(problemLine); } catch { return []; } } export async function main(opts: PrivacyAuditCliOpts): Promise { const log = opts.log ?? ((l: string) => console.log(l)); if (!opts.editor && !opts.umtool) { log("privacy audit: name at least one of --editor / --umtool "); return 2; } if (!opts.denylist && !opts.scrub) { log("privacy audit: name the literals: --denylist (and/or --scrub )"); return 2; } let literals: Literal[]; try { literals = auditLiterals(opts); } catch (err) { // A read error names the file by path; a scrub refusal by line number. const e = err as NodeJS.ErrnoException; log(`privacy audit: ${e.code ? `cannot read ${tildify(e.path ?? "")} (${e.code})` : e.message}`); return 2; } if (!literals.length) { log("privacy audit: the denylist names no literal"); return 2; } const root = getPaths().monorepoRoot; const targets: AuditTarget[] = []; if (opts.editor) targets.push({ app: "editor", baseUrl: opts.editor, appDir: path.join(root, "editor", "app") }); if (opts.umtool) targets.push({ app: "umtool", baseUrl: opts.umtool, appDir: path.join(root, "umtool", "app") }); const { chromium } = await importPlaywright(); const browser = await chromium.launch({ headless: true }); try { const result = await runPrivacyAudit({ targets, literals, browser: browser as unknown as AuditBrowser, maxPerRoute: opts.maxPerRoute, ruleProblems: ruleProblemsOf(opts.settings ?? getPaths().settingsFile), allowUnreached: opts.allowUnreached, seedPaths: opts.seedPaths, }); if (opts.json) log(JSON.stringify(privacyReportJson(result, literals, { strict: opts.strict }), null, 2)); else for (const l of formatPrivacyReport(result, literals, { strict: opts.strict })) log(l); return auditExitCode(result, opts.strict); } finally { await browser.close(); } }