Skip to content

Commit 1f205f3

Browse files
committed
[core] fix cors issues in client.meta frame logic
1 parent aa0d503 commit 1f205f3

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/client/client.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,24 @@ export class ScramjetClient {
236236
throw new Error("topFrameName was called from a worker?");
237237

238238
let currentWin = client.global;
239-
if (currentWin.parent.window == currentWin.window) {
240-
// we're top level & we don't have a frame name
241-
return null;
239+
240+
try {
241+
if (currentWin.parent.window == currentWin.window) {
242+
// we're top level & we don't have a frame name
243+
return null;
244+
}
245+
} catch {
246+
// accessing parent was blocked by CORS, we're in a frame but the parent is cross origin
242247
}
243248

244-
// find the topmost frame that's controlled by scramjet, stopping before the real top frame
245-
while (currentWin.parent.window !== currentWin.window) {
246-
if (!currentWin.parent.window[SCRAMJETCLIENT]) break;
247-
currentWin = currentWin.parent.window;
249+
try {
250+
// find the topmost frame that's controlled by scramjet, stopping before the real top frame
251+
while (currentWin.parent.window !== currentWin.window) {
252+
if (!currentWin.parent.window[SCRAMJETCLIENT]) break;
253+
currentWin = currentWin.parent.window;
254+
}
255+
} catch {
256+
// doesn't matter if it throws here just means we found the topmost one
248257
}
249258

250259
const curclient = currentWin[SCRAMJETCLIENT];
@@ -254,6 +263,7 @@ export class ScramjetClient {
254263
);
255264
if (!frame) {
256265
// we're inside an iframe, but the top frame is scramjet-controlled and top level, so we can't get a top frame name
266+
// or we're cross-origin and frameElement doesn't exist. that's a TODO because this won't work
257267
return null;
258268
}
259269
if (!frame.name) {
@@ -270,12 +280,18 @@ export class ScramjetClient {
270280
get parentFrameName() {
271281
if (!iswindow)
272282
throw new Error("parentFrameName was called from a worker?");
273-
if (client.global.parent.window == client.global.window) {
274-
// we're top level & we don't have a frame name
283+
284+
try {
285+
if (client.global.parent.window == client.global.window) {
286+
// we're top level & we don't have a frame name
287+
return null;
288+
}
289+
} catch {
290+
// accessing parent was blocked by CORS, we're in a frame but the parent is cross origin
275291
return null;
276292
}
277293

278-
let parentWin = client.global.parent.window;
294+
const parentWin = client.global.parent.window;
279295
if (parentWin[SCRAMJETCLIENT]) {
280296
// we're inside an iframe, and the parent is scramjet-controlled
281297
const parentClient = parentWin[SCRAMJETCLIENT];

0 commit comments

Comments
 (0)