CVE-2021-26540: Improper Input Validation in sanitize-html

sanitize-html Updated 02 Oct 2025

Summary

06/05/2021
Yes i
Moderate
5.3

CVE-2021-26540: Improper Input Validation in sanitize-html. Affects sanitize-html versions < 2.3.2. Fixed in 2.3.2. How to fix ↓

Free scan

Check if this vulnerability affects your application

Upload a manifest or SBOM and we'll show whether you're affected, plus every other risky package in the same scan.

Check my app free

No signup required · Results in about 60 seconds

Upload your dependency manifest or SBOM

Drag & drop your file here or

Java Javascript PHP Python Go Rust Ruby Dart C/C++ Elixir CycloneDX SPDX

Upload a supported dependency manifest or SBOM. No installation or source-code access required.

See supported files and SBOM formats
SBOM sbom.json · bom.json · bom.xml · *.spdx.json · *.cdx.json · *.cdx.xml
Java pom.xml · gradle.lockfile · buildscript-gradle.lockfile
Javascript package-lock.json · yarn.lock · pnpm-lock.yaml
PHP composer.lock
Python poetry.lock · requirements.txt · Pipfile.lock
Go go.mod
Rust Cargo.lock
Ruby Gemfile.lock
Dart pubspec.lock
C/C++ conan.lock
Elixir mix.lock
Enter your work email to receive your report link.

Verify your email to view the full report and start 30 days of continuous monitoring. No credit card required.

Sponsored Lume, the Habilume companion Habilume Habit tracker with a companion that grows with you Complete habits, earn Lume Points, and watch your Lume grow as you stay consistent. App Store Google Play

Background

The sanitize-html package is an npm package that provides a simple HTML sanitizer. It is commonly used to sanitize user-generated HTML input to prevent cross-site scripting (XSS) attacks. The package allows developers to specify a whitelist of allowed HTML tags and attributes, and it sanitizes the input by removing any disallowed tags or attributes. However, a vulnerability has been discovered in versions before 2.3.2 that allows attackers to bypass the hostname whitelist for iframe elements when the 'allowIframeRelativeUrls' option is set to true.

Vulnerability Detail

The vulnerability in sanitize-html arises from improper input validation of the hostnames set by the 'allowedIframeHostnames' option. When 'allowIframeRelativeUrls' is enabled, an attacker can bypass the hostname whitelist by using an src value that starts with '/\example.com'. This allows the attacker to inject malicious iframes from any hostname, potentially leading to XSS attacks or other security vulnerabilities.

How To Fix

Latest published version: 2.18.1 as of 23 Oct 2026

npm install sanitize-html@2.18.1

Upgrading to it may include breaking changes; the version below is the minimum fixed release for this vulnerability.

If you need the minimum change

2.3.2

This is the first release that contains the fix.

npm install sanitize-html@2.3.2

Tip — Not sure which version your app runs? Vulert can match this fix to it. Scan free →

Workaround

If upgrading to the latest version is not immediately feasible, a temporary workaround is to disable the 'allowIframeRelativeUrls' option. By setting this option to false, the vulnerability can be mitigated, but it may impact the functionality of the application if it relies on allowing relative URLs for iframes.

Conclusion

In conclusion, the sanitize-html package before version 2.3.2 is vulnerable to an improper input validation issue that allows attackers to bypass the hostname whitelist for iframes. Developers should upgrade to the latest version or apply the provided workaround to mitigate the vulnerability. For further support on vulnerability remediation, please contact DevNack.com.

See every vulnerability recorded for sanitize-html →

Share this vulnerability

FAQ

sanitize-html is an npm package used for sanitizing user-generated HTML input to prevent XSS attacks.

Versions of sanitize-html before 2.3.2 are affected by this vulnerability.

This vulnerability allows attackers to bypass the hostname whitelist for iframe elements, potentially leading to XSS attacks or other security vulnerabilities.

To fix this vulnerability, upgrade to sanitize-html version 2.3.2 or later.

Yes, a temporary workaround is to disable the 'allowIframeRelativeUrls' option.

Related vulnerabilities

Recent vulnerabilities recorded for sanitize-html.

Sponsored Lume, the Habilume companion Habilume Habit tracker with a companion that grows with you Complete habits, earn Lume Points, and watch your Lume grow as you stay consistent. App Store Google Play

How To Fix

Latest published version: 2.18.1 as of 23 Oct 2026

npm install sanitize-html@2.18.1

Upgrading to it may include breaking changes; the version below is the minimum fixed release for this vulnerability.

If you need the minimum change

2.3.2

This is the first release that contains the fix.

npm install sanitize-html@2.3.2

Tip — Not sure which version your app runs? Vulert can match this fix to it. Scan free →

Subscribe to Vulert on Telegram
Daily digest of new CVEs, critical and high severity, affected packages and fixes — plus a weekly roundup.
Join the channel →
Help improve this record
Report an issue or send feedback