Playground

See a finding, then its fix.

Test a snippet locally or explore three Angular examples and their fixes.

Analyze a TypeScript snippet

Check string literals for hardcoded credentials with NGB018. Run the CLI for template, configuration and project-wide checks.

Demonstration: the following fixture results were produced at build time.

Example
Variant

Sanitizer bypass: unsafe variant

A query parameter is wrapped in markup and passed to bypassSecurityTrustHtml().

TypeScriptpreview.component.ts
import { Component, inject } from '@angular/core';import {  DomSanitizer,  SafeHtml,} from '@angular/platform-browser';import { ActivatedRoute } from '@angular/router';@Component({  selector: 'app-preview',  template: `<article [innerHTML]="preview"></article>`,})export class PreviewComponent {  private readonly sanitizer = inject(DomSanitizer);  private readonly route = inject(ActivatedRoute);  readonly preview: SafeHtml = this.render(    this.route.snapshot.queryParamMap.get('draft') ?? '',  );  private render(markup: string): SafeHtml {    const html = `<div class="draft">${markup}</div>`;    return this.sanitizer.bypassSecurityTrustHtml(html);  }}

1 finding

scanned at build time

high NGB001 Unsafe HTML trust bypass

confidence high · dataflow · line 22

Untrusted data (URL query parameter 'draft' (ActivatedRoute)) is marked as trusted HTML with bypassSecurityTrustHtml(); Angular will not sanitize it.

  1. sourceline 17URL query parameter 'draft' (ActivatedRoute)
  2. argumentline 16passed as 'markup' to render()
  3. transformline 21interpolated into a template literal
  4. assignmentline 21assigned to 'html'
  5. sinkline 22bypassSecurityTrustHtml() disables Angular HTML sanitization
Why it matters and how to fix it

`bypassSecurityTrustHtml()` does not sanitize anything.

Fix: 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.