1001Ferramentas
🛡️ Security

Trusted Types Policy Builder

Generate CSP require-trusted-types-for and trusted-types directives with policy names — a modern anti-XSS mitigation.

Trusted Types: closing innerHTML for good

Most DOM-based XSS comes in through a handful of known sinks: innerHTML, outerHTML, document.write, a script src, eval. Trusted Types is the browser mechanism that closes all of them at once — with the policy on, assigning a plain string to any of those sinks throws. Only an object created by a policy you declared gets through.

List the policy names your application creates and the page assembles the header: the trusted-types directive with the allowed names, plus require-trusted-types-for with the value script, which is what actually turns the enforcement on. There is also the option to allow duplicate names, useful when the same code loads twice, and to emit in report-only mode with a report-to destination.

The adoption path that usually works is report mode first. You ship the Report-Only header, let it run for a few days and collect the sinks that would have been blocked — almost always a third-party library nobody remembered writes HTML directly. Only after cleaning those up is it worth switching to the enforcing header.

Frequently asked questions

Does it work in every browser?
In Chromium-based ones, yes. Firefox and Safari do not enforce it yet, and there the header is ignored without error. Which means Trusted Types is an extra layer, not the only defence — sanitising input and escaping output remain mandatory.
Do I need a policy named default?
No, and using default deserves caution: it catches every assignment that did not name a policy explicitly, which makes legacy code work unchanged — and also drains much of the protection. Treat it as a migration step, not a destination.
Does this replace a sanitiser library?
No. Trusted Types guarantees the HTML went through a function of yours; what that function does is your problem. The common pattern is for the policy to call DOMPurify inside createHTML — the two together, one making sure nobody escaped the path and the other cleaning the content.

Related Tools