Pin feed connections to the address that was validated (R2-S2, #34)

The SSRF guard checked that a hostname resolved to public addresses,
then let requests resolve it again to connect — so a DNS answer that
changed in between (DNS rebinding) could point the connection at an
internal address.

Connections now go through a pinned adapter whose socket factory
resolves, checks and connects inside one function: whatever the name
resolves to at connect time is what gets validated and used. Only the
socket is replaced, so TLS still verifies against the original
hostname. The up-front validate_fetch_url check stays as defense in
depth. Tests cover a rebinding answer at connect time being refused and
a real loopback round trip with the original Host header preserved;
checked live: a real HTTPS feed fetches, 127.0.0.1/169.254.169.254/
localhost are blocked, and a wrong-hostname certificate is still
rejected.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5.5 committed 2026-11-01 12:00:03 -07:00
1 parent 10ba491e10
commit 2e1c0e31a8
2 files changed
+163 -10

No files matched your search

+92 -10
View File
@@ -14,15 +14,15 @@ globally routable address (ip.is_global) — covering loopback, RFC1918
private ranges, link-local (including the 169.254.169.254 cloud
metadata address), and so on, for both IPv4 and IPv6.
What this does *not* close: the TCP connection isn't pinned to the
specific IP that was validated, so a DNS answer that changes between
the validation lookup and the actual connect (classic DNS rebinding)
could still slip through. Closing that fully needs a custom transport
that connects to the exact validated IP while still verifying TLS
against the original hostname — more machinery than this self-hosted
app's threat model currently justifies; this stops the much more
common case of a feed URL (or a redirect) simply pointing straight at
an internal address.
The connection is pinned too (R2-S2): the address is resolved, checked and
connected to inside one function (_connect_validated, used by the adapter
below as the connection's socket factory), so a DNS answer that changes
between a separate validation lookup and the connect (DNS rebinding) can't
steer the connection to an internal address — whatever it resolves to *at
connect time* is what gets validated. TLS still verifies against the
original hostname, because only the socket is replaced, not the host the
TLS layer is told about. The up-front validate_fetch_url check stays as
defense in depth and gives a clear error before any connection attempt.
"""
from __future__ import annotations
@@ -33,7 +33,10 @@ from dataclasses import dataclass
from urllib.parse import urljoin, urlparse
import requests
from requests.adapters import HTTPAdapter
from requests.structures import CaseInsensitiveDict
from urllib3.connection import HTTPConnection, HTTPSConnection
from urllib3.connectionpool import HTTPConnectionPool, HTTPSConnectionPool
ALLOWED_SCHEMES = {"http", "https"}
MAX_REDIRECTS = 5
@@ -55,6 +58,10 @@ class BoundedResponse:
content: bytes
def _is_allowed_ip(ip) -> bool:
return ip.is_global
def validate_fetch_url(url: str) -> None:
"""Raises SSRFBlocked unless `url` has an http(s) scheme, a hostname,
and every address that hostname resolves to is globally routable."""
@@ -71,10 +78,82 @@ def validate_fetch_url(url: str) -> None:
for *_, sockaddr in addrinfo:
ip = ipaddress.ip_address(sockaddr[0])
if not ip.is_global:
if not _is_allowed_ip(ip):
raise SSRFBlocked(f"{parsed.hostname!r} resolves to a non-public address: {ip}")
def _connect_validated(host, port, timeout, source_address, socket_options):
"""Resolves `host`, requires every address to be globally routable, and
connects to one of those exact addresses — the same lookup result is
both validated and used, so there is nothing for a rebinding DNS
answer to change in between."""
try:
addrinfo = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM)
except OSError as exc:
raise SSRFBlocked(f"Could not resolve {host!r}: {exc}") from exc
candidates = []
for family, socktype, proto, _canonname, sockaddr in addrinfo:
ip = ipaddress.ip_address(sockaddr[0])
if not _is_allowed_ip(ip):
raise SSRFBlocked(f"{host!r} resolves to a non-public address: {ip}")
candidates.append((family, socktype, proto, sockaddr))
if not candidates:
raise SSRFBlocked(f"{host!r} did not resolve to any address")
last_error: OSError | None = None
for family, socktype, proto, sockaddr in candidates:
sock = None
try:
sock = socket.socket(family, socktype, proto)
for level, option, value in socket_options or ():
sock.setsockopt(level, option, value)
if timeout is not socket._GLOBAL_DEFAULT_TIMEOUT:
sock.settimeout(timeout)
if source_address:
sock.bind(source_address)
sock.connect(sockaddr)
return sock
except OSError as exc:
last_error = exc
if sock is not None:
sock.close()
raise last_error or OSError(f"Could not connect to {host!r}")
class _PinnedHTTPConnection(HTTPConnection):
def _new_conn(self):
return _connect_validated(
self._dns_host, self.port, self.timeout, self.source_address, self.socket_options
)
class _PinnedHTTPSConnection(HTTPSConnection):
def _new_conn(self):
return _connect_validated(
self._dns_host, self.port, self.timeout, self.source_address, self.socket_options
)
class _PinnedHTTPConnectionPool(HTTPConnectionPool):
ConnectionCls = _PinnedHTTPConnection
class _PinnedHTTPSConnectionPool(HTTPSConnectionPool):
ConnectionCls = _PinnedHTTPSConnection
class PinnedAdapter(HTTPAdapter):
"""requests adapter whose connections go through _connect_validated."""
def init_poolmanager(self, *args, **kwargs):
super().init_poolmanager(*args, **kwargs)
self.poolmanager.pool_classes_by_scheme = {
"http": _PinnedHTTPConnectionPool,
"https": _PinnedHTTPSConnectionPool,
}
def safe_get(url: str, *, headers: dict, timeout: float) -> BoundedResponse:
"""GETs `url` the way fetch_and_ingest needs it: every redirect hop is
validated and followed manually (never left to requests/urllib3, so
@@ -84,6 +163,9 @@ def safe_get(url: str, *, headers: dict, timeout: float) -> BoundedResponse:
"""
current_url = url
with requests.Session() as session:
adapter = PinnedAdapter()
session.mount("http://", adapter)
session.mount("https://", adapter)
for _ in range(MAX_REDIRECTS + 1):
validate_fetch_url(current_url)
response = session.get(