Skip to the page

XSS and injection in the browser · senior

Trusted Types: locking down DOM XSS sinks

With the CSP directive require-trusted-types-for 'script', dangerous DOM sinks like innerHTML and script.src stop accepting plain strings and only accept typed values created by named policies, so every path to a sink must go through code you review.

The notes

What you must know

  • Turn it on with Content-Security-Policy: require-trusted-types-for 'script'; trusted-types myPolicy dompurify (the second directive lists allowed policy names).
  • Locked sinks: innerHTML/outerHTML/insertAdjacentHTML, iframe srcdoc, document.write, DOMParser.parseFromString, script.src and script text, eval, new Function, string setTimeout/setInterval.
  • A policy: trustedTypes.createPolicy('myPolicy', { createHTML: (s) => DOMPurify.sanitize(s) }). DOMPurify can return TrustedHTML directly with RETURN_TRUSTED_TYPE: true.
  • A policy named 'default' is applied to any plain string at a sink; use it only as a temporary bridge for third-party code.
  • Support: Chrome/Edge 83, Safari 26, Firefox 148, so it is Baseline since early 2026. React 19.3 supports it. Start with report-only.
  • Value: XSS reviews shrink from 'every innerHTML in the codebase' to 'the few policy functions'.

The red pen

Common traps: mistake → fix

  • Mistake: "Trusted Types sanitises HTML for you"

    Fix: it only forces values through your policy; the policy must sanitise or escape.

  • Mistake: "A default policy that returns its input unchanged is fine"

    Fix: that turns the protection off.

  • Mistake: "Trusted Types is Chrome-only"

    Fix: since Safari 26 and Firefox 148 it works in all major browsers.

Easy to hard

Questions interviewers ask, easy to hard

  1. What problem do Trusted Types solve that CSP script-src does not?
  2. How do you create and use a policy?
  3. How would you adopt Trusted Types in a large React codebase with third-party widgets?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview