const name = "<img src='x' onerror='alert(1)'>";
shadowRoot.innerHTML = name; // shows the alertShadowRoot: innerHTML property / notebook
ShadowRoot: innerHTML property
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
<div class="host">
<template shadowrootmode="open">
<p>My name is Joe</p>
</template>
</div>const shadowHost = document.querySelector("#host");
const shadowRoot = shadowHost.shadowRoot;
const contents = shadowRoot.innerHTML;
console.log(contents); // "\n <p>My name is Joe</p>\n"if (typeof trustedTypes === "undefined")
trustedTypes = { createPolicy: (n, rules) => rules };const policy = trustedTypes.createPolicy("my-policy", {
createHTML: (input) => DOMPurify.sanitize(input),
});// The potentially malicious string
const untrustedString = "<p>I might be XSS</p><img src='x' onerror='alert(1)'>";
// Create a TrustedHTML instance using the policy
const trustedHTML = policy.createHTML(untrustedString);
// Get the shadow root
const shadowHost = document.querySelector("#host");
const shadowRoot = shadowHost.shadowRoot;
// Inject the TrustedHTML (which contains a trusted string)
shadowRoot.innerHTML = trustedHTML;