Skip to content
Putting technology to work.
Insights to guide decisions and action.

Search articles

DOMPurify 3.5.0: sanitize inside a hook restores the allowlist

Table of contents · 6 items

CMS article bodies, contact form input, and HTML returned by AI can be sanitized with DOMPurify before being displayed in the browser. Do you restrict the allowed tags with ALLOWED_TAGS, and also run HTML inside attribute values back through DOMPurify.sanitize() inside a hook?

In DOMPurify up to the 3.4.x series, writing code this way caused the nested call to replace the outer call's configuration. The rest of the document was processed with the default allowlist, and tags you thought you had excluded passed through without any error or warning. In 3.5.0 on November 22, the outer configuration is restored when the nested call finishes.

In this article, we read the DOMPurify README changes and the fix commit, and ran 3.4.18 and 3.5.0 in a test environment to see how far the allowlist expands.

Results from the test environment

We ran DOMPurify 3.4.18 and 3.5.0 in a test environment with Node.js v22.23.5 and jsdom 26.1.0. The outer configuration restricted tags to p and b, and attributes to data-note only. The input included the disallowed <img> and <a>.

const STRICT = { ALLOWED_TAGS: ['p', 'b'], ALLOWED_ATTR: ['data-note'] };
const input = '<p data-note="<i>memo</i>">本文</p><img src="x.png">'
  + '<a href="https://example.com/">リンク</a><b>太字</b>';

// data-note の値のHTMLを、hookの中で無害化する
DOMPurify.addHook('uponSanitizeAttribute', (node, data) => {
  if (data.attrName === 'data-note') data.attrValue = DOMPurify.sanitize(data.attrValue);
});
DOMPurify.sanitize(input, STRICT);
dompurify 3.4.18
  hookなし       : <p data-note="<i>memo</i>">本文</p>リンク<b>太字</b>
  hook内で入れ子 : <p data-note="<i>memo</i>">本文</p><img src="x.png"><a href="https://example.com/">リンク</a><b>太字</b>
  setConfig併用  : <p data-note="memo">本文</p>リンク<b>太字</b>
dompurify 3.5.0
  hookなし       : <p data-note="<i>memo</i>">本文</p>リンク<b>太字</b>
  hook内で入れ子 : <p data-note="<i>memo</i>">本文</p>リンク<b>太字</b>
  setConfig併用  : <p data-note="memo">本文</p>リンク<b>太字</b>

Diagram of the results of restricting allowed tags to p and b in DOMPurify and calling sanitize() inside a hook. In 3.4.18, the disallowed img and a elements after the hook remained. In 3.5.0, they did not. When the configuration was fixed with setConfig, they did not remain even in 3.4.18

In 3.4.18, after the nested sanitize() was called inside the hook, the disallowed <img> and <a> remained. This is because, after the first hook call, the rest of the document was processed with the default allowlist. In 3.5.0, the result was the same as without the hook.

