NoSQL Injection Explained: The Bug That Bypasses Logins Without SQL
Most beginners learn SQL injection first — and then assume that apps using MongoDB or other NoSQL databases are safe from injection. They are not. NoSQL injection is the same idea in a different language: attacker-controlled input changes the meaning of a database query. And the classic NoSQL injection demo is terrifyingly simple: log in as any user without knowing their password.
How Login Normally Works
A typical Node.js + MongoDB login looks like this:
db.users.findOne({ username: inputUser, password: inputPass })
The app searches for a user whose username and password match what you typed. Simple and safe — until the attacker controls the shape of the input, not just its value.
The Injection
MongoDB queries are objects, and MongoDB has special operators like $ne (not equal) and $gt (greater than). If the app passes your input straight into the query, you can send an object instead of a plain string:
{ "username": "admin", "password": { "$ne": "" } }
Now the query becomes: find a user named admin whose password is not equal to an empty string. Every real password qualifies — so the app logs you in as admin without knowing the password at all.
A similar trick works in URL parameters: ?username=admin&password[$ne]= can turn a normal login form into an admin login, depending on how the backend parses input.
Other NoSQL Injection Flavours
- Operator injection — $where, $regex and $gt can be abused to extract data character by character, just like blind SQL injection.
- JavaScript injection — old MongoDB versions allowed $where clauses containing JavaScript, letting attackers run code inside the database.
- NoSQL OR bypass — $or operators can widen a query to match records it should never return, leaking other users’ data.
How to Test for It (Legally)
On a target you own or have written permission to test — a local lab, a bug bounty program with a web app in scope:
- Try operator syntax in login fields. Send {“$ne”: “”} as the password (via JSON body or password[$ne]= in a form) and watch whether the login succeeds or the error changes.
- Test error behaviour. A different error message for injected input versus normal wrong input is a strong signal.
- Try $regex. A payload like {“$regex”: “^a”} in a username field can reveal whether usernames start with “a” — a slow but real data-extraction channel.
- Check the stack. Apps built with Express + Mongoose or similar MongoDB drivers are the usual suspects; confirm the backend before reporting.
How Developers Fix It
- Validate input types. A password field should only ever accept a string — reject objects outright. In Mongoose, strict schemas and type checks stop operator injection cold.
- Use parameterized queries and ODM features instead of building query objects from raw user input.
- Never trust client-side validation. It is decoration; the server must enforce types.
- Hash passwords properly (bcrypt/argon2) — even if a query is manipulated, a hashed-password comparison cannot be bypassed the same way.
Why Bug Bounty Hunters Love It
NoSQL injection is common because many developers believe “no SQL = no injection” and skip input validation entirely. That false confidence is exactly what makes it a productive hunting ground: the bug class is well understood, the payloads are short, and a working login bypass is usually a high-severity payout.
Learn it in your own lab first — set up a deliberately vulnerable MongoDB app (or use a legal practice platform), reproduce the bypass, then look for it in programs that allow it. Only test systems you own or have written permission to test.



Comments
Post a Comment