Skip to content

fix: add input validation and rate limiting to /api/compare endpoint#2045

Merged
JhaSourav07 merged 4 commits into
JhaSourav07:mainfrom
vipul674:fix/compare-api-validation
Jun 1, 2026
Merged

fix: add input validation and rate limiting to /api/compare endpoint#2045
JhaSourav07 merged 4 commits into
JhaSourav07:mainfrom
vipul674:fix/compare-api-validation

Conversation

@vipul674
Copy link
Copy Markdown
Contributor

@vipul674 vipul674 commented May 31, 2026

Description

Fixes a security vulnerability in /api/compare where raw, unvalidated user1 and user2 query parameters were passed directly to getFullDashboardData() with no input validation and no IP-based rate limiting.

Every other API route (/api/streak, /api/github, /api/stats, /api/og) validates usernames via Zod schema and is covered by the middleware rate limiter. /api/compare was the sole exception — each request triggers 8 unauthenticated GitHub API calls (4 per user) with zero protection.

Changes

  1. lib/validations.ts — Added compareParamsSchema using the existing GITHUB_USERNAME_REGEX, with a .refine() to reject self-comparison
  2. app/api/compare/route.ts — Replaced manual null checks with Zod safeParse(), matching the pattern used by all other API routes
  3. middleware.ts — Added /api/compare/:path* to the rate-limiter matcher

Difficulty & Label Request

  • Assessed difficulty: level:beginner
  • Maintainer: please apply the level:beginner label if this assessment is appropriate, so this contribution is scored correctly under GSSoC 2026 guidelines.
  • Maintainer: please also apply the gssoc:approved label after review so this PR earns its base GSSoC points. Thank you!

Type

  • Bug fix

Testing & Verification

  • Code logic verified — Zod schema reuses existing GITHUB_USERNAME_REGEX
  • Rate limiter matcher now includes /api/compare
  • Pattern consistent with /api/github, /api/streak, /api/stats routes

Note on Copilot Review Comments

GSSoC 2026 Compliance & Transparency

Copilot AI review requested due to automatic review settings May 31, 2026 09:28
@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 31, 2026

@vipul674 is attempting to deploy a commit to the jhasourav07's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds support for a new /api/compare route and centralizes query parameter validation for the compare endpoint.

Changes:

  • Adds /api/compare/:path* to the middleware matcher config.
  • Introduces compareParamsSchema (Zod) for validating user1/user2 GitHub usernames (including “not the same user” constraint).
  • Updates app/api/compare/route.ts to use schema-based validation and return structured validation errors.

Reviewed changes

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

File Description
middleware.ts Adds the compare API path to middleware matching config.
lib/validations.ts Adds a Zod schema for compare endpoint query parameters and constraints.
app/api/compare/route.ts Switches to schema-based validation and returns validation details on bad requests.

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

