From 044067bbfebcfa856ef4be9431864f37815eae45 Mon Sep 17 00:00:00 2001 From: Richard Walton Date: Tue, 13 Feb 2024 11:55:50 +0000 Subject: [PATCH 1/7] Update window.js --- lib/services/window.js | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/services/window.js b/lib/services/window.js index 5784b7c..554b0e5 100644 --- a/lib/services/window.js +++ b/lib/services/window.js @@ -14,12 +14,36 @@ windowService._setMessageSource(domEmitterSource); module.exports = windowService; - _.extend(windowService, { - isTop: windowService_isTop + isTop: windowService_isTop, + getTop: windowService_getTop }); +/** + * Gets the top window containing a milo instance. + * It is not possible to use `window.top` if the application is contained + * in an iframe being served from a different domain (Trying to access + * properties of the top window in this case would breach the browser's + * cross-origin iframe policy) + */ +export const windowService_getTop = () => { + let topWindow; + + while (true) { + try { + const parentWindow = topWindow.parent; + // Trying to access this property outside of the same domain will throw an error + const milo = parentWindow.milo; + if (!milo) return topWindow; // No more milo + + topWindow = parentWindow; + } catch { + return topWindow; + } + } +} function windowService_isTop() { - return window.top == window.self || window.__karma__; + const topMiloWindow = windowService_getTop(); + return topMiloWindow == window.self || window.__karma__; } From 06a67132bbafcc9b43f1bf64f5742ec0491f9246 Mon Sep 17 00:00:00 2001 From: Richard Walton Date: Tue, 13 Feb 2024 11:56:35 +0000 Subject: [PATCH 2/7] Update window.js --- lib/services/window.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/window.js b/lib/services/window.js index 554b0e5..e9be4f5 100644 --- a/lib/services/window.js +++ b/lib/services/window.js @@ -27,7 +27,7 @@ _.extend(windowService, { * cross-origin iframe policy) */ export const windowService_getTop = () => { - let topWindow; + let topWindow = window; while (true) { try { From e8418e6245fd94d3a2493ce80ec729c6d720c7e8 Mon Sep 17 00:00:00 2001 From: Richard Walton Date: Tue, 13 Feb 2024 12:14:55 +0000 Subject: [PATCH 3/7] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ef7d0ec..7c85ab2 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "brfs": "^1.4.3", "browserify": "^14.0.0", "eslintify": "^3.1.0", - "grunt": "^1.0.1", + "grunt": "1.0.1", "grunt-browserify": "^5.0.0", "grunt-contrib-copy": "^1.0.0", "grunt-contrib-uglify": "^2.0.0", From b1ccb951b4a8f5b10fc5e9401bfa6589afb425d1 Mon Sep 17 00:00:00 2001 From: Richard Walton Date: Tue, 13 Feb 2024 12:18:32 +0000 Subject: [PATCH 4/7] Update window.js --- lib/services/window.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/window.js b/lib/services/window.js index e9be4f5..ba0c920 100644 --- a/lib/services/window.js +++ b/lib/services/window.js @@ -26,7 +26,7 @@ _.extend(windowService, { * properties of the top window in this case would breach the browser's * cross-origin iframe policy) */ -export const windowService_getTop = () => { +function windowService_getTop() { let topWindow = window; while (true) { From 69735f567845b55065ebaca60a88322ba994c8f5 Mon Sep 17 00:00:00 2001 From: Richard Walton Date: Tue, 13 Feb 2024 12:25:08 +0000 Subject: [PATCH 5/7] Update window.js --- lib/services/window.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/window.js b/lib/services/window.js index ba0c920..23d20d3 100644 --- a/lib/services/window.js +++ b/lib/services/window.js @@ -37,7 +37,7 @@ function windowService_getTop() { if (!milo) return topWindow; // No more milo topWindow = parentWindow; - } catch { + } catch (_e) { return topWindow; } } From 4536afd4cec0b5ecba63a34414a54bdcea6a2ba6 Mon Sep 17 00:00:00 2001 From: Richard Walton Date: Tue, 13 Feb 2024 12:31:22 +0000 Subject: [PATCH 6/7] Update window.js --- lib/services/window.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/services/window.js b/lib/services/window.js index 23d20d3..bd4a11a 100644 --- a/lib/services/window.js +++ b/lib/services/window.js @@ -27,13 +27,13 @@ _.extend(windowService, { * cross-origin iframe policy) */ function windowService_getTop() { - let topWindow = window; + var topWindow = window; while (true) { try { - const parentWindow = topWindow.parent; + var parentWindow = topWindow.parent; // Trying to access this property outside of the same domain will throw an error - const milo = parentWindow.milo; + var milo = parentWindow.milo; if (!milo) return topWindow; // No more milo topWindow = parentWindow; From 2caff218948a6a58d4caa1e4e9718ec106340d3d Mon Sep 17 00:00:00 2001 From: Richard Walton Date: Tue, 13 Feb 2024 17:54:15 +0000 Subject: [PATCH 7/7] Update window.js --- lib/services/window.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/window.js b/lib/services/window.js index bd4a11a..f9af85d 100644 --- a/lib/services/window.js +++ b/lib/services/window.js @@ -34,7 +34,7 @@ function windowService_getTop() { var parentWindow = topWindow.parent; // Trying to access this property outside of the same domain will throw an error var milo = parentWindow.milo; - if (!milo) return topWindow; // No more milo + if (!milo || topWindow === parentWindow) return topWindow; // No more milo / we're at the top topWindow = parentWindow; } catch (_e) {