Engineering Journal
Schema Editor
Schema Editor

postMessage Sends Messages to Any Embedder When the Origin Is '*'

2026-06-04

TLDR

Using '' as the target origin parameter in window.parent.postMessage(data, '') allows any webpage embedding your canvas app inside an <iframe> to intercept your message payloads. Replacing '*' with window.location.origin (or explicit extension schemes) ensures messages are delivered exclusively to authorized parent frames. Adding lint rules or automated CI grep checks prevents wildcard target origins from entering production.
postMessage Target OriginTarget Frame DeliverySame-Origin EnforcementVulnerability Rating
Wildcard ('*')Any embedding frame on any domainDisabled (Bypassed)Vulnerable (OWASP A07)
window.location.originStrictly same-origin parent framesEnforced by BrowserSecure

Problem statement: the wildcard origin risk

During a security audit of a webview-based canvas tool, the audit flagged multiple occurrences of postMessage(data, '*').

While the application operated correctly, passing '*' meant the browser delivered message payloads to whichever parent frame embedded the tool.


Technical failure mode: cross-origin data leakage

If an attacker embeds the canvas editor inside a hidden <iframe> on an external site, postMessage(data, '*') delivers document state and configuration objects directly to the attacker's event listeners.

// DEFECTIVE: Delivers message payload to ANY embedding domain
window.parent.postMessage({ type: 'editor:export', payload: canvasJson }, '*');

The fix: target origin constraints & CI verification

Step 1: explicit target origin assignment

Replace '*' with window.location.origin for same-origin messaging:
// REFACTORED: Target origin restricted to same-origin parent frame
window.parent.postMessage({ type: 'editor:export', payload: canvasJson }, window.location.origin);

Step 2: receiver origin guard

Validate incoming message origins explicitly:
// REFACTORED: Receiver origin check
window.addEventListener('message', (e) => {
  if (e.origin !== window.location.origin && !e.origin.startsWith('vscode-webview://')) {
    return; // Ignore messages from untrusted domains
  }
  processMessage(e.data);
});

Step 3: CI pre-commit grep guard

Add an automated check in pre-commit hooks or CI workflows to block wildcard postMessage calls:
#!/bin/bash

Block any postMessage calls using wildcard '*' target origins

if grep -rn "postMessage(" src/ | grep "'\*'"; then echo "ERROR: Wildcard postMessage('*') target origin detected! Use window.location.origin." exit 1 fi
Rule of thumb: Replace all postMessage('*') calls with window.location.origin. Enforce origin checks at CI build time to block security regressions.
Read this post in the full Engineering Journal →