Skip to content

Commit de5acba

Browse files
fix(clerk-js): Update inCrossOriginIframe to handle nested cross origin iframes (#7212)
1 parent d7d0704 commit de5acba

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

.changeset/cute-apes-watch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
fix(clerk-js): update inCrossOriginIframe to handle nested cross origin iframes

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)