analytics/event: add 60/hour rate limit + fix slowapi in test mini-app - #34
Open
brooksRoley wants to merge 1 commit into
Open
analytics/event: add 60/hour rate limit + fix slowapi in test mini-app#34brooksRoley wants to merge 1 commit into
brooksRoley wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CTO session — 2026-08-12
Deferred finding from the 2026-08-11 Staff Engineer session (flag_count=1):
POST /api/analytics/eventhad no rate limit. An authenticated user could spam it at arbitrary rate, inflatingsession_eventswith junk rows and skewing every funnel metric in the admin dashboard.What changed
server/app/analytics/router.pyRequestto FastAPI imports (required by slowapi)limiterfrom..ratelimit(shared singleton, matches auth/portrait pattern)@limiter.limit("60/hour")tolog_event+request: Requestas first paramserver/tests/test_analytics.py_make_app()to attachapp.state.limiter = limiterand registerRateLimitExceededexception handler — without this,@limiter.limit()raises unhandled exceptions in the test mini-app instead of returning 429reset_limiterautouse fixture (clears MemoryStorage before each test — same pattern astest_auth_ratelimit.pyandtest_portrait_ratelimit.py)TestEventRateLimit: 3 tests — first call passes (204), 61st returns 429, Retry-After header presentRate limit choice
60/hour(= 1/minute average). Generous enough for any legitimate UI session — the 16 valid events fire at most a handful of times per session — but stops a logged-in user from bulk-inserting analytics noise. Auth login is 5/minute; analytics events are lower-risk DB writes, so the hourly window is appropriate.Test results (not runnable in cloud agent; pattern verified against merged
test_auth_ratelimit.py)The 3 new tests follow the identical structure to
TestLoginRateLimit/TestRegisterRateLimit(PR #16, merged). Thereset_limiterfixture is the same pattern used intest_auth_ratelimit.py.Existing suite: 9 tests in
TestEventAllowlist+TestProviderAllowlistare unchanged in behaviour; the addedreset_limiterautouse fixture ensures their single-request-per-test pattern never hits the new limit.Generated by Claude Code