Enforce trusted HTML contracts with Trusted Types
Quick answer
Trusted Types adds typed values and policies for selected injection sinks. Where the API and enforcement directive are supported, the browser can reject ordinary strings at those sinks. A policy must actually constrain its output; the type alone is not a sanitizer.
How it works
Use require-trusted-types-for 'script' to enforce the relevant sinks and trusted-types to limit policy names. This lesson creates a small policy that maps known identifiers to fixed developer-authored markup and rejects every other identifier. It is deliberately not a general-purpose HTML sanitizer. Run each example in a fresh document with the API; existing CSP policies may further restrict it. Use textContent when markup is not needed.
Local examples
Run in a fresh browser document. Actual ByteMind editor support is unverified.
(async () => {const m=document.createElement("meta");m.httpEquiv="Content-Security-Policy";m.content="require-trusted-types-for 'script'; trusted-types bm-note";document.head.append(m);const d=document.createElement("div");let blocked=false;try{d.innerHTML="<p>Note</p>";}catch(e){blocked=e instanceof TypeError;}console.log(blocked,d.children.length);})()Expected console output:
true 0The ordinary string is rejected and the div remains empty.
Run in a fresh browser document. Actual ByteMind editor support is unverified.
(async () => {const m=document.createElement("meta");m.httpEquiv="Content-Security-Policy";m.content="require-trusted-types-for 'script'; trusted-types bm-note";document.head.append(m);const p=trustedTypes.createPolicy("bm-note",{createHTML(id){if(id!=="note")throw new TypeError("Unknown template");return "<p>Note</p>";}});const d=document.createElement("div");d.innerHTML=p.createHTML("note");let rejected=false;try{p.createHTML("unknown");}catch(e){rejected=e instanceof TypeError;}console.log(d.textContent,rejected);})()Expected console output:
Note trueA fixed template is rendered and an unknown identifier is rejected.
Common mistakes
Never create a policy whose HTML callback returns arbitrary untrusted input unchanged. Do not claim enforcement on a browser without the feature.
Practice
Add a second known template identifier with fixed markup, reject all others, and verify the resulting text and element type.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Enforce trusted HTML contracts with Trusted Types — Knowledge Check
Apply the concept and reason about local example output.
What does Trusted Types not supply automatically?
Why restrict policy names with the trusted-types directive?
What should a fixed-template policy do with an unknown identifier?
What is the exact console output in Enforce trusted HTML contracts with Trusted Types example 1?
(async () => {const m=document.createElement("meta");m.httpEquiv="Content-Security-Policy";m.content="require-trusted-types-for 'script'; trusted-types bm-note";document.head.append(m);const d=document.createElement("div");let blocked=false;try{d.innerHTML="<p>Note</p>";}catch(e){blocked=e instanceof TypeError;}console.log(blocked,d.children.length);})()What is the exact console output in Enforce trusted HTML contracts with Trusted Types example 2?
(async () => {const m=document.createElement("meta");m.httpEquiv="Content-Security-Policy";m.content="require-trusted-types-for 'script'; trusted-types bm-note";document.head.append(m);const p=trustedTypes.createPolicy("bm-note",{createHTML(id){if(id!=="note")throw new TypeError("Unknown template");return "<p>Note</p>";}});const d=document.createElement("div");d.innerHTML=p.createHTML("note");let rejected=false;try{p.createHTML("unknown");}catch(e){rejected=e instanceof TypeError;}console.log(d.textContent,rejected);})()