diff --git a/src/services/WebExtensionService.ts b/src/services/WebExtensionService.ts index bc37dd9..4ff5694 100644 --- a/src/services/WebExtensionService.ts +++ b/src/services/WebExtensionService.ts @@ -44,7 +44,7 @@ export default class WebExtensionService { } private receive(event: { data: ExtensionResponse }): void { - if (!event.data?.action.startsWith("web-eid:")) return; + if (!event.data?.action?.startsWith("web-eid:")) return; const message = event.data; const suffix = ["success", "failure", "ack"].find((s) => message.action.endsWith(s)); diff --git a/src/services/__tests__/WebExtensionService-test.ts b/src/services/__tests__/WebExtensionService-test.ts index 8196383..3c40aff 100644 --- a/src/services/__tests__/WebExtensionService-test.ts +++ b/src/services/__tests__/WebExtensionService-test.ts @@ -35,6 +35,35 @@ describe("WebExtensionService", () => { jest.restoreAllMocks(); }); + describe("receive", () => { + it("should ignore messages with data but no action property", async () => { + jest.spyOn(console, "warn").mockImplementation(); + + window.postMessage({ someOtherProperty: "value" }, "*"); + await new Promise((resolve) => setTimeout(resolve)); + + expect(console.warn).not.toHaveBeenCalled(); + }); + + it("should ignore messages with null data", async () => { + jest.spyOn(console, "warn").mockImplementation(); + + window.postMessage(null, "*"); + await new Promise((resolve) => setTimeout(resolve)); + + expect(console.warn).not.toHaveBeenCalled(); + }); + + it("should ignore messages with undefined data", async () => { + jest.spyOn(console, "warn").mockImplementation(); + + window.postMessage(undefined, "*"); + await new Promise((resolve) => setTimeout(resolve)); + + expect(console.warn).not.toHaveBeenCalled(); + }); + }); + describe("action web-eid:warning", () => { it("should log a warning from web-eid:warning message", async () => { jest.spyOn(console, "warn").mockImplementation();