fix(server): keep sensitive values out of settings reads, startup logs and preview snapshots #20446
Mergedjuliusmarmingemerged 3 commits into
main fromrefs/pull/20446/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
juliusmarminge added labelsmacroscope-review
- apps/server/src/preview/ServerBrowserPage.ts line 423Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -177,13 +400,19 @@177400 readonly page: Page;178401 readonly cdp: CDPSession;179402 readonly renderScale: number;180403 readonly consoleEntries: ReadonlyArray<PreviewAutomationConsoleEntry>;181404 readonly networkEntries: ReadonlyArray<PreviewAutomationNetworkEntry>;182405 readonly actionTimeline: PreviewAutomationSnapshot["actionTimeline"];183-}): Promise<PreviewAutomationSnapshot> => {406+};407+408+const capture = async (409+ input: SnapshotInput,410+ deadline: number,411+): Promise<PreviewAutomationSnapshot> => {184412 const viewport = input.page.viewportSize() ?? { width: 1280, height: 800 };185413 const scale = Math.min(1, MAX_SCREENSHOT_WIDTH / (viewport.width * input.renderScale));186414 const state = refsFor(input.page);187415 invalidateRefs(input.page);188416 const generation = state.generation;189- const [page, tree, data] = await Promise.all([417+ // Awaited after the tree: a capture that fails has finished its tree before a retry starts.418+ const pageAndScreenshot = Promise.all([apps/server/src/preview/ServerBrowserPage.ts line 465OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -211,3 +461,5 @@211461 });212462 return {213463 ...page,464+ // Text that holds a value would otherwise show where the tree masked it.465+ visibleText: mask(page.visibleText),apps/server/src/preview/ServerBrowserPage.ts lines 334 to 334OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -150,0 +314,21 @@314+ * four characters only matches a whole word; inside other words it would mask315+ * unrelated text.316+ */317+const dottedPattern = (values: ReadonlyArray<string>) => {318+ const sources = values319+ .flatMap((value) => {320+ const json = JSON.stringify(value).slice(1, -1);321+ // Playwright writes control characters like JSON does, with \xNN for the unnamed ones.322+ const quoted = value.replace(/[\\"\p{Cc}]/gu, (char) =>323+ '\\"\b\f\n\r\t'.includes(char)324+ ? JSON.stringify(char).slice(1, -1)325+ : `\\x${char.charCodeAt(0).toString(16).padStart(2, "0")}`,326+ );327+ const forms = [value, json, quoted].flatMap((form) => [form, form.replaceAll("'", "''")]);328+ return [...new Set(forms)].map((form) => ({ form, word: value.length < 4 }));329+ })330+ // Longest first, so a form inside another never leaves the rest showing.331+ .toSorted((a, b) => b.form.length - a.form.length)332+ .map(({ form, word }) => {333+ const literal = form.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");334+ return word ? `(?<![^\\s"])${literal}(?![^\\s"])` : literal;apps/server/src/preview/ServerBrowserPage.ts lines 167 to 168OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -148,3 +148,21 @@148148 };149149 })()`;150150151+const MASKED_VALUE = "••••••••";152+// The broker waits 15s for the host; a snapshot, retry included, answers well before.153+const SNAPSHOT_TIMEOUT_MS = 10_000;154+// How long a frame in another process gets to answer; one that crashed never does.155+const FRAME_ANSWER_MS = 1_000;156+157+// Values the page draws as dots (password fields, and text fields whose CSS158+// draws dots), normalized like accessible text. Open shadow roots included.159+const DOTTED_SCRIPT = `(() => {160+ const values = [];161+ const visit = (root) => {162+ for (const element of root.querySelectorAll("*")) {163+ if (element.shadowRoot) visit(element.shadowRoot);164+ if (element.localName !== "input" && element.localName !== "textarea") continue;165+ const dots = getComputedStyle(element).getPropertyValue("-webkit-text-security");166+ if (element.type !== "password" && dots === "none") continue;167+ for (const value of [element.value, element.defaultValue])168+ values.push(value.replace(/[\\u200b\\u00ad]/g, "").trim().replace(/\\s+/g, " "));
- @@ -226 +478,21 @@226478479+export const snapshot = async (input: SnapshotInput): Promise<PreviewAutomationSnapshot> => {480+ const deadline = Date.now() + SNAPSHOT_TIMEOUT_MS;481+ let timer: ReturnType<typeof setTimeout> | undefined;482+ const expired = new Promise<never>((_, reject) => {483+ timer = setTimeout(() => reject(tooBusy()), SNAPSHOT_TIMEOUT_MS);484+ });485+ // A page that changed mid-capture, like one rotating ads, has usually settled by a second486+ // try. None starts late, since it would revoke the refs of whichever snapshot came next.487+ const captured = capture(input, deadline).catch((cause: unknown) =>488+ Date.now() < deadline && !(cause instanceof Error && cause.message === VALUE_TOO_LONG)489+ ? capture(input, deadline)490+ : Promise.reject(cause),491+ );492+ try {493+ return await Promise.race([captured, expired]);494+ } catch (cause) {495+ throw cause instanceof Error && cause.name === "TimeoutError" ? tooBusy() : cause;496+ } finally {497+ clearTimeout(timer);498+ }
juliusmarminge merged this8653c8f
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed