CVE-2021-41945
Improper input validation in httpx < 0.23.0 allows security vulnerabilities
- CVSS 9.1
- CWE-20 Improper Input Validation
- Input Validation and Sanitization
- Remote
Encode OSS httpx < 0.23.0 is affected by improper input validation in `httpx.URL`, `httpx.Client` and some functions using `httpx.URL.copy_with`.
- CVSS base score
- 9.1
- Published
- 2022-04-28
- OWASP
- A06 Vulnerable and Outdated Components
- Orthogonal defect classification
- Function
- Code defect classification
- Incorrect Functionality
- Category
- Input Validation and Sanitization
- Subcategory
- Insecure Parsing or Deserialization
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- httpx
- Fixed by upgrading
- Yes
Solution
Update httpx to version 0.23.0 or higher.
Vulnerable code sample
import httpx
# Example of improper input validation leading to potential vulnerabilities
def vulnerable_url(url: str) -> httpx.URL:
# No validation on the URL input, which could lead to issues
return httpx.URL(url)
def vulnerable_client(base_url: str):
# Directly using the base URL without validation
client = httpx.Client(base_url=vulnerable_url(base_url))
return client
# Example usage
client = vulnerable_client("https://example.com")
response = client.get("/")
print(response.text)Patched code sample
import httpx
def safe_url(url: str) -> httpx.URL:
# Proper input validation to prevent improper URL handling
try:
parsed_url = httpx.URL(url)
return parsed_url
except httpx.InvalidURL:
raise ValueError("Invalid URL provided")
def safe_client(base_url: str):
# Ensure the base URL is valid before creating the client
valid_url = safe_url(base_url)
client = httpx.Client(base_url=valid_url)
return client
# Example usage
try:
client = safe_client("https://example.com")
response = client.get("/")
print(response.text)
except ValueError as e:
print(e)Published fix
Public sources link this CVE to the changes below. The code samples above were generated to illustrate the defect; these links lead to the code the project actually changed.
- Patch `copy_with` (encode/httpx)
httpx/_urls.py · URL.copy_with
Cite this entry
@misc{vaitp:cve202141945,
title = {{Improper input validation in httpx < 0.23.0 allows security vulnerabilities}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2022},
note = {VAITP Python Vulnerability Dataset, entry CVE-2021-41945},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2021-41945/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
