Skip to content

Lesson 6 of 14 · 13m

Trusted Types

require-trusted-types-for 'script' makes every DOM sink reject plain strings; policies mint TrustedHTML through DOMPurify, and the 'default' policy quietly undoes enforcement.

Enforcement at the sink

Every defense so far has the same failure mode: a developer writes el.innerHTML = something and nothing stops them. Sanitizers only run where someone remembered to call them. Trusted Types fixes the plumbing rather than the habit — it makes the DOM itself refuse plain strings.

One CSP directive turns it on:

Content-Security-Policy: require-trusted-types-for 'script'

After that header, every injection sink on the page — innerHTML, outerHTML, insertAdjacentHTML, document.write, <iframe srcdoc>, script.src, script.text, eval, new Function, setTimeout("...") — rejects a plain string with a TypeError and fires a securitypolicyviolation report. The sinks now only accept branded objects: TrustedHTML, TrustedScriptURL, or TrustedScript. The only way to mint one is through a policy:

const sanitize = trustedTypes.createPolicy('sanitize', {
  createHTML: (input) => DOMPurify.sanitize(input, {RETURN_TRUSTED_TYPE: true}),
});

el.innerHTML = sanitize.createHTML(userInput); // TrustedHTML — accepted
el.innerHTML = userInput;                      // TypeError — rejected

The enforcement property is the whole point. The browser checks the object brand at the sink, so a string that skipped your sanitizer cannot reach the DOM regardless of which code path carried it. The audit question inverts: instead of "did every developer sanitize?", it is "which policies are allowed to produce trusted objects?"

A second directive names the allowed policies:

Content-Security-Policy: require-trusted-types-for 'script'; trusted-types sanitize bundler-runtime

Any createPolicy call with a name outside the list fails. That matters because a policy is a trust grant — a third-party widget that registers createPolicy('x', {createHTML: s => s}) is a sanitizer bypass with an official name.

Enforcement extends past window: trustedTypes exists on WorkerGlobalScope, so worker code gets the same sink rules, and every rejection posts a securitypolicyviolation report carrying violatedDirective, blockedURI, and the sink name — which is also your migration inventory when you run it report-only.

What it is not

Trusted Types is enforcement, not sanitization. The TypeError tells you a plain string hit a sink; it says nothing about whether your policy's transform is safe. A policy that returns its input unchanged converts the protection into ceremony. DOMPurify inside the policy is the actual work; Trusted Types guarantees the DOMPurify call cannot be skipped.

There is also a deliberate trap: createPolicy('default', ...) registers a policy that runs transparently on plain strings — the sinks accept strings again and quietly pass them through default's transform. It exists to keep legacy code running during migration. Left in production, it is the unsafe-inline of Trusted Types: the header stays, the enforcement is gone, and you get a report-free blind spot. Audit trusted-types policy lists and treat 'default' as technical debt, not a control.

Coverage and deployment

require-trusted-types-for 'script' covers DOM sinks; adding 'trusted-types-eval' permits eval-class sinks through a policy — allow it only when a dependency genuinely needs it, because it reopens the eval hole under a branded wrapper.

Browser support finally completed: Chrome and Edge have enforced it since version 83 (May 2020), Safari added it in Safari 26 (September 2025), and Firefox shipped it in 148 (February 2026) after sitting behind dom.security.trusted_types.enabled through the 145 betas. Where it is absent, the directive is a no-op and everything degrades to ordinary behavior — so guard policy creation with if (window.trustedTypes) and treat Trusted Types as a hardening layer, not the only layer.

What to actually do

Ship it in Content-Security-Policy-Report-Only first — require-trusted-types-for 'script' in report-only logs violations without breaking the page. Each violation is a sink that needs the DOMPurify policy. Fix them, allowlist your policy names with trusted-types, then enforce. The end state is a page where unsanitized strings cannot reach the DOM because the DOM refuses them — which is the only version of "we sanitize everything" that stays true next quarter.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.