fix(api): enforce route-level rate limits#573
Conversation
|
CodeAnt AI is reviewing your PR. |
|
@eshaanag is attempting to deploy a commit to the Karan Mani Tripathi 's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughAdds enforceApiRateLimit helper and types, wires ai/general/video limiters into multiple POST routes (auth → rate-check → parse), updates middleware route matching and failure semantics, and adds tests plus a Jest mock for limiter behavior. ChangesAPI Rate Limiting Infrastructure
Middleware
AI route integration
General mutation routes
Video route integration
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@coderabbitai review |
|
CodeAnt AI finished reviewing your PR. |
|
Addressed the review findings in
Validation after the update:
|
|
Hi @eshaanag! Thanks for your work on this. Before we can merge, please address the following:
Once these are fixed, the PR will be automatically evaluated again. Let us know if you need any help! |
|
CodeAnt AI is running Incremental review |
|
CodeAnt AI Incremental review completed. |
|
Merged the latest |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/__tests__/lib/api-rate-limit.test.ts`:
- Around line 31-49: The test asserts exact equality for the "Retry-After"
header which flakes because Date.now() timing can shift; update the test in
src/__tests__/lib/api-rate-limit.test.ts to parse the response header from
enforceApiRateLimit (use response?.headers.get("Retry-After")) as a number
(e.g., parseInt) and assert it falls within a small acceptable range (e.g., >=29
and <=31 or >=29) instead of expecting "30" exactly; keep the rest of the
assertions (status and X-RateLimit-Limit) unchanged and reference the existing
createLimiter and enforceApiRateLimit calls to locate the assertion to modify.
In `@src/app/api/video/generate/route.ts`:
- Around line 13-14: The route is being rate-limited twice (middleware using
videoLimiter.limit(userId || ip) and this route calling
enforceApiRateLimit(videoLimiter, email, 'video'), causing inconsistent 429s;
fix by removing the duplicate quota path: either have the middleware skip
/api/video/* requests (remove or guard the call to videoLimiter.limit(userId ||
ip) in the middleware) so this route's enforceApiRateLimit(videoLimiter, email,
'video') is the single source of truth, or change the middleware to call
enforceApiRateLimit(...) itself (use enforceApiRateLimit with the email key and
standardized response shape instead of videoLimiter.limit(...) there); update
whichever you change so only one limiter invocation exists and all video
endpoints use the same key and response shape.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 6c6e1d6d-5518-495e-a84d-cc22ede9ad48
📒 Files selected for processing (15)
jest.setup.tssrc/__tests__/api/ask-ai.test.tssrc/__tests__/lib/api-rate-limit.test.tssrc/app/api/ai-career-chat-agent/route.tsxsrc/app/api/ask-ai/route.tssrc/app/api/doubts/[id]/bookmark/route.tssrc/app/api/doubts/check-similarity/route.tssrc/app/api/doubts/route.tssrc/app/api/replies/route.tssrc/app/api/rooms/route.tssrc/app/api/tags/route.tssrc/app/api/video/generate/route.tssrc/app/api/video/script/route.tssrc/lib/api-rate-limit.tssrc/middleware.tsx
|
CodeAnt AI is running Incremental review |
|
Automated Detailed Review
Please address the above items and request a review again. |
|
Thanks for the review. I rechecked both items:
The repo-owned checks are passing, and the earlier approval is still present. Please re-review when convenient. |
|
Hi! This PR currently has merge conflicts with the main branch. Please rebase/merge main and resolve conflicts so we can review and merge it. Thank you! |
|
Hi @eshaanag! Thanks for submitting this PR. Here is a detailed review of your changes: 🤖 Bot Review Feedback to ResolveCodeRabbit and CodeAnt have highlighted some important issues that need to be addressed. Please prioritize fixing these:
Avoid exact
Suggested test adjustment...
Unify video throttling with middleware or this still returns inconsistent 429s.
⚙️ CI/CD PipelineYour PR currently has some failing checks:
Note: We have recently fixed database configuration and This is an automated review assessment. Please address the feedback above to move your PR forward. |
|
|
CodeAnt AI is running Incremental review |
Thanks for using CodeAnt! 🎉We're free for open-source projects. if you're enjoying it, help us grow by sharing. Share on X · |
|
CodeAnt AI Incremental review completed. |
bab1031 to
7ca8fc7
Compare
7ca8fc7 to
8b7d60d
Compare
User description
Fixes #506
Summary
/api/bookmarksremains read-onlyValidation
git diff --check: passedSummary by CodeRabbit
New Features
Tests
Chores
CodeAnt-AI Description
Apply route-level rate limits to AI, video, and write actions
What Changed
Impact
✅ Fewer AI request bursts✅ Fewer video generation overages✅ Clearer rate-limit errors💡 Usage Guide
Checking Your Pull Request
Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.
Talking to CodeAnt AI
Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:
This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.
Example
Preserve Org Learnings with CodeAnt
You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:
This helps CodeAnt AI learn and adapt to your team's coding style and standards.
Example
Retrigger review
Ask CodeAnt AI to review the PR again, by typing:
Check Your Repository Health
To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.