From 0819bcc1559be8c435430c9bec331f9ac51f6255 Mon Sep 17 00:00:00 2001 From: SebTota Date: Sun, 8 Aug 2021 12:53:57 -0400 Subject: [PATCH 1/5] fix: ignore unintended window message events --- lib/componentRelay.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/componentRelay.ts b/lib/componentRelay.ts index 3c1c097..8472421 100644 --- a/lib/componentRelay.ts +++ b/lib/componentRelay.ts @@ -197,26 +197,26 @@ export default class ComponentRelay { } } + // Mobile environment sends data as JSON string. + const { data } = event + const parsedData = isValidJsonString(data) ? JSON.parse(data) : data + + if (!parsedData) { + Logger.error('Invalid data received. Skipping...') + return + } + /** * The first message will be the most reliable one, so we won't change it after any subsequent events, * in case you receive an event from another window. */ - if (!this.component.origin) { + if (parsedData['action'] === 'component-registered') { this.component.origin = event.origin } else if (event.origin !== this.component.origin) { // If event origin doesn't match first-run value, return. return } - // Mobile environment sends data as JSON string. - const { data } = event - const parsedData = isValidJsonString(data) ? JSON.parse(data) : data - - if (!parsedData) { - Logger.error('Invalid data received. Skipping...') - return - } - this.handleMessage(parsedData) } From 812ebc65ff626030dc855fab4e68fbc0cb409f8d Mon Sep 17 00:00:00 2001 From: Seb Date: Mon, 9 Aug 2021 09:36:19 -0400 Subject: [PATCH 2/5] Update lib/componentRelay.ts Co-authored-by: Mo Bitar --- lib/componentRelay.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/componentRelay.ts b/lib/componentRelay.ts index 8472421..a543fc9 100644 --- a/lib/componentRelay.ts +++ b/lib/componentRelay.ts @@ -210,7 +210,7 @@ export default class ComponentRelay { * The first message will be the most reliable one, so we won't change it after any subsequent events, * in case you receive an event from another window. */ - if (parsedData['action'] === 'component-registered') { + if (parsedData.action === ComponentAction.ComponentRegistered) { this.component.origin = event.origin } else if (event.origin !== this.component.origin) { // If event origin doesn't match first-run value, return. From fb7af6cab1667d569b210e692c874355dc693e8c Mon Sep 17 00:00:00 2001 From: Seb Date: Mon, 9 Aug 2021 09:44:23 -0400 Subject: [PATCH 3/5] Remove unnecessary space Co-authored-by: Johnny A. <5891646+johnny243@users.noreply.github.com> --- lib/componentRelay.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/componentRelay.ts b/lib/componentRelay.ts index a543fc9..1ecbe97 100644 --- a/lib/componentRelay.ts +++ b/lib/componentRelay.ts @@ -210,7 +210,7 @@ export default class ComponentRelay { * The first message will be the most reliable one, so we won't change it after any subsequent events, * in case you receive an event from another window. */ - if (parsedData.action === ComponentAction.ComponentRegistered) { + if (parsedData.action === ComponentAction.ComponentRegistered) { this.component.origin = event.origin } else if (event.origin !== this.component.origin) { // If event origin doesn't match first-run value, return. From 2f059758aeb0eb57b6350c312e95bf87019d3d71 Mon Sep 17 00:00:00 2001 From: Mo Bitar Date: Mon, 9 Aug 2021 09:01:51 -0500 Subject: [PATCH 4/5] Update lib/componentRelay.ts Co-authored-by: Seb --- lib/componentRelay.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/componentRelay.ts b/lib/componentRelay.ts index 1ecbe97..c54f40b 100644 --- a/lib/componentRelay.ts +++ b/lib/componentRelay.ts @@ -207,7 +207,7 @@ export default class ComponentRelay { } /** - * The first message will be the most reliable one, so we won't change it after any subsequent events, + * The Component Registered message will be the most reliable one, so we won't change it after any subsequent events, * in case you receive an event from another window. */ if (parsedData.action === ComponentAction.ComponentRegistered) { From cef4d6a83682867e89f2e28faae7c582d31bca54 Mon Sep 17 00:00:00 2001 From: Seb Date: Mon, 9 Aug 2021 10:50:18 -0400 Subject: [PATCH 5/5] Add check to make sure component origin can not be set multiple times --- lib/componentRelay.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/componentRelay.ts b/lib/componentRelay.ts index c54f40b..4b454a2 100644 --- a/lib/componentRelay.ts +++ b/lib/componentRelay.ts @@ -210,7 +210,7 @@ export default class ComponentRelay { * The Component Registered message will be the most reliable one, so we won't change it after any subsequent events, * in case you receive an event from another window. */ - if (parsedData.action === ComponentAction.ComponentRegistered) { + if (typeof this.component.origin === 'undefined' && parsedData.action === ComponentAction.ComponentRegistered) { this.component.origin = event.origin } else if (event.origin !== this.component.origin) { // If event origin doesn't match first-run value, return.