fix(security): prevent NoSQL injection from user-controlled query data (SonarQube jssecurity:S5147) - #221
Conversation
…5147) 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:
|
|
Remediates SonarQube issue What was wrong: What changed: both fields are coerced/validated to strings before the query is constructed — non-string input becomes Scope is limited to this single finding; other SonarQube issues in |
…onstant time (SonarQube jssecurity:S5147) Co-Authored-By: Joao Esteves <joao.esteves@cognition.ai>
…S5147) Co-Authored-By: Joao Esteves <joao.esteves@cognition.ai>
| if (validator.isEmail(req.body.username)) { | ||
| User.find({ username: req.body.username, password: req.body.password }, function (err, users) { | ||
| if (users.length > 0) { | ||
| var username = typeof req.body.username === 'string' ? req.body.username : ''; |
Summary
loginHandlerinroutes/index.jsbuilt the Mongo login query straight from the parsed JSON body, so a request could inject query operators instead of values.Attack:
POST /loginwith{"username": "admin@snyk.io", "password": {"$gt": ""}}produces{password: {$gt: ""}}, which matches regardless of the real password and grants an admin session ($ne,$regexbehave the same).Fix:
$eq, so even a string value cannot be reinterpreted as an operator document.const username = req.body.usernameshadow was dropped so the audit log / redirect use the validated value.Other SonarQube findings in this file are intentionally untouched. The pre-existing
buildcheck failure (Snyk SARIF upload) reproduces on other open PRs and is unrelated.Devin-Org: engineering
Link to Devin session: https://app.devin.ai/sessions/e811c410695844f987bdec383d7c6f3b
Requested by: @joao-cognition
Devin Review