|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import importlib.util |
| 16 | +import pathlib |
| 17 | +import socket |
| 18 | +from unittest.mock import patch |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +# Load the validator directly by file path so we don't trigger nemo_deploy/__init__.py |
| 23 | +# (which requires torch/triton). The module itself is pure stdlib. |
| 24 | +_validator_path = ( |
| 25 | + pathlib.Path(__file__).resolve().parents[3] |
| 26 | + / "nemo_deploy" |
| 27 | + / "multimodal" |
| 28 | + / "image_url_validator.py" |
| 29 | +) |
| 30 | +_spec = importlib.util.spec_from_file_location("image_url_validator", _validator_path) |
| 31 | +_mod = importlib.util.module_from_spec(_spec) |
| 32 | +_spec.loader.exec_module(_mod) |
| 33 | +validate_image_url = _mod.validate_image_url |
| 34 | + |
| 35 | + |
| 36 | +def _mock_resolve(ip_str): |
| 37 | + """Return a patch for socket.gethostbyname that always resolves to ip_str.""" |
| 38 | + return patch.object(_mod.socket, "gethostbyname", return_value=ip_str) |
| 39 | + |
| 40 | + |
| 41 | +class TestBlockedSchemes: |
| 42 | + def test_file_scheme_rejected(self): |
| 43 | + with pytest.raises(ValueError, match="scheme"): |
| 44 | + validate_image_url("file:///etc/passwd") |
| 45 | + |
| 46 | + def test_ftp_scheme_rejected(self): |
| 47 | + with pytest.raises(ValueError, match="scheme"): |
| 48 | + validate_image_url("ftp://example.com/img.png") |
| 49 | + |
| 50 | + def test_no_scheme_rejected(self): |
| 51 | + with pytest.raises(ValueError, match="scheme"): |
| 52 | + validate_image_url("example.com/img.png") |
| 53 | + |
| 54 | + |
| 55 | +class TestBlockedRanges: |
| 56 | + def test_loopback_ipv4_rejected(self): |
| 57 | + with _mock_resolve("127.0.0.1"): |
| 58 | + with pytest.raises(ValueError, match="blocked address"): |
| 59 | + validate_image_url("http://localhost/img.jpg") |
| 60 | + |
| 61 | + def test_loopback_other_subnet_rejected(self): |
| 62 | + with _mock_resolve("127.1.2.3"): |
| 63 | + with pytest.raises(ValueError, match="blocked address"): |
| 64 | + validate_image_url("http://internal.local/img.jpg") |
| 65 | + |
| 66 | + def test_cloud_imds_rejected(self): |
| 67 | + # 169.254.169.254 is the AWS/GCP/Azure metadata service |
| 68 | + with _mock_resolve("169.254.169.254"): |
| 69 | + with pytest.raises(ValueError, match="blocked address"): |
| 70 | + validate_image_url("http://169.254.169.254/latest/meta-data/") |
| 71 | + |
| 72 | + def test_link_local_rejected(self): |
| 73 | + with _mock_resolve("169.254.0.1"): |
| 74 | + with pytest.raises(ValueError, match="blocked address"): |
| 75 | + validate_image_url("http://169.254.0.1/img.jpg") |
| 76 | + |
| 77 | + def test_rfc1918_10_rejected(self): |
| 78 | + with _mock_resolve("10.0.0.1"): |
| 79 | + with pytest.raises(ValueError, match="blocked address"): |
| 80 | + validate_image_url("http://internal.corp/img.jpg") |
| 81 | + |
| 82 | + def test_rfc1918_172_rejected(self): |
| 83 | + with _mock_resolve("172.16.0.1"): |
| 84 | + with pytest.raises(ValueError, match="blocked address"): |
| 85 | + validate_image_url("http://internal.corp/img.jpg") |
| 86 | + |
| 87 | + def test_rfc1918_192_rejected(self): |
| 88 | + with _mock_resolve("192.168.1.1"): |
| 89 | + with pytest.raises(ValueError, match="blocked address"): |
| 90 | + validate_image_url("http://192.168.1.1/img.jpg") |
| 91 | + |
| 92 | + |
| 93 | +class TestAllowedUrls: |
| 94 | + def test_public_https_allowed(self): |
| 95 | + with _mock_resolve("93.184.216.34"): # example.com |
| 96 | + validate_image_url("https://example.com/image.jpg") # must not raise |
| 97 | + |
| 98 | + def test_public_http_allowed(self): |
| 99 | + with _mock_resolve("1.2.3.4"): |
| 100 | + validate_image_url("http://cdn.example.com/image.png") # must not raise |
| 101 | + |
| 102 | + |
| 103 | +class TestNoHostname: |
| 104 | + def test_url_without_hostname_rejected(self): |
| 105 | + with pytest.raises(ValueError, match="hostname"): |
| 106 | + validate_image_url("http:///image.jpg") |
| 107 | + |
| 108 | + def test_dns_failure_rejected(self): |
| 109 | + with patch.object( |
| 110 | + _mod.socket, |
| 111 | + "gethostbyname", |
| 112 | + side_effect=socket.gaierror("Name or service not known"), |
| 113 | + ): |
| 114 | + with pytest.raises(ValueError, match="Cannot resolve"): |
| 115 | + validate_image_url("http://nonexistent.invalid/img.jpg") |
0 commit comments