Skip to content

fix: ignore non-object window messages in isSameOrigin - #1366

Open
akysil wants to merge 1 commit into
PlasmoHQ:mainfrom
akysil:fix/1363-plasmohq-messaging-issameorigin-throws-t
Open

akysil wants to merge 1 commit into
PlasmoHQ:mainfrom
akysil:fix/1363-plasmohq-messaging-issameorigin-throws-t

Conversation

@akysil

@akysil akysil commented Nov 29, 2026

Copy link
Copy Markdown

Details

This PR stops relay and sendViaRelay from throwing when the host page or another script calls window.postMessage(null, "*") or window.postMessage(undefined, "*").

Both listeners filter events through isSameOrigin, which read event.data.name without checking event.data. For null data this threw TypeError: Cannot read properties of null (reading 'name'), and for undefined data TypeError: Cannot read properties of undefined (reading 'name'). Inside relay's async handler the error became an unhandled promise rejection. String and number payloads did not throw. They were ignored only because they have no name that matches the request's.

Approach

isSameOrigin now returns false unless event.data is a non-null object, so nullish and primitive payloads are both rejected explicitly. It is the shared filter for both listeners, so one guard covers both. Optional chaining (event.data?.name) would stop the exception, but a primitive could still match a request without a name.

Tests

Added tests to relay.test.ts:

  • sendViaRelay and relay ignore null, undefined, "hello" and 42. Both fail on main with the TypeError.
  • relay ignores a primitive (true) even for a request without a name. This fails if the guard only rejects null/undefined.

pnpm --filter @plasmohq/messaging test: 9 passed.

Fixes #1363

Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I agree to license this contribution under the MIT LICENSE
  • I checked the current PR for duplication.

Contacts

  • (OPTIONAL) Discord ID: b81234901824

If your PR is accepted, we will award you with the Contributor role on Discord server.

To join the server, visit: https://www.plasmo.com/s/d

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.

@plasmohq/messaging isSameOrigin throws TypeError when host page posts null or undefined via window.postMessage

1 participant