Safeguard
Developer Tools•

Trusted Types explained: a browser guardrail against DOM XSS

Trusted Types makes the browser reject plain strings at dangerous DOM sinks. See how policies work, how to enable it, and how to roll it out safely.

Safeguard Security Team
3 min read

Trusted Types is a browser feature that makes dangerous DOM functions, such as innerHTML, accept only values produced by a policy you wrote. A plain string causes a TypeError. That turns a quiet DOM XSS hole into a visible error you can find and fix. MDN lists it as Baseline 2026, newly available.

The problem it targets

DOM XSS happens when script takes attacker influenced data and hands it to a sink that parses it as HTML or runs it as code. Sinks are scattered across large codebases and third party libraries, so audits miss some.

Trusted Types moves the check into the browser. MDN describes the API as ensuring input passes through a transformation function, usually a sanitizer, before it reaches an API that could execute it.

How policies work

You define a policy with trustedTypes.createPolicy(). Each policy offers methods for the three kinds of sink and returns a typed object: TrustedHTML, TrustedScript or TrustedScriptURL.

const policy = trustedTypes.createPolicy("sanitize", {
  createHTML: (input) => DOMPurify.sanitize(input),
});

el.innerHTML = policy.createHTML(userInput);

The API supplies no sanitizer of its own. The rules are yours, which is also why policies must be small and reviewed. Few policies means a small amount of code to audit.

Turning enforcement on

Two Content Security Policy directives control it:

  • require-trusted-types-for 'script' makes the browser throw a TypeError when a string reaches an injection sink.
  • trusted-types lists the policy names that may be created. Creating a policy with an unlisted name throws an error.
Content-Security-Policy: require-trusted-types-for 'script'; trusted-types sanitize

OWASP lists Trusted Types as a control that can eliminate whole classes of DOM XSS, while still saying a CSP should not be your primary defence.

Rolling it out without breaking the site

MDN's page describes a default policy named default. It receives any string passed to a sink while enforcement is on, and its return value is used. If it returns null or undefined, a TypeError is thrown. MDN recommends using it only while migrating legacy code.

A practical order:

  1. Find sink usage with a code search and replace what you can with textContent or node creation.
  2. Create one sanitizing policy for the places that need markup.
  3. Use a temporary default policy that logs every hit so you can find the remaining callers.
  4. Enable enforcement, remove the default policy, and keep the policy list short.

Check report options in your browser documentation before enabling it for all users, since the MDN page does not cover report-only mode.

Older browsers

MDN notes the API may not work in older browsers. It also shows a tiny fallback that returns the policy options object, so sanitization still runs even though the sink receives a plain string.

Quick check

Does Trusted Types replace output encoding?

No. It guards DOM sinks in the browser. Server side output encoding and sanitization are still needed.

Does it write the sanitizer for me?

No. You supply the transformation, usually with a maintained sanitizer library.

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.