Security rules

NGB003: DOM injection sink

Reports untrusted data written to raw DOM HTML sinks that bypass Angular templating: innerHTML/outerHTML/srcdoc assignments on elements (including ElementRef.nativeElement), insertAdjacentHTML(), document.write(), Range.createContextualFragment(), setHTMLUnsafe(), and Renderer2.setProperty(el, "innerHTML", v)..

Property Value
Rule ID NGB003
Category Angular security & XSS
Default severity high
CWE CWE-79
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 untrusted data written to raw DOM HTML sinks that bypass Angular templating: innerHTML/outerHTML/srcdoc assignments on elements (including ElementRef.nativeElement), insertAdjacentHTML(), document.write(), Range.createContextualFragment(), setHTMLUnsafe(), and Renderer2.setProperty(el, "innerHTML", v).

Why it matters

Angular sanitizes [innerHTML] template bindings, but direct DOM APIs receive the raw string. Attacker-controlled markup reaching them executes in the application origin.

Detection behavior and supported patterns

  • el.innerHTML = v, el.outerHTML = v, iframe.srcdoc = v (also +=) where el is a DOM element type or ElementRef.nativeElement
  • el.insertAdjacentHTML(position, v), el.setHTMLUnsafe(v), document.write(v) / writeln(v), range.createContextualFragment(v)
  • renderer.setProperty(el, "innerHTML" | "outerHTML" | "srcdoc", v) and renderer.setAttribute(el, "srcdoc", v) with Renderer2 from @angular/core
  • Template [innerHTML] bindings are intentionally NOT reported: Angular sanitizes them

Example

TypeScript
this.host.nativeElement.innerHTML = this.route.snapshot.queryParams['msg'];

Safer alternative

TypeScript
this.host.nativeElement.textContent = this.route.snapshot.queryParams['msg'];

Remediation

Prefer template binding ([innerHTML], which Angular sanitizes) or textContent for text. If raw HTML is required, sanitize it with DOMPurify immediately before insertion.

False-positive considerations

  • Markup produced by a trusted rendering library from untrusted input (e.g. a Markdown renderer configured to escape HTML) is reported with reduced confidence because the library call is unmodeled.

Known limitations

  • An untyped receiver is only treated as an element when it is ….nativeElement; other any-typed objects are skipped.
  • jQuery/third-party DOM helpers ($(el).html(v)) are not modeled.

References

Run it

Shell
ngbastion explain NGB003