Skip to content

NoSQL Injection in findOne() function

Description

Untrusted user input in the findOne() function can result in NoSQL Injection, allowing attackers to manipulate the database query and potentially extract or modify sensitive data.

Examples

Insecure Code

javascript
const username = req.body.username; db.collection.findOne({ username: username })

Secure Code

javascript
const sanitize = require('mongo-sanitize'); const username = sanitize(req.body.username); db.collection.findOne({ username: username })

Remediation

Use a sanitization library like 'mongo-sanitize' to neutralize special elements in user input before passing it to the findOne() function.

Rule Details

FieldValue
IDCODE-0359
CategoryInjection
SeverityCRITICAL
CWECWE-943
ConfidenceHIGH
ImpactHIGH
LikelihoodHIGH
ExploitabilityEASY
TagsNoSQL Injection, User Input Validation
OWASPA1:2017-Injection, A03:2021-Injection