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.

SeverityMedium
CVSS 3.15.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.05.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
CWECWE-940: Improper Verification of Source of a Communication Channel
OWASP Top 10A07:2025 Authentication Failures
MITRE ATT&CKT1189 Drive-by Compromise
CategoryInput 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 message handler, compare event.origin exactly 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.source is that window.
  • Validate the structure and type of event.data before use, treat it as data only, and never pass it to eval(), innerHTML, document.write() or a URL that is navigated to; use textContent and 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.
const allowedOrigins = new Set(["https://app.example.com"]); window.addEventListener("message", (event) => { if (!allowedOrigins.has(event.origin)) return; const msg = event.data; if (typeof msg !== "object" || msg === null || msg.type !== "resize") return; // act on the validated message; write text with textContent, not innerHTML });

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 trial  ·  No card required

Only need the reporting? Our reporting tool is free and open source, with the same library.


More Web Applications Findings

Every Web Applications finding