Skip to content

Fix: secure paid Groq API route and enforce rate limits #163 - #214

Merged
Premshaw23 merged 1 commit into
Premshaw23:masterfrom
omnipotentchaos:feature/secure-groq-api
May 21, 2026
Merged

Fix: secure paid Groq API route and enforce rate limits #163#214
Premshaw23 merged 1 commit into
Premshaw23:masterfrom
omnipotentchaos:feature/secure-groq-api

Conversation

@omnipotentchaos

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • 🐛 Bug fix
  • ✨ New feature
  • 📚 Documentation
  • 🎨 UI/UX improvement
  • ⚡ Performance improvement
  • 🔒 Security fix

Description

Secures the paid /api/groq endpoint by introducing authentication middleware, input length constraints, server-side per-user rate limiting, and detailed usage logging to prevent quota abuse and billing spikes.

Related Issues

Closes #163

Changes Made

  • Authentication Enforcement: Configured /api/groq/route.js to parse the Authorization header and authenticate the requests with Firebase Admin SDK (verifyFirebaseToken).
  • Sliding-Window Rate Limiting: Implemented a per-user server-side rate limiter configured for 10 requests per minute returning 429 Too Many Requests upon limit violation.
  • Input Validation: Added early validation for empty inputs and strict message limits capped at 2000 characters.
  • Audit Logging: Added log messages identifying the authenticated user's UID and email for quota mapping.
  • Unit Testing: Created automated test suite covering all authentication, validation, and rate-limiting branches.

Testing

How did you test these changes?

  • Tested locally (Ran npm test - all 46 tests across 5 suites pass successfully)
  • Verified local production build compiles successfully via npm run build

Test Suite Output

PASS components/_tests_/groqRoute.test.js
PASS components/_tests_/AuthForm.test.js
PASS components/_tests_/registerRoute.test.js
PASS components/_tests_/authUtils.test.js
PASS components/_tests_/formValidation.test.js

Test Suites: 5 passed, 5 total
Tests:       46 passed, 46 total

Copilot AI review requested due to automatic review settings May 20, 2026 19:05
@vercel

vercel Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

@omnipotentchaos is attempting to deploy a commit to the Prem Shaw's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR secures the paid /api/groq Next.js route by enforcing Firebase authentication and adding a per-user rate limit, with a new Jest suite to validate key security/abuse-prevention behaviors.

Changes:

  • Require a Firebase ID token (via verifyFirebaseToken) for /api/groq requests and return 401 when unauthenticated.
  • Add a sliding-window, per-user rate limiter (10 requests/min) returning 429 when exceeded.
  • Add a Jest test suite for auth/validation/rate-limiting behavior of the route.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
app/api/groq/route.js Adds Firebase auth enforcement, in-memory per-user rate limiting, and audit logging for the paid Groq endpoint.
components/_tests_/groqRoute.test.js Introduces Jest coverage for the /api/groq route’s auth, validation, rate limiting, and success path.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread app/api/groq/route.js
Comment on lines +34 to +41
const authorization = request.headers.get("authorization");
const token = authorization?.split(" ")[1];

const decodedToken = await verifyFirebaseToken(token);

if (!decodedToken) {
return jsonError("Unauthorized", 401);
}
Comment thread app/api/groq/route.js
Comment on lines +8 to +29
const RATE_LIMIT_WINDOW = 60 * 1000; // 1 minute
const MAX_REQUESTS_PER_WINDOW = 10; // max 10 requests per minute
const rateLimitMap = new Map();

const isRateLimited = (userId) => {
const now = Date.now();
if (!rateLimitMap.has(userId)) {
rateLimitMap.set(userId, [now]);
return false;
}

const timestamps = rateLimitMap.get(userId);
const validTimestamps = timestamps.filter((t) => now - t < RATE_LIMIT_WINDOW);

if (validTimestamps.length >= MAX_REQUESTS_PER_WINDOW) {
rateLimitMap.set(userId, validTimestamps);
return true;
}

validTimestamps.push(now);
rateLimitMap.set(userId, validTimestamps);
return false;
Comment thread app/api/groq/route.js
}

// Usage logging with user ID for audit/quota tracking
console.log(`[nova-ai-quota-tracker] Paid Groq API request by User UID: ${decodedToken.uid} (${decodedToken.email}) at ${new Date().toISOString()}`);
Comment thread components/_tests_/groqRoute.test.js Outdated
Comment on lines +22 to +26
describe("POST /api/groq - Paid API Security, Authentication, and Rate Limiting Tests", () => {
beforeEach(() => {
jest.clearAllMocks();
process.env.GROQ_API_KEY = "mock-groq-key";
});
@omnipotentchaos
omnipotentchaos force-pushed the feature/secure-groq-api branch from 4edf3c5 to 0179c8c Compare May 20, 2026 19:09
@Premshaw23

Copy link
Copy Markdown
Owner

There is merge conflict fix it

@Premshaw23

Copy link
Copy Markdown
Owner

Sync to the live repo

@omnipotentchaos
omnipotentchaos force-pushed the feature/secure-groq-api branch from 0179c8c to db38d40 Compare May 20, 2026 20:00
@Premshaw23
Premshaw23 merged commit cebffeb into Premshaw23:master May 21, 2026
4 of 5 checks passed
@Premshaw23

Copy link
Copy Markdown
Owner

done👍

@github-actions github-actions Bot added GSSoC'26 Part of GirlScript Summer of Code 2026 mentor:Ayushh-Sharmaa GSSoC: Mentor — @Ayushh-Sharmaa and removed mentor:Premshaw23 labels Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

good first issue Good for newcomers gssoc:approved GSSoC'26 Part of GirlScript Summer of Code 2026 level:intermediate mentor:Ayushh-Sharmaa GSSoC: Mentor — @Ayushh-Sharmaa type:feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SECURITY: Groq endpoint accepts unauthenticated requests

3 participants