42639fcaf96f

query deep DOM roots once during detection (v5.3.28)

time
2026-11-22 20:47 UTC · 5 days ago
machine
Junes-MacBook-Pro.local
agent
gpt-5.6-sol
session
remediate interim audit findings
parent
1675d77b27bc
6 files manifest.firefox.json+1 −1manifest.json+1 −1package.json+1 −1src/content/content.detect.test.ts+6 −9src/content/detect.test.ts+88 −1src/content/detect.ts+9 −11
manifest.firefox.json+1 −1
@@ -1,7 +1,7 @@11 {22   "manifest_version": 3,33   "name": "Peach Password Manager",4-  "version": "5.3.27",4+  "version": "5.3.28",55   "description": "Zero-knowledge password manager",66   "browser_specific_settings": {77     "gecko": {

view file

manifest.json+1 −1
@@ -2,7 +2,7 @@22   "manifest_version": 3,33   "minimum_chrome_version": "116",44   "name": "Peach Password Manager",5-  "version": "5.3.27",5+  "version": "5.3.28",66   "description": "Zero-knowledge password manager",77   "action": {88     "default_popup": "src/popup/index.html",

view file

package.json+1 −1
@@ -1,6 +1,6 @@11 {22   "name": "peach",3-  "version": "5.3.27",3+  "version": "5.3.28",44   "description": "Zero-knowledge password manager",55   "license": "GPL-3.0-only",66   "type": "module",

view file

src/content/content.detect.test.ts+6 −9
@@ -24,13 +24,10 @@2424 } from "./detect";2525 import { makeFieldVisible, makeInput } from "./_setup";2626 27-// detect.ts's querySelectorAllDeep re-queries every ancestor element, so a field28-// nested under html>body>form appears 3× in detectLoginFields() output. The29-// committed detect.test.ts copes the same way we do here: assert detection by30-// *membership* (fields.some) and gate "is this page fillable?" through31-// isAnyCredentialForm (the exact predicate index.ts uses — true via the signup32-// branch even when duplication inflates the password count). The precise33-// login-vs-signup classifier contract is exercised on clean, hand-built arrays.27+// These tests assert detection by *membership* (fields.some) and gate "is this28+// page fillable?" through isAnyCredentialForm (the exact predicate index.ts29+// uses). The precise login-vs-signup classifier contract is exercised on clean,30+// hand-built arrays; detect.test.ts covers the end-to-end classification.3431 3532 beforeEach(() => {3633   document.body.innerHTML = "";@@ -182,8 +179,8 @@182179     document.body.appendChild(host);183180     const root = host.attachShadow({ mode: "open" });184181     // Real web-component login forms wrap fields in a container element inside185-    // the shadow root; querySelectorAllDeep collects inputs via each element's186-    // querySelectorAll, so the wrapper is what surfaces them.182+    // the shadow root; querySelectorAllDeep queries each shadow root directly,183+    // so both wrapped and direct children surface.187184     const wrapper = document.createElement("div");188185     root.appendChild(wrapper);189186     const user = document.createElement("input");

view file

src/content/detect.test.ts+88 −1
@@ -1,6 +1,6 @@11 // @vitest-environment jsdom22 3-import { beforeEach, describe, expect, it } from "vitest";3+import { beforeEach, describe, expect, it, vi } from "vitest";44 55 import {66   detectCardForm,@@ -10,6 +10,7 @@1010   isLoginForm,1111   isSignupForm,1212   isTOTPField,13+  querySelectorAllDeep,1314 } from "./detect";1415 1516 describe("content detect", () => {@@ -198,5 +199,91 @@198199   it("hasPaymentProcessorIframe — returns false on standard checkout page", () => {199200     document.body.innerHTML = '<input name="cardnum" />';200201     expect(hasPaymentProcessorIframe()).toBe(false);202+  });203+204+  it("querySelectorAllDeep — returns each match once regardless of nesting depth", () => {205+    document.body.innerHTML = '<div><div><div><div><form><input name="a" /></form></div></div></div></div>';206+    const matches = querySelectorAllDeep<HTMLInputElement>(document, "input");207+    expect(matches).toHaveLength(1);208+  });209+210+  it("querySelectorAllDeep — finds a field that is a direct child of a shadow root", () => {211+    const host = document.createElement("div");212+    document.body.appendChild(host);213+    const shadow = host.attachShadow({ mode: "open" });214+    const input = document.createElement("input");215+    input.name = "shadow-direct";216+    shadow.appendChild(input);217+218+    const matches = querySelectorAllDeep<HTMLInputElement>(document, "input");219+    expect(matches).toEqual([input]);220+  });221+222+  it("querySelectorAllDeep — finds a nested shadow tree and keeps light children once", () => {223+    const outer = document.createElement("div");224+    document.body.appendChild(outer);225+    const outerShadow = outer.attachShadow({ mode: "open" });226+    const lightInput = document.createElement("input");227+    outer.appendChild(lightInput);228+    const innerHost = document.createElement("div");229+    outerShadow.appendChild(innerHost);230+    const innerShadow = innerHost.attachShadow({ mode: "open" });231+    const deepInput = document.createElement("input");232+    innerShadow.appendChild(deepInput);233+234+    const matches = querySelectorAllDeep<HTMLInputElement>(document, "input");235+    expect(matches).toEqual([lightInput, deepInput]);236+  });237+238+  it("detectLoginFields — a nested username/password form classifies as login, not signup", () => {239+    document.body.innerHTML =240+      '<div><div><form><input name="username" autocomplete="username" />' +241+      '<input type="password" autocomplete="current-password" /></form></div></div>';242+    for (const input of Array.from(document.querySelectorAll("input"))) {243+      Object.defineProperty(input, "getBoundingClientRect", {244+        configurable: true,245+        value: () => ({ width: 100, height: 20, top: 0, left: 0, right: 100, bottom: 20 }),246+      });247+    }248+249+    const fields = detectLoginFields();250+    expect(fields.filter((field) => field.type === "password")).toHaveLength(1);251+    expect(isLoginForm(fields)).toBe(true);252+    expect(isSignupForm(fields)).toBe(false);253+  });254+255+  it("querySelectorAllDeep — uses a bounded number of queries for a deep chain", () => {256+    let markup = '<input name="leaf" />';257+    for (let depth = 0; depth < 40; depth++) markup = `<div>${markup}</div>`;258+    document.body.innerHTML = markup;259+260+    const spies = [261+      vi.spyOn(Element.prototype, "querySelectorAll"),262+      vi.spyOn(Document.prototype, "querySelectorAll"),263+      vi.spyOn(DocumentFragment.prototype, "querySelectorAll"),264+    ];265+    try {266+      const matches = querySelectorAllDeep<HTMLInputElement>(document, "input");267+      expect(matches).toHaveLength(1);268+      const calls = spies.reduce((total, spy) => total + spy.mock.calls.length, 0);269+      // One selector query plus one host scan for the single document root. The270+      // re-querying form issued one query per visited ancestor (~40 here).271+      expect(calls).toBe(2);272+    } finally {273+      for (const spy of spies) spy.mockRestore();274+    }275+  });276+277+  it("querySelectorAllDeep — still finds fields nested inside a shadow root", () => {278+    const host = document.createElement("div");279+    document.body.appendChild(host);280+    const shadow = host.attachShadow({ mode: "open" });281+    const wrapper = document.createElement("div");282+    const input = document.createElement("input");283+    wrapper.appendChild(input);284+    shadow.appendChild(wrapper);285+286+    const matches = querySelectorAllDeep<HTMLInputElement>(document, "input");287+    expect(matches).toEqual([input]);201288   });202289 });

view file

src/content/detect.ts+9 −11
@@ -69,17 +69,15 @@6969   root: Document | ShadowRoot | Element,7070   selector: string7171 ): T[] {72-  const results: T[] = [];73-  const walk = (node: Document | ShadowRoot | Element) => {74-    if (node instanceof Element) {75-      results.push(...(node.querySelectorAll<T>(selector)));76-      const shadowRoot = node.shadowRoot;77-      if (shadowRoot) {78-        shadowRoot.querySelectorAll<T>("*").forEach(walk);79-      }80-    }81-  };82-  root.querySelectorAll<T>("*").forEach(walk);72+  // One query per root, then recurse into each open shadow root. Re-querying73+  // every visited element returned a nested field once per ancestor and missed74+  // shadow roots' direct children.75+  const results = Array.from(root.querySelectorAll<T>(selector));76+  for (const host of root.querySelectorAll("*")) {77+    const shadowRoot = host.shadowRoot;78+    if (!shadowRoot) continue;79+    for (const match of querySelectorAllDeep<T>(shadowRoot, selector)) results.push(match);80+  }8381   return results;8482 }8583 

view file