CVE-2021-28957: Cross-Site Scripting Vulnerability in lxml

lxml Updated 02 Oct 2025

Summary

22/03/2021
Yes i
Moderate
6.1

CVE-2021-28957: lxml vulnerable to Cross-Site Scripting. Affects lxml versions < 4.6.3. Fixed in 4.6.3. How to fix ↓

Also tracked for

The same CVE-2021-28957 is tracked separately for each ecosystem and vendor. Its fix information is the same; the package and affected versions differ.

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

Sponsored Devnack Your last release was probably never penetration tested Manual testing by engineers who build software, not an auditor's checklist. Trusted by teams at Swift and Google Talk to Devnack

Background

lxml is a Python library that provides a powerful and easy-to-use interface for parsing and manipulating XML and HTML documents. It is widely used in various applications for processing and extracting data from XML and HTML files. However, versions of lxml before 4.6.3 are vulnerable to a Cross-Site Scripting (XSS) attack.

Vulnerability Detail

The XSS vulnerability in lxml arises from a flaw in the clean module. When the safe_attrs_only and forms arguments are disabled, the Cleaner class fails to remove the formaction attribute, which allows JavaScript code to bypass the sanitizer. An attacker can exploit this vulnerability to execute arbitrary JavaScript code on users who interact with improperly sanitized HTML. This can lead to various malicious activities, such as stealing sensitive information or performing unauthorized actions on behalf of the user.

How To Fix

Latest published version: 6.1.3 as of 22 Oct 2026

pip install --upgrade "lxml==6.1.3"

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

If you need the minimum change

4.6.3

This is the first release that contains the fix.

pip install --upgrade "lxml==4.6.3"

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

Workaround

If upgrading to the latest version of lxml is not feasible at the moment, a temporary workaround is to manually sanitize the HTML input to remove any potentially harmful attributes, including the formaction attribute. Here's an example of how you can sanitize HTML using the lxml.html.clean.Cleaner class:

from lxml.html.clean import Cleaner

def sanitize_html(html):
    cleaner = Cleaner(safe_attrs_only=True, forms=True)
    cleaned_html = cleaner.clean_html(html)
    return cleaned_html

Conclusion

In conclusion, the XSS vulnerability in lxml versions before 4.6.3 can allow attackers to execute arbitrary JavaScript code on users who interact with improperly sanitized HTML. It is crucial to upgrade to the latest version of lxml or apply the provided workaround to mitigate the risk. For further assistance or support in vulnerability remediation, please contact DevNack.com.

See every vulnerability recorded for lxml →

Share this vulnerability

FAQ

The vulnerability allows an attacker to execute arbitrary JavaScript code on users who interact with improperly sanitized HTML. This can lead to various malicious activities, such as stealing sensitive information or performing unauthorized actions on behalf of the user.

Versions of lxml before 4.6.3 are affected by this XSS vulnerability.

While there is no known public exploit for this vulnerability at the moment, it is important to apply the patch or upgrade to the latest version of lxml to mitigate the risk.

The recommended mitigation is to upgrade to lxml version 4.6.3 or later. If upgrading is not possible immediately, manually sanitizing the HTML input to remove potentially harmful attributes can serve as a temporary workaround.

You can find more information about this vulnerability in the GitHub Security Advisory database. The link to the advisory is provided in the sources section of this report.

Related vulnerabilities

Recent vulnerabilities recorded for lxml.

How To Fix

Latest published version: 6.1.3 as of 22 Oct 2026

pip install --upgrade "lxml==6.1.3"

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

If you need the minimum change

4.6.3

This is the first release that contains the fix.

pip install --upgrade "lxml==4.6.3"

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