Security & AccessibilityExperimental

DOM XSS Prevention with Trusted Types

Mental Model

Trusted Types eliminates DOM-based Cross-Site Scripting by locking ~60 hazardous sinks (innerHTML, document.write, script.src, eval) at the browser engine boundary. When enforced via CSP, raw strings throw a TypeError; only TrustedHTML, TrustedScript, or TrustedScriptURL objects minted by named policies succeed.

Interactive Simulator

DOM Injection Sinks: Raw String vs TrustedHTML Policy

Lines of Code
Before
2
After
4
Reflows / Browser Cost
Before
XSS Sink Open
After
Type-Locked Sink
BeforeUnchecked innerHTML Sink
const bio = new URLSearchParams(location.search).get('bio');
profileEl.innerHTML = bio; // Executes <img onerror=...>!
Lines: 2Reflows: XSS Sink OpenDependencies: 0 kB

Any attacker-controlled string flows straight into the HTML parser.

After (Native)Trusted Types + DOMPurify Policy
const policy = trustedTypes.createPolicy('app-html', {
  createHTML: (dirty) => DOMPurify.sanitize(dirty),
});
profileEl.innerHTML = policy.createHTML(bio);
Lines: 4Reflows: Type-Locked SinkDependencies: DOMPurify (~7 kB)

Raw strings throw TypeError; only TrustedHTML from app-html compiles.

Under the Hood

Trusted Types acts like a factory safety interlock on a hydraulic press: the machine refuses to cycle unless the material carries a stamped inspection tag (TrustedHTML) from an approved quality gate (trusted-types CSP allowlist).

  1. Sink Lockdown: Adding require-trusted-types-for 'script' to Content Security Policy instructs the DOM bindings to reject plain string arguments at every injection sink.
  2. Named Policies: trustedTypes.createPolicy('app-html', { createHTML }) centralizes sanitization (such as DOMPurify) into one auditable call site.
  3. Parser Isolation: Even if an attacker controls URL parameters or postMessage data, the Browser Rendering Pipeline never parses raw markup unless it passes through a policy.
JS
// Requires CSP: require-trusted-types-for 'script'; trusted-types app-sanitizer;
const sanitizer = trustedTypes.createPolicy('app-sanitizer', {
  createHTML: (input) => DOMPurify.sanitize(input),
});

container.innerHTML = sanitizer.createHTML(userBio);
// container.innerHTML = userBio; -> throws TypeError!

Problem Genealogy

  • 2005–2018 (Manual Sanitization): Teams sprinkled sanitize() calls ad hoc; one forgotten innerHTML in 500 components reopened DOM XSS.
  • 2019 (Google Trusted Types): Shifted DOM XSS defense from "find every sink" to "type-lock all sinks in the engine."
  • 2024+ (W3C Standardization): Expanded beyond Chromium into cross-engine interop baselines.

Production Pitfalls

Production Pitfall
  • Registering a pass-through default policy: Writing createHTML: (s) => s silences errors while keeping every XSS vulnerability wide open.
  • Enabling enforcement without Report-Only rollout: Turning on require-trusted-types-for 'script' immediately breaks third-party scripts that assign strings to innerHTML; audit with Content-Security-Policy-Report-Only first.
When Not to Use
  • When not to use innerHTML at all: For plain text or structured UI updates, assign element.textContent or render framework JSX nodes, they never invoke the HTML parser and need zero policies.

Connections & Logical Next Steps

Security & AccessibilityDOM XSS Prevention with Trusted Types

Seen in Architecture Autopsies

Mechanical Comparisons