Comment thread lib/validations.ts Outdated
Comment on lines +292 to +293
user1: z
.string({ error: 'Missing "user1" parameter' })
Comment thread lib/validations.ts Outdated
Comment on lines +298 to +299
user2: z
.string({ error: 'Missing "user2" parameter' })
Comment thread app/api/compare/route.ts Outdated
if (!parseResult.success) {
return NextResponse.json(
{ error: 'Both user1 and user2 query parameters are required.' },
{ error: 'Invalid parameters', details: parseResult.error.flatten() },
@Aamod007
Copy link
Copy Markdown
Collaborator

@vipul674 look for conflcts

@vipul674
Copy link
Copy Markdown
Contributor Author

vipul674 commented May 31, 2026

@Aamod007 No conflicts, the PR is mergeable. All code checks (Format, Lint, Typecheck, Test, Production Build) are passing. The remaining CI failures are Vercel authorization (needs maintainer action) and the GitHub API rate limits.

@Aamod007
Copy link
Copy Markdown
Collaborator

image work on these

@vipul674
Copy link
Copy Markdown
Contributor Author

@Aamod007 As you have requested, i have ensured that the checks pass, now only the vercel requires your authorization. Please review and let me know for any changes.

@Aamod007
Copy link
Copy Markdown
Collaborator

@vipul674 work on pipeline fix

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

1 similar comment
@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions github-actions Bot added needs-rebase This PR has merge conflicts and needs a rebase. labels May 31, 2026
@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

17 similar comments
@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

4 similar comments
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@vipul674 vipul674 force-pushed the fix/compare-api-validation branch from 3138d8d to 55183be Compare June 1, 2026 02:17
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

1 similar comment
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

⚠️ Hey @vipul674, this PR has merge conflicts with the main branch.

Please pull the latest changes and resolve the conflicts so we can review it!

git fetch origin
git rebase origin/main
# resolve any conflicts, then:
git push --force-with-lease

Once resolved, the needs-rebase label will be removed automatically on the next check. 🙌

@vipul674 vipul674 force-pushed the fix/compare-api-validation branch from 55183be to c178060 Compare June 1, 2026 02:20
@github-actions github-actions Bot added status:blocked This PR is blocked due to a failing CI check. and removed needs-rebase This PR has merge conflicts and needs a rebase. labels Jun 1, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

🚨 Hey @vipul674, the CI Pipeline is failing on this PR and it has been marked as status:blocked.

Please fix the issues before this can be reviewed. Here's how:

1. Run checks locally before pushing:

npm run format:check   # Check Prettier formatting
npm run lint           # Run ESLint
npm run typecheck      # TypeScript type check
npm run test           # Run unit tests (Vitest)
npm run build          # Verify production build passes

2. Auto-fix common issues:

npm run format         # Auto-fix formatting with Prettier
npm run lint -- --fix  # Auto-fix lint errors where possible

3. Check the full failure log here:
👉 View CI Run

Once you push a fix and the CI passes, the status:blocked label will be removed automatically. 💪

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

🚨 Hey @vipul674, the CI Pipeline is failing on this PR and it has been marked as status:blocked.

Please fix the issues before this can be reviewed. Here's how:

1. Run checks locally before pushing:

npm run format:check   # Check Prettier formatting
npm run lint           # Run ESLint
npm run typecheck      # TypeScript type check
npm run test           # Run unit tests (Vitest)
npm run build          # Verify production build passes

2. Auto-fix common issues:

npm run format         # Auto-fix formatting with Prettier
npm run lint -- --fix  # Auto-fix lint errors where possible

3. Check the full failure log here:
👉 View CI Run

Once you push a fix and the CI passes, the status:blocked label will be removed automatically. 💪

@github-actions github-actions Bot removed the status:blocked This PR is blocked due to a failing CI check. label Jun 1, 2026
@vipul674
Copy link
Copy Markdown
Contributor Author

vipul674 commented Jun 1, 2026

@Aamod007 Now the checks pass, Only the Vercel deployment authorization is blocking, please review and let me know for any other change

@vipul674
Copy link
Copy Markdown
Contributor Author

vipul674 commented Jun 1, 2026

@JhaSourav07 The changes have been approved by @Aamod007 . Please review

@Aamod007 Aamod007 added GSSoC 2026 mentor:Aamod007 type:refactor Code changes that neither fix a bug nor add a feature level:intermediate Moderate complexity tasks quality:clean PR follows clean coding practices, proper formatting, documentation, and maintainability standards. labels Jun 1, 2026
@JhaSourav07 JhaSourav07 merged commit b3407c6 into JhaSourav07:main Jun 1, 2026
9 of 10 checks passed
@JhaSourav07 JhaSourav07 added the gssoc:approved PR has been reviewed and accepted for valid contribution points label Jun 1, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 1, 2026

🎉 Congratulations @vipul674! Your PR has been successfully merged. 🚀

Thank you for contributing to CommitPulse. Your work helps us build a better tool for the community.

⚠️ Important for GSSoC Contributors:
You are strictly advised to join our Discord Server as it is mandatory for all GSSoC participants. All important announcements, point claims, and community discussions happen there.

Keep building! 💻✨

@github-actions github-actions Bot added this to the GSSoC 2026 milestone Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved PR has been reviewed and accepted for valid contribution points GSSoC 2026 level:intermediate Moderate complexity tasks mentor:Aamod007 quality:clean PR follows clean coding practices, proper formatting, documentation, and maintainability standards. type:refactor Code changes that neither fix a bug nor add a feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: /api/compare endpoint lacks input validation and rate limiting

4 participants