Skip to content

fix: reject non-numeric page/limit with 400 instead of 500 - #1458

Open
ionfwsrijan wants to merge 1 commit into
Canopus-Labs:mainfrom
ionfwsrijan:fix/1445-page-limit-validation
Open

fix: reject non-numeric page/limit with 400 instead of 500#1458
ionfwsrijan wants to merge 1 commit into
Canopus-Labs:mainfrom
ionfwsrijan:fix/1445-page-limit-validation

Conversation

@ionfwsrijan

Copy link
Copy Markdown
Contributor

Problem

GET /api/questions/my-questions with a non-numeric page or limit (e.g. ?page=abc) returned a 500. parseInt('abc') produces NaN, which Math.max(1, NaN) silently propagates into .skip(NaN)/.limit(NaN), throwing a Mongoose validation error that the catch turned into an internal-server-error response.

Fix

  • The route now validates page/limit through a zod query schema (validateGetMyQuestions), returning a clean 400 Validation failed for non-numeric, negative, zero, fractional, or oversized (limit > 100) values.
  • The controller defensively coerces via Number() + Number.isFinite, so NaN can never reach Mongo: invalid values fall back to page=1/limit=20 and limit is clamped to 100.

Files changed

  • backend/Input_validators/ValidateQuestions.jsgetMyQuestionsQuerySchema + validateGetMyQuestions middleware.
  • backend/routes/questionRoutes.js — wire validateGetMyQuestions onto GET /my-questions.
  • backend/controllers/questionController.js — defensive page/limit coercion.
  • backend/tests/questionController.pageLimit.unit.test.js — route-layer (400s) and controller-layer (coercion, no NaN, clamp) tests.

Testing

npx vitest run tests/questionController.pageLimit.unit.test.js — 17/17 passing. Full suite shows no new failures vs. the origin/main baseline.

Closes #1445

parseInt('abc') produced NaN which Math.max silently propagated into
.skip(NaN)/.limit(NaN), throwing a Mongoose validation error that the catch
turned into a 500. The route now validates page/limit via a zod schema (400
with a clear message), and the controller defensively coerces so NaN can never
reach Mongo — falling back to page=1/limit=20 and clamping limit to 100.

Closes Canopus-Labs#1445
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@ionfwsrijan, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 54 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0f94845f-0404-4683-8f02-e1bd2393c13f

📥 Commits

Reviewing files that changed from the base of the PR and between 8acb5b8 and b1fc473.

📒 Files selected for processing (4)
  • backend/Input_validators/ValidateQuestions.js
  • backend/controllers/questionController.js
  • backend/routes/questionRoutes.js
  • backend/tests/questionController.pageLimit.unit.test.js

Comment @coderabbitai help to get the list of available commands.

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.

GET /api/question/my-questions returns HTTP 500 for non-numeric page/limit query parameters (NaN propagates into Mongo)

1 participant