// THE PRIVACY AUDIT (demo epic D4): every page of the editor and umtool, as a // browser draws it, searched for the denied literals — `archilyzer privacy // audit` and the two apps' privacy-audit e2e specs. // // WHAT IS VISITED. Each app's routes come from its `app/` dir (routes.ts). The // static ones are loaded first; a dynamic one gets its URLs from the links on // the pages already visited (at most `maxPerRoute` each). A pattern no link // reaches is reported as not reached, by its pattern. // // Every page is loaded in a fresh browser context, so no state a page leaves // (a cookie) changes what the next one draws. // // WHAT IS SEARCHED, per page, twice — the server's first HTML with // JavaScript off ("ssr") and the page after it has run ("live"): every text // node, the title / aria-label / aria-description / alt / placeholder // attributes, a field's value, an option's label, the document title and the // page's URL; and, as a separate warn-only class (it fails only with // `strict`), every link's href. The browser only COLLECTS: the search runs // here, in this process, so the literals never reach a page. // // THE REPORT NEVER PRINTS A LITERAL (the source mirror's convention, // sourceAudit.ts, beside it): a literal is named by where it was written // (`denylist line 3 (len 5)`), a hit by its app, route, kind and a structural // selector (tag names, data-attribute NAMES, nth-of-type — never a value), and // every URL or selector it prints is masked. // // IT NEVER SAYS CLEAN WHEN IT DID NOT LOOK (review round 1, M4): a page that // did not load or answered ≥ 400, a route pattern not reached (unless the // caller excused it by name; a static route that answers 404 bare is visited // through a link carrying its query, else its 404 stands and cannot be // excused), or no page at all makes the report INCOMPLETE // (auditExitCode 3; 1 is a hit). Each literal is searched, and every output // masked, in its plain, percent-encoded and JSON-escaped forms. // // WHAT IS NOT READ: a canvas, CSS `content:`, text a page draws only on hover // or in another tab or query state, a closed shadow root, a cross-origin // frame, and the page's scripts — so a value the browser's privacy rules // carry in the script payload (the home dir they hide, say) is not a hit. // // SERVER ONLY. It never decides which instance to run against: the caller // names the base URLs. import { Buffer } from "node:buffer"; import { foldAscii, literalLabel, maskLiterals, scanBuffer, type Literal } from "./sourceAudit"; import { appRoutes, type AppRoute } from "../lib/privacy/routes"; export type HitWhere = | "text" | "title" | "aria-label" | "aria-description" | "alt" | "placeholder" | "value" | "option" | "document.title" | "url" | "href"; export const HIT_WHERE_ORDER: readonly HitWhere[] = [ "text", "title", "aria-label", "aria-description", "alt", "placeholder", "value", "option", "document.title", "url", "href", ]; export type Pass = "ssr" | "live"; export type PrivacyHit = { app: string; route: string; // The page's URL, masked. url: string; pass: Pass; where: HitWhere; selector: string; lit: number; }; export type AuditPage = { app: string; route: string; url: string; status: number | null; error?: string }; export type PrivacyAuditResult = { literals: number; pages: AuditPage[]; notReached: Array<{ app: string; route: string }>; hits: PrivacyHit[]; // Problems with the target's privacy rules (compile.ts problemLine). ruleProblems: string[]; // Route patterns the caller excused from "not reached" (allowUnreached). allowUnreached: string[]; }; // Structural slices of Playwright's API, so common needs no Playwright types. export type AuditResponse = { status: () => number } | null; export type AuditPageHandle = { goto: (url: string, opts?: Record) => Promise; url: () => string; evaluate: (expr: string) => Promise; waitForTimeout: (ms: number) => Promise; close: () => Promise; }; export type AuditContext = { newPage: () => Promise; close: () => Promise }; export type AuditBrowser = { newContext: (opts: Record) => Promise }; export type AuditTarget = { app: string; baseUrl: string; appDir: string }; export type PrivacyAuditOptions = { targets: readonly AuditTarget[]; literals: readonly Literal[]; browser: AuditBrowser; maxPerRoute?: number; settleMs?: number; timeoutMs?: number; // Only these routes (by pattern) — the specs' narrowing; default all. include?: (app: string, route: string) => boolean; ruleProblems?: string[]; // Dynamic route patterns ("/x/[id]" or "app /x/[id]") the caller names as // knowingly not reached: they no longer make the audit incomplete. allowUnreached?: readonly string[]; // Concrete paths to visit as well ("/projects/at?path=x"; "app /path" to // name the app): a route that needs a query or an id no page links to. seedPaths?: readonly string[]; onPage?: (page: AuditPage) => void; }; // THE COLLECTOR, run in the page. A string, not a function: a transpiler's // helpers must not leak into the page's scope. It walks the document, every // OPEN shadow root and every same-origin iframe; besides each text node it // reads the JOINED text of each element with more than one child (a literal // split across adjacent nodes — `zqsecret`) and the body's innerText. // Not read: a canvas, CSS `content:`, text only drawn on hover or in another // tab/query state, a closed shadow root, a cross-origin frame, scripts. const COLLECT = String.raw`(() => { const out = []; const sel = (el, prefix) => { const parts = []; let e = el; for (let d = 0; e && e.nodeType === 1 && d < 6; d++) { let p = e.tagName.toLowerCase(); const data = [...e.attributes].map((a) => a.name).filter((n) => n.startsWith("data-")).slice(0, 2); if (data.length) p += data.map((n) => "[" + n + "]").join(""); const par = e.parentElement; if (par) { const same = [...par.children].filter((c) => c.tagName === e.tagName); if (same.length > 1) p += ":nth-of-type(" + (same.indexOf(e) + 1) + ")"; } parts.unshift(p); if (e.tagName === "BODY") break; e = par; } return (prefix ? prefix + " >> " : "") + parts.join(" > "); }; const scopes = [{ root: document, prefix: "" }]; for (let i = 0; i < scopes.length && i < 200; i++) { const { root, prefix } = scopes[i]; for (const el of root.querySelectorAll("*")) { if (el.shadowRoot) scopes.push({ root: el.shadowRoot, prefix: sel(el, prefix) + " ::shadow" }); if (el.tagName === "IFRAME") { try { const d = el.contentDocument; if (d && d.documentElement) scopes.push({ root: d, prefix: sel(el, prefix) + " ::frame" }); } catch (e) {} } } } const SKIP = "script,style,noscript,template,option"; const ATTRS = ["title", "aria-label", "aria-description", "alt", "placeholder"]; for (const { root, prefix } of scopes) { const top = root.body || root.documentElement || root; if (!top) continue; const doc = root.nodeType === 9 ? root : root.ownerDocument; const walker = doc.createTreeWalker(top, NodeFilter.SHOW_TEXT); for (let n = walker.nextNode(); n; n = walker.nextNode()) { const t = n.nodeValue; if (!t || !t.trim()) continue; const p = n.parentElement; if (!p || p.closest(SKIP)) continue; out.push({ where: "text", selector: sel(p, prefix), text: t }); } // An element's own drawn text: its text nodes, never a script's or a // style's (textContent would read an inline