# SPDX-FileCopyrightText: 2026 Dennis Fink # # SPDX-License-Identifier: BSD-3-Clause """HTTP transport that connects only to validated DNS answers. httpcore receives the original host for Host and TLS SNI/certificate checks, but the socket is opened to an IP from a single, bounded DNS lookup. This protects the receiver and sender, including redirects and advertised URLs. """ import ipaddress import socket import time from collections.abc import Iterable import httpcore import httpx from httpcore._backends.sync import SyncStream # httpcore 1.x socket implementation from .url_security import public_addresses class NetworkDeadline: """One monotonic deadline shared by DNS, TCP, TLS, and response streaming.""" def __init__(self, seconds: float) -> None: if seconds <= 0: raise ValueError("REQUEST_TOTAL_TIMEOUT must be greater than zero") self.expires = time.monotonic() + seconds def timeout(self, original: float | None, error: type[Exception]) -> float: remaining = self.expires - time.monotonic() if remaining <= 0: raise error("Overall HTTP request deadline exceeded") return min(remaining, original) if original is not None else remaining class DeadlineStream(httpcore.NetworkStream): """Clip every read/write/TLS operation to the remaining overall deadline.""" def __init__(self, stream: httpcore.NetworkStream, deadline: NetworkDeadline): self._stream = stream self._deadline = deadline def read(self, max_bytes: int, timeout: float | None = None) -> bytes: return self._stream.read( max_bytes, timeout=self._deadline.timeout(timeout, httpcore.ReadTimeout) ) def write(self, buffer: bytes, timeout: float | None = None) -> None: self._stream.write( buffer, timeout=self._deadline.timeout(timeout, httpcore.WriteTimeout) ) def start_tls( self, ssl_context, server_hostname: str | None = None, timeout: float | None = None, ) -> httpcore.NetworkStream: return DeadlineStream( self._stream.start_tls( ssl_context, server_hostname=server_hostname, timeout=self._deadline.timeout(timeout, httpcore.ConnectTimeout), ), self._deadline, ) def get_extra_info(self, info: str): return self._stream.get_extra_info(info) def close(self) -> None: self._stream.close() class PublicNetworkBackend(httpcore.SyncBackend): """Resolve and validate in the TCP connection operation, never in a hook.""" def __init__(self, deadline: NetworkDeadline): self._deadline = deadline def connect_tcp( self, host: str, port: int, timeout: float | None = None, local_address: str | None = None, socket_options: Iterable[tuple] | None = None, ) -> httpcore.NetworkStream: # No second lookup of `host` is permitted. Even a DNS response that # changes immediately after this check cannot change the socket peer. dns_timeout = self._deadline.timeout(timeout, httpcore.ConnectTimeout) addresses = public_addresses(host, lifetime=dns_timeout) last_error: httpcore.ConnectError | httpcore.ConnectTimeout | None = None for address in addresses: family = ( socket.AF_INET6 if ipaddress.ip_address(address).version == 6 else socket.AF_INET ) sock = socket.socket(family, socket.SOCK_STREAM) try: sock.settimeout( self._deadline.timeout(timeout, httpcore.ConnectTimeout) ) if local_address is not None: sock.bind((local_address, 0)) for option in socket_options or (): sock.setsockopt(*option) sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) # This numeric address is the *same* address just validated; # no getaddrinfo or DNS resolver runs between check and connect. sock.connect((address, port)) return DeadlineStream(SyncStream(sock), self._deadline) except (TimeoutError, OSError) as exc: sock.close() if isinstance(exc, TimeoutError): last_error = httpcore.ConnectTimeout(str(exc)) else: last_error = httpcore.ConnectError(str(exc)) except BaseException: sock.close() raise assert last_error is not None raise last_error class PublicHTTPTransport(httpx.HTTPTransport): """A direct (non-proxy) httpx transport with an IP-pinning network backend. httpx 0.28 does not expose httpcore's ``network_backend`` parameter, so we initialize its compatible pool directly. Pin the httpx/httpcore major APIs and keep this integration under test when upgrading either dependency. """ def __init__(self, total_timeout: float): self._pool = httpcore.ConnectionPool( ssl_context=httpx.create_ssl_context(verify=True, trust_env=False), network_backend=PublicNetworkBackend(NetworkDeadline(total_timeout)), )