Chrome refuses an innerHTML string under a Trusted Types policy
Fix it fast
Most common cause, 1 of 3Your own code writes an HTML string into a sink
Most of these writes only set text or empty an element, and need no policy: use textContent, createElement or trustedTypes.emptyHTML.
// text and clearing need no policy
el.textContent = message;
list.innerHTML = trustedTypes.emptyHTML;
// real markup goes through one sanitizing policy
const htmlPolicy = trustedTypes.createPolicy('app-html', {
createHTML: (input) => DOMPurify.sanitize(input),
});
el.innerHTML = htmlPolicy.createHTML(markupFromServer);
# response header
Content-Security-Policy: require-trusted-types-for 'script'; trusted-types app-htmlShort answer
This document requires 'TrustedHTML' assignment is the TypeError Chrome throws when the page's Content Security Policy has require-trusted-types-for 'script' and code hands a plain string to innerHTML, insertAdjacentHTML or document.write. Write text with textContent, build markup through a named Trusted Types policy, and cover libraries you cannot edit with a default policy. Applies to current Chromium browsers.
Which cause do you have?
Confirm that the document enforces Trusted Types, then read the error's stack in DevTools. A top frame in your own file points to cause 1, frames inside a library to cause 2, and a message that goes on about the 'default' policy to cause 3. A header sent as Content-Security-Policy-Report-Only only reports; it does not throw.
curl -sI https://example.com/ | grep -i "content-security-policy"
# in the DevTools console of the failing page:
document.querySelector('meta[http-equiv="Content-Security-Policy"]')?.contentThen find the line you see in this list and open its fix:
- 1Your own code writes an HTML string into a sink
The short message, with the top stack frame on one of your lines that sets innerHTML, outerHTML, insertAdjacentHTML or document.write - 2A library you cannot change writes the string
The same short message, but the top frames sit inside jQuery, AngularJS or another vendor file - 3A default policy exists but gives back no HTML
The message continues: and the 'default' policy failed to execute. or: and no 'default' policy for 'TrustedHTML' has been defined.
What does the error look like?
This document requires 'TrustedHTML' assignment.This document requires 'TrustedHTML' assignment and the 'default' policy failed to execute.This document requires 'TrustedHTML' assignment and no 'default' policy for 'TrustedHTML' has been defined.
Cause 1 of 3
Your own code writes an HTML string into a sink
You seeThe short message, with the top stack frame on one of your lines that sets innerHTML, outerHTML, insertAdjacentHTML or document.write
Check
This document requires 'TrustedHTML' assignment appears when require-trusted-types-for 'script' is enforced and a plain string reaches an HTML sink. Under that rule Chrome accepts only TrustedHTML objects for innerHTML, outerHTML, insertAdjacentHTML, iframe srcdoc, document.write and DOMParser.parseFromString, and throws a TypeError for anything else.
grep -rnE "innerHTML|outerHTML|insertAdjacentHTML|document\.write|parseFromString" src/Fix
Most of these writes only set text or empty an element, and need no policy: use textContent, createElement or trustedTypes.emptyHTML. For real markup, create one named policy that sanitizes and pass its output to the sink. When the CSP also has a trusted-types directive, list the policy name there.
// text and clearing need no policy
el.textContent = message;
list.innerHTML = trustedTypes.emptyHTML;
// real markup goes through one sanitizing policy
const htmlPolicy = trustedTypes.createPolicy('app-html', {
createHTML: (input) => DOMPurify.sanitize(input),
});
el.innerHTML = htmlPolicy.createHTML(markupFromServer);
# response header
Content-Security-Policy: require-trusted-types-for 'script'; trusted-types app-htmlVerify
After a reload, DevTools shows no TypeError and the element renders the cleaned markup, while the response header lists your policy name.
curl -sI https://example.com/ | grep -i "trusted-types app-html"Confirmed by web.dev
Cause 2 of 3
A library you cannot change writes the string
You seeThe same short message, but the top frames sit inside jQuery, AngularJS or another vendor file
Check
Here the TrustedHTML assignment error starts in third-party code, so there is no line of yours to rewrite. Trusted Types has a hook for exactly this case: when a policy named default exists and enforcement is on, every plain string sent to a sink passes through that policy first.
grep -rn "createPolicy" src/Fix
Register a default policy that sanitizes, and load it before the library runs. The web.dev guide asks you to use it sparingly and to prefer named policies in code you control; it exists for code you cannot refactor. If the CSP lists policy names, add default to the list.
// trusted-types-default.js, loaded before the library
if (window.trustedTypes && trustedTypes.createPolicy) {
trustedTypes.createPolicy('default', {
createHTML: (input) => DOMPurify.sanitize(input),
});
}
# response header
Content-Security-Policy: require-trusted-types-for 'script'; trusted-types app-html defaultVerify
The widget the library draws renders again, and the console shows no Trusted Types error.
curl -sI https://example.com/ | grep -i "trusted-types"Confirmed by developer.mozilla.org
Cause 3 of 3
A default policy exists but gives back no HTML
You seeThe message continues: and the 'default' policy failed to execute. or: and no 'default' policy for 'TrustedHTML' has been defined.
Check
These longer forms of the TrustedHTML assignment error mean a default policy is registered but could not turn the string into HTML. In Chromium's source, the no-policy-defined wording is used when the default policy has no createHTML function, and the failed-to-execute wording when createHTML returned nothing. MDN states that a default policy returning null or undefined makes the browser throw a TypeError.
grep -rn -A4 "createPolicy('default'" src/Fix
Give the default policy a createHTML function and make it return a string on every path. Return null only for input you deliberately want blocked, because that is what makes the write throw.
trustedTypes.createPolicy('default', {
createHTML: (input) => DOMPurify.sanitize(input), // always a string
});Verify
Run a test write in the console of the page. It no longer throws, and any error left elsewhere is the short form from a sink you have not covered yet.
document.createElement('div').innerHTML = '<b>test</b>'; // no TypeError once the default policy returns a stringConfirmed by chromium.googlesource.com
How do I stop it coming back?
- Ship require-trusted-types-for 'script' under Content-Security-Policy-Report-Only first, and read the reports until no new sink shows up before you enforce it.
- List every allowed policy name in a trusted-types directive, so code you did not expect cannot create a policy of its own.
- On webpack 5.37 or later, set output.trustedTypes (and allow its policy name), so lazily loaded chunks get their script URLs from a policy; without it the next failure reads 'TrustedScriptURL' instead.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.