Security rules

NGB051: Mismatched Angular sanitization context

Reports DomSanitizer.sanitize() calls whose security context does not match where the result is used (a value sanitized as a URL passed to bypassSecurityTrustHtml() or bypassSecurityTrustResourceUrl(), or assigned to innerHTML), sanitize(SecurityContext.NONE, …), which returns its input unchanged, and ngx-markdown configured with sanitize: SecurityContext.NONE or [disableSanitizer]="true"..

Property Value
Rule ID NGB051
Category Angular security & XSS
Default severity high
CWE CWE-116, 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 DomSanitizer.sanitize() calls whose security context does not match where the result is used (a value sanitized as a URL passed to bypassSecurityTrustHtml() or bypassSecurityTrustResourceUrl(), or assigned to innerHTML), sanitize(SecurityContext.NONE, …), which returns its input unchanged, and ngx-markdown configured with sanitize: SecurityContext.NONE or [disableSanitizer]="true".

Why it matters

Angular's sanitizer is context-specific: SecurityContext.URL only neutralizes dangerous URL schemes and leaves <img onerror> intact; SecurityContext.HTML leaves https://evil.example/x.js intact. Code that sanitizes for one context and then trusts the result in another looks reviewed but is not protected, and SecurityContext.NONE is a no-op that reads like a sanitizer call.

Detection behavior and supported patterns

  • bypassSecurityTrustHtml|Style|Script|Url|ResourceUrl(sanitizer.sanitize(SecurityContext.X, v)) where X is not the bypass context (also through a const and ?? '')
  • el.innerHTML = sanitizer.sanitize(SecurityContext.URL, v) (any context other than HTML)
  • sanitizer.sanitize(SecurityContext.NONE, v) / sanitize(0, v)
  • ngx-markdown: provideMarkdown({ sanitize: SecurityContext.NONE }), MarkdownModule.forRoot({ sanitize: SecurityContext.NONE }), <markdown [disableSanitizer]="true">

Example

TypeScript
const html = this.sanitizer.sanitize(SecurityContext.URL, comment) ?? '';this.content = this.sanitizer.bypassSecurityTrustHtml(html);

Safer alternative

TypeScript
this.content = this.sanitizer.sanitize(SecurityContext.HTML, comment) ?? ''; // bound to [innerHTML]

Remediation

Sanitize with the context of the sink (SecurityContext.HTML before HTML, URL before URLs) and do not wrap the result in a bypassSecurityTrust*() call at all: bind it and let Angular sanitize. For resource URLs, check the value against an allowlist of origins instead of sanitizing it. Keep markdown rendering sanitized.

False-positive considerations

  • SecurityContext.NONE on values that are constant or already trusted (SafeValues) is reported; use the value directly instead.

Known limitations

  • The sanitized value is followed through const variables only; storing it in a property and trusting it elsewhere is not linked.
  • Whether the value is attacker-controlled is not required: NGB001 / NGB002 report the trust bypass when the origin is traced.

References

Run it

Shell
ngbastion explain NGB051