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 — rejectedThe 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-runtimeAny 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?
Keep reading
Related articles from the SudoSecurity blog.
They Got Root on a Fully Patched Ivanti VPN and the Integrity Checker Said Everything Was Fine
Volexity caught an attacker chaining zero-days into unauthenticated root on an Ivanti Connect Secure appliance running the latest build. The payload never touched disk, commands rode in over the same HTTPS requests as the exploit, and Ivanti's Integrity Checker reported no modifications.
Gitea Shipped 21 CVEs in One Release and Eleven of Them Are the Same Bug
Gitea 28.3.0 landed November 1 with twenty-one CVE fixes. Read the list and a pattern jumps out: more than half are the same failure repeated across different route handlers, where a token scoped to public data gets handed private data anyway. That's not twenty-one mistakes.
php2shell: The PHP Bug That Fires Before Your Code Runs
Two critical PHP flaws landed October 21 and were in CISA's exploited-vulnerabilities catalog five days later. The 9.2 score isn't the part that should bother you. It's where the bug lives — it goes off while PHP is still assembling $_POST, before a single line of your application has run.
A Certificate Authority's Private Keys Were the Numbers 4, 56 and 66
Eight eMudhra intermediate CA keys turned out to be recoverable from their own public certificates. Not stolen — computable, because the key generation produced almost no entropy. A researcher pulled all eight in under a second and signed a message with each one to prove it.