CVE-2021-42771
Directory traversal in Babel (before 2.9.1) allows remote code execution via arbitrary locale .dat files
- CVSS 7.8
- CWE-22 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
- Input Validation and Sanitization
- Remote
Babel.Locale in Babel before 2.9.1 allows attackers to load arbitrary locale .dat files (containing serialized Python objects) via directory traversal, leading to code execution.
- CVSS base score
- 7.8
- Published
- 2021-10-20
- 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
- Babel
- Fixed by upgrading
- Yes
Solution
Update Babel to version 2.9.1 or higher.
Vulnerable code sample
import os
import pickle
from babel import Locale
def load_locale_file(locale_name):
# Vulnerable code allowing directory traversal
locale_file_path = os.path.join('locales', f'{locale_name}.dat')
# Load the locale file without any validation
with open(locale_file_path, 'rb') as f:
locale_data = pickle.load(f)
return Locale(locale_data['language'], locale_data['territory'])
# Example usage
locale = load_locale_file('en_US') # This could be manipulated to load arbitrary files
print(locale)Patched code sample
import os
import pickle
from babel import Locale
def load_locale_file(locale_name):
# Ensure that the locale_name is safe and does not contain directory traversal characters
if '..' in locale_name or '/' in locale_name or '\\' in locale_name:
raise ValueError("Invalid locale name.")
# Construct the path to the locale file
locale_file_path = os.path.join('locales', f'{locale_name}.dat')
# Load the locale file safely
with open(locale_file_path, 'rb') as f:
locale_data = pickle.load(f)
return Locale(locale_data['language'], locale_data['territory'])
# Example usage
try:
locale = load_locale_file('en_US')
print(locale)
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.
- Run locale identifiers through `os.path.basename()` (python-babel/babel)
babel/localedata.py · exists,babel/localedata.py · load
Cite this entry
@misc{vaitp:cve202142771,
title = {{Directory traversal in Babel (before 2.9.1) allows remote code execution via arbitrary locale .dat files}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2021},
note = {VAITP Python Vulnerability Dataset, entry CVE-2021-42771},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2021-42771/}}
}
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 ::
