fix(app): collection's 15 s limit also covers the name lookup, TLS and headers

The name lookup runs on a daemon thread waited for only until the deadline, and a watchdog
shuts the connection down at the deadline, so a server that sends its status line or headers
byte by byte within the per-read limit is cut off too. A response cut short is a failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-13 21:46:10 +09:00
parent 9185265010
commit d0cb540fa0
3 changed files with 182 additions and 57 deletions

View File

@@ -319,3 +319,60 @@ def test_the_fetcher_never_logs_the_address(monkeypatch, capfd):
safe_get("http://secret-host.example/private/path?token=abc123")
out = capfd.readouterr()
assert "secret-host" not in out.err + out.out and "abc123" not in out.err + out.out
def test_a_response_cut_short_is_a_failure_not_a_page(loopback_is_public):
def short(req):
req.send_response(200)
req.send_header("Content-Length", "1000")
req.end_headers()
req.wfile.write(b"only part")
req.close_connection = True
port, _ = serve(short)
with pytest.raises(Exception):
safe_get(f"http://127.0.0.1:{port}/")
def test_the_fetcher_stops_a_server_that_sends_its_headers_slowly(loopback_is_public, monkeypatch):
"""Each byte comes before the per-read limit, so only the overall limit can end it."""
srv = socket.socket()
srv.bind(("127.0.0.1", 0))
srv.listen()
def drip():
conn, _ = srv.accept()
try:
for b in b"HTTP/1.1 200 OK\r\nX-Slow: " + b"x" * 1000:
conn.sendall(bytes([b]))
time.sleep(0.2)
except OSError:
pass
finally:
conn.close()
threading.Thread(target=drip, daemon=True).start()
monkeypatch.setattr(collect, "TOTAL_S", 1.0)
monkeypatch.setattr(collect, "READ_S", 0.5)
t0 = time.monotonic()
with pytest.raises(Exception):
safe_get(f"http://127.0.0.1:{srv.getsockname()[1]}/")
took = time.monotonic() - t0
print(f"slow headers: failed after {took:.2f}s")
assert took < 1.5
srv.close()
def test_the_fetcher_gives_up_on_a_slow_name_lookup(monkeypatch):
def slow(host, *a, **k):
time.sleep(3)
return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("1.1.1.1", 80))]
monkeypatch.setattr(socket, "getaddrinfo", slow)
monkeypatch.setattr(collect, "TOTAL_S", 1.0)
monkeypatch.setattr(collect, "READ_S", 0.5)
t0 = time.monotonic()
with pytest.raises(TimeoutError):
safe_get("http://slow-dns.example/")
took = time.monotonic() - t0
print(f"slow lookup: failed after {took:.2f}s")
assert took < 1.5