Security rules

NGB001: Unsafe HTML trust bypass

Reports calls to DomSanitizer.bypassSecurityTrustHtml() whose argument can be traced to an untrusted source (URL, postMessage, user input, storage, HTTP response).

Property Value
Rule ID NGB001
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 calls to DomSanitizer.bypassSecurityTrustHtml() whose argument can be traced to an untrusted source (URL, postMessage, user input, storage, HTTP response). Bypass calls on values whose origin cannot be resolved are reported with low confidence; constant HTML and values passed through a recognized HTML sanitizer are not reported.

Why it matters

bypassSecurityTrustHtml() does not sanitize anything. It wraps a string so Angular skips its built-in HTML sanitization when the value is bound to [innerHTML]. Any attacker-controlled markup in that string executes as script in your origin (DOM XSS).

Detection behavior and supported patterns

  • sanitizer.bypassSecurityTrustHtml(value) where sanitizer is DomSanitizer from @angular/platform-browser (constructor injection or inject())
  • Values traced through variables, class fields, function parameters/returns, services across files, template literals, string methods, RxJS map/switchMap/tap and Angular signal/computed/toSignal
  • Untrusted sources: ActivatedRoute query/route params and fragment, location.*, document.URL, window.name, MessageEvent.data, input element values, [(ngModel)] bindings, Web Storage reads, HttpClient/fetch responses
  • Recognized sanitizers: DomSanitizer.sanitize(SecurityContext.HTML, …), DOMPurify.sanitize(), functions listed in trustedSanitizers. URL encoding (encodeURIComponent(), encodeURI()) is not HTML escaping: such flows are still reported, with lowered confidence

Example

TypeScript
const html = this.route.snapshot.queryParamMap.get('preview') ?? '';this.content = this.sanitizer.bypassSecurityTrustHtml(html);

Safer alternative

TypeScript
const html = this.route.snapshot.queryParamMap.get('preview') ?? '';this.content = html; // <div [innerHTML]="content"> is sanitized by Angular

Remediation

Bind untrusted HTML with [innerHTML] directly and let Angular sanitize it, or sanitize explicitly (DOMPurify.sanitize() / sanitizer.sanitize(SecurityContext.HTML, v)) before any trust bypass. Reserve bypassSecurityTrustHtml() for constant or fully server-controlled markup.

False-positive considerations

  • HTTP responses from your own trusted CMS are reported (medium confidence). If the backend guarantees sanitized HTML, document an exception with a justification.
  • Custom sanitizers are not trusted by default; register them in trustedSanitizers after review.

Known limitations

  • Values coming from component input()s or @Input()s are reported as unresolved (the parent binding is not followed).
  • Data flowing through unmodeled libraries is propagated with reduced confidence; data stored in Maps, global state libraries (NgRx, NGXS) or closures is not followed.
  • Validation in a separate function (e.g. an allowlist check) is not recognized as sanitization unless configured in trustedSanitizers.

References

Run it

Shell
ngbastion explain NGB001