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