CVE-2020-15142
Malicious OpenAPI Document in openapi-python-client < 0.5.3 allows arbitrary Python code execution
- CVSS 9.0
- CWE-94 Improper Control of Generation of Code ('Code Injection')
- Input Validation and Sanitization
- Remote
In openapi-python-client before version 0.5.3, clients generated with a maliciously crafted OpenAPI Document can generate arbitrary Python code. Subsequent execution of this malicious client is arbitrary code execution.
- CVSS base score
- 9.0
- Published
- 2020-08-14
- OWASP
- A10 Server-Side Request Forgery (SSRF)
- 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
- Python
- Fixed by upgrading
- Yes
Solution
Upgrade to openapi-python-client version 0.5.3 or newer.
Vulnerable code sample
def generate_client(openapi_doc):
# This function simulates the unsafe execution of generated code
# from a maliciously crafted OpenAPI document
exec(openapi_doc['code']) # Potentially unsafe execution
# Example of a malicious OpenAPI document
malicious_openapi_doc = {
'code': "__import__('os').system('echo Malicious Code Executed!')"
}
# Example usage
generate_client(malicious_openapi_doc)Patched code sample
import ast
import os
def safe_eval(code):
# Limit the execution environment to prevent arbitrary code execution
allowed_builtins = {
'print': print,
'len': len,
'range': range,
# Add other safe built-in functions as needed
}
# Use ast.literal_eval to safely evaluate expressions
return ast.literal_eval(code)
def generate_client(openapi_doc):
# Example of processing the OpenAPI document safely
if not isinstance(openapi_doc, dict):
raise ValueError("Invalid OpenAPI document format")
# Process the OpenAPI document without executing arbitrary code
# Implement client generation logic here
# ...
# Example usage
try:
openapi_doc = '{"paths": {}}' # Example OpenAPI document
client_code = generate_client(openapi_doc)
except Exception as e:
print(f"Error: {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.
- Merge pull request from GHSA-9x4c-63pf-525f (triaxtec/openapi-python-client)
end_to_end_tests/fastapi_app/__init__.py,end_to_end_tests/fastapi_app/__init__.py · get_list,end_to_end_tests/golden-master/my_test_api_client/api/tests.py,end_to_end_tests/golden-master/my_test_api_client/api/tests.py · get_user_list,end_to_end_tests/golden-master/my_test_api_client/async_api/tests.py,end_to_end_tests/golden-master/my_test_api_client/async_api/tests.py · get_user_list,end_to_end_tests/golden-master/my_test_api_client/models/a_model.py,end_to_end_tests/golden-master/my_test_api_client/models/a_model.py · AModel.from_dict,end_to_end_tests/golden-master/my_test_api_client/models/a_model.py · AModel.to_dict,openapi_python_client/parser/errors.py,openapi_python_client/parser/openapi.py,openapi_python_client/parser/openapi.py · Endpoint.from_dataand 18 more
Cite this entry
@misc{vaitp:cve202015142,
title = {{Malicious OpenAPI Document in openapi-python-client < 0.5.3 allows arbitrary Python code execution}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2020},
note = {VAITP Python Vulnerability Dataset, entry CVE-2020-15142},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2020-15142/}}
}
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 ::
