fix(jssecurity:S5147): NoSQL injection in routes/index.js - #229
fix(jssecurity:S5147): NoSQL injection in routes/index.js#229devin-ai-integration[bot] wants to merge 1 commit into
Conversation
Coerce username/password to strings before building the User.find query so object payloads cannot inject MongoDB query operators. SonarQube AZhSVLrd4wErqc9Ey1Y3. Co-Authored-By: Joao Esteves <joao.esteves@cognition.ai>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
| if (validator.isEmail(req.body.username)) { | ||
| User.find({ username: req.body.username, password: req.body.password }, function (err, users) { | ||
| var username = typeof req.body.username === 'string' ? req.body.username : ''; | ||
| var password = typeof req.body.password === 'string' ? req.body.password : ''; |
|
SonarQube issue What was vulnerable: What changed: both values are coerced to strings before the query is built: var username = typeof req.body.username === 'string' ? req.body.username : '';
var password = typeof req.body.password === 'string' ? req.body.password : '';
if (validator.isEmail(username)) {
User.find({ username: username, password: password }, ...)Non-string input becomes Scope: single minimal edit in routes/index.js; no refactoring or other findings touched. Validated with |
Summary
loginHandlerbuilt the MongoDB query straight fromreq.body:Express' body parser yields objects for JSON (or bracketed form) payloads, so
{"username": {"$gt": ""}, "password": {"$gt": ""}}injects MongoDB query operators and authenticates as the first matching user without knowing any credentials — an auth bypass.Fix: coerce both fields to strings (non-strings become
'', which failsvalidator.isEmailand returns 401 as before) before they reach the query, so only primitive values can enter it.Behavior for legitimate string logins is unchanged. Verified with
node --check; the repo'snpm testis a Snyk scan requiring auth, so it was skipped.SonarQube issue key:
AZhSVLrd4wErqc9Ey1Y3(rulejssecurity:S5147, BLOCKER, routes/index.js:39).Devin-Org: engineering
Link to Devin session: https://app.devin.ai/sessions/a15ec2b7b1f44efaaf9ffbaa5ed04570
Requested by: @joao-cognition
Note
Devin errored when opening this Pull Request as joao-cognition.
As a fallback, Devin opened this PR as itself.
Devin Review