Skip to content

Conversation

@ErkoRisthein
Copy link

Summary

Fixes TypeError: Cannot read properties of undefined (reading 'startsWith') that occurs when the window receives postMessage events that have a data property but no action property.

Root Cause

The condition event.data?.action.startsWith("web-eid:") safely handles when event.data is undefined, but throws when event.data exists and event.data.action is undefined.

Fix

Add optional chaining on action:

if (!event.data?.action?.startsWith("web-eid:")) return;

Test Plan

  • Added unit tests for messages with missing action property
  • npm test passes (48 tests)

Signed-off-by: Erko Risthein erko@risthein.ee

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.
Copilot AI review requested due to automatic review settings December 11, 2025 10:14
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a TypeError that occurs when the window receives postMessage events with a data property but without an action property. The fix adds optional chaining to safely access the action property before calling startsWith().

Key Changes:

  • Added optional chaining operator to prevent TypeError when event.data.action is undefined
  • Added unit tests to verify messages without the action property are properly ignored

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/services/WebExtensionService.ts Added optional chaining to event.data.action to prevent TypeError when action is undefined
src/services/tests/WebExtensionService-test.ts Added three test cases verifying that messages with missing/null/undefined data or action properties are properly ignored

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Verify that console.warn is not called when messages are ignored,
rather than just checking that no error is thrown.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant