Trusted Types are a browser rule for dangerous sinks such as assigning HTML, inserting HTML, or setting a script URL from a string. With the policy enforced, those sinks reject a plain string. They accept a value your policy already marked as trusted. The point is to shrink the places where a cross-site scripting bug can turn text into markup. It is the layer you add after a nonce-based Content Security Policy is already in place, not a replacement for that policy and not a sanitizer by itself.
Browser support is broad in current Chromium-based browsers and incomplete elsewhere. Check a current compatibility table before you enforce the header on an audience you have not measured. One enforcement header in Chrome and a silent ignore in another browser is a partial control. Keep the rest of your XSS defenses, described in XSS prevention for web forms, whether or not Trusted Types are on.
What the policy is allowed to do
You publish a Content-Security-Policy directive that names Trusted Types and, when you are ready, requires them for script sinks. Your page also installs a policy object with a name. Application code asks that policy to bless a string before the string goes into a sink. The policy’s functions are the only place that decision should live. If those functions call a real sanitizer and return its result, you have a gate. If they return the input unchanged, you have a label that says “trusted” on attacker-controlled HTML.
That pass-through default policy is the pitfall. Teams add it so the console goes quiet. The sinks then accept the same strings they accepted before, and the header looks deployed. Treat a default policy that returns its argument as a bug. A default policy should not exist in production unless you can explain the single sink it still has to allow, and even then it should sanitize.
Reporting before enforcement
Ship the requirement in report-only mode first, the same way you should have rolled out the rest of CSP. Violations tell you which packages still assign HTML from strings. Fix those call sites, or route them through a policy that sanitizes, until the report stream is boring. Then enforce. Enforcing on day one will break a rich-text editor, a chart library, or a legacy widget, and the emergency rollback will be the pass-through policy you were trying to avoid.
Reports are not a cleaning service. A violation means a string reached a sink. It does not mean the string was an attack. Triage them the way you triage other CSP reports: group by page and by sink, find the code, and decide. Do not add a broader policy because the report was noisy.
HTML is still HTML
Trusted Types do not make an arbitrary HTML string safe to insert. They make it harder to insert a string you forgot to sanitize. If the policy’s createHTML function uses a sanitizer that allows script or event handlers, the trusted value is still an XSS bug, and it will pass the Trusted Types check because your policy said it was fine. Review the sanitizer’s allowlist. Prefer a library you keep updated over a regular expression you wrote once.
Script URLs and script text need the same skepticism. Blessing every URL that starts with https still allows an attacker-controlled host. Bless a list of hosts, or avoid creating scripts from strings at all and rely on the nonce-based script policy you already have. The CSP builder can draft a starting policy for a static site, WordPress, or Shopify. It does not know your Trusted Types functions. Those live in application code. The CSP checker can show whether the live header mentions the directive. It cannot show whether the policy function returns its input unchanged.
Where this sits next to other browser controls
Nonces and hashes decide which scripts may run. Trusted Types decide which values may enter a sink that builds script or HTML. Subresource Integrity, covered alongside CSP in the third-party script work on this blog, decides whether a file’s bytes match what you expected. You want all three on a site that loads other people’s JavaScript. Skipping Trusted Types because the nonce policy was hard is understandable. Calling the nonce policy a complete DOM XSS program is not accurate.
Frame protection, HSTS, and the other headers in security headers explained stay in force. Trusted Types do not set them. A page that is framed or loaded over plain HTTP has problems this directive will not see.
A rollout that survives contact with the codebase
Inventory the sinks in your own code first: innerHTML assignments, document write, script src built from data, and HTML parsers in widgets. Wrap the ones that must remain. Delete the ones that can become text assignments. Add the report-only directive. Fix what you own, then open tickets for libraries that still violate, with a date. Enforce when the remaining violations are ones you accept and have sanitized. Re-check the live header after each release. A build that drops the directive is a silent rollback.
If you cannot install a policy object because the site is a theme you do not script, do not pretend the header alone did the work. The header without a policy that the page actually uses will either break the theme or, if you added a pass-through to unbreak it, do nothing. Say which of those you shipped.
Name the policy and keep it boring
Use one policy name for the application and resist a second policy per plugin. Multiple policies become a way for late-loaded code to bless strings you already forbade. If a third-party widget cannot live inside your policy, load it in a frame with a tighter page of its own, or remove it. Do not weaken the main policy to keep a widget that assigns HTML from a configuration string.
After enforcement, add the directive to the release checklist next to the nonce. A refactor that moves script into a bundler can drop both. Re-check the live response header, not the file in the repo. The CSP checker is the right re-check for the header text. Pair it with a click through the rich-text field that used to violate, so you know the editor still saves without a pass-through you added under pressure.
Keep the report-only header for one release after enforcement if you can, so a new package that assigns HTML shows up as a report instead of a broken page on a customer browser. Read those reports the same week. A report-only header nobody watches is how the next widget ships a pass-through policy in a hurry.
Frequently asked questions
Do Trusted Types replace a sanitizer?
No. They require you to have a function that returns a trusted value. That function still has to sanitize. A function that returns the original string marks attacker HTML as trusted.
Will every browser enforce this?
No. Enforce where support exists, and keep encoding and a real CSP everywhere else. Check a current support table rather than a sentence in an old article.
Can a scanner prove the policy is safe?
A header check can see the directive. It cannot see a pass-through function in your JavaScript. Review the policy code. Do not close the ticket on the header alone.
