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

81fd66
From 7917dbda14ef64a5e7fdea48383a266577484ac8 Mon Sep 17 00:00:00 2001
81fd66
From: Tomas Orsava <torsava@redhat.com>
81fd66
Date: Wed, 19 Aug 2020 12:51:16 +0200
81fd66
Subject: [PATCH 2/2] FIX #6413 pip install <url> allow directory traversal
81fd66
 (tests)
81fd66
81fd66
---
81fd66
 tests/unit/test_download.py | 85 +++++++++++++++++++++++++++++++++++++
81fd66
 1 file changed, 85 insertions(+)
81fd66
81fd66
diff --git a/tests/unit/test_download.py b/tests/unit/test_download.py
81fd66
index ee4b11c..15f99ec 100644
81fd66
--- a/tests/unit/test_download.py
81fd66
+++ b/tests/unit/test_download.py
81fd66
@@ -1,5 +1,6 @@
81fd66
 import hashlib
81fd66
 import os
81fd66
+import sys
81fd66
 from io import BytesIO
81fd66
 from shutil import rmtree, copy
81fd66
 from tempfile import mkdtemp
81fd66
@@ -13,6 +14,7 @@ import pip
81fd66
 from pip.exceptions import HashMismatch
81fd66
 from pip.download import (
81fd66
     PipSession, SafeFileCache, path_to_url, unpack_http_url, url_to_path,
81fd66
+    _download_http_url, parse_content_disposition, sanitize_content_filename,
81fd66
     unpack_file_url,
81fd66
 )
81fd66
 from pip.index import Link
81fd66
@@ -123,6 +125,89 @@ def test_unpack_http_url_bad_downloaded_checksum(mock_unpack_file):
81fd66
         rmtree(download_dir)
81fd66
 
81fd66
 
81fd66
+@pytest.mark.parametrize("filename, expected", [
81fd66
+    ('dir/file', 'file'),
81fd66
+    ('../file', 'file'),
81fd66
+    ('../../file', 'file'),
81fd66
+    ('../', ''),
81fd66
+    ('../..', '..'),
81fd66
+    ('/', ''),
81fd66
+])
81fd66
+def test_sanitize_content_filename(filename, expected):
81fd66
+    """
81fd66
+    Test inputs where the result is the same for Windows and non-Windows.
81fd66
+    """
81fd66
+    assert sanitize_content_filename(filename) == expected
81fd66
+
81fd66
+
81fd66
+@pytest.mark.parametrize("filename, win_expected, non_win_expected", [
81fd66
+    ('dir\\file', 'file', 'dir\\file'),
81fd66
+    ('..\\file', 'file', '..\\file'),
81fd66
+    ('..\\..\\file', 'file', '..\\..\\file'),
81fd66
+    ('..\\', '', '..\\'),
81fd66
+    ('..\\..', '..', '..\\..'),
81fd66
+    ('\\', '', '\\'),
81fd66
+])
81fd66
+def test_sanitize_content_filename__platform_dependent(
81fd66
+    filename,
81fd66
+    win_expected,
81fd66
+    non_win_expected
81fd66
+):
81fd66
+    """
81fd66
+    Test inputs where the result is different for Windows and non-Windows.
81fd66
+    """
81fd66
+    if sys.platform == 'win32':
81fd66
+        expected = win_expected
81fd66
+    else:
81fd66
+        expected = non_win_expected
81fd66
+    assert sanitize_content_filename(filename) == expected
81fd66
+
81fd66
+
81fd66
+@pytest.mark.parametrize("content_disposition, default_filename, expected", [
81fd66
+    ('attachment;filename="../file"', 'df', 'file'),
81fd66
+])
81fd66
+def test_parse_content_disposition(
81fd66
+    content_disposition,
81fd66
+    default_filename,
81fd66
+    expected
81fd66
+):
81fd66
+    actual = parse_content_disposition(content_disposition, default_filename)
81fd66
+    assert actual == expected
81fd66
+
81fd66
+
81fd66
+def test_download_http_url__no_directory_traversal(tmpdir):
81fd66
+    """
81fd66
+    Test that directory traversal doesn't happen on download when the
81fd66
+    Content-Disposition header contains a filename with a ".." path part.
81fd66
+    """
81fd66
+    mock_url = 'http://www.example.com/whatever.tgz'
81fd66
+    contents = b'downloaded'
81fd66
+    link = Link(mock_url)
81fd66
+
81fd66
+    session = Mock()
81fd66
+    resp = MockResponse(contents)
81fd66
+    resp.url = mock_url
81fd66
+    resp.headers = {
81fd66
+        # Set the content-type to a random value to prevent
81fd66
+        # mimetypes.guess_extension from guessing the extension.
81fd66
+        'content-type': 'random',
81fd66
+        'content-disposition': 'attachment;filename="../out_dir_file"'
81fd66
+    }
81fd66
+    session.get.return_value = resp
81fd66
+
81fd66
+    download_dir = tmpdir.join('download')
81fd66
+    os.mkdir(download_dir)
81fd66
+    file_path, content_type = _download_http_url(
81fd66
+        link,
81fd66
+        session,
81fd66
+        download_dir,
81fd66
+        hashes=None,
81fd66
+    )
81fd66
+    # The file should be downloaded to download_dir.
81fd66
+    actual = os.listdir(download_dir)
81fd66
+    assert actual == ['out_dir_file']
81fd66
+
81fd66
+
81fd66
 @pytest.mark.skipif("sys.platform == 'win32'")
81fd66
 def test_path_to_url_unix():
81fd66
     assert path_to_url('/tmp/file') == 'file:///tmp/file'
81fd66
-- 
81fd66
2.25.4
81fd66