Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
Ignore closed dialogs when scoping file inputs
Claudeabd73b7e907a
diff --git a/src/chrome/src/content/content.js b/src/chrome/src/content/content.jsindex 93a677e6c..2f549a454 100644--- a/src/chrome/src/content/content.js+++ b/src/chrome/src/content/content.js@@ -4258,11 +4258,21 @@// /compose/post as a dialog over the home timeline, and the timeline's// inline composer keeps an identical hidden input (same accept and// multiple, no id or name, same ten nearest ancestors): only the dialog- // tells the two apart.+ // tells the two apart. A closed dialog that a page keeps in the DOM+ // (hidden, aria-hidden, inert or not rendered) holds no composer the model+ // opened, so its input is neither marked nor scoped by it.function _fileInputDialogScopes(el) {return ['[role="dialog"]', '[role="alertdialog"]', '[aria-modal="true"]', 'dialog[open]'].filter(scope => {- try { return !!el.closest(scope); } catch { return false; }+ try {+ const dialog = el.closest(scope);+ if (!dialog || dialog.closest('[hidden],[aria-hidden="true"],[inert]')) return false;+ if (typeof dialog.checkVisibility === 'function') {+ return dialog.checkVisibility({ checkVisibilityCSS: true, visibilityProperty: true });+ }+ const style = getComputedStyle(dialog);+ return dialog.getClientRects().length > 0 && !['hidden', 'collapse'].includes(style.visibility);+ } catch { return false; }});}diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.jsindex caabbda46..ede18bb76 100644--- a/src/firefox/src/content/content.js+++ b/src/firefox/src/content/content.js@@ -1363,11 +1363,21 @@// /compose/post as a dialog over the home timeline, and the timeline's// inline composer keeps an identical hidden input (same accept and// multiple, no id or name, same ten nearest ancestors): only the dialog- // tells the two apart.+ // tells the two apart. A closed dialog that a page keeps in the DOM+ // (hidden, aria-hidden, inert or not rendered) holds no composer the model+ // opened, so its input is neither marked nor scoped by it.function _fileInputDialogScopes(el) {return ['[role="dialog"]', '[role="alertdialog"]', '[aria-modal="true"]', 'dialog[open]'].filter(scope => {- try { return !!el.closest(scope); } catch { return false; }+ try {+ const dialog = el.closest(scope);+ if (!dialog || dialog.closest('[hidden],[aria-hidden="true"],[inert]')) return false;+ if (typeof dialog.checkVisibility === 'function') {+ return dialog.checkVisibility({ checkVisibilityCSS: true, visibilityProperty: true });+ }+ const style = getComputedStyle(dialog);+ return dialog.getClientRects().length > 0 && !['hidden', 'collapse'].includes(style.visibility);+ } catch { return false; }});}diff --git a/test/fixtures/run.mjs b/test/fixtures/run.mjsindex aaf759600..cdb1aa1d9 100644--- a/test/fixtures/run.mjs+++ b/test/fixtures/run.mjs@@ -4477,6 +4477,35 @@ for (const browserKind of ['chrome', 'firefox']) {}});+ // A closed dialog a page keeps in the DOM holds no composer the model+ // opened: its input is not marked inDialog, and no selector is scoped to it.+ // Both inputs share their ten nearest ancestors, so a dialog scope would+ // otherwise be the first candidate that tells them apart.+ const fileSlot = `<div><div><div><div><div><div><div><div><div>+ <input type="file" accept="image/*">+ </div></div></div></div></div></div></div></div></div>`;+ for (const [state, open, close] of [+ ['hidden', '<div role="dialog" hidden>', '</div>'],+ ['aria-hidden', '<div role="dialog" aria-hidden="true">', '</div>'],+ ['inert', '<div role="dialog" aria-modal="true" inert>', '</div>'],+ ['unrendered', '<div role="dialog" style="display: none">', '</div>'],+ ['closed native', '<dialog role="dialog">', '</dialog>'],+ ]) {+ test(`get_interactive_elements (${browserKind}): a file input in a closed dialog (${state}) is not marked inDialog`, async (page) => {+ const setupHtml = browserKind === 'firefox' ? setupFirefoxHtml : setupChromeHtml;+ await setupHtml(page, `<!doctype html>+ <section aria-label="Cover image">${fileSlot}</section>+ ${open}<p>Avatar</p>${fileSlot}${close}`);+ const records = (await call(page, 'get_interactive_elements_cdp', {})).filter((element) => element.type === 'file');+ if (records.length !== 2 || records.some((record) => Object.hasOwn(record, 'inDialog'))) {+ throw new Error(`a closed dialog (${state}) must not mark its input inDialog: ${JSON.stringify(records)}`);+ }+ if (records.some((record) => !record.selector || /\[role="dialog"\]|\[aria-modal="true"\]|dialog\[open\]/.test(record.selector))) {+ throw new Error(`each input needs a selector not scoped to a closed dialog (${state}): ${JSON.stringify(records)}`);+ }+ });+ }+test(`file picker guard (${browserKind}): blocks the native chooser and returns the exact input`, async (page) => {await setupContentHtml(page, `<!doctype html><button id="choose">Select a file...</button>