Blame SOURCES/pip-directory-traversal-security-issue.patch

81fd66
From 8044d9f2fbcb09f09a62b26ac1d8a134976bb2ac Mon Sep 17 00:00:00 2001
81fd66
From: gzpan123 <gzpan123@gmail.com>
81fd66
Date: Wed, 17 Apr 2019 21:25:45 +0800
81fd66
Subject: [PATCH 1/2] FIX #6413 pip install <url> allow directory traversal
81fd66
81fd66
---
81fd66
 news/6413.bugfix |  3 +++
81fd66
 pip/download.py  | 31 ++++++++++++++++++++++++++-----
81fd66
 2 files changed, 29 insertions(+), 5 deletions(-)
81fd66
 create mode 100644 news/6413.bugfix
81fd66
81fd66
diff --git a/news/6413.bugfix b/news/6413.bugfix
81fd66
new file mode 100644
81fd66
index 0000000..68d0a72
81fd66
--- /dev/null
81fd66
+++ b/news/6413.bugfix
81fd66
@@ -0,0 +1,3 @@
81fd66
+Prevent ``pip install <url>`` from permitting directory traversal if e.g.
81fd66
+a malicious server sends a ``Content-Disposition`` header with a filename
81fd66
+containing ``../`` or ``..\\``.
81fd66
diff --git a/pip/download.py b/pip/download.py
81fd66
index 039e55a..b3d169b 100644
81fd66
--- a/pip/download.py
81fd66
+++ b/pip/download.py
81fd66
@@ -54,7 +54,8 @@ __all__ = ['get_file_content',
81fd66
            'is_url', 'url_to_path', 'path_to_url',
81fd66
            'is_archive_file', 'unpack_vcs_link',
81fd66
            'unpack_file_url', 'is_vcs_url', 'is_file_url',
81fd66
-           'unpack_http_url', 'unpack_url']
81fd66
+           'unpack_http_url', 'unpack_url',
81fd66
+           'parse_content_disposition', 'sanitize_content_filename']
81fd66
 
81fd66
 
81fd66
 logger = logging.getLogger(__name__)
81fd66
@@ -824,6 +825,29 @@ def unpack_url(link, location, download_dir=None,
81fd66
         write_delete_marker_file(location)
81fd66
 
81fd66
 
81fd66
+def sanitize_content_filename(filename):
81fd66
+    # type: (str) -> str
81fd66
+    """
81fd66
+    Sanitize the "filename" value from a Content-Disposition header.
81fd66
+    """
81fd66
+    return os.path.basename(filename)
81fd66
+
81fd66
+
81fd66
+def parse_content_disposition(content_disposition, default_filename):
81fd66
+    # type: (str, str) -> str
81fd66
+    """
81fd66
+    Parse the "filename" value from a Content-Disposition header, and
81fd66
+    return the default filename if the result is empty.
81fd66
+    """
81fd66
+    _type, params = cgi.parse_header(content_disposition)
81fd66
+    filename = params.get('filename')
81fd66
+    if filename:
81fd66
+        # We need to sanitize the filename to prevent directory traversal
81fd66
+        # in case the filename contains ".." path parts.
81fd66
+        filename = sanitize_content_filename(filename)
81fd66
+    return filename or default_filename
81fd66
+
81fd66
+
81fd66
 def _download_http_url(link, session, temp_dir, hashes):
81fd66
     """Download link url into temp_dir using provided session"""
81fd66
     target_url = link.url.split('#', 1)[0]
81fd66
@@ -864,10 +888,7 @@ def _download_http_url(link, session, temp_dir, hashes):
81fd66
     # Have a look at the Content-Disposition header for a better guess
81fd66
     content_disposition = resp.headers.get('content-disposition')
81fd66
     if content_disposition:
81fd66
-        type, params = cgi.parse_header(content_disposition)
81fd66
-        # We use ``or`` here because we don't want to use an "empty" value
81fd66
-        # from the filename param.
81fd66
-        filename = params.get('filename') or filename
81fd66
+        filename = parse_content_disposition(content_disposition, filename)
81fd66
     ext = splitext(filename)[1]
81fd66
     if not ext:
81fd66
         ext = mimetypes.guess_extension(content_type)
81fd66
-- 
81fd66
2.25.4
81fd66