Additional hardening of fetching remote URLs (#13306)

This commit is contained in:
Oliver authored and GitHub committed 2026-11-17 21:35:02 +11:00
1 parent 94851144a4
commit b851402f8f
2 files changed
+64 -2

No files matched your search

@@ -94,9 +94,27 @@ _real_getaddrinfo = socket.getaddrinfo
def _is_unsafe_ip(host: str) -> bool:
"""Return True if the given (already-resolved) address string is private/reserved."""
"""Return True if the given (already-resolved) address string is not safe to connect to.
Any address which is not globally routable is treated as unsafe. This covers
ranges such as 100.64.0.0/10 (RFC 6598 shared address space, used for carrier-grade
NAT, Tailscale and some cloud metadata services) which are neither "private" nor
"reserved" according to the ipaddress module.
The explicit checks are retained in addition to `is_global`, as some special-use
ranges (e.g. the NAT64 prefix 64:ff9b::/96, which can embed loopback or link-local
IPv4 addresses) are reported as global but are still caught by `is_reserved`.
"""
ip = ipaddress.ip_address(host)
return ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved
return (
not ip.is_global
or ip.is_private
or ip.is_loopback
or ip.is_link_local
or ip.is_reserved
or ip.is_multicast
or ip.is_unspecified
)
def _guarded_getaddrinfo(host, *args, **kwargs):
+44
View File
@@ -815,6 +815,50 @@ class TestHelpers(TestCase):
# Both the validation-time and connection-time lookups must have happened.
self.assertEqual(calls['n'], 2)
def test_unsafe_ip_ranges(self):
"""Non-globally-routable and special-use addresses must be treated as unsafe."""
unsafe = [
'127.0.0.1',
'10.0.0.1',
'172.16.0.1',
'192.168.1.1',
'169.254.169.254',
'0.0.0.0',
# RFC 6598 shared address space (CGNAT / Tailscale / Alibaba Cloud metadata)
'100.64.0.1',
'100.100.100.200',
'100.127.255.255',
'::ffff:100.64.0.1',
# Multicast
'224.0.0.1',
# IPv6
'::1',
'::',
'fe80::1',
'fd00:ec2::254',
'::ffff:127.0.0.1',
# NAT64 embeddings of link-local / loopback (reported as global by Python)
'64:ff9b::a9fe:a9fe',
'64:ff9b::7f00:1',
]
safe = ['8.8.8.8', '93.184.216.34', '100.63.255.255', '100.128.0.1']
for addr in unsafe:
with self.subTest(addr=addr):
self.assertTrue(InvenTree.helpers_model._is_unsafe_ip(addr))
for addr in safe:
with self.subTest(addr=addr):
self.assertFalse(InvenTree.helpers_model._is_unsafe_ip(addr))
def test_validate_url_cgnat_blocked(self):
"""A URL resolving into 100.64.0.0/10 must be rejected by the SSRF validator."""
for addr in ['100.64.0.10', '100.100.100.200']:
with self.subTest(addr=addr):
with self.assertRaises(ValueError):
InvenTree.helpers_model.validate_url_no_ssrf(f'http://{addr}/x.png')
def test_model_mixin(self):
"""Test the getModelsWithMixin function."""
from InvenTree.models import InvenTreeBarcodeMixin