1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# 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)),
)
|