Lesson 3 of 9 · 26m
Windows, Frames, and Safe postMessage Contracts
Analyze cross-window communication as a protocol boundary: validate origins, validate message shape, and understand what attackers can still observe.
A lot of real web compromise does not begin with memory corruption. It begins with two windows trusting each other because one of them has a familiar-looking URL. postMessage is powerful because it crosses origin boundaries intentionally. That makes it a protocol, and protocols need authentication, validation, and version discipline.
Know the communication graph
Windows become connected through openers, iframes, shared workers, broadcast channels, and redirects. Each edge has different authority.
opener -> popup window.opener may exist unless severed
parent -> iframe frame.contentWindow is reachable
iframe -> parent window.parent and window.top are constrained
same-origin tabs BroadcastChannel can share messages
service worker clients controlled pages may receive messagesModern browsers often sever window.opener implicitly for cross-origin links with target="_blank", but explicit hardening is still clearer:
<a href="https://partner.example/start" target="_blank" rel="noopener">Open</a>If your threat model includes older embedders or unusual navigation paths, do not depend on half-remembered defaults.
A safe listener validates origin and schema
This is the minimum useful shape:
window.addEventListener('message', (event) => {
if (event.origin !== 'https://payments.example') return;
const msg = event.data;
if (!msg || msg.version !== 1 || msg.type !== 'payment-complete') return;
if (typeof msg.nonce !== 'string' || msg.nonce.length < 32) return;
completePayment(msg.nonce);
});Checking typeof event.data === 'object' is not validation. Attackers control the entire object. Validate the type, enum, length, integer range, and state transition. A “message” is untrusted input wearing a structured jacket.
The sender must choose the recipient carefully
The second argument is the target origin, not a hint:
frame.contentWindow.postMessage(
{version: 1, type: 'token-response', token},
'https://app.example'
);If you use '*', any origin hosting or navigating that frame may receive the payload. For sensitive data, wait until the iframe has loaded the expected origin and use a nonce or handshake so a stale frame cannot replay an old message.
event.source is context, not authentication
You can check that the message came from the frame you expected:
if (event.source !== paymentFrame.contentWindow) return;That check is valuable, but it is not enough. A frame you control can be navigated away, replaced, or confused in multi-frame layouts. Pair source checks with origin checks and application-level transaction state.
Navigation and embedding are separate attack surfaces
Even perfect postMessage validation does not stop clickjacking, hostile embedding, or referrer leakage. Use frame controls where appropriate:
Content-Security-Policy: frame-ancestors 'self' https://portal.example
X-Frame-Options: DENYframe-ancestors is the modern control; X-Frame-Options remains useful for legacy clients. They answer “who may embed this document,” not “who may message it.”
Field notes
- Never put secrets in messages sent to
*; treat that as public disclosure. - Validate
event.originexactly. Suffix matching likeendsWith('example.com')includes hostile subdomains. - Do not execute message contents as HTML or JavaScript. Structured cloning does not sanitize markup.
- Include a transaction ID or nonce so messages cannot be replayed across unrelated operations.
- Remember that cross-origin frames can still create side channels through size, timing, focus, navigation counts, and rendering behavior.
Was this lesson helpful?