Security rules
NGB007: Missing origin validation
Reports window message handlers that read event.data without an origin check that dominates the read.
| Property | Value |
|---|---|
| Rule ID | NGB007 |
| Category | iframe & cross-window communication |
| Default severity | high |
| CWE | CWE-346, CWE-940 |
| 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 window message handlers that read event.data without an origin check that dominates the read. Strict checks — equality with a known origin, or membership in an allowlist array/Set — suppress the finding. Substring-style checks (startsWith, endsWith, includes/indexOf on the origin string, unanchored regular expressions) are reported as weak. Origin checks that do not guard the data use (logged, inverted, or performed after the read) do not count.
Why it matters
Any window can send messages to yours (any page can open or frame your app). Without verifying event.origin, attacker pages can inject commands or data into your application. event.origin.startsWith("https://trusted.com") also matches https://trusted.com.attacker.net.
Detection behavior and supported patterns
window.addEventListener('message', fn),window.onmessage = fn,Renderer2.listen('window', 'message', fn)@HostListener('window:message', ['$event'])and componenthost: { '(window:message)': 'onMessage($event)' }fromEvent(window, 'message').pipe(filter(...)).subscribe(fn)(checks insidefiltercount)- Reads:
event.data,event["data"],({ data }) =>,const { data } = event; one level of delegation (this.handle(event)/service.process(event.data)) - Strict:
event.origin === ALLOWED,!==+ early return,ALLOWED.includes(event.origin),set.has(origin),indexOf(origin) !== -1on arrays, anchored regex/^https:\/\/a\.com$/.test(origin), and project validator functions whose body performs such a check
Example
window.addEventListener('message', (e) => { if (e.origin.startsWith('https://partner.example')) this.apply(e.data);});Safer alternative
const ALLOWED = ['https://partner.example.com'];window.addEventListener('message', (e) => { if (!ALLOWED.includes(e.origin)) return; this.apply(e.data);});Remediation
Compare event.origin against an exact allowlist before touching event.data: if (!ALLOWED_ORIGINS.includes(event.origin)) return;. Also verify event.source when you expect a specific frame (NGB008) and validate the payload structure (NGB009).
False-positive considerations
- Handlers that intentionally accept messages from any origin (public widget APIs) and only process inert data are reported with medium confidence; document an exception explaining why.
Known limitations
- Handlers registered through helper libraries or stored in variables before registration are not discovered.
- Checks performed in a different function than the read (other than a validator called in the guard condition, or a guard around the delegating call) are not recognized.
- An exact comparison against a value that is itself attacker-controlled is still treated as strict.
References
Run it
ngbastion explain NGB007