Security rules
NGB034: Untrusted postMessage target origin
Reports postMessage() calls whose targetOrigin is chosen by an attacker: echoed from event.origin inside a message handler that does not strictly validate the sender, or traced from URL parameters, document.referrer, window.name or message data.
| Property | Value |
|---|---|
| Rule ID | NGB034 |
| Category | iframe & cross-window communication |
| Default severity | high |
| CWE | CWE-201, CWE-345 |
| Built-in presets | recommended: error, strict: error |
Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.
What it checks
Reports postMessage() calls whose targetOrigin is chosen by an attacker: echoed from event.origin inside a message handler that does not strictly validate the sender, or traced from URL parameters, document.referrer, window.name or message data. NGB006 covers the literal '*'; this rule covers origins that are effectively wildcards because the other party picks them.
Why it matters
targetOrigin is the only thing that stops a message from being delivered to the wrong window. Replying to event.origin without checking it, or taking the origin from ?parentOrigin= / document.referrer, lets any page that frames or opens your app receive the reply — including tokens or user data in the payload.
Detection behavior and supported patterns
event.source.postMessage(reply, event.origin)and{ targetOrigin: e.origin }inside handlers found like NGB007 (addEventListener, onmessage, HostListener, fromEvent, Renderer2.listen) without a strict origin check that dominates the callparent.postMessage(data, new URL(document.referrer).origin),window.opener.postMessage(data, params.get('origin'))(taint engine)- Severity stays high when the payload is a credential or payment value; otherwise the finding is medium severity
- Strict origin allowlists (
ALLOWED.includes(origin), equality with a constant) suppress the finding
Example
window.addEventListener('message', (e) => { (e.source as Window).postMessage({ token: this.auth.token }, e.origin);});Safer alternative
window.addEventListener('message', (e) => { if (!ALLOWED_ORIGINS.includes(e.origin)) return; (e.source as Window).postMessage({ token: this.auth.token }, e.origin);});Remediation
Validate the sender first (if (!ALLOWED_ORIGINS.includes(event.origin)) return;) and only then reply to event.origin, or post to a constant origin. Never read the target origin from the URL or the referrer.
False-positive considerations
- Public widgets that deliberately answer any embedder with non-sensitive data are reported with medium severity; document an exception.
Known limitations
- Origins stored in fields or services and used later are followed by the taint engine only for modeled sources; a stored
event.originfrom an earlier message is not followed. - The payload is classified by names and types only.
References
Run it
ngbastion explain NGB034