Skip to content

fix(jssecurity:S5147): NoSQL injection in routes/index.js - #229

Open
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/sonarqube-fix-AZhSVLrd4wErqc9Ey1Y3-1787648740
Open

fix(jssecurity:S5147): NoSQL injection in routes/index.js#229
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/sonarqube-fix-AZhSVLrd4wErqc9Ey1Y3-1787648740

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Aug 25, 2026

Copy link
Copy Markdown

Summary

loginHandler built the MongoDB query straight from req.body:

User.find({ username: req.body.username, password: req.body.password }, ...)

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 fails validator.isEmail and returns 401 as before) before they reach the query, so only primitive values can enter it.

-if (validator.isEmail(req.body.username)) {
-  User.find({ username: req.body.username, password: req.body.password }, ...)
+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 }, ...)

Behavior for legitimate string logins is unchanged. Verified with node --check; the repo's npm test is a Snyk scan requiring auth, so it was skipped.

SonarQube issue key: AZhSVLrd4wErqc9Ey1Y3 (rule jssecurity: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

Status Commit
⚪ Not started

Run Devin Review

Devin Review (Staging)

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-integration

Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

Comment thread routes/index.js
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 : '';
@devin-ai-integration

Copy link
Copy Markdown
Author

SonarQube issue AZhSVLrd4wErqc9Ey1Y3 — rule jssecurity:S5147 (NoSQL injection, BLOCKER), routes/index.js:39

What was vulnerable: exports.loginHandler passed req.body.username and req.body.password directly into User.find({ username: ..., password: ... }). Because the body parser turns JSON/bracketed-form payloads into objects, a request like {"username": {"$gt": ""}, "password": {"$gt": ""}} injects MongoDB query operators into the filter, matching an arbitrary user and bypassing authentication.

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 '', fails validator.isEmail, and returns the same 401 as before, so no operator object can reach the query. Legitimate string logins behave identically.

Scope: single minimal edit in routes/index.js; no refactoring or other findings touched. Validated with node --check; npm test in this repo is a Snyk scan requiring auth, so it was not run.

Written by Devin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant