Security rules

NGB035: Untrusted dynamic method dispatch

Reports calls through a computed member whose name comes from an attacker-controllable source — handlers[event.data.cmd](…), this[params.get('action')](), window[name](…), also via .call() / .apply() — without an own-property or allowlist check on the key.

Property Value
Rule ID NGB035
Category iframe & cross-window communication
Default severity high
CWE CWE-470
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 calls through a computed member whose name comes from an attacker-controllable source — handlers[event.data.cmd](…), this[params.get('action')](), window[name](…), also via .call() / .apply() — without an own-property or allowlist check on the key. Sources: postMessage data, URL / route parameters, window.name, document.referrer.

Why it matters

A message or link that picks the function name can call any method reachable from the object: other handlers than intended, inherited members (constructor, __defineGetter__, toString), and on window or this every global function or component method — including ones that navigate, post data or touch storage. key in handlers and typeof handlers[key] === 'function' do not help, because inherited members pass both.

Detection behavior and supported patterns

  • obj[key](…), obj[key].call(…), obj[key].apply(…) where key is traced (taint engine) to MessageEvent.data, ActivatedRoute / location values, window.name or document.referrer
  • High severity on window / globalThis / this; medium on handler maps
  • Recognized allowlists guarding the call: Object.hasOwn(map, key), map.hasOwnProperty(key), ALLOWED.includes(key), set.has(key), comparisons with string literals

Example

TypeScript
window.addEventListener('message', (e) => this[e.data.method](...e.data.args));

Safer alternative

TypeScript
const HANDLERS = new Map([['resize', (a) => this.resize(a)], ['close', () => this.close()]]);window.addEventListener('message', (e) => {  if (e.origin !== PARENT_ORIGIN) return;  HANDLERS.get(e.data.method)?.(e.data.args);});

Remediation

Dispatch with a switch over the expected command names, or look the handler up in a Map / Object.create(null) table after checking Object.hasOwn(handlers, cmd); validate the message origin first (NGB007).

False-positive considerations

  • Handler maps created with Object.create(null) and frozen are safe from prototype keys but still let the sender pick any handler; the finding is still reported with medium severity.

Known limitations

  • Dispatch through Map.get(key) is not reported (Maps have no prototype keys); only computed member calls are.
  • Keys that pass through unmodeled libraries lose confidence or are not followed.

References

Run it

Shell
ngbastion explain NGB035