Commit b3407c6
authored
fix: add input validation and rate limiting to /api/compare endpoint (JhaSourav07#2045)
### 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
- [x] Bug fix
### Testing & Verification
- [x] Code logic verified — Zod schema reuses existing
`GITHUB_USERNAME_REGEX`
- [x] Rate limiter matcher now includes `/api/compare`
- [x] Pattern consistent with `/api/github`, `/api/streak`, `/api/stats`
routes
### Note on Copilot Review Comments
- Comments JhaSourav07#1 and JhaSourav07#2 suggest `z.string({ error: ... })` is invalid —
this is incorrect for **Zod v4** (`^4.4.3`). The `error` option is valid
and matches the existing pattern at `lib/validations.ts:281`
(`githubParamsSchema`).
- Comment JhaSourav07#3 suggests normalizing `error.flatten()` — this matches the
exact pattern used by every other API route (`/api/github/route.ts:31`,
`/api/streak/route.ts`, `/api/stats/route.ts`). Consistency with the
existing codebase takes precedence.
### GSSoC 2026 Compliance & Transparency
- **AI Assistance Disclosure:** AI assistance was used to develop this
solution. All generated logic has been audited, verified, and locally
tested by me to meet project standards.
- Closes JhaSourav07#20462 files changed
Lines changed: 21 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
355 | 355 | | |
356 | 356 | | |
357 | 357 | | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
358 | 378 | | |
359 | 379 | | |
360 | 380 | | |
| |||
486 | 506 | | |
487 | 507 | | |
488 | 508 | | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | | - | |
496 | | - | |
497 | | - | |
498 | | - | |
499 | | - | |
500 | | - | |
501 | | - | |
502 | | - | |
503 | | - | |
504 | | - | |
505 | | - | |
506 | | - | |
507 | | - | |
508 | | - | |
509 | 509 | | |
510 | 510 | | |
511 | 511 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
0 commit comments