Trusted Types kills DOM XSS by making it a CSP violation to assign a plain string to a dangerous sink like .innerHTML, document.write, eval, or <script src>. Strings must come from a named policy you create with trustedTypes.createPolicy(name, rules). Configure the header and the policy code below.
Content-Security-Policy-Report-Only while you find every violating sink, then flip to enforcing.'script' is defined in the spec. The directive shape allows future values like 'plugin'.trustedTypes.createPolicy(name, …) may register. Use 'none' to forbid all policies, or 'allow-duplicates' to allow the same policy name to be created more than once.'default' is a special policy name — see "Default fallback" below.report-to to the CSP and emits a paired Report-To header so violations are uploaded to your endpoint.
Support: Shipped in Chrome 83 / Edge 83 (May 2020). Firefox 134 ships behind dom.security.trusted_types.enabled. Safari does not yet implement Trusted Types — feature-detect with 'trustedTypes' in window and supply a tt polyfill (@github/template-tags, trusted-types/types) for the gap. checking…
| Type | Sinks (assigning a plain string throws) | Required wrapper |
|---|---|---|
| TrustedHTML | el.innerHTML, el.outerHTML, document.write, document.writeln, iframe.srcdoc, DOMParser.parseFromString, Range.createContextualFragment, el.insertAdjacentHTML | trustedTypes.createPolicy(name, { createHTML }) |
| TrustedScript | el.text on <script>, el.innerText/textContent on <script>, inline event handlers, eval(), Function(), setTimeout(string, …), setInterval(string, …) | createScript |
| TrustedScriptURL | el.src on <script>, importScripts(), SharedWorker, Worker, ServiceWorker.register() | createScriptURL |
innerHTML, document.write, eval, dynamic script.src assignments. Note every library that touches them (jQuery's $.html(), Lit's unsafeHTML, etc.).report-to endpoint and collect violations from real users for a week. Don't ship the default policy yet — let everything fail loudly so you see it.app-html, jquery, lit, …). Sanitize inside createHTML with DOMPurify or equivalent.default policy that logs + sanitizes anything still slipping through. It buys safety while you finish migrating.-Report-Only. The default policy stays as a belt-and-braces layer.