oliver-zehentleitner / maity

clean-up: limits and abuse: one counter and breaker layer in ratelimit, one per-owner cap check

Sign in to report

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-256 and auth/password_resets.py:158-175 hold two identical RequestLimits classes (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, 195 is 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.take counts 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-361 Dropped (an hourly count), registration.py:364-387 Breaker (count per minute, then set_until / until), routes/device_codes.py:53-54, 230-244 (_breaker dict, _breaker_shut_for, _trip_breaker), and auth/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>, which ratelimit.Limiter already 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 a Limiter and then blocked. device_codes._breaker is a bare dict without a lock.
  • Proposal: a Breaker(name, limit, window, shut_for) and a plain Counter(name, window) in ratelimit.py, on the same dual backend, used in all four places. The second-factor session count is a Counter keyed 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-66 and :126-127: the same count > self.limit guard and ValueError in take and place, 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 with count > 1 outside the tests.
  • Proposal: build take on place (dropping the mark), and blocked's local branch on a dry run of _take_at. hub/context/replicas.md:274 lists take in 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-120 refuse_project_over locks the owner's row, counts and raises.
  • routes/agents.py:92-105 (in create_profile) does the same inline for max_agents_per_human, with its error in limits.py:86.
  • orgs.py:317-332 (in create) does the same inline for max_orgs_per_human, with its error org_limit at orgs.py:63, not next to the other two in limits.py.
  • The counting queries are written again in limits.view (limits.py:134-145) for humans_at_limit and humans_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 by view. Move org_limit next to agent_limit and project_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_at counts "the humans with at least limit rows", but view and _names_at also 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-83 aliases() repeats of() (73-76) only to allow hub=None.
  • view (132-162) reads the stored limits five times (resolve once, of three times, aliases once), each through store.stored.
  • Proposal: rename to _owners_at; let of take hub: Settings | None and drop aliases; in view, compute the values from resolve'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.py is 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 RequestLimits classes in email_links and password_resets: tsk-f5sdcgr7vzj0 item 1.

Effort sum: 3S+2M.