Security rules

NGB008: Unverified message source

Reports message handlers that never compare event.source with the window they communicate with, but only where such a specific peer exists in the same class (or file, for code outside classes): an iframe contentWindow, a popup from window.open(), window.opener, or window.parent/window.top used with postMessage..

Property Value
Rule ID NGB008
Category iframe & cross-window communication
Default severity medium
CWE CWE-346
Built-in presets recommended: warn, strict: error

Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.

What it checks

Reports message handlers that never compare event.source with the window they communicate with, but only where such a specific peer exists in the same class (or file, for code outside classes): an iframe contentWindow, a popup from window.open(), window.opener, or window.parent/window.top used with postMessage.

Why it matters

An origin check proves which site sent a message, not which window. Several frames or popups can share an origin (e.g. two instances of the same widget, or an attacker-controlled page on a shared hosting origin), so a handler that expects replies from one particular frame should also verify event.source === frame.contentWindow.

Detection behavior and supported patterns

  • Peer windows: iframe.contentWindow, this.frame.nativeElement.contentWindow, window.open(...), window.opener, window.parent.postMessage(...), window.top.postMessage(...)
  • Handlers discovered like NGB007 (addEventListener('message'), @HostListener('window:message'), host bindings, fromEvent, Renderer2.listen)
  • Accepted verification: event.source === peer / !== with early return, including checks in fromEvent(...).pipe(filter(...))

Example

TypeScript
@HostListener('window:message', ['$event'])onMessage(e: MessageEvent) {  if (e.origin !== WIDGET_ORIGIN) return;  this.height = e.data.height;}

Safer alternative

TypeScript
@HostListener('window:message', ['$event'])onMessage(e: MessageEvent) {  if (e.origin !== WIDGET_ORIGIN || e.source !== this.frame.nativeElement.contentWindow) return;  this.height = e.data.height;}

Remediation

Keep a reference to the peer window and ignore other senders: if (event.source !== this.frame.nativeElement.contentWindow) return; — in addition to the origin check.

False-positive considerations

  • Handlers that only process messages from one origin that can host exactly one frame may not need a source check; suppress with a justification.

Known limitations

  • Only peers referenced in the same class (or same file outside classes) as the handler are considered.
  • The compared value is not verified to be the same peer window; any equality check on event.source is accepted.

References

Run it

Shell
ngbastion explain NGB008