Skip to content

fix: validate dates and restrict sort keys in getAllBatches - #1355

Merged
Nitya-003 merged 1 commit into
Nitya-003:mainfrom
Kirtan-pc:fix/batchcontroller-date-validation-and-sort-safety
Aug 18, 2026
Merged

Nitya-003 merged 1 commit into
Nitya-003:mainfrom
Kirtan-pc:fix/batchcontroller-date-validation-and-sort-safety

Conversation

@Kirtan-pc

Copy link
Copy Markdown
Contributor

Summary

This PR fixes Issue #1237: getAllBatches in batchController.js parses start/end dates with bare new Date(start) and passes the raw client value straight into .sort({...}). Invalid dates produce a Mongoose CastError → 500, and unvalidated sort keys let a client request arbitrary/malformed sort fields.

Changes

  • Date validation (backend/controllers/batchController.js:350-368): Dates are now parsed into temporary variables and validated with isNaN(parsedDate.getTime()) before being used in the query. Invalid dates return a 400 response with a descriptive error message instead of triggering a Mongoose CastError 500.
  • Sort key allow-list (backend/controllers/batchController.js:378-403): Added ALLOWED_SORT_FIELDS containing only safe, indexed fields (createdAt, updatedAt, batchId, farmerName, cropType, quantity, harvestDate, currentStage, status, origin). The sortBy parameter is validated against this list and falls back to createdAt if an unsupported field is provided. ALLOWED_SORT_ORDERS restricts values to "asc" / "desc" with "desc" as the safe default.

Fixes #1237

Copilot Review Feedback

Other suggestions were evaluated but intentionally left unchanged because they are pre-existing issues outside the scope of Issue #1237:

Comment Verdict Reason
Move date/sort validation into a reusable middleware Out of scope Would require changes to route definitions and testing across multiple endpoints; a targeted inline fix is appropriate for this bugfix
Add schema-level validation for all query parameters Out of scope Pre-existing gap that would require a broader refactor of query handling across the controller
Add rate limiting to the endpoint Out of scope Pre-existing infrastructure concern unrelated to the date/sort validation bug

- Parse dates with new Date() and check isValid() before querying;
  return 400 on invalid date strings instead of Mongoose CastError 500
- Add allow-list for sortBy field names to prevent arbitrary sort keys
- Add allow-list for sortOrder values (asc/desc) with safe defaults
@vercel

vercel Bot commented Aug 17, 2026

Copy link
Copy Markdown

@Kirtan-pc is attempting to deploy a commit to the Nitya Gosain's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the merge ready PR is mergeable and has no conflicts label Aug 17, 2026
@Nitya-003 Nitya-003 added bug Something isn't working gssoc:approved gssoc'26 approved quality:clean *1.2 for contributor +5 pts for mentor level:intermediate 35 pts for contributor 20 pts for mentor type:bug labels Aug 18, 2026
@Nitya-003
Nitya-003 merged commit f3f2da7 into Nitya-003:main Aug 18, 2026
15 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working gssoc:approved gssoc'26 approved level:intermediate 35 pts for contributor 20 pts for mentor merge ready PR is mergeable and has no conflicts quality:clean *1.2 for contributor +5 pts for mentor type:bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validate date-range and sort params on GET /batches

2 participants