gabriel / musehub public
test_real_client_ip.py python
62 lines 2.8 KB
Raw
sha256:de0006978cb066c122f0557ad42e0f2c015079b998ce210bd2144580572fc0c2 fix(deploy): per-IP limits key on the visitor, not the Dock… Opus 5.5 patch 1 day ago
1 """The app sees the visitor's IP, not the Docker gateway's (found on staging 2026-11-15).
2
3 nginx proxies to the container through Docker's published port, so every request arrives from the bridge gateway (172.18.0.1). uvicorn's
4 ``--proxy-headers`` trusts only 127.0.0.1 by default, so ``request.client.host`` was the gateway for every visitor and each per-IP rate
5 limit (objects, wire fetch, auth, search, anonymous MCP) was a single bucket shared by the whole site. The entrypoint now trusts the
6 loopback and Docker's private bridge range; uvicorn takes the right-most untrusted ``X-Forwarded-For`` entry (nginx's ``$remote_addr``).
7 """
8 from __future__ import annotations
9
10 import asyncio
11 import re
12 from pathlib import Path
13
14 from slowapi.util import get_remote_address
15 from starlette.requests import Request
16 from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware
17
18 ENTRYPOINT = Path(__file__).resolve().parents[1] / "entrypoint.sh"
19
20
21 def _default_trusted() -> str:
22 m = re.search(r'^FORWARDED_ALLOW_IPS="\$\{FORWARDED_ALLOW_IPS:-([^}]*)\}"$', ENTRYPOINT.read_text(), re.M)
23 assert m, "entrypoint.sh must default FORWARDED_ALLOW_IPS"
24 return m.group(1)
25
26
27 def test_both_uvicorn_launches_pass_the_trusted_proxies() -> None:
28 text = ENTRYPOINT.read_text()
29 assert text.count("--proxy-headers") == 2
30 assert text.count('--forwarded-allow-ips "$FORWARDED_ALLOW_IPS"') == 2
31
32
33 def test_default_trusts_loopback_and_the_docker_bridge_only() -> None:
34 trusted = _default_trusted().split(",")
35 assert trusted == ["127.0.0.1", "172.16.0.0/12"]
36 assert "*" not in trusted # trusting everyone would let any client pick its own rate-limit key
37
38
39 def _client_seen(peer: str, forwarded_for: str | None) -> str:
40 seen: list[str] = []
41
42 async def app(scope, receive, send) -> None: # noqa: ANN001
43 seen.append(get_remote_address(Request(scope)))
44
45 headers = [(b"x-forwarded-for", forwarded_for.encode())] if forwarded_for else []
46 scope = {"type": "http", "method": "GET", "path": "/", "headers": headers, "client": (peer, 40000), "scheme": "http",
47 "server": ("musehub", 1337), "query_string": b""}
48 asyncio.run(ProxyHeadersMiddleware(app, trusted_hosts=_default_trusted())(scope, None, None)) # type: ignore[arg-type]
49 return seen[0]
50
51
52 def test_request_through_nginx_and_the_docker_gateway_keys_on_the_visitor() -> None:
53 # nginx: X-Forwarded-For = <what the client sent>, $remote_addr (real_ip already set it from CF-Connecting-IP)
54 assert _client_seen("172.18.0.1", "203.0.113.9") == "203.0.113.9"
55
56
57 def test_a_spoofed_forwarded_for_entry_is_ignored() -> None:
58 assert _client_seen("172.18.0.1", "6.6.6.6, 203.0.113.9") == "203.0.113.9"
59
60
61 def test_an_untrusted_peer_cannot_choose_its_address() -> None:
62 assert _client_seen("198.51.100.7", "203.0.113.9") == "198.51.100.7"
File History 1 commit
sha256:de0006978cb066c122f0557ad42e0f2c015079b998ce210bd2144580572fc0c2 fix(deploy): per-IP limits key on the visitor, not the Dock… Opus 5.5 patch 1 day ago