|
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 socket |
23 | 24 | from pathlib import Path |
24 | 25 | from unittest import mock |
25 | 26 |
|
26 | 27 | from django.test import TestCase |
27 | 28 | from django.test import override_settings |
28 | 29 |
|
| 30 | +import requests |
29 | 31 | from requests import auth as request_auth |
30 | 32 |
|
31 | 33 | from scanpipe.pipes import fetch |
@@ -265,3 +267,83 @@ def test_scanpipe_pipes_fetch_git_repo(self, mock_clone_from): |
265 | 267 | self.assertEqual("", download.size) |
266 | 268 | self.assertEqual("", download.sha1) |
267 | 269 | self.assertEqual("", download.md5) |
| 270 | + |
| 271 | + @mock.patch("scanpipe.pipes.fetch.socket.gethostbyname") |
| 272 | + def test_scanpipe_pipes_fetch_is_safe_url(self, mock_gethostbyname): |
| 273 | + # Valid public URLs |
| 274 | + mock_gethostbyname.return_value = "93.184.216.34" # example.com |
| 275 | + self.assertTrue(fetch.is_safe_url("https://example.com/file.zip")) |
| 276 | + self.assertTrue(fetch.is_safe_url("http://example.com/file.zip")) |
| 277 | + |
| 278 | + # Invalid schemes |
| 279 | + self.assertFalse(fetch.is_safe_url("ftp://example.com/file.zip")) |
| 280 | + self.assertFalse(fetch.is_safe_url("docker://example.com/image")) |
| 281 | + self.assertFalse(fetch.is_safe_url("")) |
| 282 | + |
| 283 | + # No hostname |
| 284 | + self.assertFalse(fetch.is_safe_url("https://")) |
| 285 | + |
| 286 | + # Unresolvable hostname |
| 287 | + mock_gethostbyname.side_effect = socket.gaierror |
| 288 | + self.assertFalse(fetch.is_safe_url("https://thisdomaindoesnotexist.invalid/")) |
| 289 | + mock_gethostbyname.side_effect = None |
| 290 | + |
| 291 | + # Private ranges |
| 292 | + mock_gethostbyname.return_value = "192.168.1.1" |
| 293 | + self.assertFalse(fetch.is_safe_url("http://192.168.1.1/")) |
| 294 | + mock_gethostbyname.return_value = "10.0.0.1" |
| 295 | + self.assertFalse(fetch.is_safe_url("http://10.0.0.1/")) |
| 296 | + mock_gethostbyname.return_value = "172.16.0.1" |
| 297 | + self.assertFalse(fetch.is_safe_url("http://172.16.0.1/")) |
| 298 | + |
| 299 | + # Loopback |
| 300 | + mock_gethostbyname.return_value = "127.0.0.1" |
| 301 | + self.assertFalse(fetch.is_safe_url("http://127.0.0.1/")) |
| 302 | + mock_gethostbyname.return_value = "127.0.0.1" |
| 303 | + self.assertFalse(fetch.is_safe_url("http://localhost/")) |
| 304 | + |
| 305 | + # Link-local |
| 306 | + mock_gethostbyname.return_value = "169.254.169.254" |
| 307 | + self.assertFalse(fetch.is_safe_url("http://169.254.169.254/")) |
| 308 | + |
| 309 | + # Multicast |
| 310 | + mock_gethostbyname.return_value = "224.0.0.1" |
| 311 | + self.assertFalse(fetch.is_safe_url("http://224.0.0.1/")) |
| 312 | + |
| 313 | + @mock.patch("scanpipe.pipes.fetch.socket.gethostbyname") |
| 314 | + @mock.patch("requests.sessions.Session.head") |
| 315 | + def test_scanpipe_pipes_fetch_check_url(self, mock_head, mock_gethostbyname): |
| 316 | + url = "https://example.com/file.zip" |
| 317 | + |
| 318 | + # Safe and accessible URL |
| 319 | + mock_gethostbyname.return_value = "93.184.216.34" |
| 320 | + mock_head.return_value = make_mock_response(url=url) |
| 321 | + self.assertTrue(fetch.check_url(url)) |
| 322 | + |
| 323 | + # Unsafe URL |
| 324 | + mock_gethostbyname.return_value = "127.0.0.1" |
| 325 | + self.assertFalse(fetch.check_url("http://localhost/")) |
| 326 | + |
| 327 | + # Safe URL but request fails |
| 328 | + mock_gethostbyname.return_value = "93.184.216.34" |
| 329 | + mock_head.side_effect = requests.exceptions.RequestException |
| 330 | + self.assertFalse(fetch.check_url(url)) |
| 331 | + |
| 332 | + @mock.patch("scanpipe.pipes.fetch.socket.gethostbyname") |
| 333 | + @mock.patch("requests.sessions.Session.head") |
| 334 | + def test_scanpipe_pipes_fetch_check_urls_availability( |
| 335 | + self, mock_head, mock_gethostbyname |
| 336 | + ): |
| 337 | + urls = [ |
| 338 | + "https://example.com/file.zip", |
| 339 | + "https://example.com/archive.tar.gz", |
| 340 | + ] |
| 341 | + |
| 342 | + # All URLs safe and accessible |
| 343 | + mock_gethostbyname.return_value = "93.184.216.34" |
| 344 | + mock_head.return_value = make_mock_response(url="mocked_url") |
| 345 | + self.assertEqual([], fetch.check_urls_availability(urls)) |
| 346 | + |
| 347 | + # All URLs fail |
| 348 | + mock_head.side_effect = requests.exceptions.RequestException |
| 349 | + self.assertEqual(urls, fetch.check_urls_availability(urls)) |
0 commit comments