npm / sanitize-html
sanitize-html
What changed in sanitize-html 2, from its official release notes, upgrade guides and advisories. Also covers @types/sanitize-html. Sources checked 2026-11-30.
11 breaking16 security2 new APIs and options5 fixes and other
See which of these apply to your code, ranked, in your agent:
npx @unstale/cli initsanitize-html 234 changes
Breaking changes 11
CDATA sections now end at the first
>; this prevents hidden markup from being emitted as live markup and changes how CDATA text inside SVG or MathML is sanitized.Removed the dependency because sanitize-html used only a small part of its functionality, so security scanner reports for deepmerge no longer appear for sanitize-html users.
Fallback markup remains sanitized but is limited to eight nested iframe elements; deeper fallback is escaped as text, and the current nesting depth carries into nested calls so nestingLimit applies to the whole output.
Diagnostic messages are now single lines without decorative line breaks or a warning icon; their wording is unchanged.
For allowed meta elements, refresh destinations in content are checked against allowedSchemes or allowedSchemesByTag.meta; rejected or unparseable refresh content is removed.
Configurations allowing svg or math together with textarea or xmp no longer re-emit raw-text contents unescaped, preventing non-allowlisted markup from surviving sanitization.
sanitize-html now requires Node.js >=22.12.0 because htmlparser2 is ESM-only from version 11 onward.
More attributes are protected against unsafe URL types where those URLs are not appropriate.
The default allowedTags array was updated significantly, mostly adding HTML tags; review the defaults if you do not override them.
Node.js 10 or higher is required; Node 11 and Node 13 support was dropped.
For browser use, include sanitize-html in your project build rather than linking directly to its file, which uses modern JavaScript not fully supported by all major browsers.
Security 16
Inside svg or math,
<in style content is written as the CSS escape\3cso it cannot open a tag; other stylesheets are unchanged.Every colon-separated part of attributeName and the element name is now matched, preventing certain animations from retargeting links to javascript: URLs after sanitization.
ApostropheCMS sanitize-html: nonTextTags discard region ends early on an implied close, letting <noscript> content render
Markup preserved inside a disallowed iframe is no longer escaped and is fully sanitized according to the same rules as the original input.
Animation elements and attributeName are matched by local name, ignoring prefixes, so namespace-prefixed elements such as svg:animate are recognized when dropping URL-retargeting animations.
ApostropheCMS: Stored XSS via SVG SMIL URI-list scheme-policy bypass
Fixed an XSS / URL scheme policy bypass when configurations allow SVG animation elements together with attributeName and an animation value attribute.
ApostropheCMS: Mutation-XSS / allowedTags bypass via literal
</textarea/>solidus closeConfigurations allowing textarea or xmp are protected against trailing-solidus end tags causing following non-allowlisted markup to be re-emitted as raw text.
Nonstandard
nonTextTagsconfigurations no longer let nested tags pass through without sanitization unless they are expressly allowed.sanitize-html has incomplete URI scheme validation in that allows javascript: URIs through action, formaction, data, poster, and background attributes
Apostrophe has default XSS via
xmpraw-text passthrough insanitize-htmlThe xmp tag could pass forbidden markup through sanitize-html even when xmp was not explicitly allowed; users should update immediately.
Fixes an XSS vulnerability that affected configurations explicitly permitting option tags.
sanitize-html allowedTags Bypass via Entity-Decoded Text in nonTextTags Elements
These references are now correctly decoded, preventing them from bypassing javascript: URL detection.
New APIs and options 2
A console-shaped logger option routes sanitize-html diagnostics to an application logging pipeline; missing methods and an omitted option fall back to the console.
The nestingLimit option was added.
Fixes and other changes 5
With parser.decodeEntities set to false, named entities longer than 20 characters are no longer double-encoded.
Per-tag scheme allowlists now apply to srcset and imagesrcset URLs instead of falling back to the global allowedSchemes.
transformTags can emit text when textFilter is set, even if the tag is initially empty.
Entities inside raw text elements such as textarea and option are no longer double-encoded.
Unclosed tags such as
<hellono longer return an empty string inescapeandrecursiveEscapemodes.