Analyze a TypeScript snippet
Check string literals for hardcoded credentials with NGB018. Run the CLI for template, configuration and project-wide checks.
Playground
Test a snippet locally or explore three Angular examples and their fixes.
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.
A query parameter is wrapped in markup and passed to bypassSecurityTrustHtml().
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); }}high NGB001 Unsafe HTML trust bypass
Untrusted data (URL query parameter 'draft' (ActivatedRoute)) is marked as trusted HTML with bypassSecurityTrustHtml(); Angular will not sanitize 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.