Skip to content

windowPostMessageTransport throws when an unrelated postMessage has null data #108

Open

Description

@omerbek

What happened?

getWindowPostMessageTransport().connect() installs a global window.addEventListener('message', ...) listener. That listener receives every postMessage on the page, including messages sent by unrelated scripts or iframes.

The current listener destructures event.data before validating it:

const { target, data } = event.data;

If another page script sends a valid postMessage with null or undefined data, this throws a TypeError before the listener can ignore the unrelated message.

Minimal reproduction

After connect() has registered the message listener, dispatching a message shaped like this is enough to trigger the crash:

messageHandler({
  data: null,
  origin: location.origin,
} as MessageEvent);

Expected: unrelated/non-object message payloads are ignored, the same as wrong target/stream/origin messages.

Actual: the listener throws while destructuring event.data.

Suggested fix

Add a small guard before destructuring:

if (!event.data || typeof event.data !== 'object') {
  return;
}

This keeps the existing MetaMask message handling unchanged while preventing unrelated page messages from surfacing avoidable listener errors.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions