fix(#97): add auth middleware, rate limiting, and model allowlist to /api/chat#103
Merged
durdana3105 merged 1 commit intoMay 26, 2026
Merged
Conversation
…lowlist to /api/chat The POST /api/chat route accepted requests from unauthenticated callers and allowed the client to specify any model and any max_tokens value, creating a direct financial risk via API cost exhaustion. Changes: - src/backend/middlewares/requireAuth.js (new): verifies the Supabase JWT from the Authorization header before passing the request through; returns 401 for missing or invalid tokens - src/backend/routers/chatRoutes.js: apply requireAuth and a per-user in-memory rate limiter (20 req/min) to the /chat route; whitelist allowed models and cap max_tokens server-side at 512 regardless of caller input
|
@anshul23102 is attempting to deploy a commit to the durdana3105's projects Team on Vercel. A member of the Team first needs to authorize it. |
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.
Closes #97
Problem
The
POST /api/chatroute accepted requests from any HTTP client with no authentication check. Themodelandmax_tokensfields were taken directly from the request body with no validation, allowing any caller to substitute an expensive model or inflate token usage to exhaust the platform's OpenRouter API credit.Changes
src/backend/middlewares/requireAuth.js(new file)Authorization: Bearer <token>headersupabase.auth.getUser(token)to validate the JWT server-sidereq.useron success; returns401for missing or invalid tokenssrc/backend/routers/chatRoutes.jsrequireAuthmiddleware before the route handler429when exceededmodelagainst an allowlist (openai/gpt-3.5-turbo,openai/gpt-4o-mini); reject unknown models with400max_tokensat512server-side regardless of what the caller sendsmessage/messagesfieldTesting