feat(ratelimit): add per-IP rate limiting for registration API #526
No reviewers
Labels
No labels
Compat/Breaking
Kind/Bug
Kind/Documentation
Kind/Enhancement
Kind/Feature
Kind/Security
Kind/Testing
Priority
Critical
Priority
High
Priority
Low
Priority
Medium
Reviewed
Confirmed
Reviewed
Duplicate
Reviewed
Invalid
Reviewed
Won't Fix
Status
Abandoned
Status
Blocked
Status
Need More Info
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
thwap/thwap-pagesd!526
Loading…
Reference in a new issue
No description provided.
Delete branch "feat/rate-limit"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
Add per-IP rate limiting to the site registration API.
internal/ratelimitpackage — per-IP token buckets (fromgolang.org/x/time/rate, promoted to a direct dependency) keyed byClientIP, applied toPOST /api/v1/sitesonly. Exceeding the limit returns429 Too Many RequestswithRetry-After: 1.rate_limit.{enabled,rps,burst}section, injected asTHWAP_PAGESD_RATE_LIMIT_*env vars and added to the Helm chart values/README. Disabled by default, preserving existing behavior.thwap_pagesd_rate_limited_totalcounter on the/metricsendpoint.Why
Phase 9 task #119 — protects the registration endpoint from abuse.
Testing
go build ./...go vet ./...go test -race -count=1 ./...— full suite passes (new: ratelimit unit tests + handler-level test confirming only the registration route is limited)gofmt -l ./cmd ./internal ./pkgcleangolangci-lint run— 0 issueshelm lint+helm templaterender the rate_limit env keysBreaking Changes
None. Rate limiting is opt-in via
rate_limit.enabled.Notes
Closes #119
Add a rate_limit configuration section and a new internal/ratelimit package that limits the POST /api/v1/sites endpoint per client IP using a token bucket from golang.org/x/time/rate (promoted to a direct dependency). - rate_limit.{enabled,rps,burst} config, injected as THWAP_PAGESD_RATE_LIMIT_* env vars and added to the Helm chart - per-IP buckets bounded at 1024 entries with idle cleanup, so spoofed or retired addresses cannot grow memory without bound - exceeded limits return 429 Too Many Requests with Retry-After: 1 - new thwap_pagesd_rate_limited_total metric - disabled by default, preserving existing behavior Tests cover burst-then-limited, per-IP isolation, disabled/noop limiters, the rate-limited metric, cleanup, map eviction, and a handler-level test confirming only the registration route is limited. closes #119Review
No blocking issues found.
go build ./...,go vet ./..., andgo test -race -count=1 ./...pass, including the newinternal/ratelimitand handler-level tests. Token-bucket logic, per-IP isolation, bounded map, metrics counter, config/env wiring, and Helm chart all check out.Suggestions
internal/ratelimit/ratelimit.go:53— buckets key ongin.ClientIP(), but the daemon never configures trusted proxies. Behind a reverse proxy or the Helm-chart ingress, every client resolves to the proxy's IP, collapsing per-IP limiting into a single global bucket. Consider a trusted-proxies/forwarded-headers config or documenting the limitation. Filed as #527.internal/ratelimit/ratelimit.go:122—StartCleanupis never called fromcmd/thwap-pagesd/main.go, so the claimed "periodic idle cleanup" never runs (memory stays bounded viamaxIPseviction regardless). Wire it up or remove it. Filed as #528.internal/ratelimit/ratelimit_test.go—TestEvictionBoundsMaponly generates 256 distinct IPs (i%256) againstmaxIPs = 1024, so eviction never triggers and the test passes trivially; the eviction path is untested. AlsoforIP's "oldest-inserted" comment mismatches the fewest-tokens policy. Filed as #529.Question
rate_limit.enabled: truewithrps <= 0orburst <= 0silently disables limiting rather than erroring. Intentional? Consider validating at config load.Praise
internal/ratelimitowns all bucket/eviction/cleanup logic;httpapistays thin; nil-receiver and disabled-limiter paths are safe and covered.Retry-Afterheader asserted.Approving.