Security rules

NGB009: Unvalidated message payload

Traces MessageEvent.data from window message handlers into security-relevant operations (DOM HTML sinks, navigation, eval, Web Storage writes, HttpClient / fetch requests, Router navigation, Renderer2 property writes, trust-bypass APIs) and reports flows that are not preceded by runtime validation of the payload..

Property Value
Rule ID NGB009
Category iframe & cross-window communication
Default severity high
CWE CWE-20, 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

Traces MessageEvent.data from window message handlers into security-relevant operations (DOM HTML sinks, navigation, eval, Web Storage writes, HttpClient / fetch requests, Router navigation, Renderer2 property writes, trust-bypass APIs) and reports flows that are not preceded by runtime validation of the payload.

Why it matters

TypeScript types such as (e: MessageEvent<Command>) or e.data as Command are erased at runtime; the payload is whatever the sender posted. Without structural validation, unexpected types and fields reach sensitive code — even from an allowed origin that is itself compromised or buggy.

Detection behavior and supported patterns

  • Data flow from event.data (typed MessageEvent, destructured { data }) through variables, helpers and services (taint engine) to the operations listed above
  • Full validation: typeof / instanceof / in checks, Array.isArray, schema calls such as schema.parse() / safeParse() / validate() (in a dominating guard or on the data path)
  • User type-predicate functions (function isCommand(x): x is Command) are recognized, but reported with low confidence because their body is not verified
  • Discriminant-only checks (data.type === 'resize') do not validate the rest of the payload: reported with medium confidence
  • A strict origin check (see NGB007) lowers confidence to low

Example

TypeScript
window.addEventListener('message', (e: MessageEvent<{ url: string }>) => {  if (e.origin !== PARTNER) return;  location.href = e.data.url;});

Safer alternative

TypeScript
const Msg = z.object({ url: z.string().url().startsWith('https://partner.example.com/') });window.addEventListener('message', (e) => {  if (e.origin !== PARTNER) return;  const parsed = Msg.safeParse(e.data);  if (parsed.success) location.href = parsed.data.url;});

Remediation

Validate the full message shape at runtime before acting on it — with a schema library (zod, valibot, ajv) or explicit typeof/in checks — and only then read fields. Combine with origin (NGB007) and source (NGB008) checks.

False-positive considerations

  • Libraries that validate internally (e.g. a typed RPC layer) are not recognized unless the call appears as a validation guard.

Known limitations

  • Only handlers on window are considered (not MessagePort/Worker/BroadcastChannel).
  • Validation must be a guard on the flow (same function as the source or sink) or a call on the data path; validation stored in a flag variable is not recognized.
  • The handler parameter must be typed MessageEvent or be inferred as one (e.g. inline addEventListener callbacks); any-typed events are not recognized as sources.

References

Run it

Shell
ngbastion explain NGB009