In SQL, quotes change grammar. In document databases, types and operators change the query. If the parser accepts an object where it expected a string, the attacker can supply an operator.
The login passes JSON directly into a document query. A password object such as `{"$ne": null}` means not equal to null, which is true for the admin password, so the check is bypassed.
We guard quotes and forget types. The database speaks both.
What is actually at stake?
Before touching a payload, map the assets, the actors and the boundary that is supposed to protect them. A finding only becomes a vulnerability when you can connect the failed boundary to a real asset.
Assets
- User accounts and password hashes
- Tenant-scoped documents
- Search and reporting APIs
- Administrative flags stored in documents
Actors
- Attacker with a login or search form
- Authenticated user probing JSON APIs
- Malicious integration sending nested objects
Trust boundaries
- JSON value type to query operator
- String equality to object predicate
- Client filter to database selector
- Application role to collection access
Abuse cases
- Send password={$ne:null} to match any password
- Use $gt or $regex to discover values
- Inject operators through a query-string parser
- Use $where or mapReduce to execute server-side JavaScript
Follow the decision, not the payload
Every family has a moment where trusted data becomes an authorization, parsing or state decision. Learn to point at that moment in the request path.
- 01
Parse
The API accepts JSON or a query-string syntax that can express nested objects and operators.
- 02
Build selector
The handler places client input into a document query, sometimes after string interpolation.
- 03
Interpret
The database treats a key beginning with $ as an operator rather than a literal field value.
- 04
Match
The operator changes the predicate, often matching the first or every document.
- 05
Escalate
Authentication, authorization or data filtering is bypassed; blind extraction can continue through timing or error differences.
The login that accepted an operator
Composite case study from public NoSQL injection disclosuresA composite case based on repeated public disclosures in JSON APIs: a login handler passed the request body directly into a document query.
Instead of a password string, a tester sent {"$ne": null}. The query matched the administrator because the operator compared the stored hash with a non-null value.
Authentication was bypassed without knowing the password. Similar selectors in search endpoints exposed documents across tenants.
The handler did not enforce primitive types or strip operator keys before building the query. The database faithfully interpreted the attacker's object as query syntax.
Validate credentials as strings, reject keys beginning with $, build queries from named fields, and give the application a least-privilege database role.
Read it as a chain: discovery → failed decision → impact → fix. Then ask which link would have broken earliest.
A repeatable testing sequence
- Inspect the content type and whether the API accepts JSON.
- Replace a string with an object containing `$ne`, `$gt`, `$regex` or `$exists`.
- Try bracket notation in form bodies.
- Use the bypass to reach an admin session.
Where smart people get fooled
- NoSQL does not mean no injection.
- JSON is data, but operators are still interpreted.
- A string-only UI can still have a JSON API.
What would the logs show?
A control is stronger when someone can see it fail. These signals are starting points for a detection rule, not a complete SIEM use case.
- Request bodies containing keys that begin with $
- Login or search requests where a field is an object instead of a scalar
- Unexpectedly broad result sets from a narrow filter
- Database errors mentioning operators or selector syntax
- Repeated requests with regex or comparison operators
index=web sourcetype=app_logs | eval nosql=if(match(request_body,"\"\$(ne|gt|gte|lt|regex|where|exists)\""),1,0) | where nosql=1 | stats count values(path) by clientip, user_id | where count > 0Fix the decision, then prove it stays fixed
- Validate types and reject objects where scalars are expected.
- Strip or reject query operators from user input.
- Use a schema and parameterized query builders.
const { username, password } = await request.json();
const user = await users.findOne({ username, password });const input = loginSchema.strict().parse(await request.json());
const user = await users.findOne({ username: input.username });
const ok = user && await verifyPassword(input.password, user.passwordHash);Use a strict schema that requires strings, then compare the password with a password hash. Never query by a client-supplied object or store plaintext passwords.
Regression tests a reviewer would ask for
Operator in login
GivenA login endpoint accepts JSON
Whenpassword is {"$ne": null}
ThenThe schema rejects the request and no user is returned
Nested filter
GivenA search endpoint accepts a filter object
WhenThe filter contains $where or $regex
ThenThe server rejects operator keys or maps only allowlisted fields
Content type
GivenThe UI submits form-encoded data
WhenThe same endpoint receives application/json with an object value
ThenThe handler enforces the same scalar validation
Tenant scope
GivenA user searches within their tenant
WhenThe filter is empty or manipulated
ThenThe server adds the tenant predicate and returns only the user's documents
What this finding is not
Strong testers kill their own findings before someone else does. These are the shortcuts that turn a real bug into a weak report — or a false alarm into a wasted escalation.
- A JSON body is accepted but the password field is cast to a string: test whether the cast rejects or stringifies the object.
- A 500 error follows an operator: it may be a parser error rather than a successful injection; confirm with a controlled boolean or timing test.
- The UI sends a string: the API may still accept JSON with a different content type.
- A WAF blocks $ne: alternate operators, URL encoding and nested fields may bypass it.
Terms you should be able to use precisely
- NoSQL injection
- Manipulating a document-database query through client-controlled operators or syntax.
- Query operator
- A special key such as $ne or $gt that changes how a value is compared.
- Type confusion
- Supplying an object where the application expected a string or number.
- Selector
- The document query object that determines which records match.
- Blind extraction
- Recovering data through yes/no, timing or error differences when results are hidden.
- Schema validation
- Enforcing the expected type and shape of every input before it reaches a query.
Answer these without reciting a payload
Why does {"$ne": null} bypass a login?
The database interprets the object as an operator, not a password string. It matches documents whose password field is not null, including the administrator.
Is escaping the $ character enough?
No. Type enforcement is the stronger control: credentials must be strings, filters must be allowlisted, and operator keys must never be accepted from clients.
How do you test a NoSQL endpoint safely?
Use a controlled boolean comparison, observe whether the result set changes, then confirm with timing or a second test. Avoid destructive $where or write operators.
Can you separate the mechanism from the payload?
Four questions, graded on the server. Answers and explanations are released only after you submit, and signed-in learners keep a best score.
في NoSQL لا تكون الحقنة دائمًا نصًا، بل قد تكون كائن JSON يحوّل قيمة عادية إلى معامل استعلام مثل $ne.
لهذا فإن التحقق من النوع هو خط الدفاع الأول: كلمة المرور نص، والفلاتر حقول محددة بقيم بسيطة. لا تمرر جسم الطلب إلى الاستعلام، ولا تقبل مفاتيح تبدأ بـ$ من العميل. وبما أن قاعدة البيانات قد تنفذ JavaScript عبر $where، فإن صلاحياتها يجب أن تكون محدودة جدًا.
What happens if the password field is an object instead of a string?