fix(batchController): validate date-range and allow-list sort on GET /batches - #1349
Open
saidai-bhuvanesh wants to merge 1 commit into
Open
Conversation
|
@openhands-agent 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. |
…es (Nitya-003#1237) Rebased onto current main. getBatches passed raw start/end query params straight into new Date() (an invalid string becomes Invalid Date and a silent $gte/$lte that matches nothing or everything) and echoed the client-supplied sortBy straight into the Mongoose sort object, letting a client inject arbitrary sort fields. Validate both dates (400 on NaN) and restrict sortBy to an allow-list, defaulting to createdAt. Also removes a stray trailing .catch() block left after the module export that made the file syntactically ambiguous.
saidai-bhuvanesh
force-pushed
the
fix/1237-batches-date-sort-validation
branch
from
August 15, 2026 05:13
3e218b6 to
ea0fd55
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1237
getAllBatches(backend/controllers/batchController.js) parsedstart/endwith barenew Date(start)and passed the raw clientsortBystraight into.sort({...}):GET /api/batches?start=notadate→ MongooseCastError→500.GET /api/batches?sortBy=<arbitrary-key>→ arbitrary/malformed sort field forwarded into the query.Fix
Number.isNaN(date.getTime())and return400with a clear message for invalidstart/end; empty/absent ends no longer leave an emptycreatedAtobject on the query.sortByto an allow-list of real fields (createdAt,updatedAt,batchId,cropType,quantity,farmerName,status,currentStage), falling back tocreatedAtfor anything else. Default behaviour (nosortBy→createdAtdesc) is unchanged.Also fixes a pre-existing syntax error
The file on
mainends with an orphaned top-level.catch(err => console.error("Promise.all failed:", err));(not attached to any Promise), which is invalid JS and made the whole module fail to load (SyntaxError: Unexpected token '.'). I removed this dead line so the file compiles; without this, neither my change nor the existing handler could run. The existingexports.*assignments continue to definemodule.exports.Files
backend/controllers/batchController.jsThis PR was created by an AI agent (OpenHands) on behalf of @saidai-bhuvanesh.