` in this case), the parsing is done once internally, and the result is directly expanded into the DOM. To get the result of sanitization as a string, you can use `.innerHTML` from the `setHTML()` results. `const $div = document.createElement('div') $div.setHTML(user_input) $div.innerHTML //
hello world![]()
` Customize with configuration The Sanitizer API is configured by default to remove strings that would trigger script execution. However, you can also add your own customizations to the sanitization process with a configuration object. `const config = { allowElements: [], blockElements: [], dropElements: [], allowAttributes: {}, dropAttributes: {}, allowCustomElements: true, allowComments: true }; // sanitized result is customized by configuration new Sanitizer(config)` The following options specify how the sanitization result should treat the specified element. `allowElements`: Names of elements that the sanitizer should retain. `blockElements`: Names of elements the sanitizer should remove, while retaining their children. `dropElements`: Names of elements the sanitizer should remove, along with their children. `const str = `hello
world` $div.setHTML(str) //
hello world
$div.setHTML(str, { sanitizer: new Sanitizer({allowElements: [ "b" ]}) }) //
hello world
$div.setHTML(str, { sanitizer: new Sanitizer({blockElements: [ "b" ]}) }) //
hello world
$div.setHTML(str, { sanitizer: new Sanitizer({allowElements: []}) }) //
hello world
` You can also control whether the sanitizer will allow or deny specified attributes with the following options: `allowAttributes` `dropAttributes` `allowAttributes` and `dropAttributes` properties expect **attribute match lists**âobjects whose keys are attribute names, and values are lists of target elements or the `*` wildcard. `const str = `
hello` $div.setHTML(str) //
hello
$div.setHTML(str, { sanitizer: new Sanitizer({allowAttributes: {"style": ["span"]}}) }) //
hello
$div.setHTML(str, { sanitizer: new Sanitizer({allowAttributes: {"style": ["p"]}}) }) //
hello
$div.setHTML(str, { sanitizer: new Sanitizer({allowAttributes: {"style": ["*"]}}) }) //
hello
$div.setHTML(str, { sanitizer: new Sanitizer({dropAttributes: {"id": ["span"]}}) }) //
hello
$div.setHTML(str, { sanitizer: new Sanitizer({allowAttributes: {}}) }) //
hello
` `allowCustomElements` is the option to allow or deny custom elements. If they're allowed, other configurations for elements and attributes still apply. `const str = `
hello` $div.setHTML(str) //
const sanitizer = new Sanitizer({ allowCustomElements: true, allowElements: ["div", "custom-elem"] }) $div.setHTML(str, { sanitizer }) //
hello
` API surface Comparison with DomPurify [DOMPurify](https://github.com/cure53/DOMPurify) is a well-known library that offers sanitization functionality. The main difference between the Sanitizer API and DOMPurify is that DOMPurify returns the result of the sanitization as a string, which you need to write into a DOM element with `.innerHTML`. `const user_input = `
hello world![]()
` const sanitized = DOMPurify.sanitize(user_input) $div.innerHTML = sanitized // `
hello world![]()
`` DOMPurify can serve as a fallback when the Sanitizer API is not implemented in the browser. The DOMPurify implementation has a couple of downsides. If a string is returned, then the input string is parsed twice, by DOMPurify and `.innerHTML`. This double parsing wastes processing time, but can also lead to interesting vulnerabilities caused by cases where the result of the second parsing is different from the first. HTML also needs **context** to be parsed. For example, `
` makes sense in ``, but not in ``. Since `DOMPurify.sanitize()` only takes a string as an argument, the parsing context had to be guessed. The [Sanitizer API](https://wicg.github.io/sanitizer-api/) improves upon the DOMPurify approach and is designed to eliminate the need for double parsing and to clarify the parsing context. API status and browser support The Sanitizer API is under discussion in the standardization process and Chrome is in the process of implementing it. Step Status 1. Create explainer [Complete](https://github.com/WICG/sanitizer-api/blob/main/README.md) 2. Create specification draft [Complete](https://wicg.github.io/sanitizer-api/) 3. Gather feedback and iterate on design [Complete](https://github.com/w3ctag/design-reviews/issues/619) 4. Chrome origin trial [Complete](https://groups.google.com/a/chromium.org/g/blink-dev/c/OrWQnXVQJ0A/m/TbF-0Dw3BQAJ) 5. Launch [Intent to Ship on M105](https://groups.google.com/a/chromium.org/g/blink-dev/c/KOpwkS-bgl0/m/hmJ6gfU7AQAJ) Mozilla: Considers this proposal [worth prototyping](https://mozilla.github.io/standards-positions/#sanitizer-api), and is [actively implementing it](https://groups.google.com/g/mozilla.dev.platform/c/C4EHeQlaMbU). WebKit: See the response on the [WebKit mailing list](https://lists.webkit.org/pipermail/webkit-dev/2021-March/031731.html). How to enable the Sanitizer API Browser Support
146
146
148
x
[Source](https://developer.mozilla.org/docs/Web/API/Sanitizer) Chrome is in the process of implementing the Sanitizer API. In Chrome 93 or later, you can try out the behavior by enabling `about://flags/#enable-experimental-web-platform-features` flag. In earlier versions of Chrome Canary and Dev channel, you can enable it with `--enable-blink-features=SanitizerAPI`. Check out the [instructions for how to run Chrome with flags](https://developer.chrome.com/docs/web-platform/chrome-flags). Firefox Firefox also implements the Sanitizer API as an experimental feature. To enable it, set the `dom.security.sanitizer.enabled` flag to `true` in `about:config`. Feature detection `if (window.Sanitizer) { // Sanitizer API is enabled }` Feedback If you try this API and have some feedback, we'd love to hear it. Share your thoughts on [Sanitizer API GitHub issues](https://github.com/WICG/sanitizer-api/issues) and discuss with the spec authors and folks interested in this API. If you find any bugs or unexpected behavior in Chrome's implementation, [file a bug to report it](https://new.crbug.com/). Select the `Blink>SecurityFeature>SanitizerAPI` components and share details to help implementers track the problem. Demo To see the Sanitizer API in action check out the [Sanitizer API Playground](https://sanitizer-api.dev/) by [Mike West](https://twitter.com/mikewest): References [HTML Sanitizer API specification](https://wicg.github.io/sanitizer-api/) [WICG/sanitizer-api repo](https://github.com/WICG/sanitizer-api) [Sanitizer API FAQ](https://github.com/WICG/sanitizer-api/blob/main/faq.md) [HTML Sanitizer API reference documentation on MDN](https://developer.mozilla.org/docs/Web/API/HTML_Sanitizer_API) Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2021-10-06 UTC.
|