Skip to content

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

fix(linkedin): enforce active modal ownership

16Miku1d665ae5f0a4
diff --git a/src/chrome/src/content/content.js b/src/chrome/src/content/content.js
index 518729d81..aee2a25d0 100644
--- a/src/chrome/src/content/content.js
+++ b/src/chrome/src/content/content.js
@@ -346,6 +346,22 @@
return null;
}
+ function _someComposedDescendant(root, selector, predicate, limit = 2000) {
+ const pending = [root];
+ let visited = 0;
+ while (pending.length && visited < limit) {
+ const scope = pending.shift();
+ const elements = scope?.querySelectorAll?.('*') || [];
+ for (const element of elements) {
+ visited += 1;
+ if (element.matches?.(selector) && predicate(element)) return true;
+ if (element.shadowRoot) pending.push(element.shadowRoot);
+ if (visited >= limit) break;
+ }
+ }
+ return false;
+ }
+
function isVisiblyInteractive(el) {
if (!el || el.tagName === 'BODY' || el.tagName === 'HTML') return false;
// aria-hidden / inert subtrees are non-interactive for assistive tech
@@ -5176,14 +5192,26 @@
|| _composedClosestElement(link, 'form')
|| link.hasAttribute?.('download')
|| (link.getAttribute?.('role') && link.getAttribute('role') !== 'link')) return 'blocked';
+ const heuristicModalSelector = '[data-overlay],.modal.show,.modal-overlay,.overlay,'
+ + '[class*="modal"][class*="open"],[class*="overlay"][class*="active"],'
+ + '[class*="DialogOverlay"],[class*="ModalOverlay"]';
const composedModal = _composedClosestElement(
link,
- 'dialog,[role="dialog"],[role="alertdialog"],[data-overlay],.modal.show,.modal-overlay,.overlay,'
- + '[class*="modal"][class*="open"],[class*="overlay"][class*="active"],'
- + '[class*="DialogOverlay"],[class*="ModalOverlay"]',
+ `dialog,[role="dialog"],[role="alertdialog"],${heuristicModalSelector}`,
);
const modal = composedModal
|| (blockingModal && _isComposedAncestor(blockingModal, link) ? blockingModal : null);
+ const modalIsBlocking = !!modal && (
+ modal === blockingModal
+ || (modal.tagName === 'DIALOG' && modal.hasAttribute('open') && _isNativeBlockingDialog(modal))
+ || (/^(?:dialog|alertdialog)$/.test(modal.getAttribute?.('role') || '')
+ && modal.getAttribute?.('aria-modal') === 'true')
+ || (() => {
+ if (!modal.matches?.(heuristicModalSelector)) return false;
+ const rect = modal.getBoundingClientRect();
+ return rect.width > 100 && rect.height > 100;
+ })()
+ );
const unresolved = () => modal ? 'blocked' : 'none';
try {
const href = String(link.getAttribute('href') || '').trim();
@@ -5208,8 +5236,8 @@
if (modal) {
const contactInfoOverlay = /^\/in\/([^/]+)\/overlay\/contact-info\/?$/.exec(location.pathname);
const profilePath = contactInfoOverlay ? `/in/${contactInfoOverlay[1]}` : '';
- const ownsContactInfoRoute = !!profilePath && Array.from(modal.querySelectorAll?.('a[href]') || [])
- .some((candidate) => {
+ const ownsContactInfoRoute = modalIsBlocking && !!profilePath
+ && _someComposedDescendant(modal, 'a[href]', (candidate) => {
try {
const candidateUrl = new URL(candidate.getAttribute('href'), document.baseURI);
return isLinkedInHost(candidateUrl.hostname)
@@ -5219,10 +5247,11 @@
}
});
if (!ownsContactInfoRoute || !verifiedExternalRedirect) return 'blocked';
+ return 'navigation';
}
if (!linkedInDestination) return 'navigation';
- return (verifiedExternalRedirect
- || /^\/(?:feed|jobs|mynetwork|messaging|notifications)\/?$/.test(destination.pathname)
+ if (redirectPath) return 'blocked';
+ return (/^\/(?:feed|jobs|mynetwork|messaging|notifications)\/?$/.test(destination.pathname)
|| /^\/in\/[^/]+\/overlay\/contact-info\/?$/.test(destination.pathname))
? 'navigation'
: 'none';
diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.js
index 4fab3b570..c4c0e88ea 100644
--- a/src/firefox/src/content/content.js
+++ b/src/firefox/src/content/content.js
@@ -569,6 +569,22 @@
return null;
}
+ function _someComposedDescendant(root, selector, predicate, limit = 2000) {
+ const pending = [root];
+ let visited = 0;
+ while (pending.length && visited < limit) {
+ const scope = pending.shift();
+ const elements = scope?.querySelectorAll?.('*') || [];
+ for (const element of elements) {
+ visited += 1;
+ if (element.matches?.(selector) && predicate(element)) return true;
+ if (element.shadowRoot) pending.push(element.shadowRoot);
+ if (visited >= limit) break;
+ }
+ }
+ return false;
+ }
+
function isVisiblyInteractive(el) {
if (!el || el.tagName === 'BODY' || el.tagName === 'HTML') return false;
if (_hasComposedClosest(el, '[aria-hidden="true"], [inert]')) return false;
@@ -4339,14 +4355,26 @@
|| _composedClosestElement(link, 'form')
|| link.hasAttribute?.('download')
|| (link.getAttribute?.('role') && link.getAttribute('role') !== 'link')) return 'blocked';
+ const heuristicModalSelector = '[data-overlay],.modal.show,.modal-overlay,.overlay,'
+ + '[class*="modal"][class*="open"],[class*="overlay"][class*="active"],'
+ + '[class*="DialogOverlay"],[class*="ModalOverlay"]';
const composedModal = _composedClosestElement(
link,
- 'dialog,[role="dialog"],[role="alertdialog"],[data-overlay],.modal.show,.modal-overlay,.overlay,'
- + '[class*="modal"][class*="open"],[class*="overlay"][class*="active"],'
- + '[class*="DialogOverlay"],[class*="ModalOverlay"]',
+ `dialog,[role="dialog"],[role="alertdialog"],${heuristicModalSelector}`,
);
const modal = composedModal
|| (blockingModal && _isComposedAncestor(blockingModal, link) ? blockingModal : null);
+ const modalIsBlocking = !!modal && (
+ modal === blockingModal
+ || (modal.tagName === 'DIALOG' && modal.hasAttribute('open') && _isNativeBlockingDialog(modal))
+ || (/^(?:dialog|alertdialog)$/.test(modal.getAttribute?.('role') || '')
+ && modal.getAttribute?.('aria-modal') === 'true')
+ || (() => {
+ if (!modal.matches?.(heuristicModalSelector)) return false;
+ const rect = modal.getBoundingClientRect();
+ return rect.width > 100 && rect.height > 100;
+ })()
+ );
const unresolved = () => modal ? 'blocked' : 'none';
try {
const href = String(link.getAttribute('href') || '').trim();
@@ -4371,8 +4399,8 @@
if (modal) {
const contactInfoOverlay = /^\/in\/([^/]+)\/overlay\/contact-info\/?$/.exec(location.pathname);
const profilePath = contactInfoOverlay ? `/in/${contactInfoOverlay[1]}` : '';
- const ownsContactInfoRoute = !!profilePath && Array.from(modal.querySelectorAll?.('a[href]') || [])
- .some((candidate) => {
+ const ownsContactInfoRoute = modalIsBlocking && !!profilePath
+ && _someComposedDescendant(modal, 'a[href]', (candidate) => {
try {
const candidateUrl = new URL(candidate.getAttribute('href'), document.baseURI);
return isLinkedInHost(candidateUrl.hostname)
@@ -4382,10 +4410,11 @@
}
});
if (!ownsContactInfoRoute || !verifiedExternalRedirect) return 'blocked';
+ return 'navigation';
}
if (!linkedInDestination) return 'navigation';
- return (verifiedExternalRedirect
- || /^\/(?:feed|jobs|mynetwork|messaging|notifications)\/?$/.test(destination.pathname)
+ if (redirectPath) return 'blocked';
+ return (/^\/(?:feed|jobs|mynetwork|messaging|notifications)\/?$/.test(destination.pathname)
|| /^\/in\/[^/]+\/overlay\/contact-info\/?$/.test(destination.pathname))
? 'navigation'
: 'none';
diff --git a/test/fixtures/message-recipient-navigation.mjs b/test/fixtures/message-recipient-navigation.mjs
index 62c81f7a4..0f61094f9 100644
--- a/test/fixtures/message-recipient-navigation.mjs
+++ b/test/fixtures/message-recipient-navigation.mjs
@@ -205,6 +205,41 @@ export function registerMessageRecipientNavigationFixtures({
assert.equal((await guard('click_ax', args))?.noDispatch, true);
});
+ register(`${kind}: LinkedIn Contact info ownership crosses open shadow roots`, async (page) => {
+ const { guard, probe } = await setup(page);
+ await page.evaluate(() => {
+ history.replaceState(null, '', '/in/alice/overlay/contact-info/');
+ const dialog = document.querySelector('#contact-info-dialog');
+ dialog.hidden = false;
+ const profile = document.querySelector('#contact-profile');
+ const host = document.createElement('span');
+ dialog.append(host);
+ host.attachShadow({ mode: 'open' }).append(profile);
+ });
+ const args = { text: 'portfolio.example', textMatch: 'exact' };
+ const result = await probe('click', args);
+ assert.equal(result.navigation, true, JSON.stringify(result));
+ assert.equal(await guard('click', args), null);
+ });
+
+ register(`${kind}: LinkedIn non-blocking dialogs cannot claim Contact info redirects`, async (page) => {
+ const { guard, probe } = await setup(page);
+ await page.evaluate(() => {
+ history.replaceState(null, '', '/in/alice/overlay/contact-info/');
+ document.querySelector('#chat').hidden = false;
+ document.body.insertAdjacentHTML('beforeend', `
+ <div role="dialog" style="position:fixed;inset:120px;background:white">
+ <a href="/in/alice/">linkedin.com/in/alice</a>
+ <a id="non-blocking-dialog-link" href="/safety/go/?url=https%3A%2F%2Fportfolio.example%2F">Non-blocking dialog link</a>
+ </div>`);
+ });
+ const args = { selector: '#non-blocking-dialog-link' };
+ const result = await probe('click', args);
+ assert.equal(result.navigationBlocked, true, JSON.stringify(result));
+ assert.equal(result.conclusive, false, JSON.stringify(result));
+ assert.equal((await guard('click', args))?.noDispatch, true);
+ });
+
register(`${kind}: LinkedIn safety redirects cannot escape a heuristic modal`, async (page) => {
const { guard, probe } = await setup(page);
await page.evaluate(() => {
@@ -256,6 +291,7 @@ export function registerMessageRecipientNavigationFixtures({
'https://linkedin.com/messaging/send/',
'https://m.linkedin.com/messaging/send/',
'https://m.linkedin.com./messaging/send/',
+ '/safety/go/?url=https%3A%2F%2Fportfolio.example%2F',
'/safety/go/',
'/safety/go/?url=javascript%3Aalert(1)',
'/safety/go/?url=https%3A%2F%2Fwww.linkedin.com%2Fmessaging%2Fsend',