Even with the default allowlist, event attributes such as onerror and <script> are removed. So scripts do not run right away. However, things you meant to exclude get through, such as links to external sites, external images (loading them sends information such as the viewer's IP address to an external server), and <form> elements that submit data externally. When we tried the default configuration on 3.4.18 in the same environment, <form action="https://example.com/"> and <input> remained, while onerror and <script> were removed. The problem is that it breaks the assumption that a restricted allowlist is protecting you.

What changed in 3.5.0

The 3.5.0 README states that sanitize() can now be called from inside hooks and from inside configuration callbacks such as CUSTOM_ELEMENT_HANDLING.tagNameCheck. When the nested call finishes, the configuration, the allowlist and DOMPurify.removed are restored for the outer call.

The README up to the 3.4.x series contained a warning that sanitize() is not re-entrant and should not be called from inside a hook. It also said that a nested call replaces the outer configuration, and that a strict ALLOWED_TAGS allowlist could revert to the defaults partway through the document, with no error or warning. In other words, up to 3.4.x this was a documented limitation, and 3.5.0 removed it.

Besides this issue, the fix commit (0229a2f, Cure53 feature hardening) includes the following changes (based on the commit description; we have not tested them).

  • Fixed an issue where removing several attributes of the same element at once in a uponSanitizeAttribute hook could cause sanitize() to throw an exception
  • In trees built with the DOM (shapes that cannot be produced by HTML parsing), removes <textarea> and <title> that have element children, and void elements such as <img> that have children
  • Applies a namespace check to custom elements kept by CUSTOM_ELEMENT_HANDLING (elements with the same name inside SVG or MathML are removed)
  • Hardened the handling when IN_PLACE or SAFE_FOR_TEMPLATES is used

The 3.5.0 release notes state that this release includes security fixes and several behavior changes, and that no further 3.4.x releases will be made. On November 23, the DOMPurify repository published an advisory fixed in 3.5.0 (GHSA-hrxw-jjj6-2g2j, severity Low). A string sanitized with SAFE_FOR_TEMPLATES: true reverts to a {{…}} expression when inserted back into innerHTML, and the expression is executed by frameworks that read that DOM as a template (the advisory confirmed this with Vue 3 and AngularJS 1.8.3). It follows a fix for an advisory in the same family fixed in 3.4.18 (GHSA-qm7q-q8h9-jg8q, severity Medium, November 14). You are not affected if you do not use SAFE_FOR_TEMPLATES.

As of November 30, these two are not listed in GitHub's advisory database, so npm audit does not warn about them. On November 28, in a project in the test environment with 3.4.18 installed, npm audit also reported found 0 vulnerabilities. No advisory has been issued for the nested sanitize() issue that is the subject of this article.

Affected code pattern

Based on our test results and the README, you are affected when all of the following conditions apply (editorial summary).

  • You use DOMPurify 3.4.x or earlier (the latest 2.x release is 2.5.9 from March, and this change was made only in 3.5.0 on the 3.x series. The README describes the 2.x series as maintained only for IE compatibility. We have not checked the behavior on 2.x)
  • You pass a configuration with each sanitize() call (DOMPurify.sanitize(html, config))
  • You call sanitize() inside a hook (addHook) or a CUSTOM_ELEMENT_HANDLING callback

This applies to code that re-sanitizes, inside a hook, HTML stored in attribute values (such as data attributes for tooltips or previews) or embedded content. You are not affected if you do not use hooks.

# hookの中で sanitize を呼んでいないかを探す
grep -rn "addHook" src/ --include=*.js --include=*.ts --include=*.vue --include=*.tsx -A8 | grep "sanitize("

You may also be using it through a library. Use npm ls dompurify to check which packages install which versions.

How to Fix

Upgrading to 3.5.0 is the basic fix. The release notes say no further 3.4.x releases will be made, and SECURITY.md also states that the supported version is always the latest release. After npm i dompurify@^3.5.0, also use npm ls dompurify to check that no other package installs an older version. As of November 30, no version newer than 3.5.0 has been released.

The 3.5.0 release notes list, as a behavior change, that sanitize() inside a hook no longer changes the remaining behavior of the outer call, and ask code that relied on the old side effect to pass its configuration explicitly. If the output changes after upgrading, check this first.

If you cannot upgrade right away, the README lists two approaches.

  • Fix the configuration with DOMPurify.setConfig(). The fixed configuration is also used by nested calls. Even on 3.4.18 in the test environment, <img> and <a> did not remain. However, because nested calls also use the same strict configuration, in the example above <i> inside data-note was also removed, leaving only memo
  • Sanitize nested HTML after the outer call finishes. Have the hook only collect the values, and run sanitize() on them separately after the outer sanitize() returns

Even in 3.5.0, hooks still run inside nested calls. The README cautions that when calling from inside a hook you should take care to avoid recursion, and that DOMPurify.removed reflects the result of the outer call.

We covered an issue in .NET's HtmlSanitizer, which sanitizes HTML on the server side, in our article on the HtmlSanitizer style attribute. For an issue on the rendering side, see also our article on XSS via attribute names in Vue SSR.

Scope of verification

On November 28, 2026, we reviewed the cure53/DOMPurify repository (commits from 3.4.18 to 3.5.0, the descriptions of 0229a2f and 32428fb and the README diff, and the 3.5.0 README), the publication times in the npm registry, and github/advisory-database (document research). On November 30, we used the GitHub API to recheck the 3.5.0 release notes, the repository's advisories and whether they are listed in the database, SECURITY.md, and commits after 3.5.0. We checked the nested call issue by installing dompurify 3.4.18 and 3.5.0 on Node.js v22.23.5 and jsdom 26.1.0 (hands-on testing). We have not checked behavior in a browser, reproduced the other changes in the commit or GHSA-hrxw-jjj6-2g2j, checked the behavior on 2.x, or checked the versions in libraries that use DOMPurify internally.

For reviews of sites that handle HTML in a CMS or forms, or for web development and renovation projects, please contact us through consultation on development, AI, and automation.

References

Share this articleXFacebook
Kakeru Suzuki

Fascinated by the possibilities of technology, has had a deep interest in programming and digital art since student days

Turn this article's theme into your company's next step

Starting from what you want to achieve with your website.

We organize user goals, required features, and ongoing maintenance structures to determine the first steps in development and improvement.

  • Website objectives
  • Features and usability
  • Post-launch operations
Consult on web development and improvements

You can consult with us from the initial conceptual stage. Details from this article will be carried over to the inquiry form.

Receive the latest articles via email · Read the web production guide
Free download

Complete Guide to Web Production: Costs, Vendor Selection & Traffic Acquisition [2026 Edition]

We have compiled cost benchmarks, vendor selection criteria, and traffic acquisition strategies into a PDF.

The PDF and newsletter emails are currently in Japanese.

You will also be subscribed to our newsletter. You can unsubscribe at any time.