~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

The host limits how much one caller asks, and how often

Merged
Merged chadtmiller.com asked to merge into main from chadtmiller.com/group-host:host/rate-limits

The host now limits how much one caller may ask, and how often: by client address for the routes anyone can call, by group for token writes, and by person for reports and join requests. It's on by default (RATE_LIMITS=off turns it off), and past a limit a request gets a 429 RateLimitExceeded with Retry-After and RateLimit-* headers.

Claude Opus 5.5 · high effort

What changes

Limits. A new limits module keeps fixed windows of points per limit and key, in memory (one host runs over a data directory, so counts are exact without Redis). The numbers follow the reference PDS where it has an equivalent route, and the host adds its own for routes the PDS leaves open, its OAuth endpoints among them:

WhatPoints / windowCounted by
every request but _health, /tls/ask, subscribeRepos, getRepo3000 / 5 minaddress
sync.getRepo6000 / 5 minaddress
createGroup, provisionGroup100 / 5 minaddress
/oauth/par300 / 5 minaddress
/mcp/register30 / 5 minaddress
/console/login, /console/move/login30 / 5 minaddress
searchActors600 / 5 minaddress
server.createSession30 / 5 min, 300 / dayhandle + address
repo.* / space.* writes5000 / hour, 35000 / day (create 3, update 2, delete 1)group
repo.uploadBlob1000 / daygroup
updateGroupHandle (a real change)10 / 5 min, 50 / daygroup
createReport (one the host takes into the queue)10 / 5 min, 100 / dayperson
requestJoin10 / 5 min, 100 / dayperson

The client's address. The host is served with connect info. The client is the peer address unless that's loopback, private or link-local (a proxy here), in which case X-Forwarded-For is read from the right past each such address. IPv6 counts by /64. Calls the host makes inside itself (MCP tools through routes()) carry no address and spend nothing by address, but still count against group and person limits.

Refusals. XrpcError gains limited: Option<Spent>. A limited refusal answers 429 RateLimitExceeded with Retry-After, RateLimit-Limit, -Remaining, -Reset and -Policy, Access-Control-Allow-Origin: *, and those headers exposed. oauth::cors now exposes them too, so a browser app can read why its token write was refused.

Config. RATE_LIMITS (on by default, off) in Config, in deploy/docker-compose.yml as GROUP_HOST_RATE_LIMITS, and documented in docs/reference/config.md and docs/deploying.md.

The console. handleFetch in web/src/hooks.server.ts passes the browser request's X-Forwarded-For (which Caddy sets) on to the host, so a page's server-side loads count against the person and not against the console. handle already passes every header through.

Harness. harness/world.mjs and examples/kicker/harness/world.mjs start the host with RATE_LIMITS=off, because the dev world, e2e, just look and just shots are all one address asking for every browser and command. The cargo tests' hosts (tests/common) are served with connect info like production and keep limits on.

Docs. A new design page, docs/design/limits.md, in the overview and the sidebar. RateLimitExceeded is added to the errors any method may answer in docs/reference/api.md, and docs/deploying.md notes trusted_proxies for a CDN in front of Caddy.

Why

Before this, nothing limited anything. /oauth/par fetched any client_id URL and cached only successful fetches, so random ids turned the host into a request reflector. /mcp/register kept a row per call for a day. searchActors forwarded every call to the typeahead service or the PLC directory, which could get the host's IP limited there, and that's the directory group creation and handle changes need. Any atproto account could fill a group's report queue or join requests, and a token could write or upload without bound. The reference PDS's design (fixed windows, global per-IP plus per-route plus shared write buckets, a 429 with these headers) is the convention atproto clients already handle. It does nothing for OAuth (oauth-provider has a TODO for it), so those routes have limits of the host's own.

Done in-process, not at the proxy: stock Caddy has no rate limiting, and other people deploy this host from the compose file as it is.

How it was checked

  • tests/limits.rs:
    • The limiter's windows: spending, refusing without spending, a new window, separate keys, and the off switch.
    • Client-address resolution: public peers, proxies, unparseable entries, IPv4-mapped, IPv6 /64 keys.
    • A flood of /mcp/register refused at 31 with every header, while another forwarded address and _health still get through.
    • The global limit sparing _health and getRepo.
    • RATE_LIMITS=off.
    • Write points by kind through space.* writes.
    • requestJoin counted per person.
  • tests/config.rs: RATE_LIMITS on, off, and a bad value.
  • Run locally: the whole workspace's tests and clippy. The runner's check, test (the 100% gate, on the spaces alpha), web, e2e and docs pass at a4bd81d.

Shortcomings

  • The numbers are a starting point. A large PDS's many members syncing spaces or sending notifyRepoUpdate all come from that PDS's few addresses, against 3000 per 5 minutes. That's fine at alpha scale. If it starts to bite, those server-to-server methods should count by the signing DID instead.
  • A client on a private network that reaches the host directly, not through a proxy, could name any address in X-Forwarded-For. Production reaches the host only through Caddy, which overwrites the header.
  • There's no per-group storage quota yet. The blob limit caps uploads per day, not bytes kept.

Review

Start at crates/group-host/src/limits.rs (the limits table, client, by_address), then xrpc.rs's refusal headers and the one-line spend calls in the handlers.

Approved

This adds in-memory fixed-window rate limits to the host. They count by client address, by group for writes, and by person for reports and joins. The 429 RateLimitExceeded carries the PDS-style headers, and RATE_LIMITS=off turns the limits off. I read limits.rs, xrpc.rs, lib.rs and the hooks.server.ts change, and checked them against AGENTS.md. I did not read the per-handler spend call sites or the tests/docs/harness files, so those are unreviewed. No runs showed for this pull, so I could not see the checks.

The client-address resolution looks right. It takes the peer address unless that is a proxy address, then walks X-Forwarded-For from the right. Public peers cannot spoof it, and IPv6 counts by /64. Windows are swept and a refusal spends nothing.

One minor point, which I'm not asking you to change: when a request passes the first limit of a pair and is refused by the second (for example BY_ADDRESS then NEW_GROUPS, or the 5-minute then the daily session limit), the first limit's point stays spent. That is harmless at these numbers.

Approved

Carried forward: the change this pull makes at a30fed154535ca513c94cb276bc43168b814c655 is the one reviewed at 030abc37128ff8a72c28ece184351630a95f279b, so that review stands. What is new comes from its base.

Approved

The change since my approval moves the createReport spend to after the validation checks, so a refused report no longer costs the reporter a point. The new position is correct, because the spend still runs before the event is written. The design doc now says so. I found no problems and I'm keeping my approval.

Approved

Since my approval, the only change is in limits.rs. The const fn limit helper became a limit! macro, so each limit is built as a constant with nothing running. Every name, point count and window is unchanged, so the behaviour is the same. I found no problems and I'm keeping my approval.

Approved

Since my approval, the only change is a test addition in tests/limits.rs. It checks that ended windows are let go after a minute and open ones are kept. It touches no production code. I found no problems and I'm keeping my approval.

Merged as bbb6d41