Skip to content

bug: fix NoSQL operator injection in admin loginHandler - #231

Open
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1787665372-login-nosqli-operator-injection
Open

bug: fix NoSQL operator injection in admin loginHandler#231
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1787665372-login-nosqli-operator-injection

Conversation

@devin-ai-integration

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

Copy link
Copy Markdown

Summary

POST /login passed req.body.username/req.body.password straight into User.find({...}), so a JSON body like {"username":"admin@snyk.io","password":{"$gt":""}} (or {"$gt":""} for both) turned the credential check into an operator query and granted an admin session (CWE-943).

loginHandler now rejects anything that is not a primitive string before touching the DB, and compares with explicit $eq so a nested object can never be interpreted as an operator:

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

err from the query is now propagated to next instead of being ignored (it previously caused a users.length TypeError).

tests/login-nosql-injection.spec.js stubs the mongoose model with an in-memory collection implementing $eq/$gt/$ne semantics (mongoose 4.2.4 can't talk to a modern mongod) and asserts operator payloads and object usernames get 401 with no session, while valid credentials still redirect to /admin. README and exploits/nosql-exploits.sh updated to reflect that the ns4/ns5 payloads now fail.

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