🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!
Security: reject NAT64 and translation-embedded private addresses, bound the photo fetch (SSRF-02, SSRF-03)
Omega (QA)8db8cdcf6541
diff --git a/backend/warranties_routes.py b/backend/warranties_routes.pyindex b6ee441..5a9f2f8 100644--- a/backend/warranties_routes.py+++ b/backend/warranties_routes.py@@ -13,6 +13,8 @@ import mimetypesfrom flask import Responseimport uuidimport socket+import threading+import timeimport ipaddressimport urllib.parseimport requests@@ -120,6 +122,37 @@ def unique_upload_filename(original_name, infix=""):stamp = datetime.now().strftime('%Y%m%d%H%M%S')return f"{stamp}_{uuid.uuid4().hex[:8]}_{infix}{safe}"+# NAT64 well-known prefix (RFC 6052): the low 32 bits embed an IPv4+# address, so 64:ff9b::/96 can smuggle a private IPv4 past is_global.+_NAT64_WELL_KNOWN_PREFIX = ipaddress.ip_network('64:ff9b::/96')+++def _ip_address_is_public(addr):+ """True when an address is a genuine public destination (SSRF-02).++ ipaddress.is_global alone is not enough: on the interpreters tested+ it reports True for the NAT64 well-known prefix embedding a private+ IPv4 (64:ff9b::a9fe:a9fe, the cloud metadata address, passes). The+ NAT64 prefix is rejected wholesale, and translation addresses+ (IPv4-mapped, 6to4, Teredo) must pass both the wrapper check and+ the check on the IPv4 they embed. Nothing previously allowed+ becomes allowed; only translation tricks are removed.+ """+ if addr.version == 6:+ if addr in _NAT64_WELL_KNOWN_PREFIX:+ return False+ mapped = addr.ipv4_mapped+ if mapped is not None:+ return addr.is_global and mapped.is_global+ sixtofour = addr.sixtofour+ if sixtofour is not None:+ return addr.is_global and sixtofour.is_global+ teredo = addr.teredo+ if teredo is not None:+ return addr.is_global and teredo[0].is_global and teredo[1].is_global+ return addr.is_global++def _photo_url_host_is_public(hostname):"""SSRF guard for photo-by-URL: every address the host resolves to mustbe a public (global) IP. Skipped only when the deployment explicitly sets@@ -137,7 +170,7 @@ def _photo_url_host_is_public(hostname):addr = ipaddress.ip_address(info[4][0])except ValueError:return False- if not addr.is_global:+ if not _ip_address_is_public(addr):return Falsereturn True@@ -154,6 +187,27 @@ def _detect_image_extension(data):return '.webp'return None+def _interrupt_photo_fetch(resp):+ """Interrupt a blocked streamed read (SSRF-03). Closing the response+ does not wake a thread blocked in recv, and the pooled connection's+ sock is already detached by the time the response streams, so the+ socket is reached through the response's read buffer+ (raw._fp.fp.raw._sock). Shutting it down makes the blocked read+ return, so the deadline check fires. Best effort: any failure+ leaves the per-chunk deadline check and the per-read timeout as+ the backstop."""+ try:+ sock = resp.raw._fp.fp.raw._sock+ if sock is not None:+ sock.shutdown(socket.SHUT_RDWR)+ except Exception:+ pass+ try:+ resp.close()+ except Exception:+ pass++def download_product_photo(url):"""Fetch an image from a user-supplied URL for use as a product photo.Returns (bytes, extension). Raises ValueError with a user-facing message."""@@ -161,14 +215,22 @@ def download_product_photo(url):if not current:raise ValueError("Photo URL is empty.")max_bytes = current_app.config.get('MAX_CONTENT_LENGTH') or 32 * 1024 * 1024+ # SSRF-03: one total deadline for the whole fetch, redirects and body+ # reads included. The per-read timeout alone let a server dripping+ # one chunk every few seconds hold a fetch open indefinitely.+ total_timeout = current_app.config.get('PHOTO_FETCH_TIMEOUT_SECONDS') or 60+ deadline = time.monotonic() + total_timeoutfor _ in range(4):parsed = urllib.parse.urlparse(current)if parsed.scheme not in ('http', 'https') or not parsed.hostname:raise ValueError("Photo URL must be a valid http(s) URL.")if not _photo_url_host_is_public(parsed.hostname):raise ValueError("Photo URL must point to a public address.")+ if time.monotonic() >= deadline:+ raise ValueError("Timed out fetching the photo URL.")try:- resp = requests.get(current, timeout=10, stream=True, allow_redirects=False,+ resp = requests.get(current, timeout=min(10, deadline - time.monotonic()),+ stream=True, allow_redirects=False,headers={'User-Agent': 'Warracker photo fetch'})except requests.RequestException:raise ValueError("Could not fetch the photo URL.")@@ -183,14 +245,35 @@ def download_product_photo(url):resp.close()raise ValueError("Could not fetch the photo URL.")chunks, total = [], 0+ # A per-chunk deadline check alone does not bound a slow drip:+ # a read for a full chunk blocks until the chunk fills or the+ # connection closes, so the check may never run. A timer shuts+ # the socket down at the deadline, which unblocks the read;+ # the post-loop check then turns a shutdown-caused end of+ # stream into the timeout error instead of accepting bytes+ # that arrived after the deadline.+ timer = threading.Timer(+ max(0.0, deadline - time.monotonic()),+ _interrupt_photo_fetch, args=(resp,))+ timer.daemon = True+ timer.start()try:for chunk in resp.iter_content(chunk_size=65536):+ if time.monotonic() >= deadline:+ raise ValueError("Timed out fetching the photo URL.")total += len(chunk)if total > max_bytes:raise ValueError("Photo URL file is too large.")chunks.append(chunk)+ except requests.RequestException:+ if time.monotonic() >= deadline:+ raise ValueError("Timed out fetching the photo URL.")+ raise ValueError("Could not fetch the photo URL.")finally:+ timer.cancel()resp.close()+ if time.monotonic() >= deadline:+ raise ValueError("Timed out fetching the photo URL.")data = b''.join(chunks)ext = _detect_image_extension(data)if not ext:diff --git a/tests/test_photo_url.py b/tests/test_photo_url.pyindex 4a1c254..763ef12 100644--- a/tests/test_photo_url.py+++ b/tests/test_photo_url.py@@ -133,3 +133,105 @@ def test_update_with_photo_url(client, user_factory, warranty_factory,})assert r.status_code == 200, r.get_data(as_text=True)assert _photo_path(client, h, wid)+++# --- SSRF-02: translation addresses must not smuggle private IPv4s ---++def test_nat64_embedded_private_is_not_public():+ from backend.warranties_routes import _ip_address_is_public+ import ipaddress+ # The NAT64 well-known prefix embedding the cloud metadata address:+ # is_global reports True for it, the guard must not.+ assert not _ip_address_is_public(+ ipaddress.ip_address("64:ff9b::a9fe:a9fe"))+ # The whole prefix is rejected, even embedding a public IPv4.+ assert not _ip_address_is_public(+ ipaddress.ip_address("64:ff9b::808:808"))+++def test_translation_addresses_judged_by_embedded_ipv4():+ from backend.warranties_routes import _ip_address_is_public+ import ipaddress+ assert not _ip_address_is_public(+ ipaddress.ip_address("::ffff:10.0.0.1"))+ assert _ip_address_is_public(ipaddress.ip_address("::ffff:8.8.8.8"))+ # 6to4 and Teredo wrappers stay rejected (as before); a wrapper+ # embedding a private IPv4 is rejected on the embedded check too.+ assert not _ip_address_is_public(+ ipaddress.ip_address("2002:0a00:0001::"))+ assert not _ip_address_is_public(+ ipaddress.ip_address("2001:0000:4136:e378:8000:63bf:f5ff:fffe"))+++def test_plain_addresses_unchanged():+ from backend.warranties_routes import _ip_address_is_public+ import ipaddress+ assert _ip_address_is_public(ipaddress.ip_address("8.8.8.8"))+ assert not _ip_address_is_public(ipaddress.ip_address("10.0.0.1"))+ assert not _ip_address_is_public(ipaddress.ip_address("127.0.0.1"))+ assert not _ip_address_is_public(ipaddress.ip_address("169.254.169.254"))+ assert _ip_address_is_public(+ ipaddress.ip_address("2001:4860:4860::8888"))+++def test_guard_rejects_host_resolving_to_nat64(monkeypatch, app):+ """End to end through the resolver: a hostname whose only address is+ a NAT64-embedded private IPv4 is refused by the photo fetch."""+ import socket+ from backend.warranties_routes import download_product_photo+ real_getaddrinfo = socket.getaddrinfo++ def _fake_getaddrinfo(host, port, *args, **kwargs):+ if host == "nat64.example.invalid":+ return [(socket.AF_INET6, socket.SOCK_STREAM, 0, "",+ ("64:ff9b::a9fe:a9fe", 0, 0, 0))]+ return real_getaddrinfo(host, port, *args, **kwargs)++ monkeypatch.setattr(socket, "getaddrinfo", _fake_getaddrinfo)+ app.config["ALLOW_PRIVATE_PHOTO_URLS"] = False+ with app.app_context():+ with pytest.raises(ValueError, match="public address"):+ download_product_photo("http://nat64.example.invalid/photo.png")+++# --- SSRF-03: the fetch has a total deadline, not just a per-read timeout ---++class _DripHandler(BaseHTTPRequestHandler):+ protocol_version = "HTTP/1.0"++ def do_GET(self):+ self.send_response(200)+ self.send_header("Content-Type", "image/png")+ self.end_headers()+ try:+ for _ in range(100):+ self.wfile.write(b"\x89")+ self.wfile.flush()+ import time as _time+ _time.sleep(0.3)+ except (BrokenPipeError, ConnectionResetError):+ pass++ def log_message(self, *args):+ pass+++def test_slow_drip_fetch_times_out(app):+ import time+ from backend.warranties_routes import download_product_photo+ server = ThreadingHTTPServer(("127.0.0.1", 0), _DripHandler)+ thread = threading.Thread(target=server.serve_forever, daemon=True)+ thread.start()+ try:+ url = f"http://127.0.0.1:{server.server_address[1]}/drip.png"+ app.config["ALLOW_PRIVATE_PHOTO_URLS"] = True+ app.config["PHOTO_FETCH_TIMEOUT_SECONDS"] = 1+ start = time.monotonic()+ with app.app_context():+ with pytest.raises(ValueError, match="Timed out"):+ download_product_photo(url)+ assert time.monotonic() - start < 10+ finally:+ app.config.pop("PHOTO_FETCH_TIMEOUT_SECONDS", None)+ app.config["ALLOW_PRIVATE_PHOTO_URLS"] = False+ server.shutdown()