Summary
An incomplete query sanitization issue in Mongoose allows attackers to bypass the sanitizeFilter protection by abusing the $nor logical operator.
When untrusted filter input is passed to Mongoose with sanitizeFilter enabled, malicious query operators can still be injected inside a $nor clause and executed by MongoDB.
Affected context
This vulnerability affects Node.js applications that:
- Use Mongoose as an ODM for MongoDB
- Enable and rely on
sanitizeFilterto protect against query injection - Accept untrusted input (such as request parameters, body fields, or query strings) that is mapped directly or indirectly into MongoDB filter objects
The issue is specifically triggered when filters can contain a $nor clause whose elements are not further constrained or validated by the application.
Exploit preconditions
- Attack surface: Application endpoints or code paths that build MongoDB filters from user-controlled input (e.g., REST/GraphQL APIs, search endpoints, admin panels).
- Required access: Ability to send crafted requests that influence filter construction (typically anonymous or authenticated web/API access).
- Trigger point: Execution of a Mongoose query (e.g.,
find,findOne,update,delete) where the filter includes user-controlled data andsanitizeFilteris enabled. - Why exploitation is possible: Prior to the fix,
sanitizeFilterdid not recursively sanitize the$norlogical operator. Since$noraccepts an array and arrays do not triggerhasDollarKeys(), nested operators like$ne,$gt, or$regexinside$norwere not wrapped in$eq, allowing them to be interpreted as real MongoDB operators.
Why this happens
The underlying issue is an incomplete defense-in-depth implementation of query sanitization. Mongoose's sanitizeFilter feature attempts to neutralize potentially dangerous MongoDB operators by wrapping them in $eq. This relies on recognizing operator keys beginning with $ and applying sanitization recursively across logical operators.
However, $nor was missing from the set of logical operators that receive recursive sanitization. Because $nor consumes an array of conditions, and array elements bypass the existing hasDollarKeys() check, attacker-controlled objects within the $nor array could introduce unsanitized $-prefixed operators. This design gap maps to CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component, where special tokens (MongoDB operators) are not consistently neutralized before being passed to the database query engine.
Potential impact
- Bypass of
sanitizeFilterprotections, undermining application assumptions about query safety - Unauthorized data access by expanding or altering query conditions (e.g., returning more records than intended)
- Circumvention of business logic checks that rely on strict filters (e.g., multi-tenant or ownership constraints)
- Data exfiltration via permissive or crafted
$regexor range queries - Potential data tampering or deletion when combined with update/delete operations using attacker-controlled filters
General mitigation guidance
Effective mitigation revolves around not relying solely on generic sanitization and instead combining multiple defensive patterns:
- Validate and constrain filters: Explicitly validate allowed fields, types, and operators before passing filters into Mongoose. Use allowlists of permissible keys and reject or strip unexpected operators.
- Restrict logical operators: Limit or disallow complex logical operators (
$nor,$or,$and) in user-controlled filters unless there is a clear, validated need. - Normalize input structures: Convert user input into a known-safe query DSL or parameter format rather than accepting raw filter objects.
- Enforce authorization at the application layer: Do not rely on query filters alone for enforcing tenant or ownership checks; compute security constraints server-side and merge them with user filters in a controlled way.
- Least-privilege for database roles: Ensure MongoDB users used by the application have only the minimum permissions required, reducing impact even if filters are abused.
How teams detect issues like this
Detection can focus on both code-level patterns and runtime behavior:
- Static analysis & code review:
- Identify code paths where request data is directly spread or merged into Mongoose filter objects (e.g.,
{ ...req.query }intoModel.find()). - Flag usage of
sanitizeFilteras a potential risk anchor and verify all entry points also apply field/operator validation. - Review any custom "sanitization" helpers that attempt to generically strip
$keys without considering array contexts or logical operators. - Automated tooling:
- Use SAST tools or linters configured to detect direct propagation of untrusted objects into database query APIs.
- Integrate policy-based checks in CI (e.g., disallowing unvalidated
req.body/req.queryin query constructors). - Runtime monitoring:
- Inspect MongoDB logs for suspicious patterns such as unexpected use of
$nor,$regex, or large$or/$$andstructures originating from user traffic. - Implement anomaly detection on query shapes, alerting on atypical operator combinations or filter depth.

