const untrustedString = "abc <script>alert(1)<" + "/script> def";
const someElement = document.getElementById("target");
// someElement.innerHTML = untrustedString;
someElement.setHTML(untrustedString);
console.log(someElement.innerHTML); // abc defUsing the HTML Sanitizer API / notebook
Using the HTML Sanitizer API
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const sanitizerOne = new Sanitizer({ elements: ["p", "a"] });
sanitizerOne.allowAttribute("href");
someElement.setHTML(untrustedString, { sanitizer: sanitizerOne });const untrustedString = '<button onclick="alert(1)">Button text</button>';
const someElement = document.getElementById("target");
const sanitizerOne = new Sanitizer(); // Default sanitizer
sanitizerOne.allowElement({ name: "button", attributes: ["onclick"] });
someElement.setHTMLUnsafe(untrustedString, { sanitizer: sanitizerOne });const sanitizer = new Sanitizer({
elements: ["p", "div"],
attributes: ["cite", "onclick"],
replaceWithChildrenElements: ["b"],
});const sanitizer = new Sanitizer({
elements: ["div", "span"],
});const sanitizer = new Sanitizer({
elements: [
{
name: "div",
namespace: "http://www.w3.org/1999/xhtml",
},
{
name: "span",
namespace: "http://www.w3.org/1999/xhtml",
},
],
});const sanitizer = new Sanitizer({});
sanitizer.allowElement("div");
sanitizer.allowElement({
name: "span",
namespace: "http://www.w3.org/1999/xhtml",
});const sanitizer = new Sanitizer({
attributes: ["cite", "onclick"],
});const sanitizer = new Sanitizer({
attributes: [
{
name: "cite",
namespace: null,
},
{
name: "onclick",
namespace: null,
},
],
});const sanitizer = new Sanitizer({});
sanitizer.allowAttribute("cite");
sanitizer.allowAttribute("onclick");const sanitizer = new Sanitizer({
elements: [
"div",
{ name: "a", attributes: ["href", "rel", "hreflang", "type"] },
"span",
],
});const sanitizer = new Sanitizer({
elements: ["div", { name: "a", removeAttributes: ["type"] }],
});const replaceBoldSanitizer = new Sanitizer({
replaceWithChildrenElements: ["b"],
});
targetElement.setHTML("This <b>highlighting</b> isn't needed", {
sanitizer: replaceBoldSanitizer,
});
// Log the result
console.log(targetElement.innerHTML); // This highlighting isn't neededconst sanitizer = new Sanitizer({});
sanitizer.replaceElementWithChildren("b");
sanitizer.replaceElementWithChildren({
name: "i",
namespace: "http://www.w3.org/1999/xhtml",
});const sanitizer = new Sanitizer({
removeElements: ["script", "div", "span"],
removeAttributes: ["onclick"],
});const sanitizer = new Sanitizer();
sanitizer.removeElement("div");const sanitizer = new Sanitizer({
removeElements: ["div", "span"],
});const sanitizer = new Sanitizer({});
sanitizer.removeElement("div");
sanitizer.removeElement({
name: "span",
namespace: "http://www.w3.org/1999/xhtml",
});const sanitizer = new Sanitizer({
removeAttributes: ["onclick", "lang"],
});const sanitizer = new Sanitizer({});
sanitizer.removeAttribute("onclick");
sanitizer.removeAttribute("lang");const sanitizer = new Sanitizer({
comments: true,
dataAttributes: true,
});const sanitizer = new Sanitizer({});
sanitizer.setComments(true);
sanitizer.setDataAttributes(true);<div class="button-group">
<button id="defaultSanitizerBtn">Default Sanitizer</button>
<button id="emptySanitizerBtn">Empty Sanitizer</button>
</div><div class="select-group">
<label for="allowElementSelect">allowElement:</label>
<select id="allowElementSelect">
<option value="">--Choose element--</option>
<option value="h1">h1</option>
<option value="div">div</option>
<option value="span">span</option>
<option value="script">script</option>
<option value="p">p</option>
<option value="button">button</option>
<option value="img">img</option>
</select>
<label for="removeElementSelect">removeElement:</label>
<select id="removeElementSelect">
<option value="">--Choose element--</option>
<option value="h1">h1</option>
<option value="div">div</option>
<option value="span">span</option>
<option value="script">script</option>
<option value="p">p</option>
<option value="button">button</option>
<option value="img">img</option>
</select>
</div>
<div class="select-group">
<label for="allowAttributeSelect">allowAttribute:</label>
<select id="allowAttributeSelect">
<option value="">--Choose attribute--</option>
<option value="class">class</option>
<option value="autocapitalize">autocapitalize</option>
<option value="hidden">hidden</option>
<option value="lang">lang</option>
<option value="title">title</option>
<option value="onclick">onclick</option>
</select>
<label for="removeAttributeSelect">removeAttribute:</label>
<select id="removeAttributeSelect">
<option value="">--Choose attribute--</option>
<option value="class">class</option>
<option value="autocapitalize">autocapitalize</option>
<option value="hidden">hidden</option>
<option value="lang">lang</option>
<option value="title">title</option>
<option value="onclick">onclick</option>
</select>
</div><div class="button-group">
<button id="toggleCommentsBtn">Toggle comments</button>
<button id="toggleDataAttributesBtn">Toggle data-attributes</button>
</div><div>
<p>Original string (Editable)</p>
<pre contenteditable id="unmodified"></pre>
<p>setHTML() (HTML as string)</p>
<pre id="setHTML"></pre>
<p>setHTMLUnsafe() (HTML as string)</p>
<pre id="setHTMLUnsafe"></pre>
</div>