From db8bbe55c030c1be37cc64247e8aa90eb7205f1f Mon Sep 17 00:00:00 2001 From: nicholas Date: Thu, 30 Nov 2023 14:17:41 +0100 Subject: [PATCH 1/7] detectInIframe fn fix --- packages/lib/src/utils/detectInIframe.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/lib/src/utils/detectInIframe.ts b/packages/lib/src/utils/detectInIframe.ts index 5307d7028e..c7accda301 100644 --- a/packages/lib/src/utils/detectInIframe.ts +++ b/packages/lib/src/utils/detectInIframe.ts @@ -1,2 +1,9 @@ -// Returns true if the page is being run in an iframe -export default () => window.location !== window.parent.location; +//TODO - rename this module to detectInIframeInSameDomain + +// Returns true if the page is being run in an iframe (in a domain we have access to) +export default () => { + if (window.parent.location instanceof Location) { + return window.location !== window.parent.location; + } + return false; // we cannot access window.parent.location - so consider us "not to be in an iframe" for the purpose of Redirects +}; From 4e1f2915c83136115e105854fc20ee6af614b16a Mon Sep 17 00:00:00 2001 From: nicholas Date: Mon, 4 Dec 2023 10:17:28 +0100 Subject: [PATCH 2/7] detectInIframe renamed to the more accurate (if lengthy!) detectInIframeInAccessibleDomain --- .../Redirect/components/RedirectShopper/RedirectShopper.tsx | 6 +++--- ...etectInIframe.ts => detectInIframeInAccessibleDomain.ts} | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) rename packages/lib/src/utils/{detectInIframe.ts => detectInIframeInAccessibleDomain.ts} (86%) diff --git a/packages/lib/src/components/Redirect/components/RedirectShopper/RedirectShopper.tsx b/packages/lib/src/components/Redirect/components/RedirectShopper/RedirectShopper.tsx index ac69bd52a8..30b3727fd1 100644 --- a/packages/lib/src/components/Redirect/components/RedirectShopper/RedirectShopper.tsx +++ b/packages/lib/src/components/Redirect/components/RedirectShopper/RedirectShopper.tsx @@ -1,5 +1,5 @@ import { Component, h } from 'preact'; -import detectInIframe from '../../../../utils/detectInIframe'; +import detectInIframeInAccessibleDomain from '../../../../utils/detectInIframeInAccessibleDomain'; interface RedirectShopperProps { beforeRedirect: (resolve, reject, url) => Promise; @@ -21,7 +21,7 @@ class RedirectShopper extends Component { if (this.postForm) { this.postForm.submit(); } else { - if (this.props.redirectFromTopWhenInIframe && detectInIframe()) { + if (this.props.redirectFromTopWhenInIframe && detectInIframeInAccessibleDomain()) { // if in an iframe and the config prop allows it - try to redirect from the top level window window.top.location.assign?.(this.props.url); } else { @@ -51,7 +51,7 @@ class RedirectShopper extends Component { ref={ref => { this.postForm = ref; }} - {...(this.props.redirectFromTopWhenInIframe && detectInIframe() && { target: '_top' })} + {...(this.props.redirectFromTopWhenInIframe && detectInIframeInAccessibleDomain() && { target: '_top' })} > {Object.keys(data).map(key => ( diff --git a/packages/lib/src/utils/detectInIframe.ts b/packages/lib/src/utils/detectInIframeInAccessibleDomain.ts similarity index 86% rename from packages/lib/src/utils/detectInIframe.ts rename to packages/lib/src/utils/detectInIframeInAccessibleDomain.ts index c7accda301..fdda91bfa1 100644 --- a/packages/lib/src/utils/detectInIframe.ts +++ b/packages/lib/src/utils/detectInIframeInAccessibleDomain.ts @@ -1,5 +1,3 @@ -//TODO - rename this module to detectInIframeInSameDomain - // Returns true if the page is being run in an iframe (in a domain we have access to) export default () => { if (window.parent.location instanceof Location) { From da5959969cf2e3617a0dc9a167f30e7518c06443 Mon Sep 17 00:00:00 2001 From: nicholas Date: Mon, 4 Dec 2023 10:20:47 +0100 Subject: [PATCH 3/7] adding changeset file --- .changeset/twelve-clouds-float.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/twelve-clouds-float.md diff --git a/.changeset/twelve-clouds-float.md b/.changeset/twelve-clouds-float.md new file mode 100644 index 0000000000..68377dbf7c --- /dev/null +++ b/.changeset/twelve-clouds-float.md @@ -0,0 +1,6 @@ +--- +'@adyen/adyen-web': patch +--- + +Renaming detectInIframe to the more accurate, but lengthy, detectInIframeInAccessibleDomain. +Now the functionality only considers itself to be running in an iframe _if_ it is possible to access the parent domain and thus be able to redirect the top, parent, window From 6ace028dce3b0396edd07064bd89f583558cc81f Mon Sep 17 00:00:00 2001 From: nicholas Date: Mon, 4 Dec 2023 11:49:50 +0100 Subject: [PATCH 4/7] changed import in unit test --- packages/lib/src/components/Redirect/Redirect.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/lib/src/components/Redirect/Redirect.test.tsx b/packages/lib/src/components/Redirect/Redirect.test.tsx index d128c00de6..d9184477cd 100644 --- a/packages/lib/src/components/Redirect/Redirect.test.tsx +++ b/packages/lib/src/components/Redirect/Redirect.test.tsx @@ -4,7 +4,7 @@ import Redirect from './Redirect'; import RedirectShopper from './components/RedirectShopper'; import RedirectElement from './Redirect'; -jest.mock('../../utils/detectInIframe', () => { +jest.mock('../../utils/detectInIframeInAccessibleDomain', () => { return jest.fn().mockImplementation(() => { return true; }); From c31de29358cd282a99aaa93bebdf55530b916f91 Mon Sep 17 00:00:00 2001 From: nicholas Date: Mon, 4 Dec 2023 13:51:43 +0100 Subject: [PATCH 5/7] Changed check for reading window.parent.location --- .../lib/src/utils/detectInIframeInAccessibleDomain.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/lib/src/utils/detectInIframeInAccessibleDomain.ts b/packages/lib/src/utils/detectInIframeInAccessibleDomain.ts index fdda91bfa1..218010505f 100644 --- a/packages/lib/src/utils/detectInIframeInAccessibleDomain.ts +++ b/packages/lib/src/utils/detectInIframeInAccessibleDomain.ts @@ -1,7 +1,10 @@ // Returns true if the page is being run in an iframe (in a domain we have access to) export default () => { - if (window.parent.location instanceof Location) { - return window.location !== window.parent.location; + try { + if (window.parent.location.href) { + return window.location !== window.parent.location; // iframe check: locations will differ if we're in an iframe + } + } catch (e) { + return false; // we cannot access window.parent.location.href - so consider us "not to be in an iframe" for the purpose of Redirects } - return false; // we cannot access window.parent.location - so consider us "not to be in an iframe" for the purpose of Redirects }; From 0b6c5fb6ef0f540ad0b4b3b06684e077f1b1a495 Mon Sep 17 00:00:00 2001 From: nicholas Date: Tue, 5 Dec 2023 11:56:25 +0100 Subject: [PATCH 6/7] Clarified function name and purpose (via comments) --- packages/lib/src/components/Redirect/Redirect.test.tsx | 2 +- .../Redirect/components/RedirectShopper/RedirectShopper.tsx | 6 +++--- ...eInAccessibleDomain.ts => detectInIframeInSameOrigin.ts} | 6 +++++- 3 files changed, 9 insertions(+), 5 deletions(-) rename packages/lib/src/utils/{detectInIframeInAccessibleDomain.ts => detectInIframeInSameOrigin.ts} (57%) diff --git a/packages/lib/src/components/Redirect/Redirect.test.tsx b/packages/lib/src/components/Redirect/Redirect.test.tsx index d9184477cd..0f1ac19e6b 100644 --- a/packages/lib/src/components/Redirect/Redirect.test.tsx +++ b/packages/lib/src/components/Redirect/Redirect.test.tsx @@ -4,7 +4,7 @@ import Redirect from './Redirect'; import RedirectShopper from './components/RedirectShopper'; import RedirectElement from './Redirect'; -jest.mock('../../utils/detectInIframeInAccessibleDomain', () => { +jest.mock('../../utils/detectInIframeInSameOrigin', () => { return jest.fn().mockImplementation(() => { return true; }); diff --git a/packages/lib/src/components/Redirect/components/RedirectShopper/RedirectShopper.tsx b/packages/lib/src/components/Redirect/components/RedirectShopper/RedirectShopper.tsx index 30b3727fd1..2748a556be 100644 --- a/packages/lib/src/components/Redirect/components/RedirectShopper/RedirectShopper.tsx +++ b/packages/lib/src/components/Redirect/components/RedirectShopper/RedirectShopper.tsx @@ -1,5 +1,5 @@ import { Component, h } from 'preact'; -import detectInIframeInAccessibleDomain from '../../../../utils/detectInIframeInAccessibleDomain'; +import detectInIframeInSameOrigin from '../../../../utils/detectInIframeInSameOrigin'; interface RedirectShopperProps { beforeRedirect: (resolve, reject, url) => Promise; @@ -21,7 +21,7 @@ class RedirectShopper extends Component { if (this.postForm) { this.postForm.submit(); } else { - if (this.props.redirectFromTopWhenInIframe && detectInIframeInAccessibleDomain()) { + if (this.props.redirectFromTopWhenInIframe && detectInIframeInSameOrigin()) { // if in an iframe and the config prop allows it - try to redirect from the top level window window.top.location.assign?.(this.props.url); } else { @@ -51,7 +51,7 @@ class RedirectShopper extends Component { ref={ref => { this.postForm = ref; }} - {...(this.props.redirectFromTopWhenInIframe && detectInIframeInAccessibleDomain() && { target: '_top' })} + {...(this.props.redirectFromTopWhenInIframe && detectInIframeInSameOrigin() && { target: '_top' })} > {Object.keys(data).map(key => ( diff --git a/packages/lib/src/utils/detectInIframeInAccessibleDomain.ts b/packages/lib/src/utils/detectInIframeInSameOrigin.ts similarity index 57% rename from packages/lib/src/utils/detectInIframeInAccessibleDomain.ts rename to packages/lib/src/utils/detectInIframeInSameOrigin.ts index 218010505f..dcb58ccec8 100644 --- a/packages/lib/src/utils/detectInIframeInAccessibleDomain.ts +++ b/packages/lib/src/utils/detectInIframeInSameOrigin.ts @@ -1,4 +1,8 @@ -// Returns true if the page is being run in an iframe (in a domain we have access to) +/** + * Returns true if the page is being run in an iframe with the same origin as the parent. + * In this scenario, if the merchant has set redirectFromTopWhenInIframe: true, then we can perform the redirect on the top level, parent, window; + * rather than on the iframe's window + */ export default () => { try { if (window.parent.location.href) { From 620ffe3c4d2e4b996dd4a47dd4fc8c0dfc29ab3b Mon Sep 17 00:00:00 2001 From: nicholas Date: Tue, 5 Dec 2023 12:11:21 +0100 Subject: [PATCH 7/7] update comment in changeset file --- .changeset/twelve-clouds-float.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/twelve-clouds-float.md b/.changeset/twelve-clouds-float.md index 68377dbf7c..4b8340efb1 100644 --- a/.changeset/twelve-clouds-float.md +++ b/.changeset/twelve-clouds-float.md @@ -2,5 +2,5 @@ '@adyen/adyen-web': patch --- -Renaming detectInIframe to the more accurate, but lengthy, detectInIframeInAccessibleDomain. +Renaming detectInIframe to the more accurate, but lengthy, detectInIframeInSameOrigin. Now the functionality only considers itself to be running in an iframe _if_ it is possible to access the parent domain and thus be able to redirect the top, parent, window