Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

fix: support trusted Firefox BiDi clicks in shadow roots

Emre Sokullu59edf0375a9a
diff --git a/firefox-companion/session.mjs b/firefox-companion/session.mjs
index a1ed9b363..b79672ce1 100644
--- a/firefox-companion/session.mjs
+++ b/firefox-companion/session.mjs
@@ -146,8 +146,22 @@ export class BidiSession {
const px = x === null ? r.x+r.width/2 : x;
const py = y === null ? r.y+r.height/2 : y;
if (px < 0 || py < 0 || px >= innerWidth || py >= innerHeight) return false;
- const hit = document.elementFromPoint(px, py);
- if (!r.width || !r.height || !(hit === el || el.contains(hit))) return false;
+ let hit = document.elementFromPoint(px, py);
+ // elementFromPoint stops at each open shadow host. Descend through those
+ // roots so trusted input accepts the same visible target as content.js.
+ while (hit?.shadowRoot) {
+ const inner = hit.shadowRoot.elementFromPoint(px, py);
+ if (!inner || inner === hit) break;
+ hit = inner;
+ }
+ const reaches = (node, target) => {
+ while (node) {
+ if (node === target) return true;
+ node = node.parentNode || node.host;
+ }
+ return false;
+ };
+ if (!r.width || !r.height || !reaches(hit, el)) return false;
if (action === 'type' || action === 'field' || action === 'key') {
el.focus({preventScroll:true});
if (el.getRootNode().activeElement !== el) return false;
diff --git a/test/firefox-bidi-e2e.mjs b/test/firefox-bidi-e2e.mjs
index 9e2354311..001ef4dd3 100644
--- a/test/firefox-bidi-e2e.mjs
+++ b/test/firefox-bidi-e2e.mjs
@@ -93,6 +93,14 @@ try {
assert.equal(JSON.parse(belowFoldPrepared.value).bidiPrepared, true);
assert.equal((await session.perform(runId, 'click', { token: belowFoldToken, url })).success, true);
assert.equal((await evaluate('window.belowFoldTrusted')).value, true);
+ const shadowToken = crypto.randomUUID();
+ await evaluate(`const host=document.createElement('div'); host.id='shadow-host'; host.style.cssText='position:fixed;left:20px;top:300px'; host.attachShadow({mode:'open'}).innerHTML='<button id="shadow-button">Shadow button</button>'; host.shadowRoot.querySelector('button').setAttribute('data-webbrain-bidi', ${JSON.stringify(shadowToken)}); host.shadowRoot.querySelector('button').onclick=e=>window.shadowTrusted=e.isTrusted; document.body.append(host);`);
+ const shadowNode = await session.send('script.evaluate', { target: { context }, expression: "document.querySelector('#shadow-host').shadowRoot.querySelector('#shadow-button')", awaitPromise: true });
+ const locate = session.locate;
+ session.locate = async () => ({ context, node: { sharedId: shadowNode.result.sharedId } });
+ assert.equal((await session.perform(runId, 'click', { token: shadowToken, url })).success, true);
+ session.locate = locate;
+ assert.equal((await evaluate('window.shadowTrusted')).value, true);
const obscured = await mark('#point-canvas');
await evaluate(`document.body.insertAdjacentHTML('beforeend','<div id="point-cover" style="position:fixed;left:20px;top:205px;width:10px;height:10px;background:red;z-index:99999"></div>')`);
await assert.rejects(session.perform(runId,'click',{...obscured,point:{x:22,y:210}}), error=>error.dispatchState.noDispatch===true && /covered/.test(error.message));
diff --git a/test/firefox-bidi.mjs b/test/firefox-bidi.mjs
index 67623d130..7c8c7c4de 100644
--- a/test/firefox-bidi.mjs
+++ b/test/firefox-bidi.mjs
@@ -233,6 +233,16 @@ test('trusted checkable clicks report a prevented state transition', async () =>
assert.equal(result.checkedAfter, false);
assert.equal(result.checkedChanged, false);
});
+test('trusted target validation descends through open shadow roots', async () => {
+ const session = new BidiSession(); const runId = id(); let validation;
+ session.runs.set(runId, { context: 'tab' });
+ session.locate = async () => ({ context: 'tab', node: { sharedId: 'shadow-button' } });
+ session.call = async (_match, fn) => { validation = fn; return { result: { value: true } }; };
+ session.send = async () => ({});
+ await session.perform(runId, 'click', {});
+ assert.match(validation, /shadowRoot\.elementFromPoint/);
+ assert.match(validation, /node\.parentNode \|\| node\.host/);
+});
test('post-dispatch failure and transport uncertainty are never safe retries', async () => {
const session = new BidiSession(); const runId = id();
session.runs.set(runId, {context:'tab'});