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