oliver-zehentleitner / maity
clean-up: limits and abuse: one counter and breaker layer in ratelimit, one per-owner cap check
tsk-5sfts81hr516 Open
From the clean-up audit tsk-hgnbapyw7hnh, Area 2 (Limits and abuse), read at 80647565. Line numbers are at 80647565; check each one against current main before changing it. Finding numbers are the audit's (A2-n).
Counting in one place: one counter and breaker layer in ratelimit.py, and one check for the per-owner caps in limits.py. Two other area 2 cards were filed at the same time (idempotency; serving constants and docs), plus two separate cards for A2-2 and A2-5.
1. The anonymous mail request limits: the registration copy (A2-1, duplication)
auth/email_links.py:238-256andauth/password_resets.py:158-175hold two identicalRequestLimitsclasses (10/h per source, 3/15 min and 10/day per pair). Merging those two is tsk-f5sdcgr7vzj0 item 1.- Left here:
routes/registration.py:49-55, 156, 195is a third copy of the same limits, and it counts differently:BY_PAIR_SHORT.hit(pair) or BY_PAIR_DAY.hit(pair) or ...stops at the first refusal, so the day bucket is not counted while the short one refuses.RequestLimits.takecounts both buckets every time. - Proposal: registration uses the shared
RequestLimits(prefix)from tsk-f5sdcgr7vzj0 and adds its per-address-day bucket on top. - Open question: one counting rule for all three: count both buckets, or stop at the first refusal?
- Effort: S. Risk: low. Bucket names stay, so the shared counts carry over; test_ratelimit.py:228-238 names the attributes.
2. Four hand-written "shared counts on PostgreSQL, else in the process" counters beside Limiter (A2-3, duplication)
registration.py:336-361Dropped(an hourly count),registration.py:364-387Breaker(count per minute, thenset_until/until),routes/device_codes.py:53-54, 230-244(_breakerdict,_breaker_shut_for,_trip_breaker), andauth/second_factor.py:49-52, 494-514(_wrong_in_session,_count_wrong,_forget_wrong).- Each repeats
if shared_counts.active(): ... else: <its own list or dict, its own lock or none, its own pruning>, whichratelimit.Limiteralready does. The two breakers implement one rule (N per minute across the instance shuts everything for a minute) differently: registration counts its own estimate, device codes uses aLimiterand thenblocked.device_codes._breakeris a bare dict without a lock. - Proposal: a
Breaker(name, limit, window, shut_for)and a plainCounter(name, window)inratelimit.py, on the same dual backend, used in all four places. The second-factor session count is aCounterkeyed by the session hash. - Effort: M. Risk: medium. The replica tests (test_ratelimit.py:158-170) and the device-code breaker tests must pass on all three engines.
3. Limiter repeats itself (A2-12, debt, small)
ratelimit.py:65-66and:126-127: the samecount > self.limitguard andValueErrorintakeandplace, then the same_shared()branch (67-69 and 128-133).ratelimit.py:114-116(_take_at) and:175-178(blocked) trim the deque and compute the wait the same way.take(key, count)has no caller withcount > 1outside the tests.- Proposal: build
takeonplace(dropping the mark), andblocked's local branch on a dry run of_take_at.hub/context/replicas.md:274liststakein the interface, so keep the name. - Effort: S. Risk: low (test_ratelimit.py covers both backends).
4. The per-owner caps of §16.5 are enforced in three places, three ways (A2-4, duplication)
limits.py:104-120refuse_project_overlocks the owner's row, counts and raises.routes/agents.py:92-105(increate_profile) does the same inline formax_agents_per_human, with its error inlimits.py:86.orgs.py:317-332(increate) does the same inline formax_orgs_per_human, with its errororg_limitatorgs.py:63, not next to the other two in limits.py.- The counting queries are written again in
limits.view(limits.py:134-145) forhumans_at_limitandhumans_at_org_limit, so the gate and the admin's count can drift apart. - Proposal: one
limits.refuse_over(conn, name, owner_id, limit)with each cap's count query defined once, used by the three gates and byview. Moveorg_limitnext toagent_limitandproject_limit. - Effort: M. Risk: low to medium. The lock order (human row before org row, §6.26) must stay.
5. Names and repeated reads in limits.py (A2-13, design, small)
limits.py:123_humans_atcounts "the humans with at leastlimitrows", butviewand_names_atalso use it for organisation namespace owners (154-156), for alias owners that are organisations or agent profiles, and for projects (165-172).limits.py:79-83aliases()repeatsof()(73-76) only to allowhub=None.view(132-162) reads the stored limits five times (resolveonce,ofthree times,aliasesonce), each throughstore.stored.- Proposal: rename to
_owners_at; letoftakehub: Settings | Noneand dropaliases; inview, compute the values fromresolve's one read. - Effort: S. Risk: very low (admin route).
Order
- Item 1 after tsk-f5sdcgr7vzj0 item 1 (the shared
RequestLimits), which itself waits for tsk-q4zynccy5t0b. - Item 2:
auth/second_factor.pyis being changed by tsk-8mgb85ne0nrq; do the second-factor part after it. - Item 3 before item 2, so the new classes build on the simpler
Limiter. - Item 4: tsk-a75h06dtnsfr (open) adds a fourth per-owner cap (
max_instances_per_human) to limits.py. Whoever comes second uses the shared check. - Items 4 and 5 go together (both in limits.py).
Covered elsewhere
- A2-1, the two
RequestLimitsclasses in email_links and password_resets: tsk-f5sdcgr7vzj0 item 1.
Effort sum: 3S+2M.