Insecure postMessage Handling
The page exchanges window.postMessage messages without checking the origin of the sender or recipient, so another website can send it commands or receive its data.
| Severity | Medium |
| CVSS 3.1 | 5.4 CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:L/I:L/A:N Open in the calculator |
| CVSS 4.0 | 5.3 CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:P/VC:L/VI:L/VA:N/SC:N/SI:N/SA:N Open in the calculator |
| CWE | CWE-940: Improper Verification of Source of a Communication Channel |
| OWASP Top 10 | A07:2025 Authentication Failures |
| MITRE ATT&CK | T1189 Drive-by Compromise |
| Category | Input Validation |
Description
The application uses the browser's window.postMessage() API to pass messages between windows or frames from different origins, for example between a page and an embedded widget, a payment frame or a single sign-on popup. The API exists to allow cross-origin communication, so each side must check who it is talking to. The receiving page's message event handler does not compare event.origin with the origins it expects, or checks it loosely, for example by looking for the expected domain name anywhere in the origin string, which a domain registered by an attacker can also satisfy. It then acts on event.data.
An attacker who persuades a logged-in user to visit a page under their control can therefore open or frame the vulnerable page and send it messages that it treats as trusted. Depending on what the handler does, this can change settings, trigger actions or redirect the user. It is most serious when message data reaches a dangerous sink such as innerHTML, eval() or location, which turns it into DOM-based cross-site scripting: script then runs in the application's origin and can act as the user and read their data.
The weakness also appears on the sending side, where a page posts sensitive data, such as tokens or personal details, with a target origin of *, so whichever document is loaded in the target window receives it, and an attacker whose page is loaded there receives the data directly.
The issue is identified by reviewing client-side JavaScript for message event listeners and postMessage() calls, then confirming from a page on another origin that opens or frames the application and sends crafted messages.
Remediation
Treat every message as untrusted input from another website:
- In each
messagehandler, compareevent.originexactly with an allowlist of expected origins (scheme, host and port) and ignore messages from anything else. Do not use substring, prefix or unanchored pattern checks. - Where the sender is a known window, also check that
event.sourceis that window. - Validate the structure and type of
event.databefore use, treat it as data only, and never pass it toeval(),innerHTML,document.write()or a URL that is navigated to; usetextContentand safe DOM methods instead. - When sending, always give the exact target origin as the second argument of
postMessage(), never*for anything that is not public. - Remove message handlers that are no longer needed, including those added by third-party scripts that are no longer used.
A Content Security Policy that blocks inline script and eval() limits the effect of an injection through a message handler while the code is corrected. Confirm the fix by sending crafted messages from a page on another origin and checking that they are ignored.
References
Add This Finding to Your Library
In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.
Free trialYour trial starts when your portal goes live, not when you sign up, so the time you spend setting up does not count. · No card required
Only need the reporting? Our reporting tool is free and open source, with the same library.