VAITP Dataset

← Back to the dataset

CVE-2019-20916

Directory Traversal in pip < 19.2 allows overwriting files via URL with "../" in Content-Disposition header

  • CVSS 7.5
  • CWE-22 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
  • Input Validation and Sanitization
  • Remote

The pip package before 19.2 for Python allows Directory Traversal when a URL is given in an install command, because a Content-Disposition header can have ../ in a filename, as demonstrated by overwriting the /root/.ssh/authorized_keys file. This occurs in _download_http_url in _internal/download.py.

CVSS base score
7.5
Published
2020-09-04
OWASP
A10 Server-Side Request Forgery (SSRF)
Orthogonal defect classification
Function
Code defect classification
Incorrect Functionality
Category
Input Validation and Sanitization
Subcategory
Path Traversal
Accessibility scope
Remote
Impact
Unauthorized Access
Affected component
pip
Fixed by upgrading
Yes

Solution

Update pip to version 19.2 or higher.

Vulnerable code sample

import os
import requests

def download_file(url):
    response = requests.get(url)
    content_disposition = response.headers.get('Content-Disposition', '')
    
    # Vulnerable code: directly using the filename from the Content-Disposition header
    if content_disposition:
        filename = content_disposition.split('filename=')[1].strip('"')
    else:
        filename = url.split('/')[-1]

    # This could lead to directory traversal if the filename contains '../'
    with open(filename, 'wb') as f:
        f.write(response.content)

# Example usage
download_file('http://example.com/file?name=example.txt')

Patched code sample

import os
import re
import requests

def safe_filename(filename):
    # Remove any directory traversal characters
    return os.path.basename(filename)

def download_file(url):
    response = requests.get(url)
    content_disposition = response.headers.get('Content-Disposition', '')
    filename_match = re.search(r'filename="([^"]+)"', content_disposition)

    if filename_match:
        filename = safe_filename(filename_match.group(1))
    else:
        filename = safe_filename(url.split('/')[-1])

    with open(filename, 'wb') as f:
        f.write(response.content)

# Example usage
download_file('http://example.com/file?name=example.txt')

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.

Cite this entry

@misc{vaitp:cve201920916,
  title        = {{Directory Traversal in pip < 19.2 allows overwriting files via URL with "../" in Content-Disposition header}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2020},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2019-20916},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2019-20916/}}
}

Download this entry as JSON Download the whole dataset

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 ::

Before you use the VAITP AI models

Please read and accept the following disclaimer:

By choosing "I understand and accept", you acknowledge that you understand and agree to these terms.