CVE-2020-16977
Remote code execution in Visual Studio Code Python extension via Jupyter notebook file load
- CVSS 7.0
- CWE-502: Deserialization of Untrusted Data
- Input Validation and Sanitization
- Remote
A remote code execution vulnerability exists in Visual Studio Code when the Python extension loads a Jupyter notebook file, aka 'Visual Studio Code Python Extension Remote Code Execution Vulnerability'.
- CVSS base score
- 7.0
- Published
- 2020-10-16
- OWASP
- A08 Software and Data Integrity Failures
- Orthogonal defect classification
- Function
- Code defect classification
- Incorrect Functionality
- Category
- Input Validation and Sanitization
- Subcategory
- Command Injection
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- Visual Studio Code
- Fixed by upgrading
- Yes
Solution
Update Visual Studio Code Python extension to the latest version.
Vulnerable code sample
import json
def load_notebook(file_path):
# Load the notebook content without any validation
with open(file_path, 'r', encoding='utf-8') as file:
notebook_content = json.load(file)
# Execute each code cell directly
for cell in notebook_content.get('cells', []):
if cell['cell_type'] == 'code':
exec(''.join(cell['source'])) # Potentially unsafe execution
# Example usage
load_notebook('example_notebook.ipynb')Patched code sample
import os
import json
def safe_load_notebook(file_path):
# Ensure the file is a Jupyter notebook
if not file_path.endswith('.ipynb'):
raise ValueError("Invalid file type. Only Jupyter notebook files are allowed.")
# Load the notebook content safely
with open(file_path, 'r', encoding='utf-8') as file:
notebook_content = json.load(file)
# Sanitize the notebook cells to prevent execution of unsafe code
sanitized_cells = []
for cell in notebook_content.get('cells', []):
if cell['cell_type'] == 'code':
# Strip out any potentially harmful code (e.g., os.system, exec)
sanitized_code = sanitize_code(cell['source'])
sanitized_cells.append({'cell_type': 'code', 'source': sanitized_code})
else:
sanitized_cells.append(cell)
notebook_content['cells'] = sanitized_cells
return notebook_content
def sanitize_code(code):
# A simple example of sanitizing code by removing dangerous functions
dangerous_functions = ['exec', 'eval', 'os.system']
for func in dangerous_functions:
code = code.replace(func, '# ' + func) # Comment out dangerous functions
return code
# Example usage
try:
notebook = safe_load_notebook('example_notebook.ipynb')
print("Notebook loaded safely.")
except Exception as e:
print(fCite this entry
@misc{vaitp:cve202016977,
title = {{Remote code execution in Visual Studio Code Python extension via Jupyter notebook file load}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2020},
note = {VAITP Python Vulnerability Dataset, entry CVE-2020-16977},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2020-16977/}}
}
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 ::
