From 2eebf2a4a8a9bbe3afd1b489fd00f09261355f7b Mon Sep 17 00:00:00 2001 From: Erko Risthein Date: Thu, 11 Dec 2025 12:07:03 +0200 Subject: [PATCH 1/2] Fix TypeError when receiving messages without action property Add optional chaining on action property to prevent "Cannot read properties of undefined (reading 'startsWith')" error when window receives messages that have data but no action property. --- src/services/WebExtensionService.ts | 2 +- .../__tests__/WebExtensionService-test.ts | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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..524ec91 100644 --- a/src/services/__tests__/WebExtensionService-test.ts +++ b/src/services/__tests__/WebExtensionService-test.ts @@ -35,6 +35,23 @@ describe("WebExtensionService", () => { jest.restoreAllMocks(); }); + describe("receive", () => { + it("should ignore messages with data but no action property", async () => { + window.postMessage({ someOtherProperty: "value" }, "*"); + await new Promise((resolve) => setTimeout(resolve)); + }); + + it("should ignore messages with null data", async () => { + window.postMessage(null, "*"); + await new Promise((resolve) => setTimeout(resolve)); + }); + + it("should ignore messages with undefined data", async () => { + window.postMessage(undefined, "*"); + await new Promise((resolve) => setTimeout(resolve)); + }); + }); + describe("action web-eid:warning", () => { it("should log a warning from web-eid:warning message", async () => { jest.spyOn(console, "warn").mockImplementation(); From 2898b3fda57a3facc4146263062c7e95898b9e71 Mon Sep 17 00:00:00 2001 From: Erko Risthein Date: Thu, 11 Dec 2025 12:30:36 +0200 Subject: [PATCH 2/2] Address code review feedback: add explicit assertions to tests Verify that console.warn is not called when messages are ignored, rather than just checking that no error is thrown. --- src/services/__tests__/WebExtensionService-test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/services/__tests__/WebExtensionService-test.ts b/src/services/__tests__/WebExtensionService-test.ts index 524ec91..3c40aff 100644 --- a/src/services/__tests__/WebExtensionService-test.ts +++ b/src/services/__tests__/WebExtensionService-test.ts @@ -37,18 +37,30 @@ describe("WebExtensionService", () => { 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(); }); });