Security rules

NGB052: Unsafe custom Angular sanitizer

Reports providers that replace Angular's Sanitizer or DomSanitizer ({ provide: Sanitizer, useClass | useValue | useExisting … }) with an implementation whose sanitize() returns its input unchanged (high), does so for some security contexts (high, medium confidence), or contains custom logic that does not delegate to Angular's sanitizer or DOMPurify (medium, low confidence)..

Property Value
Rule ID NGB052
Category Angular security & XSS
Default severity high
CWE CWE-79, CWE-116
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 providers that replace Angular's Sanitizer or DomSanitizer ({ provide: Sanitizer, useClass | useValue | useExisting … }) with an implementation whose sanitize() returns its input unchanged (high), does so for some security contexts (high, medium confidence), or contains custom logic that does not delegate to Angular's sanitizer or DOMPurify (medium, low confidence).

Why it matters

Every [innerHTML], [href], [src] and [style] binding in the application goes through the Sanitizer token. Replacing it is a single line that silently disables Angular's XSS protection everywhere — typically added to make a rich-text editor or a legacy widget "work". Hand-written sanitizers are also a classic source of bypasses (regex-based tag stripping, allowlists that forget srcdoc or xlink:href).

Detection behavior and supported patterns

  • { provide: Sanitizer | DomSanitizer, useClass: X } where X.sanitize(ctx, value) returns value (also String(value), value as string, value?.toString())
  • { provide: Sanitizer, useValue: { sanitize: (_c, v) => v } }
  • useClass / useValue implementations that return the input only under some conditions (if (ctx === SecurityContext.HTML) return value;)
  • Implementations that delegate to DOMPurify.sanitize() or an injected Angular DomSanitizer are not reported

Example

TypeScript
@Injectable() class NoopSanitizer extends Sanitizer { sanitize(_ctx: SecurityContext, value: string) { return value; } }providers: [{ provide: Sanitizer, useClass: NoopSanitizer }]

Safer alternative

TypeScript
providers: [] // keep Angular's DomSanitizer; sanitize rich text with DOMPurify before binding

Remediation

Do not replace Angular's sanitizer. Keep the default DomSanitizer, sanitize rich text with DOMPurify before binding, and use bypassSecurityTrust*() only for specific constant values. If a replacement is unavoidable, delegate to Angular's ɵDomSanitizerImpl or DOMPurify for every context.

False-positive considerations

  • Test-only providers inside spec files are excluded by the default exclude patterns; other test harness modules may be reported.

Known limitations

  • Factories (useFactory) and implementations defined in libraries are reported only when the class is visible in the project.
  • The quality of genuinely custom sanitization logic is not evaluated; such replacements are reported with low confidence for review.

References

Run it

Shell
ngbastion explain NGB052