Safeguard
Security Guides•

When is Angular's bypassSecurityTrust safe to use?

bypassSecurityTrustHtml and its siblings switch off Angular's sanitizer. Learn when that is justified, what to check first, and how to audit existing calls.

Safeguard Security Team
4 min read

Only when you control the value completely, or have already sanitized it, and you choose the matching security context. The bypassSecurityTrust methods tell Angular to stop sanitizing a value. If an attacker can influence that value, you have created an XSS hole yourself.

What the methods do

Angular treats all values as untrusted by default. When a value enters the DOM through a template binding or interpolation, Angular sanitizes and escapes it. Interpolated content is always escaped, so markup shows as text. Binding to innerHTML makes Angular interpret HTML, but it still sanitizes: the Angular guide shows a script element being removed while a harmless bold tag stays.

To opt out, you inject DomSanitizer and call one of five methods: bypassSecurityTrustHtml, bypassSecurityTrustScript, bypassSecurityTrustStyle, bypassSecurityTrustUrl or bypassSecurityTrustResourceUrl. The guide warns that if you trust a value that might be malicious, you introduce a vulnerability, and that if in doubt you should find a professional reviewer.

Match the context

Safety depends on context. Angular defines four security contexts.

ContextWhere it appliesSanitized by Angular?
HTMLBinding to innerHTMLYes
StyleBinding CSS to the style propertyNot listed: the guide says HTML and URL values are sanitized
URLProperties such as a link hrefYes
Resource URLA URL loaded and run as code, such as a script sourceNo, because it contains arbitrary code

Resource URLs cannot be sanitized, which is why a bypass for them deserves the most scrutiny. Trusting a URL value is not the same as trusting an HTML value, so pick the method that fits where the value lands.

A decision checklist

  1. Where does the value come from? Constants and values built only from your own code are the safest case.
  2. Can any part of it come from a user, a query string, an API or a CMS? If yes, sanitize first with DomSanitizer.sanitize and the right SecurityContext.
  3. Is the bypass the narrowest one that works? Prefer the URL method over the HTML one when you only need a link.
  4. Is the call isolated in one small, reviewed service rather than scattered through components?
  5. Is there a comment explaining why the value is trusted, so the next reviewer can challenge it?

Auditing an existing app

Search for each of the five method names and for direct DOM access through ElementRef and document, because the guide notes that built-in browser DOM APIs do not protect you unless you enforce Trusted Types. Then consider enforcing Trusted Types: apps that use any bypass method must enable the angular#unsafe-bypass policy, which makes bypass use visible in your headers and easier to audit. See the Angular Trusted Types policy names explained.

Try it yourself

Click through it at your own pace, or open it full screen. Look for how findings group by file, which helps when bypass calls are spread across components.

Safeguard scans a project's dependencies with reachability analysis, and offers a free scan at gold.safeguard.sh/scan.

Quick check

Does bypassSecurityTrustHtml sanitize the value?

No. It marks the value as trusted so Angular leaves it alone. Sanitize beforehand if the source is not fully under your control.

Is it fine to bypass for a URL I built from user input?

Not without validating the scheme and destination. A trusted URL that came from an attacker is still an attack.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.