Skip to content

Commit c20f858

Browse files
committed
fix(react-dom): check if iframe belongs to the same origin
The try / catch block doesn't catch cross domain security error but it doesn't affect the code execution flow. This mean that the code after the try / catch block will be executed. We can do the following To check if the parent page has access to the iframe document: ``javascript` let hasAccessToDocument = false; // declare an unitialized variable try { iframe.contentWindow.location.href; // try to access the iframe property hasAccessToDocument = href != null; // This line will be executed if it has access } catch (err) { // Catch block is not executed since the browser throws a cross-domain error } return hasAccessToDocument; // This value will be set to true if the parent page has access to the iframe content. ```
1 parent 2aabdf5 commit c20f858

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

packages/react-dom/src/client/ReactInputSelection.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,31 @@ function isInDocument(node) {
4040
);
4141
}
4242

43+
function isSameOriginFrame(iframe) {
44+
let hasAccessToDocument = false;
45+
try {
46+
const href = iframe.contentWindow.location.href;
47+
// This line is only invoked if the iframe belongs to the same domain
48+
hasAccessToDocument = href != null;
49+
} catch (err) {
50+
// Catch block is not executed since the browser throws a cross-domain error
51+
}
52+
53+
return hasAccessToDocument;
54+
}
55+
4356
function getActiveElementDeep() {
4457
let win = window;
4558
let element = getActiveElement();
4659
while (element instanceof win.HTMLIFrameElement) {
4760
// Accessing the contentWindow of a HTMLIframeElement can cause the browser
4861
// to throw, e.g. if it has a cross-origin src attribute
4962
try {
50-
win = element.contentWindow;
63+
if (isSameOriginFrame(element)) {
64+
win = element.contentWindow;
65+
} else {
66+
return element;
67+
}
5168
} catch (e) {
5269
return element;
5370
}

0 commit comments

Comments
 (0)