Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Angular Security


Angular defends against XSS by sanitizing template bindings.


Security Essentials

  • XSS protection: Angular sanitizes template bindings to block script injection.
  • Sanitization: Dangerous values are stripped or transformed before writing to the DOM.
  • Bind properties: Use property bindings over string concatenation for URLs/HTML.
  • Avoid bypass: Only use DomSanitizer in audited, rare cases.
// Angular sanitizes [innerHTML], [href], [src]
@Component({ template: `<div [innerHTML]="html"></div>` })
class C { html = '<b>Hello</b> <script>alert(1)</script>'; }

Related: See HTTP and Templates.

Tip: Bind with [href]/[src]/[innerHTML] and let Angular sanitize.

Only use DomSanitizer in rare, audited cases.


Sanitization Basics

// Property binding runs the sanitizer
<img [src]="photoUrl" alt="...">
<a [href]="profileUrl">Profile</a>

Example

import { bootstrapApplication } from '@angular/platform-browser';
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  standalone: true,
  template: `
    <h3>Sanitized HTML</h3>
    <div [innerHTML]="html"></div>
  `
})
class App {
  html = `Hello <script>alert('xss')</script>`;
}

bootstrapApplication(App);
<app-root></app-root>

Run Example »

Example explained

  • [innerHTML]: Angular sanitizes bound HTML, removing dangerous content (e.g., scripts).
  • Property bindings: Use [innerHTML]/[href]/[src] so the sanitizer runs.
  • Interpolation: {{ ... }} escapes HTML by default; use bindings for safe HTML insertion.

Key points:

  • Angular sanitizes values bound to [innerHTML], [src], and [href].
  • Avoid string concatenation for HTML/URLs.
  • Use property bindings so the sanitizer runs.
  • Avoid bypassing sanitizer; Use safe data flow and templates.

REMOVE ADS


Safe DOM APIs

  • Use property bindings for URLs and HTML to ensure the sanitizer runs.
  • Audit any use of DomSanitizer.bypassSecurityTrust*; document scope and rationale.
  • Limit bypass to vetted, static content paths encapsulated in small utilities.
import { DomSanitizer } from '@angular/platform-browser';

// Use: [href]="safeUrl" with validated values
// Avoid bypass unless strictly necessary and audited:
// const safe = sanitizer.bypassSecurityTrustUrl(untrusted);

Guidelines:

  • Do not pass untrusted/user input to bypassSecurityTrust*.
  • Only bypass for vetted, static sources; encapsulate in a small utility with comments.
  • Enable Content Security Policy (CSP); consider Trusted Types for stronger XSS defenses.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.