Full answer
Angular treats every template value as untrusted. Interpolation ({{ text }}) escapes HTML, so markup shows as text. Bindings that go into dangerous places, such as [innerHTML], [src], [href] and [style], are sanitized: scripts are removed, event attributes such as onmouseover are dropped, and javascript: URLs are rewritten to unsafe:.... In development, the console warns "sanitizing HTML stripped some content".
Sometimes this blocks valid content: an embedded video iframe, trusted CMS HTML with inline styles, a blob: or tel: link. DomSanitizer offers bypassSecurityTrustHtml, bypassSecurityTrustStyle, bypassSecurityTrustScript, bypassSecurityTrustUrl and bypassSecurityTrustResourceUrl. The returned object is a marker: Angular puts the value into the DOM as it is.
Why it is dangerous: the name says "trust", and you take over the responsibility. If a part of the value comes from a user, a database field or an API, an attacker can store <img src=x onerror=stealToken()> and run script in every visitor's browser (stored XSS). That script can read tokens, send requests as the user, and change the page.
Safe patterns:
- Prefer no bypass.
[innerHTML]with the default sanitizer is already safe for most rich text. - Build, do not accept. For a video, accept only an id, validate it with a regular expression (
/^[\w-]{11}$/), build the URL yourself from a fixed origin, and only then callbypassSecurityTrustResourceUrl. - Sanitize with a dedicated library such as DOMPurify (with a strict allowlist of tags and attributes) before bypassing, for HTML that must keep more than Angular allows.
- Isolate: use a sandboxed iframe (
sandboxattribute) for untrusted documents. - Defence in depth: a Content Security Policy that blocks inline scripts, and Trusted Types. Angular supports Trusted Types, which makes dangerous sinks accept only trusted objects.
- Keep the bypass call in one small, reviewed service or pipe, never inline in components, so it is easy to search and audit. Add a lint rule or a code review check for
bypassSecurityTrust.
Also never use ElementRef.nativeElement.innerHTML or document.write with user data. They skip Angular's protection completely.
import { Component, inject } from '@angular/core';
import { DomSanitizer, SafeHtml, SafeResourceUrl } from '@angular/platform-browser';
export const hostile = `<b onmouseover="1">bold</b><script>1</script><a href="javascript:void(0)">link</a>`;
@Component({
selector: 'app-html-demo',
template: `
<div id="sanitized" [innerHTML]="hostile"></div>
<div id="bypassed" [innerHTML]="trusted"></div>
`,
})
export class HtmlDemoComponent {
private readonly sanitizer = inject(DomSanitizer);
readonly hostile = hostile;
// Risky: only acceptable for HTML that you wrote yourself.
readonly trusted: SafeHtml = this.sanitizer.bypassSecurityTrustHtml(hostile);
}
// Safer pattern: accept an id, validate it, build the URL, then trust the result.
export function videoEmbedUrl(sanitizer: DomSanitizer, id: string): SafeResourceUrl | null {
if (!/^[\w-]{11}$/.test(id)) {
return null;
}
return sanitizer.bypassSecurityTrustResourceUrl(`https://www.youtube-nocookie.com/embed/${id}`);
}Angular 20.3, strict template check passes. A Karma test renders the same hostile HTML through the default sanitizer and through the bypass, and checks that `onmouseover` and the script tag are removed in the first and kept in the second. No hostile code was executed. The embed helper was checked with valid and invalid ids.
Key points
- Angular sanitizes innerHTML, URL and style bindings. Bypass methods switch this off for one value.
- Bypassing with user-influenced data creates stored or reflected XSS.
- Safe patterns: avoid bypass, build from validated parts, sanitize with an allowlist library, isolate with sandbox, use CSP and Trusted Types.
- Keep bypass calls in one audited place.
Common mistakes
- Calling `bypassSecurityTrustHtml(userInput)` to "make the warning go away".
- Setting `nativeElement.innerHTML` directly.
- Accepting a full URL from users for an iframe and bypassing it.
Follow-ups
- What does Angular do to a `javascript:` link by default?
- How would you allow YouTube embeds safely?
- What does a Content Security Policy add?
Tips
- Show one attack string and what the sanitizer does to it.
- Give the "validate, build, then trust" rule.
- Mention CSP and Trusted Types as extra layers.
Evaluation rubric
- Explains default sanitization and the bypass methods.
- Describes the XSS risk precisely.
- Gives several practical safe patterns.