CVE-2022-40897 Overview
CVE-2022-40897 is a Regular Expression Denial of Service (ReDoS) vulnerability affecting Python Packaging Authority (PyPA) setuptools before version 65.5.1. The vulnerability allows remote attackers to cause a denial of service via crafted HTML content in a malicious package or custom PackageIndex page. The vulnerable code resides in package_index.py, where an inefficient regular expression pattern can be exploited to cause catastrophic backtracking.
Critical Impact
Remote attackers can exhaust server CPU resources by supplying specially crafted HTML content that triggers exponential regex backtracking, leading to denial of service conditions in Python package management operations.
Affected Products
- Python setuptools versions prior to 65.5.1
- Systems using setuptools for package management and installation
- CI/CD pipelines and build systems utilizing vulnerable setuptools versions
Discovery Timeline
- 2022-12-23 - CVE CVE-2022-40897 published to NVD
- 2025-11-04 - Last updated in NVD database
Technical Details for CVE-2022-40897
Vulnerability Analysis
This vulnerability is classified under CWE-1333 (Inefficient Regular Expression Complexity). The flaw exists in the package_index.py module where a regular expression used to parse HTML content containing rel attributes allows for unlimited whitespace matching. When processing malicious input with excessive whitespace characters, the regex engine enters a state of catastrophic backtracking, consuming significant CPU cycles.
The vulnerable regex pattern <([^>]*\srel\s*=\s*['\"]?([^'\">]+)[^>]*)> permits unbounded whitespace (\s*) around the equals sign in HTML rel attribute parsing. An attacker can craft input that forces the regex engine to explore an exponentially growing number of matching paths before failing to match, effectively freezing the process.
Root Cause
The root cause is an inefficient regular expression pattern in the HTML parsing logic of setuptools' package index functionality. The pattern uses unbounded quantifiers (\s*) for whitespace matching, which creates the potential for exponential time complexity when processing adversarial input. The regex engine's backtracking behavior attempts numerous unsuccessful match combinations before determining no valid match exists.
Attack Vector
The attack vector is network-based and requires no authentication or user interaction. An attacker can exploit this vulnerability by:
- Creating a malicious Python package with crafted HTML content containing excessive whitespace
- Hosting a custom PackageIndex page with specially crafted HTML that triggers the vulnerable regex
- When a victim's setuptools processes this content (e.g., during pip install), the ReDoS condition is triggered
The following patch from the official repository demonstrates the fix:
return wrapper
-REL = re.compile(r"""<([^>]*\srel\s*=\s*['\"]?([^'">]+)[^>]*)>""", re.I)
+REL = re.compile(r"""<([^>]*\srel\s{0,10}=\s{0,10}['\"]?([^'" >]+)[^>]*)>""", re.I)
"""
Regex for an HTML tag with 'rel="val"' attributes.
"""
Source: GitHub Commit Reference
The patch limits whitespace matching to a maximum of 10 characters (\s{0,10}) instead of unlimited (\s*), preventing the exponential backtracking condition.
Detection Methods for CVE-2022-40897
Indicators of Compromise
- Abnormally high CPU usage during package installation or indexing operations
- Process hangs or timeouts when setuptools processes external package metadata
- Unusual network traffic to untrusted package index servers
- Build pipeline failures or timeouts during dependency resolution
Detection Strategies
- Monitor for CPU spikes correlated with Python package management processes (pip, easy_install, or custom scripts using setuptools)
- Implement Software Composition Analysis (SCA) to detect vulnerable setuptools versions in your codebase
- Use dependency scanning tools to identify packages with setuptools versions below 65.5.1
- Review CI/CD logs for unexpected delays during package installation steps
Monitoring Recommendations
- Enable process monitoring for Python-related processes to detect abnormal resource consumption
- Implement timeout mechanisms for package installation operations in automated pipelines
- Use internal package mirrors or caching proxies to control and scan incoming package metadata
- Configure alerting for processes exceeding normal CPU thresholds during build operations
How to Mitigate CVE-2022-40897
Immediate Actions Required
- Upgrade setuptools to version 65.5.1 or later immediately using pip install --upgrade setuptools>=65.5.1
- Audit all Python environments (virtual environments, containers, CI/CD runners) for vulnerable versions
- Update base container images and system packages that include setuptools
- Review and pin setuptools versions in requirements.txt, setup.py, and pyproject.toml files
Patch Information
The vulnerability is fixed in setuptools version 65.5.1. The patch commit (43a9c9bfa6aa626ec2a22540bea28d2ca77964be) modifies the REL regex pattern in setuptools/package_index.py to limit whitespace matching to a maximum of 10 characters. This change prevents the catastrophic backtracking condition while maintaining normal parsing functionality. The fix is available via PyPI and can be installed using standard package managers. See the GitHub Version Comparison for full details.
Workarounds
- Use trusted internal package repositories or mirrors rather than public PyPI during the upgrade process
- Implement network-level filtering to restrict access to unknown package index servers
- Apply timeouts to package installation operations to limit the impact of potential DoS
- For systems that cannot be immediately upgraded, consider isolating package management operations in resource-limited containers
# Configuration example
# Upgrade setuptools to patched version
pip install --upgrade "setuptools>=65.5.1"
# Verify installed version
pip show setuptools | grep Version
# Pin version in requirements.txt
echo "setuptools>=65.5.1" >> requirements.txt
# Update all virtual environments
find /path/to/projects -name "requirements.txt" -exec grep -l setuptools {} \; | xargs -I {} dirname {} | xargs -I {} sh -c 'cd {} && pip install --upgrade "setuptools>=65.5.1"'
Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.


