Skip to content

Lesson 13 of 14 · 12m

postMessage Boundaries

Sender-side targetOrigin, receiver-side exact origin matching, why substring checks fail, and event.data as an untrusted source feeding DOM sinks.

The one sanctioned cross-origin channel

Same-origin policy walls windows off from each other; postMessage is the deliberate door through the wall. targetWindow.postMessage(data, targetOrigin) serializes data via structured clone and delivers a MessageEvent to the target's onmessage handler — iframe to parent, popup to opener, tab to tab. It works precisely because no other channel does.

The API has two security halves, and both exist because both fail:

Sender side: targetOrigin is the recipient allowlist

postMessage(payload, 'https://embed.example.com') delivers only if the target window's origin matches. The asterisk is the bug: postMessage(payload, '*') fires the data at whatever window happens to be there — and windows navigate. If an attacker iframes your widget or navigates your popup, '*' hands them the payload: auth tokens, PII, whatever you posted. The check is the browser's, but you write it. Any '*' argument carrying data that is not public is a leak waiting for a navigation.

Receiver side: the browser checks nothing

The MessageEvent arrives with three fields that matter — origin (the sender's origin string), source (a reference to the sending window), and data (the payload). Whether to trust it is entirely your handler's decision; the browser delivers every message posted to the window from anywhere — an iframe, a popup, an extension. The check gets skipped for a predictable reason: developers assume the browser only delivers messages from the window they opened. It delivers all of them.

The failure patterns are all substring-shaped. e.origin.includes('trusted.com') passes https://trusted.com.evil.example. e.origin.endsWith('trusted.com') passes https://eviltrusted.com. e.origin.startsWith('https://trusted.com') passes https://trusted.com.evil.example. Regexes with unescaped dots pass strustedXcom. The correct check is boring:

const ALLOWED = new Set(['https://embed.example.com', 'https://admin.example.com']);
window.addEventListener('message', (e) => {
  if (!ALLOWED.has(e.origin)) return;
  if (e.source !== expectedWindow) return; // when replying to a specific window
  // validate e.data's shape before use
});

Exact equality against an allowlist — nothing else. source equality pins the specific window when the conversation is pairwise; an iframe-based integration where any window may legitimately send still needs the origin allowlist plus strict shape validation on data. Two edge cases recur in real handlers: sandboxed iframes and about: documents report e.origin === 'null' (the string), which is not an allowlist entry, and structured clone means e.data arrives as deserialized objects — shapes, not code — so validating the shape is the boundary, because the content is still attacker-written.

The sink at the end of the handler

e.data is attacker-controlled input crossing an origin boundary — a source in the lesson-1 wiring sense. Handlers that build HTML from it (el.innerHTML = e.data.html, $(e.data), template interpolation into markup) convert a message-boundary bug into full DOM XSS: any window on the internet posts the payload, the handler parses it as code. This chain is one of the most common high-severity findings in bug bounty writeups on major platforms — origin check absent or substring-broken, message data piped into a sink.

The adjacent opener problem

window.open and target="_blank" historically gave the new page a window.opener handle back to your page — reverse tabnabbing: the opened page navigates opener.location to a phishing clone while the user is away. Browsers fixed the default: target="_blank" now implies noopener in Chrome 88 (January 2021), Firefox 79 (2020), Safari 12.1 (2019). Still write rel="noopener" for old engines — it costs ten characters. Cross-Origin-Opener-Policy: same-origin on your response goes further: it removes your page from any shared browsing-context group with cross-origin openers — lesson 14.

What to actually do

Exact-match the origin, pin source when the channel is pairwise, never '*' for non-public data, schema-validate e.data, and treat everything a handler produces as untrusted input to the sink list. The browser routes messages; the boundary is application code.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.