﻿# V5921\. OWASP\. Possible NoSQL injection\. Potentially tainted data is used to create a NoSQL query\.

The analyzer has detected that a query to a NoSQL database \(MongoDB, Cassandra, Elasticsearch\) is constructed from unverified data sent by the client\. This could lead to a NoSQL injection\.

This vulnerability falls under the OWASP Top 10 Application Security Risks 2025: [A05:2025 Injection](https://top10.owasp.org/2025/A05_2025-Injection/)\.

The example:

```cpp
func ResetPassword(w http.ResponseWriter, r *http.Request) {
  var req map[string]interface{}
  if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
    http.Error(w, "bad request", http.StatusBadRequest)
    return
  }
  filter := bson.M{
    "email":       req["email"],
    "reset_token": req["token"],
  }
  var user User
  err := users.FindOne(r.Context(), filter).Decode(&user)
  if err != nil {
    http.Error(w, "invalid token", http.StatusForbidden)
    return
  }
  ....
}
```

In the password reset function, the request body is parsed in a `map` without types\. Therefore, instead of a string, the field may contain an embedded document with MongoDB query operators, and MongoDB will execute them\. For example, with a request body like this, an attacker could reset the password for someone else's account without knowing the token:

```cpp
{ "email": "admin@example.com", "token": { "$ne": "" } }
```

To ensure security, the requested data should be parsed into structures with typed fields, and field names and operators should be taken only from a whitelist\. You cannot insert user data into `$where`; it is better to write the condition using standard query operators\. Patterns for `$regex` should be escaped using [`regexp.QuoteMeta`](https://pkg.go.dev/regexp#QuoteMeta)\. 

The fixed example:

```cpp
type ResetRequest struct {
  Email string `json:"email"`
  Token string `json:"token"`
}

func ResetPassword(w http.ResponseWriter, r *http.Request) {
  var req ResetRequest
  err := json.NewDecoder(r.Body).Decode(&req)
  if err != nil || req.Token == "" {
    http.Error(w, "bad request", http.StatusBadRequest)
    return
  }
  filter := bson.M{
    "email":       req.Email,
    "reset_token": req.Token,
  }
  ....
}
```

The `{ "$ne": "" }` object cannot be parsed in a field of type `string`\. An empty token is rejected separately; otherwise, it would match the empty `reset_token` in the database\.

In Cassandra and Elasticsearch, it is dangerous to construct CQL text, a JSON query body, or a Painless script by concatenating strings\. Values should be passed as request parameters, and the request body should be constructed using [`json.Marshal`](https://pkg.go.dev/encoding/json#Marshal) or client request builders\. You should not pass user data in the `query_string` either: its syntax allows searches based on any fields\.

For more information on protecting against NoSQL injections, see the [OWASP NoSQL Security Guide](https://cheatsheetseries.owasp.org/cheatsheets/NoSQL_Security_Cheat_Sheet.html)\.