Skip to content

fix(auth): prevent NoSQL injection in login (vuln_id 81) - #249

Open
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1788220509-vuln81-login-nosqli
Open

fix(auth): prevent NoSQL injection in login (vuln_id 81)#249
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1788220509-vuln81-login-nosqli

Conversation

@devin-ai-integration

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

Copy link
Copy Markdown

Summary

Remediates security finding vuln_id 81 — NoSQL injection in user authentication (CWE-943), a bug in routes/index.js loginHandler.

Confirmed exploitable path: POST /login passed req.body.username / req.body.password verbatim into User.find({ username, password }), so a JSON body like {"username":"admin@snyk.io","password":{"$gt":""}} injected a MongoDB operator and authenticated without the password.

Fix:

  • loginHandler rejects (401) any credential that is not a primitive string before querying.
  • Both values are wrapped in explicit $eq comparisons so they can never be interpreted as operators.
  • Query errors now propagate via next(err) instead of being ignored.

Reviewer notes / behavior change: requests with non-string username/password now get 401 instead of a query attempt; valid string logins are unchanged. README and exploits/nosql-exploits.sh comments updated to reflect that ns4/ns5 payloads now return 401.

Testing

  • New regression test tests/login-nosql-injection.spec.js (fails before fix): NODE_OPTIONS=--openssl-legacy-provider ./node_modules/.bin/tap tests/login-nosql-injection.spec.js --no-coverage → 4/4 pass.
  • node --check routes/index.js

Note: this finding duplicates the loginHandler NoSQLi previously fixed in PRs #130, #153, #169, #186, #193, #205, #218, #231 (unmerged); this PR fixes it independently on a fresh branch.

Written by Devin

Devin-Org: engineering


Devin Review

Status Commit
⚪ Not started

Run Devin Review

Devin Review (Staging)

Comment thread routes/index.js
});
} else {
const username = req.body.username;
const password = req.body.password;
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