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

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