Skip to content

The agent that grows with you

fix(vault): shadow-root fills reach host listeners and shadow login tabs

Tekniumb79946d2ee78
diff --git a/agent/vault_login_classifier.py b/agent/vault_login_classifier.py
index 8ed0f81a287..cee60f0b334 100644
--- a/agent/vault_login_classifier.py
+++ b/agent/vault_login_classifier.py
@@ -318,38 +318,36 @@ _FILL_JS_TEMPLATE = """(() => {
}
const fills = __FILLS__;
const nonce = __NONCE__;
- // Shadow-piercing lookup: stamped controls may live inside a shadow root (see the
- // shadow-piercing collect in the inspection script), so plain document.querySelector
- // would never find them.
- const shadowQuery = (selector) => {
- const out = [];
- (function walk(root) {
- for (const n of root.querySelectorAll(selector)) out.push(n);
- for (const e of root.querySelectorAll("*")) if (e.shadowRoot) walk(e.shadowRoot);
- })(document);
- return out;
- };
+ // Stamped controls may live inside a shadow root (see the inspection script's walk): collect every
+ // stamp once, across open shadow roots, and reuse the map for the fills and the cleanup.
+ const stamped = [];
+ (function walk(root) {
+ for (const n of root.querySelectorAll("[data-hermes-vault-slot]")) stamped.push(n);
+ for (const e of root.querySelectorAll("*")) if (e.shadowRoot) walk(e.shadowRoot);
+ })(document);
+ const slots = new Map(stamped.map((n) => [n.getAttribute("data-hermes-vault-slot"), n]));
let filled = 0;
const norm = (t) => String(t || "").trim().toLowerCase();
for (const f of fills) {
- const el = shadowQuery('[data-hermes-vault-slot="' + nonce + ':' + f.index + '"]')[0];
+ const el = slots.get(nonce + ":" + f.index);
if (!el || (f.token === "current-password" && el.type !== "password")) continue;
try {
if (el.tagName === "SELECT") {
const want = norm(f.value);
const opt = Array.from(el.options).find((o) => [o.value, o.textContent].some((t) => norm(t) === want || norm(t) === want.replace(/^0/, "")));
- if (opt) { el.value = opt.value; el.dispatchEvent(new Event("change", { bubbles: true })); filled += 1; }
+ if (opt) { el.value = opt.value; el.dispatchEvent(new Event("change", { bubbles: true, composed: true })); filled += 1; }
continue;
}
el.focus();
const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value");
// one-time-code split into single-character boxes: f.value is the slice for THIS box (see build_otp_fills)
if (setter && setter.set) { setter.set.call(el, f.value); } else { el.value = f.value; }
- el.dispatchEvent(new InputEvent("input", { bubbles: true, inputType: "insertText" }));
- el.dispatchEvent(new Event("change", { bubbles: true }));
+ // composed: a component listening on its shadow host must see the value change too.
+ el.dispatchEvent(new InputEvent("input", { bubbles: true, composed: true, inputType: "insertText" }));
+ el.dispatchEvent(new Event("change", { bubbles: true, composed: true }));
if (el.value.length > 0) filled += 1;
} catch (e) { /* skip */ }
}
- shadowQuery("[data-hermes-vault-slot]").forEach((n) => n.removeAttribute("data-hermes-vault-slot"));
+ stamped.forEach((n) => n.removeAttribute("data-hermes-vault-slot"));
return JSON.stringify({ filled });
})()"""
diff --git a/tools/browser_vault_tool.py b/tools/browser_vault_tool.py
index b6c8406ba2b..baf2dd0f048 100644
--- a/tools/browser_vault_tool.py
+++ b/tools/browser_vault_tool.py
@@ -203,11 +203,18 @@ def _current_page_origin(task_id: str) -> Optional[str]:
return None
+def _deep_probe(selector: str) -> str:
+ """A JS expression truthy when ``selector`` matches in the document or any open shadow root (the
+ inspection and fill scripts reach shadow-root forms, so tab selection must too)."""
+ return ("((s) => { const w = (r) => r.querySelector(s) || Array.from(r.querySelectorAll('*'))"
+ ".some((e) => e.shadowRoot && w(e.shadowRoot)); return !!w(document); })(" + json.dumps(selector) + ")")
+
+
# Per kind: a JS probe that is truthy on a tab holding the form this kind fills.
_TAB_PROBES = {
- "login": "!!document.querySelector('input[type=password]')",
- "payment": "!!document.querySelector('input[autocomplete^=cc-], [name*=card i], [placeholder*=card i], [name*=cvc i], [name*=cvv i]')",
- "address": "!!document.querySelector('input[autocomplete^=address-], [autocomplete=postal-code], [name*=address i], [name*=zip i], [name*=postal i]')",
+ "login": _deep_probe("input[type=password]"),
+ "payment": _deep_probe("input[autocomplete^=cc-], [name*=card i], [placeholder*=card i], [name*=cvc i], [name*=cvv i]"),
+ "address": _deep_probe("input[autocomplete^=address-], [autocomplete=postal-code], [name*=address i], [name*=zip i], [name*=postal i]"),
}
@@ -367,8 +374,8 @@ def browser_vault_save_login(label: str = "", task_id: Optional[str] = None) ->
ensure_ascii=False)
-_TAB_PROBES["otp"] = ("!!document.querySelector('input[autocomplete=one-time-code], input[name*=otp i], input[name*=code i], "
- "input[id*=otp i], input[id*=code i], input[name*=totp i], input[aria-label*=code i]')")
+_TAB_PROBES["otp"] = _deep_probe("input[autocomplete=one-time-code], input[name*=otp i], input[name*=code i], "
+ "input[id*=otp i], input[id*=code i], input[name*=totp i], input[aria-label*=code i]")
def _saved_login(handle: str) -> tuple[Any, Any]: