Coverage for app/backend/src/tests/test_ratelimit.py: 99%
178 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-15 23:45 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-15 23:45 +0000
1import os
2from uuid import uuid4
4import grpc
5import pytest
6import valkey
8from couchers import metrics
9from couchers.middleware import ratelimit
10from couchers.middleware.interceptors import CouchersHeaders
11from couchers.proto import api_pb2, auth_pb2
12from tests.fixtures.db import generate_user
13from tests.fixtures.feature_flags import FeatureFlags
14from tests.fixtures.rpc_errors import expect_rpc_error
15from tests.fixtures.sessions import auth_api_session, real_api_session
17AUTHENTICATE = "/org.couchers.auth.Auth/Authenticate"
18USERNAME_VALID = "/org.couchers.auth.Auth/UsernameValid"
20# Where the Valkey integration tests look for a server; docker-compose.test.yml publishes one on 6545.
21VALKEY_TEST_HOST = os.environ.get("VALKEY_TEST_HOST", "localhost")
22VALKEY_TEST_PORT = int(os.environ.get("VALKEY_TEST_PORT", "6545"))
25class InMemoryCounterStore:
26 """A pure-Python fixed-window store mirroring the Valkey one, for hermetic tests."""
28 def __init__(self) -> None:
29 self.counts: dict[str, int] = {}
31 def incr_and_check(self, entries: list[tuple[str, int]], ttl: int) -> list[int]:
32 tripped = []
33 for i, (key, limit) in enumerate(entries):
34 self.counts[key] = self.counts.get(key, 0) + 1
35 if self.counts[key] > limit:
36 tripped.append(i)
37 return tripped
40class AlwaysTripStore:
41 def incr_and_check(self, entries: list[tuple[str, int]], ttl: int) -> list[int]:
42 return list(range(len(entries)))
45class BrokenStore:
46 def incr_and_check(self, entries: list[tuple[str, int]], ttl: int) -> list[int]:
47 raise RuntimeError("valkey down")
50def _use_store(monkeypatch, store):
51 """Point the rate limiter at this counter store (None meaning none configured)."""
52 monkeypatch.setattr(ratelimit, "_get_store", lambda: store)
55@pytest.fixture
56def store(monkeypatch):
57 """Inject an in-memory counter store, bypassing Valkey."""
58 s = InMemoryCounterStore()
59 _use_store(monkeypatch, s)
60 return s
63@pytest.fixture
64def valkey_client():
65 """A raw client against the test Valkey, skipping the test if there isn't one running."""
66 client = valkey.Valkey(host=VALKEY_TEST_HOST, port=VALKEY_TEST_PORT, socket_connect_timeout=1, socket_timeout=1)
67 try:
68 client.ping()
69 except valkey.ConnectionError as e:
70 pytest.skip(
71 f"no Valkey at {VALKEY_TEST_HOST}:{VALKEY_TEST_PORT} ({e}); "
72 f"start one with `docker compose -f docker-compose.test.yml up -d valkey_tests`"
73 )
74 return client
77@pytest.fixture
78def valkey_store(valkey_client):
79 """The real Valkey-backed store, so the Lua script itself is exercised rather than a stand-in."""
80 return ratelimit.ValkeyCounterStore(VALKEY_TEST_HOST, VALKEY_TEST_PORT)
83@pytest.fixture
84def key_prefix():
85 """A prefix unique to this test run, so counters can't collide with a previous run's leftovers."""
86 return f"test:{uuid4().hex}"
89def _limited(method: str, ip: str | None = None) -> bool:
90 """should_rate_limit for an unauthenticated call from this IP; the limiter reads no other header."""
91 headers = CouchersHeaders(
92 token=None,
93 is_api_key=False,
94 ip_address=ip,
95 user_agent=None,
96 client_platform=None,
97 ui_lang=None,
98 user_id_str=None,
99 sofa=None,
100 )
101 return ratelimit.should_rate_limit(method, headers, None)
104def test_ip_to_key_ipv4():
105 assert ratelimit.ip_to_key("1.2.3.4", 64) == "1.2.3.4/32"
108def test_ip_to_key_ipv6_masks_to_prefix():
109 assert ratelimit.ip_to_key("2001:db8::1", 64) == "2001:db8::/64"
110 assert ratelimit.ip_to_key("2001:0db8:0000:0000:dead:beef:0:1", 64) == "2001:db8::/64"
111 assert ratelimit.ip_to_key("2001:db8::1", 64) == ratelimit.ip_to_key("2001:db8::ffff", 64)
114def test_ip_to_key_ipv6_prefix_configurable():
115 assert ratelimit.ip_to_key("2001:db8:abcd:1234::1", 48) == "2001:db8:abcd::/48"
118def test_resolve_method_rate_limits_method_override():
119 limits = ratelimit.resolve_method_rate_limits(AUTHENTICATE)
120 assert limits.rpc == {"per_ip": 10, "per_user": 120, "global": 6000}
123def test_resolve_method_rate_limits_defaults():
124 limits = ratelimit.resolve_method_rate_limits(USERNAME_VALID)
125 assert limits.rpc == {"per_ip": 60, "per_user": 120, "global": 6000}
126 assert limits.svc == {"per_ip": 300, "per_user": 600, "global": 20000}
127 assert limits.api == {"per_ip": 600, "per_user": 1200, "global": 60000}
130def test_no_store_means_rate_limiting_is_off():
131 # this is the one test that goes through the real accessor, so it can't reuse another test's store
132 ratelimit._get_store.cache_clear()
133 assert ratelimit._get_store() is None
134 assert not _limited(AUTHENTICATE, ip="1.2.3.4")
137def test_trips_per_ip(feature_flags: FeatureFlags, store):
138 feature_flags.set("rate_limiting_enabled", True)
139 # Authenticate per_ip = 10 and every other limit is far higher, so the 11th call from an IP is the first
140 # to trip anything
141 for _ in range(10):
142 assert not _limited(AUTHENTICATE, ip="1.2.3.4")
143 assert _limited(AUTHENTICATE, ip="1.2.3.4")
146def test_per_ip_skipped_without_ip(feature_flags: FeatureFlags, store):
147 feature_flags.set("rate_limiting_enabled", True)
148 # no IP → the per_ip dimension is not counted, so the per_ip=10 limit can never trip
149 for _ in range(20):
150 assert not _limited(AUTHENTICATE)
153def test_separate_subnets_counted_separately(feature_flags: FeatureFlags, store):
154 feature_flags.set("rate_limiting_enabled", True)
155 for _ in range(11):
156 _limited(AUTHENTICATE, ip="2001:db8:1::1")
157 assert not _limited(AUTHENTICATE, ip="2001:db8:2::1")
160def test_store_error_fails_open(feature_flags: FeatureFlags, monkeypatch):
161 feature_flags.set("rate_limiting_enabled", True)
162 _use_store(monkeypatch, BrokenStore())
163 captured = []
164 monkeypatch.setattr("couchers.middleware.ratelimit.sentry_sdk.capture_exception", lambda e: captured.append(e))
165 monkeypatch.setattr("couchers.middleware.ratelimit.sentry_sdk.set_tag", lambda *a, **k: None)
167 assert not _limited(AUTHENTICATE, ip="1.2.3.4")
168 assert len(captured) == 1
171def test_interceptor_superuser_exempt_when_enforcing(db, feature_flags: FeatureFlags, monkeypatch):
172 feature_flags.set("rate_limiting_enabled", True)
173 _use_store(monkeypatch, AlwaysTripStore())
174 superuser, token = generate_user(is_superuser=True)
176 # real_api_session, not api_session: only the real server runs the interceptor the limiter lives in
177 with real_api_session(token) as api:
178 assert api.Ping(api_pb2.PingReq()).user.user_id == superuser.id
181def test_interceptor_non_superuser_still_blocked_when_enforcing(db, feature_flags: FeatureFlags, monkeypatch):
182 feature_flags.set("rate_limiting_enabled", True)
183 _use_store(monkeypatch, AlwaysTripStore())
184 _, token = generate_user()
186 with real_api_session(token) as api:
187 with expect_rpc_error(grpc.StatusCode.RESOURCE_EXHAUSTED):
188 api.Ping(api_pb2.PingReq())
191def test_interceptor_no_store_allows(db, feature_flags: FeatureFlags, monkeypatch):
192 feature_flags.set("rate_limiting_enabled", True)
193 _use_store(monkeypatch, None)
194 with auth_api_session() as (auth_api, _):
195 assert auth_api.UsernameValid(auth_pb2.UsernameValidReq(username="test")).valid
198def test_interceptor_shadow_allows(db, feature_flags: FeatureFlags, monkeypatch):
199 feature_flags.set("rate_limiting_enabled", False)
200 _use_store(monkeypatch, AlwaysTripStore())
201 with auth_api_session() as (auth_api, _):
202 assert auth_api.UsernameValid(auth_pb2.UsernameValidReq(username="test")).valid
205def test_interceptor_enforce_rejects(db, feature_flags: FeatureFlags, monkeypatch):
206 feature_flags.set("rate_limiting_enabled", True)
207 _use_store(monkeypatch, AlwaysTripStore())
208 with auth_api_session() as (auth_api, _):
209 with expect_rpc_error(grpc.StatusCode.RESOURCE_EXHAUSTED):
210 auth_api.UsernameValid(auth_pb2.UsernameValidReq(username="test"))
213def test_interceptor_fails_open_when_enforcing(db, feature_flags: FeatureFlags, monkeypatch):
214 feature_flags.set("rate_limiting_enabled", True)
215 _use_store(monkeypatch, BrokenStore())
216 with auth_api_session() as (auth_api, _):
217 assert auth_api.UsernameValid(auth_pb2.UsernameValidReq(username="test")).valid
220def _metric_value(counter, name: str, **labels: str) -> float:
221 return float(
222 sum(
223 s.value
224 for m in counter.collect()
225 for s in m.samples
226 if s.name == name and all(s.labels.get(k) == v for k, v in labels.items())
227 )
228 )
231def test_interceptor_emits_metrics_on_enforce(db, feature_flags: FeatureFlags, monkeypatch):
232 feature_flags.set("rate_limiting_enabled", True)
233 _use_store(monkeypatch, AlwaysTripStore())
235 blocked_before = _metric_value(
236 metrics.rate_limit_checks_counter,
237 "couchers_rate_limit_checks_total",
238 method=USERNAME_VALID,
239 decision="blocked",
240 )
241 # no IP/user on this call, so the global dimension trips at every scope
242 trip_before = _metric_value(
243 metrics.rate_limit_trips_counter,
244 "couchers_rate_limit_trips_total",
245 method=USERNAME_VALID,
246 scope="rpc",
247 dimension="global",
248 enforced="true",
249 )
251 with auth_api_session() as (auth_api, _):
252 with pytest.raises(grpc.RpcError):
253 auth_api.UsernameValid(auth_pb2.UsernameValidReq(username="test"))
255 assert (
256 _metric_value(
257 metrics.rate_limit_checks_counter,
258 "couchers_rate_limit_checks_total",
259 method=USERNAME_VALID,
260 decision="blocked",
261 )
262 == blocked_before + 1
263 )
264 assert (
265 _metric_value(
266 metrics.rate_limit_trips_counter,
267 "couchers_rate_limit_trips_total",
268 method=USERNAME_VALID,
269 scope="rpc",
270 dimension="global",
271 enforced="true",
272 )
273 == trip_before + 1
274 )
277# The tests below run the real Lua script against a real Valkey; everything above uses a stand-in store.
280def test_valkey_store_counts_and_trips(valkey_store, key_prefix):
281 key = f"{key_prefix}:counted"
282 for _ in range(3):
283 assert valkey_store.incr_and_check([(key, 3)], 120) == []
284 assert valkey_store.incr_and_check([(key, 3)], 120) == [0]
285 assert valkey_store.incr_and_check([(key, 3)], 120) == [0]
288def test_valkey_store_returns_indices_of_tripped_entries(valkey_store, key_prefix):
289 # a limit of 0 trips on the first increment, a high limit never does; this pins the Lua script's
290 # 1-based indices being translated back to the 0-based positions of the entries passed in
291 entries = [
292 (f"{key_prefix}:high:0", 100),
293 (f"{key_prefix}:zero:1", 0),
294 (f"{key_prefix}:high:2", 100),
295 (f"{key_prefix}:zero:3", 0),
296 ]
297 assert valkey_store.incr_and_check(entries, 120) == [1, 3]
300def test_valkey_store_counts_keys_independently(valkey_store, key_prefix):
301 a, b = f"{key_prefix}:a", f"{key_prefix}:b"
302 for _ in range(5):
303 valkey_store.incr_and_check([(a, 5)], 120)
304 assert valkey_store.incr_and_check([(a, 5), (b, 5)], 120) == [0]
307def test_valkey_store_sets_ttl_on_first_increment(valkey_store, valkey_client, key_prefix):
308 key = f"{key_prefix}:ttl"
309 valkey_store.incr_and_check([(key, 100)], 120)
310 # without a TTL the counter would never reset and the key would leak
311 assert 0 < valkey_client.ttl(key) <= 120
314def test_valkey_store_does_not_extend_ttl_on_later_increments(valkey_store, valkey_client, key_prefix):
315 key = f"{key_prefix}:ttl-once"
316 valkey_store.incr_and_check([(key, 100)], 120)
317 # pull the expiry in, then increment again: the window must not slide, or a sustained flood would keep
318 # renewing its own counter and the fixed window would never roll over
319 valkey_client.expire(key, 5)
320 valkey_store.incr_and_check([(key, 100)], 120)
321 assert 0 < valkey_client.ttl(key) <= 5
324def test_rate_limiting_end_to_end_against_valkey(feature_flags: FeatureFlags, valkey_store, monkeypatch):
325 feature_flags.set("rate_limiting_enabled", True)
326 _use_store(monkeypatch, valkey_store)
327 # a /64 unique to this run, so the per-IP counters start clean
328 ip = f"2001:db8:{uuid4().hex[:4]}:{uuid4().hex[:4]}::1"
330 # Authenticate annotates per_ip = 10
331 for _ in range(10):
332 assert not _limited(AUTHENTICATE, ip=ip)
333 assert _limited(AUTHENTICATE, ip=ip)
335 other_ip = f"2001:db8:{uuid4().hex[:4]}:{uuid4().hex[:4]}::1"
336 assert not _limited(AUTHENTICATE, ip=other_ip)