Fix TypeError when receiving messages without action property#64
Fix TypeError when receiving messages without action property#64mrts merged 1 commit intoweb-eid:mainfrom
Conversation
There was a problem hiding this comment.
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.actionis undefined - Added unit tests to verify messages without the
actionproperty 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.
There was a problem hiding this comment.
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.
|
Thanks for noticing the issue! Made this mistake when I changed: if (!/^web-eid:/.test(event.data?.action)) return;to if (!event.data?.action.startsWith("web-eid:")) return;Your change does fix the issue, but only if we expect window.postMessage(
{
action: {
id: "<actionId>",
_t: "<actionTimestamp>",
},
payload: {
...
}
},
"*"
);We could add another optional chaining check, so window.postMessage(
{
action: {
startsWith: "2022-10-12",
endsWith: "2026-10-12
}
},
"*"
);Adding type checks would make this IF statement harder to read at a glance, so I think the best option would be to revert it back to: if (!/^web-eid:/.test(event.data?.action)) return;The regex What do you think, @ErkoRisthein ? |
|
Thanks for the review, @taneltm. You're right that the optional chaining approach doesn't handle all edge cases. I tried reverting to the regex approach you suggested: However, I discovered a non-intuitive edge case: JavaScript's So I've added an explicit type guard before calling This handles all edge cases:
I've added test cases for these scenarios. All tests pass. |
taneltm
left a comment
There was a problem hiding this comment.
- Original issue reproduced in
main - PR fixes the issue
- Confirmed that the tests are working properly (tests fail, as expected, when the fix is reverted)
|
@ErkoRisthein, heartfelt thanks for your contribution! A couple of formalities still remain, then I will gladly merge this: can you please squash the commits into one, sign the Certificate of Origin with |
48a5149 to
62a703d
Compare
- Add typeof check to reject non-string action values early - Use startsWith() safely after confirming action is a string - Add test cases for various edge cases (object, number, array) Signed-off-by: Erko Risthein <erko@risthein.ee>
62a703d to
58f85d8
Compare
Summary
Fixes
TypeError: Cannot read properties of undefined (reading 'startsWith')that occurs when the window receives postMessage events that have adataproperty but noactionproperty.Root Cause
The condition
event.data?.action.startsWith("web-eid:")safely handles whenevent.datais undefined, but throws whenevent.dataexists andevent.data.actionis undefined.Fix
Add optional chaining on
action:Test Plan
Signed-off-by: Erko Risthein erko@risthein.ee