From b851402f8fe6fce46d1c32ead4ae1f24337331bc Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 17 Nov 2026 21:35:02 +1100 Subject: [PATCH] Additional hardening of fetching remote URLs (#13306) --- .../InvenTree/InvenTree/helpers_model.py | 22 +++++++++- src/backend/InvenTree/InvenTree/tests.py | 44 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/backend/InvenTree/InvenTree/helpers_model.py b/src/backend/InvenTree/InvenTree/helpers_model.py index e4f61b288e..c4cb852d0c 100644 --- a/src/backend/InvenTree/InvenTree/helpers_model.py +++ b/src/backend/InvenTree/InvenTree/helpers_model.py @@ -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): diff --git a/src/backend/InvenTree/InvenTree/tests.py b/src/backend/InvenTree/InvenTree/tests.py index cdf27a7024..298cddb3ef 100644 --- a/src/backend/InvenTree/InvenTree/tests.py +++ b/src/backend/InvenTree/InvenTree/tests.py @@ -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