Skip to content

fix: sanitize non-numeric pagination query parameters#375

Open
Clinton6801 wants to merge 8 commits into
geevapp:mainfrom
Clinton6801:fix/non-numeric-pagination
Open

fix: sanitize non-numeric pagination query parameters#375
Clinton6801 wants to merge 8 commits into
geevapp:mainfrom
Clinton6801:fix/non-numeric-pagination

Conversation

@Clinton6801

@Clinton6801 Clinton6801 commented Jun 25, 2026

Copy link
Copy Markdown

Closes #358


Description

Fixes issue #358 where non-numeric pagination query parameters were causing 500 errors across multiple list endpoints.

Problem

Several API endpoints were using parseInt() directly on query parameters without proper validation. When clients passed non-numeric values (e.g., ?page=abc&limit=xyz), parseInt() would return NaN, which Prisma would reject with a 500 Internal Server Error instead of gracefully handling the invalid input.

Solution

Created a reusable parsePaginationParam() utility function in lib/validation.ts that:

  • Safely parses query parameters to integers with base-10 radix
  • Returns sensible defaults when parsing fails (NaN result)
  • Enforces configurable minimum and maximum bounds
  • Prevents invalid values from reaching the database layer
  • Provides clear, predictable behavior across all endpoints

Changes Made

  • lib/validation.ts: Added parsePaginationParam() helper function
  • Updated 9 API endpoints to use the helper instead of raw parseInt() calls

Affected Endpoints

  • /api/posts - Fixed page and limit parameters
  • /api/posts/[id]/entries - Fixed page and limit parameters
  • /api/posts/[id]/comments - Fixed page and limit parameters
  • /api/users/[id]/followers - Fixed limit and skip parameters
  • /api/users/[id]/following - Fixed limit and skip parameters
  • /api/wallet/transactions - Fixed page and limit parameters
  • /api/leaderboard - Fixed page and limit parameters
  • /api/notifications - Fixed page and pageSize parameters
  • /api/discovery - Fixed page and limit parameters

Behavior Changes

Before: Non-numeric pagination params → 500 error
After: Non-numeric pagination params → sensible defaults (page=1, limit=10 or 20, skip=0)

Testing Recommendations

  • Test each endpoint with invalid pagination parameters: ?page=abc, ?limit=xyz, ?skip=invalid
  • Verify endpoints return 200 with default pagination instead of 500
  • Test boundary cases: ?page=0, ?limit=-5, ?limit=999
  • Confirm valid numeric parameters still work correctly

Files Changed

  • 10 files modified
  • 136 insertions(+), 35 deletions(-)

Fixes issue geevapp#358 where non-numeric pagination parameters (page, limit, skip)
caused 500 errors across list endpoints.

Changes:
- Add parsePaginationParam() helper in lib/validation.ts to safely parse
  and validate pagination parameters with configurable min/max bounds
- Replaces unsafe parseInt() calls across all affected endpoints:
  * /api/posts
  * /api/posts/[id]/entries
  * /api/posts/[id]/comments
  * /api/users/[id]/followers
  * /api/users/[id]/following
  * /api/wallet/transactions
  * /api/leaderboard
  * /api/notifications
  * /api/discovery

The helper function:
- Returns default values when parsing fails (NaN result)
- Validates minimum and maximum bounds
- Prevents invalid values from reaching Prisma queries
- Gracefully handles edge cases without throwing errors
Updated tests to reflect new pagination behavior:
- Invalid/non-numeric parameters now use sensible defaults instead of 400 error
- Added test for handling non-numeric pagination parameters
- Tests now verify default values are used for invalid input
- Maintains validation for other query parameters (e.g., rankBy, period)
Fixed parse error in tests/api/discovery.test.ts where the describe block
was missing its closing brace. This was causing vitest to fail with:
[PARSE_ERROR] Expected } but found EOF
@drips-wave

drips-wave Bot commented Jun 25, 2026

Copy link
Copy Markdown

@Clinton6801 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@3m1n3nc3 3m1n3nc3 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.

Please review and fix the failing CI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-numeric pagination query params produce 500s across list endpoints

3 participants