A self-hosted PLAYMATCH_API_URL on a LAN IP, localhost, or a Docker
service name is rejected by the shared HTTP client's SSRF defense before
connecting, so health checks and lookups fail. Add a narrowly scoped
allowlist, derived from the admin-configured provider URL, that both SSRF
gates (the static URL hook and the connect-time backend) honor. The
exception is matched by exact host and port, and the public default URL
is unaffected since public addresses already pass.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
DNS64/NAT64 networks synthesize IPv6 answers that embed a public IPv4
in the RFC 6052 well-known prefix 64:ff9b::/96. The SSRF classifier
rejected these because the prefix is is_reserved, breaking all outbound
metadata and cover fetching on NAT64-only deployments.
is_forbidden_ip now unwraps a NAT64-wrapped address and evaluates the
embedded IPv4 instead, so a wrapped public IPv4 is allowed while a
wrapped private/internal IPv4 (e.g. 127.0.0.1, 10.0.0.1) stays blocked.
Fixes#3668
Generated-By: PostHog Code
Task-Id: 73b38615-966e-4955-9228-2cb6bbcda277
The async backend's `loop.getaddrinfo` ran without any timeout, so a
slow or hanging resolver could outlive the timeout the caller passed —
the previous code only bounded the TCP connect inside the inner
backend. Wrap the resolution in `asyncio.timeout(timeout)` and surface
the timeout as `httpcore.ConnectTimeout`.
Also tidy the test stubs (mypy func-returns-value) and add explicit
type annotations to the `calls` lists (mypy var-annotated). A targeted
`# noqa: ASYNC109` sits on the `timeout` parameter of `connect_tcp` /
`connect_unix_socket` with an explanatory comment: the rule advises
against `timeout` parameters on async APIs we author, but here we're
implementing `AsyncNetworkBackend`, and the timeout is consumed in the
asyncio-native pattern the rule endorses.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The previous validator did a preflight `socket.getaddrinfo` before each
httpx request. Two problems:
* DNS rebinding / TOCTOU: httpx re-resolves at connect time, so a
hostname can answer with a public IP for the validator and a
private IP for the real request. The preflight check did not
constrain the connection.
* Event-loop blocking: `socket.getaddrinfo` is synchronous, and the
media-download callers are async. Slow resolvers stalled
unrelated requests.
Replace it with two layers, both wired automatically onto every httpx
client built by `utils.context`:
1. A request event hook running `validate_url_for_http_request`
(syntactic checks only: scheme, reserved hostnames, literal IPs,
internal TLDs). No DNS, no call-site responsibility.
2. `SSRFProtectedAsyncBackend` / `SSRFProtectedSyncBackend`, custom
httpcore network backends that resolve the hostname inside
`connect_tcp`, reject any address in a forbidden range, then
connect to that *same* validated address. The async variant uses
`loop.getaddrinfo` so it doesn't block the loop. httpcore calls
`start_tls(server_hostname=<URL host>)` after `connect_tcp`, so
TLS SNI and cert verification still use the original hostname
even though the TCP layer connects by IP.
Drop the explicit `validate_url_for_http_request(...)` calls from
`resources_handler.py` — the event hook covers them. Consolidate the
URL validator and its tests under `utils/ssrf.py` /
`tests/utils/test_ssrf.py` so the SSRF surface lives in one module.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>