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

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