Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

harden: reject private hosts in is_downloadable_domain, anchor the PubMed rule (#5692)

LearningCircuita21af1d4eece
diff --git a/changelog.d/+downloadable-domain-private-hosts.bugfix.md b/changelog.d/+downloadable-domain-private-hosts.bugfix.md
new file mode 100644
index 0000000000..9be5aa151e
--- /dev/null
+++ b/changelog.d/+downloadable-domain-private-hosts.bugfix.md
@@ -0,0 +1 @@
+The Research Library now rejects recognized private, local and CGNAT host literals before considering PDF-shaped URLs. For that private-host check only, the relevance filter normalizes IDNA hostnames, trailing dots, percent-encoded and legacy IPv4 spellings, and IPv4-mapped IPv6, without resolving DNS; the allowlist match itself stays an exact/dot-suffix comparison against the literal hostname. PubMed hostname lookalikes no longer qualify by substring; `pubmed.gov` and `pubmedcentral.nih.gov` are now explicit allowlist entries alongside `pubmed.ncbi.nlm.nih.gov`, so genuine academic hosts, public PDF links and the existing PubMed-path heuristic remain supported. Download requests still require independent SSRF validation through the validated HTTP transports.
diff --git a/src/local_deep_research/research_library/utils/__init__.py b/src/local_deep_research/research_library/utils/__init__.py
index 9415a76d7f..dad599245b 100644
--- a/src/local_deep_research/research_library/utils/__init__.py
+++ b/src/local_deep_research/research_library/utils/__init__.py
@@ -1,18 +1,22 @@
"""Shared utility functions for the Research Library."""
import hashlib
+import ipaddress
import os
+import socket
import subprocess
import sys
from pathlib import Path
from typing import Optional
-from urllib.parse import urlparse
+from urllib.parse import unquote, urlparse
from fastapi.responses import JSONResponse
from loguru import logger
from ...config.paths import get_library_directory
from ...database.models.library import Document, DocumentCollection
+from ...security.ip_ranges import PRIVATE_IP_RANGES
+from ...security.network_utils import is_private_ip
from ...security.path_validator import PathValidator
@@ -36,27 +40,93 @@ def escape_like(text: str) -> str:
)
+def _decode_ipv4_literal(hostname: str) -> Optional[str]:
+ """Decode an ASCII IPv4 literal using the platform's ``inet_aton``.
+
+ This handles decimal, hexadecimal, octal and shortened dotted forms
+ such as ``2130706433``, ``0x7f000001``, ``0177.0.0.1`` and ``127.1``.
+ It performs no DNS lookup and returns None when parsing fails.
+ Callers normalize IDNA hostnames before invoking this helper.
+ """
+ if not hostname:
+ return None
+ try:
+ packed = socket.inet_aton(hostname)
+ except OSError:
+ return None
+ return socket.inet_ntoa(packed)
+
+
+def _is_blocked_private_host(hostname: str) -> bool:
+ """Identify private, local and CGNAT hosts without resolving DNS.
+
+ IDNA normalization covers Unicode numeric labels and dot separators.
+ Trailing dots and legacy IPv4 literals are handled here without
+ changing the shared ``is_private_ip`` helper — and this normalization
+ (including the trailing-dot strip) applies only to this private-host
+ check; it never feeds the allowlist match in ``is_downloadable_domain``,
+ which stays an exact/dot-suffix comparison against the literal hostname
+ (see ``test_trailing_dot_fqdn_is_refused``). The hostname is also
+ percent-decoded one pass via ``urllib.parse.unquote`` before any of the
+ above, so an encoded numeric IPv4 disguise such as ``%31%32%37.0.0.1``
+ (``127.0.0.1``) is normalized to the same candidate a plain literal
+ would produce, rather than sailing past every subsequent check as an
+ opaque, non-matching hostname string. Each parsed candidate — and, for
+ the IPv4-mapped form, its embedded IPv4 — is checked against the shared
+ ``security.ip_ranges.PRIVATE_IP_RANGES`` recognition table (the same
+ table the fetch chokepoint uses), so every wrapped IPv6 spelling of a
+ private destination that the table knows (6to4, NAT64, IPv4-compatible,
+ IPv4-translated, ...) is rejected here too, not just the CGNAT range
+ behind the ``ipv4_mapped`` unwrap.
+ """
+ normalized = unquote(hostname).rstrip(".")
+ try:
+ normalized = normalized.encode("idna").decode("ascii").rstrip(".")
+ except UnicodeError:
+ pass
+ candidates = [normalized]
+ decoded = _decode_ipv4_literal(candidates[0])
+ if decoded is not None:
+ candidates.append(decoded)
+
+ for candidate in candidates:
+ if is_private_ip(candidate):
+ return True
+ try:
+ address = ipaddress.ip_address(candidate)
+ except ValueError:
+ continue
+ addresses = [address]
+ if isinstance(address, ipaddress.IPv6Address):
+ mapped = address.ipv4_mapped
+ if mapped is not None:
+ addresses.append(mapped)
+ if any(
+ addr in network
+ for addr in addresses
+ for network in PRIVATE_IP_RANGES
+ ):
+ return True
+ return False
+
+
def is_downloadable_domain(url: str) -> bool:
- """Check if URL is from a downloadable academic domain using proper URL parsing.
-
- **This is a relevance filter, NOT the SSRF control.** It answers "is this
- worth trying to download", not "is this safe to fetch". The security gate is
- ``security.safe_requests.safe_get``, which every outbound fetch in
- ``research_library/services/download_service.py`` goes through: it calls
- ``ssrf_validator.validate_url()``, raises on failure, re-validates after
- redirects, and always blocks cloud-metadata addresses
- (``ssrf_validator.ALWAYS_BLOCKED_METADATA_IPS``) even when private IPs are
- permitted. A resource URL also cannot enter the system unchecked —
- ``web/routers/api.py::api_add_resource`` validates before inserting a row.
-
- Saying so explicitly because the shape of this function invites the opposite
- reading, and a 2026-08 security audit made exactly that mistake: the
- PDF-shaped short-circuits below return True *before* the hostname allowlist
- is consulted, which looks like an allowlist bypass. It is untidy — a
- PDF-shaped URL does pass this filter whatever its host — but it grants no
- reach, because nothing downstream treats a True from here as permission to
- fetch. If you are adding a new outbound fetch, do NOT rely on this function
- for safety; call ``safe_get``.
+ """Identify likely academic resources and direct PDF links.
+
+ This is a relevance filter. Private IP literals, local names and CGNAT
+ are rejected before the academic-domain, PDF and PubMed-path heuristics.
+ Literal normalization (IDNA, trailing dots, legacy/percent-encoded IPv4,
+ IPv4-mapped IPv6) is best-effort, does not resolve DNS, and applies only
+ to that private-host rejection step — the allowlist match below is an
+ exact/dot-suffix comparison against the literal (non-normalized)
+ hostname, so a public hostname that resolves to a private address can
+ still pass, and a trailing-dot FQDN of an allowlisted domain is still
+ refused (see ``test_trailing_dot_fqdn_is_refused``).
+
+ Fetches must independently use validated transports such as
+ ``security.safe_requests.SafeSession`` or ``safe_get``. Those transports
+ use ``security.ssrf_validator`` for address and redirect validation;
+ a True result here is never permission to bypass those checks.
"""
try:
if not url:
@@ -67,6 +137,11 @@ def is_downloadable_domain(url: str) -> bool:
path = parsed.path or ""
query = parsed.query or ""
+ # Reject recognized private/local literals before any heuristic
+ # can accept them. DNS and transport validation happen at fetch time.
+ if _is_blocked_private_host(hostname):
+ return False
+
# Check for direct PDF files
if path.endswith(".pdf") or ".pdf?" in url.lower():
return True
@@ -78,6 +153,8 @@ def is_downloadable_domain(url: str) -> bool:
"medrxiv.org",
"ncbi.nlm.nih.gov",
"pubmed.ncbi.nlm.nih.gov",
+ "pubmed.gov",
+ "pubmedcentral.nih.gov",
"europepmc.org",
"semanticscholar.org",
"researchgate.net",
@@ -133,8 +210,9 @@ def is_downloadable_domain(url: str) -> bool:
if hostname == domain or hostname.endswith("." + domain):
return True
- # Special case for PubMed which might appear in path
- if "pubmed" in hostname or "/pubmed/" in path:
+ # Preserve the host-independent PubMed-path heuristic. Real PubMed
+ # hosts match the allowlist above; hostname substrings do not qualify.
+ if "/pubmed/" in path:
return True
# Check for PDF in path or query parameters
diff --git a/src/local_deep_research/security/ip_ranges.py b/src/local_deep_research/security/ip_ranges.py
index 1728d25409..c1e24468e8 100644
--- a/src/local_deep_research/security/ip_ranges.py
+++ b/src/local_deep_research/security/ip_ranges.py
@@ -7,6 +7,9 @@ duplicating IP range definitions.
import ipaddress
+# RFC 6598 shared address space, also used by download candidate filtering.
+CGNAT_NETWORK = ipaddress.ip_network("100.64.0.0/10")
+
# RFC1918 private networks + loopback + link-local + CGNAT + IPv6 equivalents
# nosec B104 - These hardcoded IPs are intentional for security validation
PRIVATE_IP_RANGES = [
@@ -15,9 +18,7 @@ PRIVATE_IP_RANGES = [
ipaddress.ip_network("10.0.0.0/8"), # RFC1918 Class A private
ipaddress.ip_network("172.16.0.0/12"), # RFC1918 Class B private
ipaddress.ip_network("192.168.0.0/16"), # RFC1918 Class C private
- ipaddress.ip_network(
- "100.64.0.0/10"
- ), # CGNAT - used by Podman/rootless containers
+ CGNAT_NETWORK, # CGNAT - used by Podman/rootless containers
ipaddress.ip_network("169.254.0.0/16"), # Link-local
ipaddress.ip_network("fe80::/10"), # IPv6 link-local
ipaddress.ip_network("fc00::/7"), # IPv6 unique local
diff --git a/tests/security/test_library_rag_security_fastapi.py b/tests/security/test_library_rag_security_fastapi.py
index 1d0ead026d..7237e8bc8f 100644
--- a/tests/security/test_library_rag_security_fastapi.py
+++ b/tests/security/test_library_rag_security_fastapi.py
@@ -54,11 +54,18 @@ COVERAGE AREA 5 -- ``research_library/utils/is_downloadable_domain``. It had no
``tests/web/routers/test_library_download_outcomes.py`` now cover it. It is
the
allowlist gate deciding which remote URLs
- the server will fetch (``web/routers/library.py:1345`` and ``:1568``), so
- it is SSRF-adjacent. The tests below pin the policy the implementation and
+ the server will fetch (``web/routers/library.py:879``, ``:1539`` and
+ ``:1766``), so it is SSRF-adjacent. The tests below pin the policy the implementation and
its docstring actually describe -- "academic domain OR direct PDF link" --
- and deliberately pin two CURRENT-BEHAVIOUR weaknesses rather than
- inventing a stricter policy (see ``TestIsDownloadableDomainKnownWeaknesses``).
+ and deliberately pin the remaining CURRENT-BEHAVIOUR weaknesses (plus the
+ regressions closed by ``harden: reject private hosts in
+ is_downloadable_domain, anchor the PubMed rule``) rather than inventing a
+ stricter policy (see ``TestIsDownloadableDomainKnownWeaknesses``).
+ ``TestIsDownloadableDomainPrivateHostNormalization`` separately pins the
+ call-site-only IP-literal normalization (trailing dot, decimal/hex,
+ shorthand-dotted, per-octet octal, RFC 6598 CGNAT) added on top of that
+ hardening -- see that class's docstring for why the shared
+ ``is_private_ip()`` helper itself is untouched.
Harness: the ``auth_client`` idiom from
``tests/web/routers/test_library_delete_hostile_input.py`` -- a real,
@@ -1012,8 +1019,8 @@ class TestSanitizedIndexingErrors:
class TestIsDownloadableDomainAllowlist:
"""``research_library/utils/is_downloadable_domain`` decides which remote
- URLs the server will fetch (``library.py:1345`` and the
- ``POST /library/api/download-source`` gate at ``:1568``). This class is the
+ URLs the server will fetch (``library.py:879`` and ``:1539``, and the
+ ``POST /library/api/download-source`` gate at ``:1766``). This class is the
direct policy regression evidence that was absent at the review snapshot.
The policy these tests pin is the one the implementation and the
@@ -1159,25 +1166,27 @@ class TestIsDownloadableDomainAllowlist:
class TestIsDownloadableDomainKnownWeaknesses:
"""CURRENT BEHAVIOUR, pinned deliberately -- these are NOT assertions that
- the behaviour is correct.
-
- The brief for this file is explicit: derive the intended policy from the
- implementation and its docstring, do not invent a stricter policy and then
- "fix" ``src/`` to match. Two properties below are real weaknesses of the
- gate. They are pinned so that (a) they are visible rather than silently
- assumed safe, and (b) any future hardening shows up as a deliberate,
- test-visible change. See the module docstring / the agent report for the
- write-up.
+ the remaining behaviour is correct.
+
+ Private-host PDF candidates and PubMed hostname lookalikes are rejected.
+ PDF-shaped URLs on public hosts and the host-independent PubMed-path
+ heuristic remain accepted for compatibility.
"""
- def test_any_url_whose_path_looks_like_a_pdf_bypasses_the_allowlist(self):
+ def test_pdf_shaped_paths_still_bypass_the_allowlist_for_public_hosts(
+ self,
+ ):
"""The ``.pdf`` / ``/pdf/`` / ``type=pdf`` / ``format=pdf`` checks run
- BEFORE the host allowlist and are host-independent. This is the
- documented "or is a direct PDF link" half of the policy
- (``is_downloadable_url``'s docstring), but it means the allowlist does
- not constrain the fetch target at all when the attacker controls the
- path or query -- e.g. an internal endpoint reached as
- ``http://127.0.0.1:8080/x?type=pdf``.
+ independently of the host allowlist: the direct-PDF check precedes
+ it, while the path/query checks follow it. They accept hosts that
+ pass the private/local exclusion. This remains the documented
+ "or is a direct PDF link" half of the policy (``is_downloadable_url``'s
+ docstring): the allowlist still does not constrain the fetch target
+ when the attacker controls the path or query on a public host --
+ e.g. ``https://evil.test/x?type=pdf``.
+
+ (Internal/metadata hosts are covered separately below -- those are
+ now rejected regardless of PDF shape.)
"""
from local_deep_research.research_library.utils import (
is_downloadable_domain,
@@ -1188,22 +1197,43 @@ class TestIsDownloadableDomainKnownWeaknesses:
"https://evil.test/pdf/anything",
"https://evil.test/x?type=pdf",
"https://evil.test/x?format=pdf",
+ ]:
+ assert is_downloadable_domain(url) is True, url
+
+ def test_private_host_pdf_shaped_paths_are_now_rejected(self):
+ """Regression pin for ``harden: reject private hosts in
+ is_downloadable_domain``. A private/loopback/link-local hostname is
+ now rejected FIRST, before any PDF-shape heuristic can short-circuit
+ past it -- closing the misleading appearance that the PDF checks
+ bypass the allowlist for internal or cloud-metadata targets such as
+ ``http://127.0.0.1:8080/x?type=pdf`` or
+ ``http://169.254.169.254/...?type=pdf``.
+ """
+ from local_deep_research.research_library.utils import (
+ is_downloadable_domain,
+ )
+
+ for url in [
"http://127.0.0.1:8080/internal?type=pdf",
"http://169.254.169.254/latest/meta-data/x?type=pdf",
]:
- assert is_downloadable_domain(url) is True, (
- f"{url}: if this now returns False the PDF-shape bypass was "
- "closed -- good; update this test and the report"
- )
+ assert is_downloadable_domain(url) is False, url
- def test_the_pubmed_check_is_an_unanchored_substring_match(self):
- """``if "pubmed" in hostname or "/pubmed/" in path`` is a plain
- substring test, unlike the exact/dot-suffix matching used for every
- entry in ``downloadable_domains``. Any attacker-registered host
- containing "pubmed" -- or any host at all serving a ``/pubmed/`` path
- -- is admitted. This is the widest hole in the gate and looks
- unintentional (``pubmed.ncbi.nlm.nih.gov`` is already in the
- allowlist, so the special case buys nothing legitimate).
+ def test_the_pubmed_hostname_check_is_now_anchored(self):
+ """Regression pin for ``harden: ... anchor the PubMed rule``. The
+ unanchored ``"pubmed" in hostname`` substring arm was removed, unlike
+ the exact/dot-suffix matching used for every entry in
+ ``downloadable_domains``. An attacker-registered host that merely
+ contains "pubmed" as a substring -- and is not itself allowlisted or
+ a dot-suffix match of an allowlisted domain -- is no longer admitted.
+
+ (The real PubMed domains are unaffected: they are already in the
+ allowlist above and matched there by exact/suffix comparison.)
+
+ The same anchoring applies to the ``pubmed.gov`` /
+ ``pubmedcentral.nih.gov`` entries added alongside this test: a host
+ that merely starts or ends with those strings -- without being an
+ exact match or dot-suffix -- does not qualify either.
"""
from local_deep_research.research_library.utils import (
is_downloadable_domain,
@@ -1212,16 +1242,300 @@ class TestIsDownloadableDomainKnownWeaknesses:
for url in [
"https://pubmed.evil.test/anything",
"https://evil-pubmed-mirror.test/x",
- "https://evil.test/pubmed/x",
+ "https://pubmed.gov.evil.test/anything",
+ "https://evilpubmed.gov/x",
+ "https://pubmedcentral.nih.gov.attacker.com/x",
]:
- assert is_downloadable_domain(url) is True, (
- f"{url}: if this now returns False the pubmed substring "
- "match was tightened -- good; update this test and the report"
- )
+ assert is_downloadable_domain(url) is False, url
+
+ def test_the_pubmed_path_check_remains_an_unanchored_substring_match(
+ self,
+ ):
+ """CURRENT BEHAVIOUR, pinned deliberately -- the hardening commit
+ intentionally kept the PATH arm of the PubMed special case
+ (``"/pubmed/" in path``), and it is still an unanchored substring
+ match: any public host serving a ``/pubmed/`` path is admitted. This
+ is narrower than the removed hostname arm (an attacker needs a path
+ segment, not just a hostname substring) but is still not
+ allowlist-anchored.
+ """
+ from local_deep_research.research_library.utils import (
+ is_downloadable_domain,
+ )
+
+ assert is_downloadable_domain("https://evil.test/pubmed/x") is True
+
+ def test_pubmed_and_pmc_real_host_forms_still_pass(self):
+ """The hardening commit removed only the unanchored ``"pubmed" in
+ hostname`` substring arm; the exact/dot-suffix allowlist match
+ above it (untouched by that removal) is what actually admits every
+ real PubMed/PMC/NCBI host, so none of them regressed.
+
+ ``pubmed.gov`` and ``pubmedcentral.nih.gov`` are the official
+ short-form PubMed/PMC hosts and are separate entries from
+ ``pubmed.ncbi.nlm.nih.gov`` / ``ncbi.nlm.nih.gov`` -- removing the
+ unanchored hostname substring arm dropped them (they are not a
+ dot-suffix of any allowlisted domain), so they are pinned here as
+ their own explicit allowlist entries rather than folded into the
+ broader ``nih.gov``.
+ """
+ from local_deep_research.research_library.utils import (
+ is_downloadable_domain,
+ )
+
+ for url in [
+ "https://pubmed.ncbi.nlm.nih.gov/12345678/",
+ "https://www.ncbi.nlm.nih.gov/pmc/articles/PMC1234567/",
+ "https://europepmc.org/article/MED/12345678",
+ "https://ftp.ncbi.nlm.nih.gov/pub/pmc/oa_bulk/",
+ "https://pubmed.gov/12345678",
+ "https://www.pubmed.gov/12345678",
+ "https://www.pubmedcentral.nih.gov/articlerender.fcgi?artid=123456",
+ ]:
+ assert is_downloadable_domain(url) is True, url
+
+
+class TestIsDownloadableDomainPrivateHostNormalization:
+ """Regression pins for the call-site-only normalization layered on top
+ of ``is_private_ip()`` inside ``is_downloadable_domain`` (trailing dot,
+ single-token decimal/hex IPv4, RFC 6598 CGNAT).
+
+ ``is_private_ip()`` itself is unmodified: it is a SHARED helper (also
+ used by ``utilities/url_utils.py`` for outbound-request scheme
+ selection and ``web/dependencies/rate_limit.py`` for the local-network
+ rate-limit exemption), so changing what it considers private is out of
+ scope for this filter and could change behaviour for those unrelated
+ callers. The normalization instead lives in
+ ``research_library/utils/__init__.py`` (``_decode_ipv4_literal``,
+ ``_is_blocked_private_host``), applied only at this call site.
+ """
+
+ def test_alternate_ip_encodings_of_a_private_host_are_now_rejected(self):
+ """``is_private_ip()`` only parses a hostname already written as a
+ dotted-quad / colon-hex literal, so a private-host URL that is
+ PDF-shaped used to sail straight past the guard (the PDF check
+ returns True before the allowlist, and the private-host check
+ could not recognise the host). These four textual variants of
+ ``127.0.0.1`` / ``192.168.1.10`` / ``printer.local`` -- all
+ resolved by glibc/requests to their private target -- are now
+ normalized and rejected.
+ """
+ from local_deep_research.research_library.utils import (
+ is_downloadable_domain,
+ )
+
+ for url in [
+ "http://2130706433/x.pdf", # decimal for 127.0.0.1
+ "http://0x7f000001/x.pdf", # hex for 127.0.0.1
+ "http://192.168.1.10./x.pdf", # trailing-dot RFC1918
+ "http://printer.local./x.pdf", # trailing-dot mDNS
+ ]:
+ assert is_downloadable_domain(url) is False, url
+
+ def test_idna_private_host_spellings_are_rejected(self):
+ """Normalize Unicode numeric labels and separators before filtering."""
+ from local_deep_research.research_library.utils import (
+ is_downloadable_domain,
+ )
+
+ for host in [
+ "127.0.0.1",
+ "127。0。0。1",
+ "127.0.0.1",
+ "127。0。0。1",
+ "2130706433",
+ "0x7f000001",
+ "printer。local。",
+ "100.64.0.1",
+ ]:
+ for suffix in ["/x.pdf", "/pdf/x", "/x?type=pdf", "/pubmed/x"]:
+ url = f"http://{host}{suffix}"
+ assert is_downloadable_domain(url) is False, url
+
+ for host in [
+ "8.8.8.8",
+ "100.63.255.255",
+ "bücher.example",
+ ]:
+ url = f"http://{host}/x.pdf"
+ assert is_downloadable_domain(url) is True, url
+
+ def test_mapped_ipv6_cgnat_hosts_are_rejected(self):
+ """Apply the CGNAT exclusion to the embedded IPv4 address."""
+ from local_deep_research.research_library.utils import (
+ is_downloadable_domain,
+ )
+
+ for host in [
+ "::ffff:100.64.0.0",
+ "::ffff:100.64.0.1",
+ "::ffff:100.127.255.255",
+ "::ffff:6440:1",
+ ]:
+ for suffix in ["/x.pdf", "/x?format=pdf", "/pubmed/x"]:
+ url = f"http://[{host}]{suffix}"
+ assert is_downloadable_domain(url) is False, url
+
+ for host in [
+ "::ffff:100.63.255.255",
+ "::ffff:100.128.0.0",
+ "::ffff:8.8.8.8",
+ ]:
+ url = f"http://[{host}]/x.pdf"
+ assert is_downloadable_domain(url) is True, url
+
+ def test_wrapped_ipv6_private_spellings_are_rejected(self):
+ """The candidate loop's ``ipv4_mapped`` unwrap used to feed ONLY the
+ CGNAT check, so IPv6-wrapped spellings of exactly the private
+ targets this filter claims to reject were admitted: the
+ IPv4-compatible (``::169.254.169.254``), IPv4-translated
+ (``::ffff:0:169.254.169.254``) and NAT64 (``64:ff9b::a9fe:a9fe``)
+ forms all parse to IPv6 addresses whose ``ipv4_mapped`` is None,
+ and ``is_private_ip`` does not classify the transition prefixes as
+ private. The shared recognition table
+ ``security.ip_ranges.PRIVATE_IP_RANGES`` -- already the source of
+ truth for the fetch-time chokepoint -- recognizes every one of
+ these as private, so the candidate loop now checks each parsed
+ candidate (and its mapped unwrap) against that table.
+ """
+ from local_deep_research.research_library.utils import (
+ is_downloadable_domain,
+ )
+
+ for host in [
+ "::169.254.169.254", # IPv4-compatible wrap of IMDS
+ "::ffff:0:169.254.169.254", # IPv4-translated wrap of IMDS
+ "64:ff9b::a9fe:a9fe", # NAT64 well-known prefix wrap of IMDS
+ "::127.0.0.1", # IPv4-compatible wrap of loopback
+ "::ffff:0:100.64.0.1", # translated wrap of the pinned CGNAT vector
+ ]:
+ for suffix in ["/x.pdf", "/latest/meta-data/x?type=pdf"]:
+ url = f"http://[{host}]{suffix}"
+ assert is_downloadable_domain(url) is False, url
+
+ # WHAT-HELD controls: public IPv6 literals (native and mapped) and
+ # the CGNAT boundary publics keep their exact head behaviour.
+ for url in [
+ "http://[2600::]/x.pdf",
+ "http://[::ffff:8.8.8.8]/x.pdf",
+ "http://100.63.255.255/x.pdf",
+ "http://100.128.0.1/x.pdf",
+ ]:
+ assert is_downloadable_domain(url) is True, url
+
+ def test_rfc6598_cgnat_hosts_are_now_rejected(self):
+ """``is_private_ip()`` does not classify ``100.64.0.0/10`` (RFC 6598
+ shared/CGNAT address space) as private; this module checks the
+ range itself at the call site, without touching the shared helper.
+ """
+ from local_deep_research.research_library.utils import (
+ is_downloadable_domain,
+ )
+
+ assert is_downloadable_domain("http://100.64.0.5/x.pdf") is False
+ # Sanity: just outside the /10 range is unaffected either way.
+ assert is_downloadable_domain("http://100.63.255.255/x.pdf") is True
+ assert is_downloadable_domain("http://100.128.0.1/x.pdf") is True
+
+ def test_dotted_ipv4_literal_shorthand_and_octal_are_now_rejected(self):
+ """Regression pin: the shorthand-dotted (``127.1``) and per-octet
+ octal (``0177.0.0.1``) gaps that ``test_is_downloadable_domain_known_
+ private_ip_gaps`` used to pin as open are now closed.
+ ``_decode_ipv4_literal`` decodes via ``socket.inet_aton`` (glibc's
+ own IPv4-literal grammar), not a hand-rolled single-token parser, so
+ it recognizes every dotted/shorthand/octal/hex form a resolver
+ does -- not just a bare integer with no dots.
+ """
+ from local_deep_research.research_library.utils import (
+ is_downloadable_domain,
+ )
+
+ for url in [
+ "http://127.1/x.pdf", # shorthand for 127.0.0.1
+ "http://0177.0.0.1/x.pdf", # per-octet octal for 127.0.0.1
+ ]:
+ assert is_downloadable_domain(url) is False, url
+
+ def test_single_token_leading_zero_octal_is_now_rejected(self):
+ """CONFIRMED bypass, now fixed: the previous decoder used
+ ``int(hostname, 0)`` -- Python integer-literal syntax -- which
+ raises ``ValueError`` on a single-token leading-zero octal string
+ like ``017700000001`` (Python requires an explicit ``0o`` prefix
+ for octal; a bare leading zero is a ``SyntaxError``/``ValueError``
+ in modern Python). glibc's ``inet_aton`` has no such requirement:
+ ``017700000001`` and ``0177`` are valid octal literals to it and
+ resolve to ``127.0.0.1`` / ``0.0.0.127`` respectively, so a URL
+ built from either sailed straight past the old guard. Verified via
+ ``socket.getaddrinfo`` that this is exactly what a real resolver
+ does with these strings too.
+ """
+ from local_deep_research.research_library.utils import (
+ is_downloadable_domain,
+ )
+
+ assert is_downloadable_domain("http://017700000001/x.pdf") is False
+ assert is_downloadable_domain("http://0177/x.pdf") is False
+
+ def test_python_literal_only_forms_are_no_longer_misdecoded(self):
+ """The old ``int(hostname, 0)`` decoder also over-decoded: Python
+ accepts ``0b``-binary and underscore-grouped digits as integer
+ literals, but no resolver treats either as an IP address --
+ ``socket.inet_aton`` (and ``socket.getaddrinfo``) reject both
+ outright. These hostnames both encode ``127.0.0.1`` under Python's
+ literal grammar, but since a resolver never decodes them that way,
+ they must NOT be treated as the private loopback address by this
+ filter either -- confirmed here via a PDF-shaped path, which would
+ be forced to False if (wrongly) decoded as private.
+ """
+ from local_deep_research.research_library.utils import (
+ is_downloadable_domain,
+ )
+
+ for url in [
+ # 0b01111111... is 127.0.0.1 in binary -- a Python int literal,
+ # not anything a resolver treats as an IPv4 address.
+ "http://0b01111111000000000000000000000001/x.pdf",
+ # Underscore-grouped decimal for 2130706433 (127.0.0.1) -- valid
+ # to Python's int(x, 0), meaningless to a resolver.
+ "http://2_130_706_433/x.pdf",
+ ]:
+ assert is_downloadable_domain(url) is True, url
+
+ def test_percent_encoded_private_hosts_are_now_rejected(self):
+ """CONFIRMED bypass, now fixed: the private-host check ran against
+ the raw, still-percent-encoded ``urlparse().hostname`` string, so an
+ encoded loopback/local host never matched any recognized literal
+ form and sailed straight past the guard as an opaque non-matching
+ hostname -- exactly the same way an unrecognized public hostname
+ would. ``%31%32%37.0.0.1`` percent-decodes to ``127.0.0.1``;
+ ``%6c%6f%63%61%6c%68%6f%73%74`` decodes to ``localhost``;
+ ``%31%30%2e%30%2e%30%2e%31`` decodes to ``10.0.0.1``. The hostname
+ is now percent-decoded (one pass, via ``urllib.parse.unquote``)
+ before IDNA normalization and the legacy-IPv4 decode, so these are
+ recognized the same as their plain-text spellings.
+ """
+ from local_deep_research.research_library.utils import (
+ is_downloadable_domain,
+ )
+
+ for host in [
+ "%31%32%37.0.0.1", # percent-encoded 127.0.0.1
+ "127.0.0.1%00", # trailing percent-encoded NUL after loopback
+ "127.0.0.1%2e", # percent-encoded trailing dot
+ "%6c%6f%63%61%6c%68%6f%73%74", # percent-encoded "localhost"
+ "%31%30%2e%30%2e%30%2e%31", # percent-encoded 10.0.0.1
+ ]:
+ for suffix in ["/x.pdf", "/pubmed/x", "/x?type=pdf"]:
+ url = f"http://{host}{suffix}"
+ assert is_downloadable_domain(url) is False, url
+
+ # WHAT-HELD control: an ordinary allowlisted host is unaffected by
+ # percent-decoding (there is nothing to decode).
+ assert is_downloadable_domain("https://arxiv.org/abs/1") is True
class TestDownloadSourceEnforcesTheAllowlist:
- """The HTTP enforcement point (``library.py:1568``). Pairs the refusal
+ """The HTTP enforcement point (``library.py:1766``). Pairs the refusal
with a positive control that an allowlisted URL gets PAST the gate --
otherwise a route that 400s on everything would satisfy the refusal test.
Neither case performs any network IO: the refusal returns before the