feat(chat): rate limit and validate /api/chat to protect the OpenAI budget#69
Open
Muawiya-contact wants to merge 1 commit into
Open
feat(chat): rate limit and validate /api/chat to protect the OpenAI budget#69Muawiya-contact wants to merge 1 commit into
Muawiya-contact wants to merge 1 commit into
Conversation
…udget Adds IP-based rate limiting (Upstash Redis, no-op until env vars are set) and Zod validation that returns a clean 400 on malformed bodies instead of an unhandled 500. Fixes codeforpakistan#68
|
@Muawiya-contact is attempting to deploy a commit to the Team BoostPanda Team on Vercel. A member of the Team first needs to authorize it. |
Author
|
@aliirz Heads-up: the Vercel preview needs a maintainer to authorize the deploy on your side I don't have access to the team's Vercel project. Happy to adjust anything if the build surfaces an issue. |
Contributor
|
Let me test this locally @Muawiya-contact |
Author
|
@aliirz, any updates? |
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.
Fixes #68
What & why
POST /api/chatwas public with no rate limiting and no input validation. Every request fans out to three billed OpenAI calls (the query-type router, an embedding, and the streaming completion), so a simple loop against the endpoint could run up unbounded cost — a real exposure for a nonprofit project. Malformed bodies also threw an unhandled 500 atmessages[messages.length - 1].This PR hardens the endpoint with the tools already in the stack.
Changes
lib/ratelimit.ts): IP-based sliding window (20 req/min) via@upstash/ratelimit+ Upstash Redis. Returns429when exceeded.app/api/chat/route.tsx): validates the body with Zod inside atry/catchand returns a clean400on malformed input instead of a 500. Message shape is preserved (.passthrough()), and theroleenum matches the AI SDK'sMessagetype, so streaming behavior is unchanged for valid requests.@upstash/ratelimitand@upstash/redisto dependencies.Safe to merge as-is
The limiter is disabled unless both env vars are set, so this changes nothing in local dev, preview, or production until you opt in. Nothing downstream of the guards was touched.
Enabling in production
Create a Redis database in Upstash (separate from the existing QStash instance) and set:
Tune the window in
lib/ratelimit.ts(Ratelimit.slidingWindow(20, "1 m")) as needed.Testing
POST /api/chatwith{}→400(previously an unhandled 500).429.Notes
Scope is endpoint hardening only. The retrieval-context fix is a separate PR/issue and doesn't overlap functionally.