feat: add Upstash rate limiting + per-user tool-call cap#33
Open
Shubham Kumar (shubhamkr790) wants to merge 1 commit into
Open
feat: add Upstash rate limiting + per-user tool-call cap#33Shubham Kumar (shubhamkr790) wants to merge 1 commit into
Shubham Kumar (shubhamkr790) wants to merge 1 commit into
Conversation
- Sliding-window rate limiter on /api/chat (20 req/60s default) - Monthly per-user tool-call cap enforced server-side (1000/month default) - Both opt-in via UPSTASH_REDIS_REST_URL/TOKEN env vars - Rate limit returns 429 with Retry-After header - Tool-call cap returns 403 when exhausted, auto-resets monthly - Counter keys auto-expire after 35 days (self-cleaning) - Zero impact on existing deployments without Upstash configured New dependencies: @upstash/ratelimit, @upstash/redis Addresses the security gap documented in README (lines 124-131).
|
Review the following changes in direct dependencies. Learn more about Socket 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.
What
Adds two server-side guards that the README explicitly calls out as missing (lines 124–131):
/api/chat— 20 requests / 60 s per user (configurable).Both are opt-in: if
UPSTASH_REDIS_REST_URLandUPSTASH_REDIS_REST_TOKENare not set, everything passes through and existing deployments are unaffected.Why this matters
How it works
route.tsPOST handlerRetry-Afterheaderroute.tsPOST handlersetup.tsonFinishtrustclaw:toolcap:{userId}:{YYYY-MM}— new month = new key, old ones self-clean after 35 days.RATE_LIMIT_CHAT_REQUESTS,RATE_LIMIT_CHAT_WINDOW,MONTHLY_TOOL_CALL_CAP).Changed files
src/server/clients/ratelimit.ts— new module: Upstash Redis client, rate limiter, cap check/incrementsrc/app/api/chat/route.ts— rate limit + cap enforcement before agent executionsrc/server/api/routers/trustclaw/agent/setup.ts— tool-call count increment in onFinish.env.example— documents new env varspackage.json— adds@upstash/ratelimit,@upstash/redisNew dependencies
@upstash/ratelimit— sliding window rate limiter built for serverless@upstash/redis— REST-based Redis client (works on Vercel Edge + serverless)Testing
tsc --noEmit— only pre-existing Prisma-generation errors remain).