|
| 1 | +import pytest |
| 2 | + |
| 3 | +from mindee.error.mindee_error import MindeeSourceError |
| 4 | +from mindee.mindee_http.response_validation import validate_url_for_source |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.v1 |
| 8 | +class TestValidateUrlScheme: |
| 9 | + def test_rejects_http(self): |
| 10 | + with pytest.raises(MindeeSourceError, match="HTTPS"): |
| 11 | + validate_url_for_source("http://example.com/file.pdf") |
| 12 | + |
| 13 | + def test_rejects_ftp(self): |
| 14 | + with pytest.raises(MindeeSourceError, match="HTTPS"): |
| 15 | + validate_url_for_source("ftp://example.com/file.pdf") |
| 16 | + |
| 17 | + def test_accepts_https(self): |
| 18 | + validate_url_for_source("https://example.com/file.pdf") |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.v1 |
| 22 | +class TestValidateUrlUserinfo: |
| 23 | + def test_rejects_username_and_password(self): |
| 24 | + with pytest.raises(MindeeSourceError, match="credentials"): |
| 25 | + validate_url_for_source("https://user:pass@example.com/file.pdf") |
| 26 | + |
| 27 | + def test_rejects_username_only(self): |
| 28 | + with pytest.raises(MindeeSourceError, match="credentials"): |
| 29 | + validate_url_for_source("https://user@example.com/file.pdf") |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.v1 |
| 33 | +class TestValidateUrlLoopbackHostnames: |
| 34 | + def test_rejects_localhost(self): |
| 35 | + with pytest.raises(MindeeSourceError, match="Loopback"): |
| 36 | + validate_url_for_source("https://localhost/file.pdf") |
| 37 | + |
| 38 | + def test_rejects_localhost_subdomain(self): |
| 39 | + with pytest.raises(MindeeSourceError, match="Loopback"): |
| 40 | + validate_url_for_source("https://myapp.localhost/file.pdf") |
| 41 | + |
| 42 | + def test_rejects_ip6_localhost(self): |
| 43 | + with pytest.raises(MindeeSourceError, match="Loopback"): |
| 44 | + validate_url_for_source("https://ip6-localhost/file.pdf") |
| 45 | + |
| 46 | + def test_rejects_ip6_loopback(self): |
| 47 | + with pytest.raises(MindeeSourceError, match="Loopback"): |
| 48 | + validate_url_for_source("https://ip6-loopback/file.pdf") |
| 49 | + |
| 50 | + |
| 51 | +@pytest.mark.v1 |
| 52 | +class TestValidateUrlLoopbackIPs: |
| 53 | + def test_rejects_ipv4_loopback(self): |
| 54 | + with pytest.raises(MindeeSourceError, match="disallowed"): |
| 55 | + validate_url_for_source("https://127.0.0.1/file.pdf") |
| 56 | + |
| 57 | + def test_rejects_ipv4_loopback_other(self): |
| 58 | + with pytest.raises(MindeeSourceError, match="disallowed"): |
| 59 | + validate_url_for_source("https://127.0.0.2/file.pdf") |
| 60 | + |
| 61 | + def test_rejects_ipv6_loopback(self): |
| 62 | + with pytest.raises(MindeeSourceError, match="disallowed"): |
| 63 | + validate_url_for_source("https://[::1]/file.pdf") |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.v1 |
| 67 | +class TestValidateUrlPrivateIPs: |
| 68 | + def test_rejects_rfc1918_10_block(self): |
| 69 | + with pytest.raises(MindeeSourceError, match="disallowed"): |
| 70 | + validate_url_for_source("https://10.0.0.1/file.pdf") |
| 71 | + |
| 72 | + def test_rejects_rfc1918_172_block(self): |
| 73 | + with pytest.raises(MindeeSourceError, match="disallowed"): |
| 74 | + validate_url_for_source("https://172.16.0.1/file.pdf") |
| 75 | + |
| 76 | + def test_rejects_rfc1918_192_block(self): |
| 77 | + with pytest.raises(MindeeSourceError, match="disallowed"): |
| 78 | + validate_url_for_source("https://192.168.1.1/file.pdf") |
| 79 | + |
| 80 | + def test_rejects_link_local(self): |
| 81 | + with pytest.raises(MindeeSourceError, match="disallowed"): |
| 82 | + validate_url_for_source("https://169.254.0.1/file.pdf") |
| 83 | + |
| 84 | + def test_rejects_unspecified(self): |
| 85 | + with pytest.raises(MindeeSourceError, match="disallowed"): |
| 86 | + validate_url_for_source("https://0.0.0.0/file.pdf") |
| 87 | + |
| 88 | + def test_rejects_multicast(self): |
| 89 | + with pytest.raises(MindeeSourceError, match="disallowed"): |
| 90 | + validate_url_for_source("https://224.0.0.1/file.pdf") |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.v1 |
| 94 | +class TestValidateUrlCgnat: |
| 95 | + def test_rejects_cgnat_start(self): |
| 96 | + with pytest.raises(MindeeSourceError, match="disallowed"): |
| 97 | + validate_url_for_source("https://100.64.0.1/file.pdf") |
| 98 | + |
| 99 | + def test_rejects_cgnat_end(self): |
| 100 | + with pytest.raises(MindeeSourceError, match="disallowed"): |
| 101 | + validate_url_for_source("https://100.127.255.255/file.pdf") |
| 102 | + |
| 103 | + def test_accepts_just_outside_cgnat(self): |
| 104 | + # 100.128.0.1 is outside 100.64.0.0/10 |
| 105 | + validate_url_for_source("https://100.128.0.1/file.pdf") |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.v1 |
| 109 | +class TestValidateUrlIpv6UniqueLocal: |
| 110 | + def test_rejects_ipv6_ula_fc(self): |
| 111 | + with pytest.raises(MindeeSourceError, match="disallowed"): |
| 112 | + validate_url_for_source("https://[fc00::1]/file.pdf") |
| 113 | + |
| 114 | + def test_rejects_ipv6_ula_fd(self): |
| 115 | + with pytest.raises(MindeeSourceError, match="disallowed"): |
| 116 | + validate_url_for_source("https://[fd00::1]/file.pdf") |
0 commit comments