Security: XSS, sanitization, and CSP

Intermediate · 12 min

Angular's built-in DOM sanitization, the bypassSecurityTrust escape hatches, and Content-Security-Policy considerations for a real app.

Before thisBuilt-in and custom pipes

The DotNet Office · 12 min

Security: XSS, sanitization, and CSP

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by The DotNet OfficeReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Angular sanitizes values bound into security-sensitive contexts (innerHTML, URLs, styles) by default, stripping anything that looks like it could execute as script — this is on automatically, not something you opt into.
  • The bypassSecurityTrust* family of functions exists specifically to opt out of that default sanitization for one value you've judged safe — and every one of them is a real risk if used on anything derived from user input.
  • A Content-Security-Policy header is a second, independent layer of defense, enforced by the browser itself — it restricts what scripts are allowed to run at all, as a backstop even if a sanitization bug somehow let something through.

Sanitization is on by default — you have to actively turn it off

A content-projection-style vulnerability is an old, well-understood problem: render user-supplied content as raw HTML, and a malicious user can inject a <script> tag, or an onerror handler on an <img>, that executes arbitrary JavaScript in another user’s browser — a cross-site scripting (XSS) attack. Angular’s default behavior is built to close this off automatically: any value bound into a security-sensitive context — [innerHTML], a URL used in [src] or [href], inline styles — is run through Angular’s DomSanitizer first, which strips anything that looks like it could execute as script, before the value ever reaches the real DOM.

<div [innerHTML]="userComment"></div>
<!-- a userComment containing <img src=x onerror="alert(1)"> renders the
     image tag with the onerror attribute stripped — nothing executes -->

This is the default, happening whether or not you asked for it. It’s also why [innerHTML] is usually safe to bind directly to user-supplied text, in a way that would be a serious vulnerability in a framework with no equivalent default protection.

bypassSecurityTrust*: a real escape hatch, and a real risk

Sometimes you genuinely need to render HTML (or a URL, or a style) that Angular’s sanitizer would otherwise strip something out of — markup you, the developer, constructed yourself and know is entirely safe, say. DomSanitizer provides a family of functions for exactly that: bypassSecurityTrustHtml, bypassSecurityTrustUrl, bypassSecurityTrustStyle, and others, each telling Angular “skip sanitization for this specific value, I’m vouching for it.”

trustedHtml = this.sanitizer.bypassSecurityTrustHtml(this.staticMarketingHtml);

The name is deliberately alarming, and that’s the point: calling one of these on anything that traces back to user input — even indirectly, even through several steps of processing — reopens exactly the vulnerability the default sanitization exists to close. The only safe use of bypassSecurityTrust* is on content you, the developer, fully control and can verify contains nothing an attacker influenced.

CSP: a second, independent layer, enforced by the browser itself

Sanitization happens inside your app’s own code — it’s Angular doing the right thing, but it’s still your code that has to do it correctly, every time. A Content-Security-Policy header is a different kind of defense entirely: a policy, sent with every response, that the browser itself enforces, restricting what scripts are allowed to execute on the page at all — which domains scripts can be loaded from, whether inline <script> tags are allowed, and more. It’s not a replacement for correct sanitization; it’s a backstop that limits the damage even if a sanitization bug, a bypassed value, or a compromised third-party script somehow got something malicious onto the page. For an Angular app specifically, setting up a CSP that’s compatible with how Angular itself works (its use of certain inline styles, in particular) takes some real, deliberate configuration rather than a copy-pasted generic policy.

This lesson’s video covers DomSanitizer and the bypass functions directly, with the risk framing central to the explanation rather than an afterthought; the linked “deeper” video covers setting up a real CSP for an Angular app specifically, including the NGINX-side configuration a production deployment actually needs.

Check yourself

  1. 01If a component binds `[innerHTML]="userComment"` where userComment contains `<img src=x onerror="alert(1)">`, what does Angular do with that value by default?

    It sanitizes it before inserting it into the DOM — stripping the `onerror` handler (and anything else that looks like it could execute as script), so the image tag renders but the injected script does not run. This sanitization happens automatically for innerHTML and other security-sensitive bindings; you don't have to request it, and bypassing it requires explicitly calling one of the bypassSecurityTrust* functions.

  2. 02Why is calling `sanitizer.bypassSecurityTrustHtml(userComment)` on a value that came from user input a serious risk, even though the function itself is a documented, real Angular API?

    bypassSecurityTrust* functions tell Angular "trust this value completely, skip sanitization" — which is exactly correct for content you, the developer, constructed and know is safe, and exactly wrong for anything that passed through user input along the way. Calling it on user-supplied content re-opens the XSS vulnerability Angular's default sanitization exists specifically to close, with no warning that you've done so.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark