Skip to content

bug: reject non-string login credentials to block NoSQL injection - #1130

Open
Shrinandan-N wants to merge 3 commits into
mainfrom
devin/1793995123-fix-login-nosql-injection
Open

Shrinandan-N wants to merge 3 commits into
mainfrom
devin/1793995123-fix-login-nosql-injection

Conversation

@Shrinandan-N

@Shrinandan-N Shrinandan-N commented Nov 6, 2026 •

Copy link
Copy Markdown

Summary

Fixes the NoSQL injection in loginHandler (Snyk Code SNYK-CODE-NOSQLI-943, CWE-943, routes/index.js:39). Because bodyParser.json() is enabled, req.body.password could be an object like {"$gt": ""}. It went straight into User.find, so anyone who knew the [REDACTED SECRET] email could log in without the password.

loginHandler now returns 401 unless both username and password are strings. It also coerces them with String() in the query, because Snyk Code only recognizes that as a sanitizer and still flagged a typeof guard or a { $eq: ... } query. Separately, it passes User.find errors to next(err) instead of crashing on users.length.

if (typeof username !== 'string' || typeof password !== 'string') return res.status(401).send()
User.find({ username: String(username), password: String(password) }, ...)

Verification

Tested locally against mongo:3 (seeded [REDACTED SECRET]@snyk.io) with POST /login and a JSON body:

body main this PR
password: {"$gt":""} 302 → /[REDACTED SECRET] (bypass) 401
username/password both {"$gt":""} 500 401
wrong password string 401 401
correct password 302 → /[REDACTED SECRET] 302 → /[REDACTED SECRET]

A local snyk code test on this branch reports no injection result on loginHandler (22 results remain elsewhere in the repo, all pre-existing).

About the failing SnykCode check

This check does not scan the PR. .github/workflows/snyk-code-manual.yml uploads the committed static sarif.json, which pins a javascript/Sqli result to routes/index.js:39. Since this PR changes that line, GitHub fingerprints the static result as "1 new alert". It will fail for any change to this line until sarif.json is regenerated, which this PR doesn't do.

Note: #1033 is an earlier open PR for the same finding. Only one of the two should land.

Devin-Org: engineering

Link to Devin session: https://devin-gtm.devinenterprise.com/sessions/d33c608b65f4427a81e8f294550675d1
Open in Devin Desktop: https://devin-gtm.devinenterprise.com/desktop/session/d33c608b65f4427a81e8f294550675d1?variant=devin
Requested by: @Shrinandan-N


Devin Review

Status Commit
⚪ Not started —

Run Devin Review

Enable auto-review to review your pull requests automatically.

💡 Connect your GitHub account to enable automatic code reviews.

Devin Review (Staging)


Devin Review

…loginHandler

Co-Authored-By: nandu.narayanan <nandu.narayanan@cognition.ai>
@devin-ai-integration

Copy link
Copy Markdown

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • 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) {
const username = req.body.username
const password = req.body.password

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

devin-ai-integration Bot and others added 2 commits November 6, 2026 20:01
…ral values

Co-Authored-By: nandu.narayanan <nandu.narayanan@cognition.ai>
…he sanitizer

Co-Authored-By: nandu.narayanan <nandu.narayanan@cognition.ai>
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.

2 participants