kdocs
GitHub
Lang - Web
Lang - Web
  • Base
    • Css
    • Javascript
    • Typescript
      • New Project
  • Frameworks
    • Angular
      • Directives
      • Components
      • Templates
        • Bindings
        • Control Flow
        • Variables
      • Signals
      • Pipes
      • Services
        • Dependency Injection
      • Forms
        • Reactive Form
        • Template-Driven Form
      • Router
      • HTTP Client
      • Observables RxJS
      • Testing
        • Components
        • Directives
        • Pipes
        • Services
      • Optimization & Performance
      • Security
Powered by GitBook
On this page
  • Best Practices
  • Bypass Security
  1. Frameworks
  2. Angular

Security

Best Practices

  • To systematically block XSS bugs, Angular treats all values as untrusted by default.

  • Values inserted into the DOM are sanitized and escaped.

    • You can manually sanitize untrusted values with DomDanitizer.sanitize.

  • Always use AOT Ahead-of-Time template compiler in production deployments.

Bypass Security

  • To mark values as truted, inject DomSanitizer

constructor(private sanitizer: DomSanitizer) {
    // javascript: URLs are dangerous if attacker controlled.
    // Angular sanitizes them in data binding, but you can
    // explicitly tell Angular to trust this value:
    this.dangerousUrl = 'javascript:alert("Hi there")';
    this.trustedUrl = sanitizer.bypassSecurityTrustUrl(this.dangerousUrl);
}
PreviousOptimization & Performance

Last updated 7 months ago