Skip to main content
HighClient-sideOWASP A05:2021CWE-346

How to fix: postMessage Without Origin Validation

A window.addEventListener('message', ...) handler was found without an origin check. Without validating the sender's origin, any webpage can send arbitrary messages to your page and have them processed.

Why it matters

A malicious page that can embed your page in an iframe (or communicate cross-window) can send crafted messages that trigger privileged actions: authentication, navigation, data submission, or DOM manipulation.

postMessage is designed for cross-origin communication. The receiver must always validate event.origin against an expected value to prevent malicious pages from sending commands.

How to fix it

  1. 1Always check event.origin at the top of your message handler.
  2. 2Maintain a whitelist of allowed origins rather than using a boolean flag.
  3. 3Also validate event.source if you expect messages from a specific window.
  4. 4Treat event.data as untrusted input: validate its structure with a schema.

Code examples

Safe postMessage handler

JavaScript
const ALLOWED_ORIGINS = new Set(['https://parent.example.com']);

window.addEventListener('message', (event) => {
  if (!ALLOWED_ORIGINS.has(event.origin)) return; // Ignore unknown senders
  const { type, payload } = event.data ?? {};
  if (type === 'AUTH_TOKEN') {
    handleToken(payload); // payload is still untrusted, validate it
  }
});

References

Detected automatically by VulnRadar. The check id is postmessage-no-origin-check, stable across every scan so you can filter or diff on it.

See every client-side check

Questions

How severe is postMessage Without Origin Validation?
VulnRadar rates this high severity in the client-side category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
Does VulnRadar check for this automatically?
Yes. It runs as part of every scan under the check id postmessage-no-origin-check, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does postMessage Without Origin Validation map to?
It maps to CWE-346. In the OWASP Top 10 it falls under A05:2021.

Missing a check?

The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.

Propose a checkHow a check is built

Scan your own site for postmessage without origin validation

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

postMessage Without Origin Validation fix | VulnRadar