CVE-2025-4516
CPython vulnerable to DoS via crafted bytes.decode("unicode_escape").
- CVSS 5.9
- CWE-416
- Numeric Errors
- Remote
There is an issue in CPython when using `bytes.decode("unicode_escape", error="ignore|replace")`. If you are not using the "unicode_escape" encoding or an error handler your usage is not affected. To work-around this issue you may stop using the error= handler and instead wrap the bytes.decode() call in a try-except catching the DecodeError.
- CWE
- CWE-416
- CVSS base score
- 5.9
- Published
- 2025-05-15
- OWASP
- A03 Injection
- Orthogonal defect classification
- Interface
- Code defect classification
- Incorrect Functionality
- Category
- Numeric Errors
- Subcategory
- Insecure Parsing or Deserialization
- Accessibility scope
- Remote
- Impact
- Denial of Service (DoS)
- Affected component
- Python
- Fixed by upgrading
- Yes
Solution
Upgrade to CPython 3.11.8, 3.12.2, or later. If upgrading is not possible, use a try-except block around `bytes.decode()` to catch `UnicodeDecodeError` when using "unicode_escape" encoding with error handlers.
Vulnerable code sample
# This code is a simplified representation of the potential vulnerability
# described in CVE-2025-4516. It is not the exact vulnerable code,
# as the original code is complex and potentially involves internal
# CPython implementation details. This example aims to illustrate
# a scenario where using "unicode_escape" with error handlers like
# "ignore" or "replace" *might* have led to unexpected behavior
# before the fix.
def decode_unicode_escape_with_error_handling(data, error_handler):
"""
Simulates decoding a bytes object with "unicode_escape" and an error handler.
"""
try:
decoded_string = data.decode("unicode_escape", errors=error_handler)
return decoded_string
except UnicodeDecodeError as e:
print(f"Decoding error: {e}") # This might have been unexpected or incorrectly handled
return None # or some other error handling strategy
# Example usage (before the supposed fix)
if __name__ == "__main__":
data = b"hello\\xFbworld" # Invalid unicode escape sequence (before potential fix)
#Example using "ignore"
decoded_ignore = decode_unicode_escape_with_error_handling(data, "ignore")
print(f"Decoded (ignore): {decoded_ignore}")
#Example using "replace"
decoded_replace = decode_unicode_escape_with_error_handling(data, "replace")
print(f"Decoded (replace): {decoded_replace}")
#Example without an error handler, this wasn't vulnerable
try:
decoded_strict = data.decode("unicode_escape")
print(f"Decoded (strict): {decoded_strict}")
except UnicodeDecodeError as e:
print(f"Strict decoding error: {e}")
data2 = b"\\u0041\\u0042\\u0043" # Valid unicode escape sequence
decoded2_ignore = decode_unicode_escape_with_error_handling(data2, "ignore")
print(f"Decoded 2 (ignore): {decoded2_ignore}")Patched code sample
def decode_with_try_except(data: bytes, encoding: str = "unicode_escape", errors: str = "ignore") -> str:
"""
Safely decodes bytes using a try-except block to handle potential DecodeErrors.
Replaces the error handler approach with try-except block
This avoids the CVE-2025-4516 issue, if it occurs when using the "unicode_escape" encoding
"""
try:
return data.decode(encoding)
except UnicodeDecodeError:
# Handle the decoding error as needed. In this example, it is replaced or ignored
if errors == "replace":
return data.decode(encoding, errors="replace") # Decode, replacing invalid chars.
elif errors == "ignore":
return data.decode(encoding, errors="ignore") # Decode, ignoring invalid chars.
else:
# Re-raise the exception if errors is not 'replace' or 'ignore'. This matches
# standard decode behavior if an error occurs and error handling is not available.
raise
# Example usage:
if __name__ == '__main__':
# Potential vulnerable example
data = b'abc\\ud800def' # Invalid unicode sequence
# Fixed version using try-except:
decoded_string = decode_with_try_except(data, "unicode_escape", errors="ignore") # or "replace"
print(f"Decoded string (ignore): {decoded_string}")
decoded_string = decode_with_try_except(data, "unicode_escape", errors="replace")
print(f"Decoded string (replace): {decoded_string}")
# Demonstrate the default case that re-raises the error:
try:
decoded_string = decode_with_try_except(data, "unicode_escape", errors="strict") # errors="strict" or any other value except "ignore" and "replace" re-raises the exception
print(f"Decoded string (strict): {decoded_string}")
except UnicodeDecodeError as e:
print(f"Caught expected UnicodeDecodeError: {e}")Payload
b'\\ud800'
Cite this entry
@misc{vaitp:cve20254516,
title = {{CPython vulnerable to DoS via crafted bytes.decode("unicode_escape").
}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2025},
note = {VAITP Python Vulnerability Dataset, entry CVE-2025-4516},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2025-4516/}}
}
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 ::
