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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause
"""Security regression tests for DNS-pinned network connections."""
import ipaddress
import socket
from http import HTTPStatus
from unittest.mock import Mock, PropertyMock, patch
import httpcore
import httpx
import pytest
from flask import Flask
from webmentions_ssg.net import (
AddressResolutionError,
DeadlineStream,
PublicHTTPTransport,
PublicNetworkBackend,
RequestDeadline,
ResponseTooLargeError,
UnsafeAddressError,
is_http_url,
is_public_address,
media_type_of,
normalized_hostname,
read_limited_body,
resolve_public_addresses,
)
@pytest.mark.parametrize("host", ["127.0.0.1", "169.254.169.254", "::1"])
def test_backend_rejects_private_ip_without_socket(
monkeypatch: pytest.MonkeyPatch, host: str
) -> None:
create_socket = Mock(side_effect=AssertionError("must not open socket"))
monkeypatch.setattr(socket, "socket", create_socket)
with pytest.raises(UnsafeAddressError):
PublicNetworkBackend(RequestDeadline(5)).connect_tcp(host, 80)
create_socket.assert_not_called()
def test_rebinding_cannot_change_socket_peer(monkeypatch: pytest.MonkeyPatch) -> None:
"""Verify that DNS rebinding cannot change the connected socket peer.
:param monkeypatch: Pytest fixture for replacing the DNS resolver and socket calls.
"""
dns = Mock(return_value=("93.184.216.34",))
monkeypatch.setattr("webmentions_ssg.net.resolve_public_addresses", dns)
socket_instance = Mock()
create_socket = Mock(return_value=socket_instance)
monkeypatch.setattr(socket, "socket", create_socket)
monkeypatch.setattr(
socket, "getaddrinfo", Mock(side_effect=AssertionError("second resolution"))
)
stream = PublicNetworkBackend(RequestDeadline(5)).connect_tcp(
"rebinding.example", 443, timeout=2.0
)
assert isinstance(stream, DeadlineStream)
assert stream.get_extra_info("socket") is socket_instance
dns.assert_called_once()
assert dns.call_args.args == ("rebinding.example",)
assert 0 < dns.call_args.kwargs["lifetime"] <= 2.0
socket_instance.connect.assert_called_once_with(("93.184.216.34", 443))
create_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM)
def test_backend_closes_failed_socket_and_tries_next_address(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(
"webmentions_ssg.net.resolve_public_addresses",
Mock(return_value=("93.184.216.34", "8.8.8.8")),
)
first, second = Mock(), Mock()
first.connect.side_effect = TimeoutError("connection timed out")
monkeypatch.setattr(socket, "socket", Mock(side_effect=[first, second]))
stream = PublicNetworkBackend(RequestDeadline(5)).connect_tcp("example.com", 443)
assert isinstance(stream, DeadlineStream)
first.close.assert_called_once_with()
second.close.assert_not_called()
first.connect.assert_called_once_with(("93.184.216.34", 443))
second.connect.assert_called_once_with(("8.8.8.8", 443))
stream.close()
second.close.assert_called_once_with()
@pytest.mark.parametrize(
("failure", "expected"),
[
pytest.param(TimeoutError("timed out"), httpcore.ConnectTimeout, id="timeout"),
pytest.param(OSError("refused"), httpcore.ConnectError, id="os-error"),
pytest.param(ValueError("unexpected"), ValueError, id="unexpected"),
],
)
def test_backend_closes_socket_on_failure(
monkeypatch: pytest.MonkeyPatch, failure: Exception, expected: type[Exception]
) -> None:
sock = Mock()
sock.connect.side_effect = failure
monkeypatch.setattr(socket, "socket", Mock(return_value=sock))
with pytest.raises(expected, match=str(failure)):
PublicNetworkBackend(RequestDeadline(5)).connect_tcp("8.8.8.8", 443)
sock.close.assert_called_once_with()
@pytest.mark.parametrize("mode", ["declared", "streamed"])
def test_read_limited_body_raises_specific_size_error(mode: str) -> None:
response = (
httpx.Response(
HTTPStatus.OK, headers={"Content-Length": "11"}, content=b"x" * 11
)
if mode == "declared"
else httpx.Response(HTTPStatus.OK, stream=httpx.ByteStream(b"x" * 11))
)
with pytest.raises(ResponseTooLargeError, match="Source document is too large"):
read_limited_body(response, 10, label="Source")
class FakeStream(httpcore.NetworkStream):
"""In-memory TCP/TLS stream so HTTP framing and SNI can be tested."""
def __init__(self, response: bytes) -> None:
self.response = response
self.writes = bytearray()
self.sni: str | None = None
def read(self, max_bytes: int, timeout: float | None = None) -> bytes:
part, self.response = self.response[:max_bytes], self.response[max_bytes:]
return part
def write(self, buffer: bytes, timeout: float | None = None) -> None:
self.writes.extend(buffer)
def start_tls(
self,
ssl_context,
server_hostname: str | None = None,
timeout: float | None = None,
) -> httpcore.NetworkStream:
self.sni = server_hostname
return self
def get_extra_info(self, info: str):
return None
def close(self) -> None:
pass
@pytest.mark.parametrize("scheme", ["http", "https"])
def test_transport_keeps_original_http_host_and_tls_sni(
monkeypatch: pytest.MonkeyPatch, scheme: str
) -> None:
stream = FakeStream(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK")
backend = Mock(return_value=stream)
monkeypatch.setattr(PublicNetworkBackend, "connect_tcp", backend)
with httpx.Client(transport=PublicHTTPTransport(5.0), trust_env=False) as client:
response = client.get(f"{scheme}://source.example/path")
assert response.status_code == HTTPStatus.OK
assert response.text == "OK"
assert b"Host: source.example\r\n" in stream.writes
assert b"GET /path HTTP/1.1\r\n" in stream.writes
assert stream.sni == ("source.example" if scheme == "https" else None)
backend.assert_called_once()
assert backend.call_args.kwargs["host"] == "source.example"
assert backend.call_args.kwargs["port"] == (443 if scheme == "https" else 80)
def test_deadline_clips_each_read_and_stops_trickle(
monkeypatch: pytest.MonkeyPatch,
) -> None:
clock = [100.0]
monkeypatch.setattr("webmentions_ssg.net.time.monotonic", lambda: clock[0])
fake_stream = Mock()
fake_stream.read.return_value = b"x"
stream = DeadlineStream(fake_stream, RequestDeadline(5.0))
clock[0] = 103.0
assert stream.read(1, timeout=5.0) == b"x"
assert fake_stream.read.call_args.kwargs["timeout"] == pytest.approx(2.0)
clock[0] = 105.1
with pytest.raises(httpcore.ReadTimeout, match="Overall HTTP request deadline"):
stream.read(1, timeout=5.0)
assert fake_stream.read.call_count == 1
def test_redirects_go_through_same_destination_guard(
monkeypatch: pytest.MonkeyPatch,
) -> None:
redirect = FakeStream(
b"HTTP/1.1 302 Found\r\nLocation: http://internal.example/\r\n"
b"Content-Length: 0\r\n\r\n"
)
checked_hosts: list[str] = []
def connect(
self: PublicNetworkBackend, host: str, port: int, **kwargs: object
) -> httpcore.NetworkStream:
checked_hosts.append(host)
if host == "internal.example":
raise UnsafeAddressError("Non-public network address")
return redirect
monkeypatch.setattr(PublicNetworkBackend, "connect_tcp", connect)
with (
httpx.Client(
transport=PublicHTTPTransport(5.0), follow_redirects=True
) as client,
pytest.raises(UnsafeAddressError, match="Non-public"),
):
client.get("http://source.example/post")
assert checked_hosts == ["source.example", "internal.example"]
@pytest.mark.parametrize(
("url", "expected"),
[
pytest.param("http://example.com/", True, id="http"),
pytest.param("https://example.com/path", True, id="https"),
pytest.param("HTTP://EXAMPLE.COM/", True, id="scheme-case-insensitive"),
pytest.param("https://example.com:8443/path", True, id="port"),
pytest.param("http://[2001:db8::1]/", True, id="ipv6"),
pytest.param("ftp://example.com/", False, id="ftp"),
pytest.param("mailto:example@example.com", False, id="mailto"),
pytest.param("/relative/url", False, id="relative"),
pytest.param("//example.com/path", False, id="scheme-relative"),
pytest.param("https:///missing-host", False, id="missing-host"),
pytest.param("", False, id="empty"),
pytest.param("http://[::1", False, id="malformed"),
],
)
def test_is_http_url(url: str, expected: bool) -> None:
assert is_http_url(url) is expected
def test_ip_literal_does_not_use_dns() -> None:
with patch("webmentions_ssg.net.dns.resolver.resolve_name") as dns_resolve:
with pytest.raises(UnsafeAddressError):
resolve_public_addresses("169.254.169.254")
with pytest.raises(UnsafeAddressError):
resolve_public_addresses("::1")
assert resolve_public_addresses("8.8.8.8") == ("8.8.8.8",)
dns_resolve.assert_not_called()
def test_public_addresses_resolves_hostname_once() -> None:
with patch("webmentions_ssg.net.dns.resolver.resolve_name") as resolve:
resolve.return_value.addresses.return_value = ["93.184.216.34", "8.8.8.8"]
assert resolve_public_addresses("example.com", lifetime=1.5) == (
"93.184.216.34",
"8.8.8.8",
)
resolve.assert_called_once_with("example.com", lifetime=1.5)
@pytest.mark.parametrize(
"address",
[
pytest.param("64:ff9b::a9fe:a9fe", id="nat64-metadata"),
pytest.param("64:ff9b::808:808", id="nat64-public-ipv4"),
pytest.param("::10.0.0.1", id="ipv4-compatible-private"),
pytest.param("::8.8.8.8", id="ipv4-compatible-public"),
pytest.param("2002:a9fe:a9fe::1", id="6to4-metadata"),
pytest.param("2002:0a00:0001::1", id="6to4-private"),
pytest.param("::ffff:10.0.0.1", id="mapped-private"),
pytest.param("::ffff:169.254.169.254", id="mapped-metadata"),
],
)
def test_public_addresses_rejects_ipv6_transition_literals(address: str) -> None:
with patch("webmentions_ssg.net.dns.resolver.resolve_name") as resolve:
with pytest.raises(UnsafeAddressError, match="Non-public"):
resolve_public_addresses(address)
resolve.assert_not_called()
@pytest.mark.parametrize(
"address", ["64:ff9b::a9fe:a9fe", "::10.0.0.1", "2002:a9fe:a9fe::1"]
)
def test_public_addresses_rejects_transition_addresses_in_dns(address: str) -> None:
with patch("webmentions_ssg.net.dns.resolver.resolve_name") as resolve:
resolve.return_value.addresses.return_value = ["93.184.216.34", address]
with pytest.raises(UnsafeAddressError, match="Non-public"):
resolve_public_addresses("transition.example")
@pytest.mark.parametrize("address", ["2002:0a00:0001::1", "::ffff:10.0.0.1"])
def test_private_embedded_ipv4_rejected_even_if_ipv6_is_global(address: str) -> None:
# Simulate a Python version treating the outer transition address as global.
with patch.object(
ipaddress.IPv6Address, "is_global", new_callable=PropertyMock
) as global_:
global_.return_value = True
assert not is_public_address(ipaddress.ip_address(address))
def test_public_addresses_rejects_mixed_dns_results() -> None:
with patch("webmentions_ssg.net.dns.resolver.resolve_name") as resolve:
resolve.return_value.addresses.return_value = ["93.184.216.34", "127.0.0.1"]
with pytest.raises(UnsafeAddressError, match="Non-public"):
resolve_public_addresses("rebinding.example", lifetime=1.5)
resolve.assert_called_once_with("rebinding.example", lifetime=1.5)
def test_public_addresses_rejects_empty_dns_result() -> None:
with patch("webmentions_ssg.net.dns.resolver.resolve_name") as resolve:
resolve.return_value.addresses.return_value = []
with pytest.raises(AddressResolutionError):
resolve_public_addresses("empty.example")
@pytest.mark.parametrize(
("status", "expected"),
[
pytest.param(HTTPStatus.OK, False, id="success"),
pytest.param(HTTPStatus.BAD_REQUEST, False, id="client-error"),
pytest.param(HTTPStatus.REQUEST_TIMEOUT, True, id="request-timeout"),
pytest.param(HTTPStatus.TOO_EARLY, True, id="too-early"),
pytest.param(HTTPStatus.TOO_MANY_REQUESTS, True, id="too-many-requests"),
pytest.param(
HTTPStatus.INTERNAL_SERVER_ERROR, True, id="server-error-lower-bound"
),
pytest.param(599, True, id="server-error-upper-bound"),
pytest.param(600, False, id="outside-server-error-range"),
],
)
def test_is_retryable_status(app: Flask, status: int, expected: bool) -> None:
_ = app
from webmentions_ssg.tasks import endpoint_discovery
assert endpoint_discovery.is_retryable_status(status) is expected
@pytest.mark.parametrize(
("url", "expected"),
[
pytest.param("https://EXAMPLE.com./path", "example.com", id="trailing-dot"),
pytest.param("http://[2001:db8::1]/", "2001:db8::1", id="ipv6"),
pytest.param("https:///path", None, id="missing-host"),
pytest.param("http://[::1", None, id="malformed-ipv6"),
],
)
def test_normalized_hostname(url: str, expected: str | None) -> None:
assert normalized_hostname(url) == expected
@pytest.mark.parametrize(
("headers", "expected"),
[
pytest.param({}, "", id="missing"),
pytest.param(
{"Content-Type": " Text/HTML ; charset=UTF-8"}, "text/html", id="params"
),
pytest.param(
{"content-type": "APPLICATION/XHTML+XML"},
"application/xhtml+xml",
id="case",
),
],
)
def test_media_type_of(headers: dict[str, str], expected: str) -> None:
assert media_type_of(httpx.Headers(headers)) == expected
|