From e08e46190145e27119a88ac39cb2cea904b1c39b Mon Sep 17 00:00:00 2001 From: Matt Hillsdon Date: Tue, 10 Sep 2024 13:43:46 +0100 Subject: [PATCH] Tweak origin check to respect the configured URL --- src/vanilla/makecode-frame-driver.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/vanilla/makecode-frame-driver.ts b/src/vanilla/makecode-frame-driver.ts index 30cddf7..ba317eb 100644 --- a/src/vanilla/makecode-frame-driver.ts +++ b/src/vanilla/makecode-frame-driver.ts @@ -166,8 +166,22 @@ export class MakeCodeFrameDriver { } >(); + private _expectedOrigin: string | undefined; + private expectedOrigin = () => { + if (this._expectedOrigin) { + return this._expectedOrigin; + } + const src = this.iframe()?.src; + if (src) { + this._expectedOrigin = new URL(src).origin; + return this._expectedOrigin; + } + return undefined; + }; + private listener = (event: MessageEvent) => { - if (event.origin !== 'https://makecode.microbit.org') { + const expectedOrigin = this.expectedOrigin(); + if (!expectedOrigin || event.origin !== expectedOrigin) { return; } const { data } = event;