01Issue
Overview
The browser’s postMessage API allows pages, windows, and frames to exchange data, including across different origins. The receiving page must check who sent the message and whether its contents match the expected format before acting. A weakness occurs when a message from an untrusted page can reveal data or trigger an action, or when sensitive data is sent to an unintended recipient.
Common causes
- A message handler accepts any origin or uses a loose hostname text match.
- The receiver trusts message fields without validating the message type and value shapes.
- Message data flows into HTML, navigation, or another sensitive browser API without safe handling.
- A sender uses
*as the target origin for information meant for one known site.
References: MDN: Window.postMessage() ↗OWASP WSTG: Testing Web Messaging ↗CWE-346: Origin Validation Error ↗
02Examples
Illustrative receiver: if (event.origin !== "https://widget.example.test") return; then validate event.data.type and its fields. From another controlled origin, send {"type":"resize","height":300} and an unknown action. The unknown action should be ignored. This checks origin and schema handling; it does not prove any other message flow is safe.
03Impact
A message must cross a trust boundary, and an untrusted sender must be able to make the application reveal data or perform a meaningful action. A message event or permissive-looking source code alone does not establish impact; verify the handler’s data flow.
Depending on the handler, a confirmed weakness may expose data to another origin, change application state, or feed untrusted content into a browser sink. The impact depends on the data and action available to that message; do not infer script execution from a message being accepted.
04Testing
Test cases
1. Check whether an untrusted origin can trigger a sensitive message action
Load the receiving page in a frame on a separate controlled origin, then send a test message to that frame. Observe whether it changes a test setting or exposes information.
Example: A message event arriving is not proof of impact. Confirm the receiver accepts it and performs a meaningful action.
document.querySelector("iframe").contentWindow.postMessage({action:"test"}, "https://app.example.test")
2. Check origin, source, and message validation
Compare messages from trusted and untrusted test origins, including malformed payloads. Inspect how the receiver checks event.origin, event.source, and message fields.
Example: A hostname substring can match an unintended origin. Verify the exact origin the receiver accepts.
document.querySelector("iframe").contentWindow.postMessage({action:"update", value:"demo"}, "https://app.example.test")
3. Review message destinations and data handling
Check targetOrigin on outgoing messages and trace received data into navigation, HTML, or privileged actions. Confirm any downstream effect in the browser.
Example: A broad targetOrigin can expose data in some flows. Verify which page actually receives the message.
Testing references: MDN: Window.postMessage() ↗OWASP WSTG: Testing Web Messaging ↗CWE-346: Origin Validation Error ↗
05Mitigation
Use an exact origin allowlist and, where the design expects one specific window, verify event.source as well. Validate message type, fields, and value ranges before use. Send data with an exact target origin. Treat message contents as untrusted at every DOM or navigation sink, and avoid placing sensitive capabilities in cross-origin messages.
Retest
Repeat trusted and untrusted sender tests with valid and malformed messages. Confirm only expected origins and schemas are accepted, protected state is unchanged for rejected messages, and intended integrations still work.