Fix NoSQL injection vulnerability (SonarQube S5147)#36
Fix NoSQL injection vulnerability (SonarQube S5147)#36devin-ai-integration[bot] wants to merge 1 commit into
Conversation
| exports.loginHandler = function (req, res, next) { | ||
| if (validator.isEmail(req.body.username)) { | ||
| User.find({ username: req.body.username, password: req.body.password }, function (err, users) { | ||
| User.find({ username: String(req.body.username), password: String(req.body.password) }, function (err, users) { |
🤖 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:
|
Remediation details — SonarQube
|
CI status analysis (issue
|
095e9a6 to
cd03716
Compare
Remediation details — SonarQube
|
cd03716 to
3544e2c
Compare
Remediation — SonarQube
|
Cast req.body.username and req.body.password to String() before passing
them into the MongoDB query to prevent MongoDB operator injection.
Without this, an attacker could send a JSON object like {"$gt": ""}
as the password field, bypassing authentication.
SonarQube issue: AZhSVLrd4wErqc9Ey1Y3
Rule: jssecurity:S5147
Co-Authored-By: Joao Esteves <joao.esteves@cognition.ai>
3544e2c to
7826bb9
Compare
SonarQube Vulnerability Fix: NoSQL Injection (S5147)SonarQube Issue Key: What was changedIn Fix appliedBoth User.find({ username: String(req.body.username), password: String(req.body.password) }, ...)
|
Summary
Fixes NoSQL injection in
loginHandler(routes/index.js:39) — SonarQube issueAZhSVLrd4wErqc9Ey1Y3, rulejssecurity:S5147.req.body.passwordwas passed directly into the MongoDBUser.find()query. An attacker could send a JSON body withpassword: {"$gt": ""}to bypass authentication by injecting a MongoDB operator that matches any document.Fix: wrap both query parameters with
String()to coerce any non-string input (including operator objects) into a plain string before the query executes:String()on an object like{"$gt":""}produces"[object Object]", which will never match a real password — neutralizing the injection.Link to Devin session: https://app.devin.ai/sessions/207e1fa2cadd455ba87bed324959e943
Requested by: @joao-cognition
Devin Review