Skip to content

Commit b0a3c52

Browse files
committed
fix(clerk-js): update inCrossOriginIframe to handle nested cross origin iframes
This handles the following dom structure ``` top (replit.com) - iframe1 (foo.janeway.replit.dev/__iframe.html) - iframe2(foo.janeway.replit.dev; Clerk loaded here) ```
1 parent 42f0d95 commit b0a3c52

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

packages/clerk-js/src/utils/runtime.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,19 @@ export function inIframe() {
2121
}
2222

2323
export function inCrossOriginIframe() {
24-
// https://developer.mozilla.org/en-US/docs/Web/API/Window/frameElement
25-
// frameElement: if the document into which it's embedded has a different origin, the value is null instead.
26-
return inIframe() && !window.frameElement;
24+
if (!inIframe()) {
25+
return false;
26+
}
27+
28+
try {
29+
// Try to access top window's location to check if any ancestor is cross-origin
30+
// This will throw a SecurityError if any iframe in the chain is cross-origin
31+
// Handles nested iframes where immediate parent might be same-origin
32+
// but a higher-level ancestor is cross-origin
33+
void window.top?.location.href;
34+
return false;
35+
} catch {
36+
// SecurityError thrown - we're in a cross-origin iframe (at any level)
37+
return true;
38+
}
2739
}

0 commit comments

Comments
 (0)