- architecture/: overview, technology stack, auth (OIDC + built-in IndieAuth), syndication (POSSE) rewritten; new database.md, deployment.md and indieweb-protocols.md; legacy stack marked historical - decisions/: reconstructed ADR-060 and ADR-063; 31 status corrections (superseded auth ADRs, proposed -> accepted); INDEX rebuilt - design/: reconstructed the v1.6.0 documents the webmention code cites; reports for 18 v2 releases without a design folder; INDEX rebuilt - projectplan/: BACKLOG rewritten (shipped items removed, open issues from the reviews added); RELEASE.md for v2.0.0-v2.10.1; INDEX - standards/: development setup (uv, tests, DEV_MODE), testing (CI test stage), git and release flow, versioning, coding standards, cookies - README, CLAUDE.md, .env.example (every setting the code reads, with defaults) brought up to date - 137 broken links repaired (old absolute paths, moved files); stale ADR references in code comments corrected (comments only) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
85 lines
3.5 KiB
Markdown
85 lines
3.5 KiB
Markdown
# v1.6.0 Design: Webmention Rate Limiting
|
||
|
||
> Reconstructed on 2026-11-03 from the implementation; the original v1.6.0 document was lost. Later changes: v2.6.8–v2.7.2 (see CHANGELOG).
|
||
> The rate limiter itself has not changed since v1.6.0; v2.10.0 reused `RateLimiter` for the WebSub hub. Describes `starpunk/rate_limiter.py` and `starpunk/routes/webmention.py` as of v2.10.1.
|
||
|
||
Related: ADR-063, [api-contract.md](api-contract.md).
|
||
|
||
**Note on ADR numbering:** the code cites "ADR-064: Webmention Rate
|
||
Limiting". In this repository ADR-064 is the OIDC admin login decision
|
||
(v2.0.0); the webmention rate-limiting ADR from v1.6.0 was lost with the
|
||
other v1.6.0 documents and its number was reused. This document is the
|
||
surviving description of that decision.
|
||
|
||
## Goal
|
||
|
||
Stop `POST /webmention` from being used to fill the database or the
|
||
validation queue (each accepted webmention costs one outbound fetch).
|
||
|
||
## Algorithm
|
||
|
||
`RateLimiter(limit, window_seconds)`: sliding-window log, in memory.
|
||
|
||
- Per key, a list of request timestamps (`time.time()`), guarded by one
|
||
`threading.Lock`.
|
||
- `is_allowed(key)`: drop timestamps older than the window; if the count is
|
||
`>= limit` refuse (not recorded); otherwise record and allow.
|
||
- `get_retry_after(key)`: seconds until the oldest timestamp leaves the
|
||
window, clamped to 1..window.
|
||
- `cleanup(max_idle_seconds=300)`: drop keys with no recent requests.
|
||
- `get_request_count(key)`: current count (tests/logging).
|
||
|
||
In-memory state was chosen to match the other in-process caches (the
|
||
v1.6.0 code cites the worker's endpoint `_discovery_cache`, removed in
|
||
v2.7.0); no new table, no Redis.
|
||
|
||
## Webmention limits
|
||
|
||
Two module-level limiters, created by `init_rate_limiters(app)` in
|
||
`create_app()`:
|
||
|
||
| Setting | Default | Key |
|
||
|---|---|---|
|
||
| `WEBMENTION_RATE_LIMIT_ENABLED` | true | Off: both limiters are `None`, everything allowed |
|
||
| `WEBMENTION_RATE_LIMIT_IP` | 60 | Client IP |
|
||
| `WEBMENTION_RATE_LIMIT_DOMAIN` | 10 | Host (`netloc`) of the `source` URL |
|
||
| `WEBMENTION_RATE_LIMIT_WINDOW` | 60 s | Both |
|
||
|
||
`check_rate_limit(ip, source_domain)` checks the IP first, then the source
|
||
domain (if one could be parsed). Only POST is limited, in the webmention
|
||
blueprint's `before_request`, before any validation, reading `source` from
|
||
the form or JSON body.
|
||
|
||
Client IP (`get_client_ip`): the first `X-Forwarded-For` entry when
|
||
`TRUSTED_PROXY=true`, else `request.remote_addr`. Behind Caddy without
|
||
`TRUSTED_PROXY` every client shares the proxy's address (documented in
|
||
`.env.example` since v2.10.1).
|
||
|
||
## Response
|
||
|
||
```
|
||
HTTP/1.1 429
|
||
Retry-After: <WEBMENTION_RATE_LIMIT_WINDOW>
|
||
{"error": "rate_limit_exceeded",
|
||
"error_description": "Too many requests from your IP. Please retry after N seconds."}
|
||
```
|
||
|
||
The description carries the computed wait; the `Retry-After` header carries
|
||
the full window.
|
||
|
||
## Other users
|
||
|
||
`routes/websub.py` (v2.10.0) has its own `RateLimiter(30, 60)` per client
|
||
IP for subscription requests, calling `cleanup()` every 500 requests.
|
||
|
||
## Known limits
|
||
|
||
- Counters are per gunicorn process, so the effective limit is up to
|
||
limit × processes, and they reset on restart.
|
||
- `cleanup_rate_limiters()` exists but nothing calls it, so the webmention
|
||
limiters never drop a key (old timestamps are trimmed when the key is
|
||
checked again, but the key stays); memory grows slowly with distinct
|
||
client IPs and source domains until the process restarts.
|
||
- Requests refused by the IP limit are not recorded, and a request refused
|
||
by the domain limit has already been counted against the IP.
|