|
20 | 20 | # ScanCode.io is a free software code scanning tool from nexB Inc. and others. |
21 | 21 | # Visit https://github.com/nexB/scancode.io for support and download. |
22 | 22 |
|
| 23 | +import os |
23 | 24 | import socket |
| 25 | +import tempfile |
24 | 26 | from pathlib import Path |
25 | 27 | from unittest import mock |
26 | 28 |
|
@@ -94,6 +96,172 @@ def test_scanpipe_pipes_fetch_http(self, mock_get, mock_is_safe_url): |
94 | 96 | downloaded_file = fetch.fetch_http(url) |
95 | 97 | self.assertTrue(Path(downloaded_file.directory, "another_name.zip").exists()) |
96 | 98 |
|
| 99 | + @mock.patch("scanpipe.pipes.fetch.is_safe_url", return_value=True) |
| 100 | + @mock.patch("requests.sessions.Session.get") |
| 101 | + def test_scanpipe_pipes_fetch_http_content_disposition_traversal_attempts( |
| 102 | + self, mock_get, mock_is_safe_url |
| 103 | + ): |
| 104 | + url = "https://example.com/filename.zip" |
| 105 | + invalid_filenames = [ |
| 106 | + "../../evil.zip", |
| 107 | + "../evil.zip", |
| 108 | + "sub/dir/evil.zip", |
| 109 | + "..", |
| 110 | + ".", |
| 111 | + ] |
| 112 | + for invalid_name in invalid_filenames: |
| 113 | + headers = {"content-disposition": f'attachment; filename="{invalid_name}"'} |
| 114 | + mock_get.return_value = make_mock_response(url=url, headers=headers) |
| 115 | + with self.assertRaises(ValueError) as cm: |
| 116 | + fetch.fetch_http(url) |
| 117 | + self.assertIn("Invalid or unsafe filename", str(cm.exception)) |
| 118 | + |
| 119 | + @mock.patch("scanpipe.pipes.fetch.is_safe_url", return_value=True) |
| 120 | + @mock.patch("requests.sessions.Session.get") |
| 121 | + def test_scanpipe_pipes_fetch_http_content_disposition_absolute_paths( |
| 122 | + self, mock_get, mock_is_safe_url |
| 123 | + ): |
| 124 | + url = "https://example.com/filename.zip" |
| 125 | + invalid_filenames = [ |
| 126 | + "/etc/passwd", |
| 127 | + "/evil.zip", |
| 128 | + "/var/test.zip", |
| 129 | + ] |
| 130 | + for invalid_name in invalid_filenames: |
| 131 | + headers = {"content-disposition": f'attachment; filename="{invalid_name}"'} |
| 132 | + mock_get.return_value = make_mock_response(url=url, headers=headers) |
| 133 | + with self.assertRaises(ValueError) as cm: |
| 134 | + fetch.fetch_http(url) |
| 135 | + self.assertIn("Invalid or unsafe filename", str(cm.exception)) |
| 136 | + |
| 137 | + @mock.patch("scanpipe.pipes.fetch.is_safe_url", return_value=True) |
| 138 | + @mock.patch("requests.sessions.Session.get") |
| 139 | + def test_scanpipe_pipes_fetch_http_content_disposition_windows_syntax( |
| 140 | + self, mock_get, mock_is_safe_url |
| 141 | + ): |
| 142 | + url = "https://example.com/filename.zip" |
| 143 | + invalid_filenames = [ |
| 144 | + r"..\..\evil.zip", |
| 145 | + r"C:\evil.zip", |
| 146 | + r"C:evil.zip", |
| 147 | + r"D:/evil.zip", |
| 148 | + r"\\server\share\evil.zip", |
| 149 | + ] |
| 150 | + for invalid_name in invalid_filenames: |
| 151 | + headers = {"content-disposition": f'attachment; filename="{invalid_name}"'} |
| 152 | + mock_get.return_value = make_mock_response(url=url, headers=headers) |
| 153 | + with self.assertRaises(ValueError) as cm: |
| 154 | + fetch.fetch_http(url) |
| 155 | + self.assertIn("Invalid or unsafe filename", str(cm.exception)) |
| 156 | + |
| 157 | + @mock.patch("scanpipe.pipes.fetch.is_safe_url", return_value=True) |
| 158 | + @mock.patch("requests.sessions.Session.get") |
| 159 | + def test_scanpipe_pipes_fetch_http_content_disposition_control_chars( |
| 160 | + self, mock_get, mock_is_safe_url |
| 161 | + ): |
| 162 | + url = "https://example.com/filename.zip" |
| 163 | + invalid_filenames = [ |
| 164 | + "evil\x00.zip", |
| 165 | + "evil\n.zip", |
| 166 | + "evil\r\ntest.zip", |
| 167 | + "evil\x1f.zip", |
| 168 | + "evil\x7f.zip", |
| 169 | + ] |
| 170 | + for invalid_name in invalid_filenames: |
| 171 | + headers = {"content-disposition": f'attachment; filename="{invalid_name}"'} |
| 172 | + mock_get.return_value = make_mock_response(url=url, headers=headers) |
| 173 | + with self.assertRaises(ValueError) as cm: |
| 174 | + fetch.fetch_http(url) |
| 175 | + self.assertIn("Invalid or unsafe filename", str(cm.exception)) |
| 176 | + |
| 177 | + @mock.patch("scanpipe.pipes.fetch.is_safe_url", return_value=True) |
| 178 | + @mock.patch("requests.sessions.Session.get") |
| 179 | + def test_scanpipe_pipes_fetch_http_content_disposition_empty_and_fallback( |
| 180 | + self, mock_get, mock_is_safe_url |
| 181 | + ): |
| 182 | + url = "https://example.com/safe_fallback.zip" |
| 183 | + |
| 184 | + # Empty filename parameter falls back to URL filename |
| 185 | + headers = {"content-disposition": 'attachment; filename=""'} |
| 186 | + mock_get.return_value = make_mock_response(url=url, headers=headers) |
| 187 | + downloaded = fetch.fetch_http(url) |
| 188 | + self.assertEqual("safe_fallback.zip", downloaded.filename) |
| 189 | + self.assertTrue(Path(downloaded.directory, "safe_fallback.zip").exists()) |
| 190 | + |
| 191 | + # Whitespace-only filename parameter falls back to URL filename |
| 192 | + headers = {"content-disposition": 'attachment; filename=" "'} |
| 193 | + mock_get.return_value = make_mock_response(url=url, headers=headers) |
| 194 | + downloaded = fetch.fetch_http(url) |
| 195 | + self.assertEqual("safe_fallback.zip", downloaded.filename) |
| 196 | + self.assertTrue(Path(downloaded.directory, "safe_fallback.zip").exists()) |
| 197 | + |
| 198 | + # Empty header filename and empty URL filename raises ValueError |
| 199 | + empty_path_url = "https://example.com/" |
| 200 | + headers = {"content-disposition": 'attachment; filename=""'} |
| 201 | + mock_get.return_value = make_mock_response(url=empty_path_url, headers=headers) |
| 202 | + with self.assertRaises(ValueError) as cm: |
| 203 | + fetch.fetch_http(empty_path_url) |
| 204 | + self.assertIn("Invalid or unsafe filename", str(cm.exception)) |
| 205 | + |
| 206 | + @mock.patch("scanpipe.pipes.fetch.is_safe_url", return_value=True) |
| 207 | + @mock.patch("requests.sessions.Session.get") |
| 208 | + def test_scanpipe_pipes_fetch_http_content_disposition_safe_names( |
| 209 | + self, mock_get, mock_is_safe_url |
| 210 | + ): |
| 211 | + url = "https://example.com/default.zip" |
| 212 | + safe_names = [ |
| 213 | + "safe-release_v1.0.tar.gz", |
| 214 | + "report (final).pdf", |
| 215 | + "archive.bundle.zip", |
| 216 | + ] |
| 217 | + for safe_name in safe_names: |
| 218 | + headers = {"content-disposition": f'attachment; filename="{safe_name}"'} |
| 219 | + mock_get.return_value = make_mock_response(url=url, headers=headers) |
| 220 | + downloaded = fetch.fetch_http(url) |
| 221 | + self.assertEqual(safe_name, downloaded.filename) |
| 222 | + self.assertEqual(downloaded.path, Path(downloaded.directory) / safe_name) |
| 223 | + self.assertTrue(downloaded.path.exists()) |
| 224 | + self.assertGreater(downloaded.size, 0) |
| 225 | + self.assertTrue(downloaded.sha1) |
| 226 | + self.assertTrue(downloaded.md5) |
| 227 | + |
| 228 | + @mock.patch("scanpipe.pipes.fetch.is_safe_url", return_value=True) |
| 229 | + @mock.patch("requests.sessions.Session.get") |
| 230 | + def test_scanpipe_pipes_fetch_http_content_disposition_symlink( |
| 231 | + self, mock_get, mock_is_safe_url |
| 232 | + ): |
| 233 | + url = "https://example.com/file.zip" |
| 234 | + headers = {"content-disposition": 'attachment; filename="link.zip"'} |
| 235 | + mock_get.return_value = make_mock_response(url=url, headers=headers) |
| 236 | + |
| 237 | + with ( |
| 238 | + tempfile.TemporaryDirectory() as temp_dir, |
| 239 | + tempfile.TemporaryDirectory() as outside_dir, |
| 240 | + ): |
| 241 | + download_dir = Path(temp_dir) |
| 242 | + outside_target = Path(outside_dir) / "target.txt" |
| 243 | + symlink_path = download_dir / "link.zip" |
| 244 | + |
| 245 | + can_symlink = True |
| 246 | + try: |
| 247 | + os.symlink(outside_target, symlink_path) |
| 248 | + except (OSError, NotImplementedError): |
| 249 | + can_symlink = False |
| 250 | + |
| 251 | + if can_symlink: |
| 252 | + with self.assertRaises(ValueError) as cm: |
| 253 | + fetch.fetch_http(url, to=str(download_dir)) |
| 254 | + self.assertIn("Invalid or unsafe filename", str(cm.exception)) |
| 255 | + self.assertFalse(outside_target.exists()) |
| 256 | + else: |
| 257 | + with mock.patch.object(Path, "is_symlink", return_value=True): |
| 258 | + with mock.patch.object( |
| 259 | + Path, "resolve", return_value=outside_target.resolve() |
| 260 | + ): |
| 261 | + with self.assertRaises(ValueError) as cm: |
| 262 | + fetch.fetch_http(url, to=str(download_dir)) |
| 263 | + self.assertIn("Invalid or unsafe filename", str(cm.exception)) |
| 264 | + |
97 | 265 | @mock.patch("scanpipe.pipes.fetch.is_safe_url", return_value=True) |
98 | 266 | @mock.patch("requests.sessions.Session.get") |
99 | 267 | def test_scanpipe_pipes_fetch_package_url(self, mock_get, mock_is_safe_url): |
|
0 commit comments