Skip to content

Commit 632b8f9

Browse files
Avoid triggering security errors for iframes
1 parent 0e2e27e commit 632b8f9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

injected/src/features/page-context.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ function isHtmlElement(node) {
3636
* @returns {Document | null}
3737
*/
3838
function getSameOriginIframeDocument(iframe) {
39+
// Pre-check conditions that would prevent access without triggering security errors
40+
const src = iframe.src;
41+
const sandbox = iframe.sandbox;
42+
43+
// Skip sandboxed iframes unless they explicitly allow scripts
44+
// Avoids: Blocked script execution in 'about:blank' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.
45+
if (sandbox && sandbox.length > 0 && !sandbox.contains('allow-scripts')) {
46+
return null;
47+
}
48+
49+
// Check for cross-origin URLs (but allow about:blank and empty src as they inherit parent origin)
50+
if (src && src !== 'about:blank' && src !== '') {
51+
try {
52+
const iframeUrl = new URL(src, window.location.href);
53+
if (iframeUrl.origin !== window.location.origin) {
54+
return null;
55+
}
56+
} catch (e) {
57+
// Invalid URL, skip
58+
return null;
59+
}
60+
}
61+
3962
try {
4063
// Try to access the contentDocument - this will throw if cross-origin
4164
const doc = iframe.contentDocument;

0 commit comments

Comments
 (0)