Wat is Trusted Types Policy Generator?
Trusted Types is a browser security API that prevents DOM-based XSS by requiring typed values instead of strings for dangerous DOM sinks like innerHTML, eval(), and script.src. Developers use trustedTypes.createPolicy() to define sanitization rules that transform or reject untrusted strings before they reach injection sinks. Combined with a Content-Security-Policy directive, Trusted Types closes a major class of XSS vulnerabilities by enforcing that only explicitly sanctioned operations can assign values to high-risk DOM properties.
Snel antwoord
Trusted Types prevents DOM-based XSS by blocking strings from reaching dangerous DOM sinks like innerHTML and eval(). Use trustedTypes.createPolicy() with createHTML/createScript/createScriptURL operations, then enforce with Content-Security-Policy: require-trusted-types-for 'script'. Always test in report-only mode first using Content-Security-Policy-Report-Only to avoid breaking existing functionality.
Beperkingen
- Trusted Types requires browser support in Chrome 83+, Edge 83+, and Safari 17+. Firefox does not support Trusted Types as of 2026. Applications must handle Firefox gracefully without Trusted Types protection by providing fallback behavior for unsupported browsers.
- Trusted Types does not cover all XSS vectors - it protects DOM injection sinks (innerHTML, outerHTML, insertAdjacentHTML, eval, document.write) but does not prevent XSS through URL attributes (href, src), event handler attributes, or CSS-based injection vectors.
- Migrating large applications to Trusted Types requires significant refactoring. Every innerHTML, insertAdjacentHTML, outerHTML, and document.write usage must be wrapped in a policy, which can involve hundreds of source changes in a legacy codebase with extensive dynamic DOM manipulation.
Zo gebruik je deze tool
- Configure the allowed operations - createHTML, createScript, createScriptURL - and decide how each operation handles input: strict rejection (block all untrusted input), sanitization (pass through a sanitizer function), or passthrough for trusted sources.
- Choose the CSP mode - enforce (require-trusted-types-for 'script' in Content-Security-Policy) blocks violations, or report-only (same directive in Content-Security-Policy-Report-Only) logs violations without blocking. Always start with report-only mode during migration.
- Select your server platform - Nginx or Apache - to generate the corresponding CSP header configuration that integrates with your existing server blocks or virtual hosts.
- Deploy the policy JavaScript file and server config, then test your application in report-only mode. Review console violation reports and adjust policies until no violations appear, then switch to enforce mode.
Waarvoor je het kunt gebruiken
- Protect a content-heavy web application from stored and reflected XSS by requiring all HTML injection points to go through a Trusted Types policy with a configured sanitizer function.
- Migrate a legacy application to Trusted Types by deploying in report-only mode first, fixing violation reports incrementally across the codebase, then switching to full enforce mode.
- Enforce a strict Trusted Types policy in third-party script-heavy environments where external widgets and embeds load dynamic content via innerHTML, ensuring all injected HTML passes through the policy.