NoSQL Security Cheat Sheet
Introduction
NoSQL databases (MongoDB, CouchDB, Cassandra etc.) power many modern applications with flexible schemas and horizontal scale. But their different query models and deployment patterns create more security risks compared with relational databases.
This cheat sheet summarizes guidance to reduce risk when using NoSQL systems.
Threats & Common Failure Modes
- NoSQL Injection — Unsafe construction of query objects or query strings from untrusted input.
- Exposed Management Interfaces — Admin GUIs, database ports or REST endpoints exposed to the internet.
- Weak/No Authentication & Authorization — Default open access or excessive privileges for clients.
- Insecure Network Exposure — No TLS, open ports, insufficient network segmentation.
- Insecure Defaults — Default admin accounts, default passwords, unsecured configs.
- Poor Access Control Models — Coarse roles allowing lateral abuse.
- Insecure Serialization / Deserialization — Remote code execution via unsafe object deserialization.
- Misconfigured CORS / Public APIs — APIs accidentally allow cross-origin requests or wide access.
- Credential & Secret Leaks — Hardcoded DB credentials in code, images, CI logs.
- Unsafe Backup Exposure — Backups left unencrypted or publicly accessible.
- Supply-chain / Dependency Risks — Vulnerable drivers, ORMs/ODMs, or plugins.
Secure-by-Design Principles
- Treat all input as untrusted — validate, sanitize, and normalize.
- Use least privilege — narrow roles for users, services, and operators.
- Defense in depth — combine network controls, auth, input validation, and monitoring.
- Secure defaults — change default ports/accounts, enable auth and TLS by default.
- Automate secrets & rotation — vaults and short-lived credentials.
- Monitor & audit — log access and detect anomalies.
Practical Defenses & Examples
Prevent NoSQL Injection
Unsafe (string-based filter building — Node.js / MongoDB):
// DANGEROUS: building query from untrusted input
const q = "{ name: '" + req.query.name + "' }";
const filter = eval("(" + q + ")"); // NEVER do this
db.collection('users').find(filter)
Safe (use driver query objects / parameterization):
// SAFE: let driver handle query structure
const filter = { name: req.query.name };
db.collection('users').find(filter)
Safe (whitelisting for operators):
// Reject operator injection by disallowing $ in keys or operator values
if (JSON.stringify(req.body).includes('"$')) throw Error("Invalid input");
Notes:
- Do not accept raw JSON fragments from the client to execute as queries.
- Disallow client-controlled query operators (like
$where, $regex, or $expr) unless strictly required and validated.
- For text-based search parameters, use safe driver APIs (e.g.,
$text with controlled input).
Use Secure Driver / ODM Patterns
- Prefer high-level APIs (ODM/ORM) that build queries safely (e.g., Mongoose, Spring Data, Datastax driver patterns).
- Avoid
.eval()-like functionality and raw query execution from untrusted data.
- Sanitize and validate any raw expressions before passing to the DB.
Example — PyMongo safe usage
from pymongo import MongoClient
client = MongoClient(uri, tls=True)
collection = client.mydb.users
user = collection.find_one({"email": email_input})
Authentication & Authorization
- Enable authentication (do not run databases unauthenticated).
- Use role-based access control (RBAC), least privilege for service accounts.
- Use separate users for admin/backup/readonly/application.
- Use identity federation or short-lived credentials when supported (e.g., AWS IAM -> DynamoDB).
For more information please check following cheat sheets:
Authentication Cheat Sheet
Authorization Cheat Sheet
Network & Transport Security
- Bind services to internal interfaces, not
0.0.0.0.
- Use network segmentation / private subnets and security groups.
- Enforce TLS (in transit encryption) for driver connections and admin consoles.
- Turn off remote management or restrict it to admin networks/VPNs.
Configuration Hardening
- Change default ports and disable sample/demo users.
- Turn off or restrict features that execute code on the server (e.g., MongoDB
db.eval, server-side scripting).
- Require TLS for internal replication links where supported.
Secrets Management
- Do not hardcode DB credentials — use a secret manager (Vault, AWS Secrets Manager, Azure Key Vault).
- Avoid baking credentials into container images or environment variables in CI logs.
- Rotate credentials regularly and use ephemeral tokens when possible.
Logging, Monitoring & Auditing
- Enable audit logging (connection attempts, admin actions, failed auth).
- Send logs to a tamper-evident SIEM.
- Alert on anomalous patterns (spike in queries, slow queries, large data exports).
- Monitor for suspicious commands (e.g., admin actions,
$where, map-reduce jobs).
Backups & Snapshots
- Encrypt backups at rest and during transfer.
- Restrict access to backup storage.
- Sanitize backups for PII as required by policy.
- Validate restore procedures regularly.
Quick NoSQL Security Checklist
- Enable authentication & RBAC
- Enforce TLS for client and node communication
- Bind DB to internal IPs / use private networks
- Use least privilege service accounts
- Disallow client-controlled query operators unless validated
- Avoid raw query execution / eval on server
- Store credentials in secret manager & rotate them
- Harden configs (disable unsafe defaults)
- Encrypt and secure backups
- Monitor/audit DB access and admin actions
- Keep DB and drivers patched
Do’s and Don’ts
Do:
- Use driver query objects rather than building query strings.
- Validate and whitelist user-supplied fields (columns/keys).
- Restrict management interfaces and require MFA for admin access.
- Automate security testing in CI/CD pipelines.
Don’t:
- Expose DB ports/admin consoles to the public Internet.
- Accept raw JSON queries from clients or eval untrusted strings.
- Use root/admin DB accounts for application connections.
- Rely only on network controls to protect badly written queries.
Examples of Dangerous Patterns (brief)
- Allowing client to submit
{ "$where": "this.balance > 0" } → remote code execution or heavy CPU.
- Concatenating user input into query language strings or shell commands for DB tools.
- Leaving MongoDB unsecured (no auth) listening on public IP.
References
1---2name: nosql-security-cheat-sheet3description: NoSQL Security Cheat Sheet4---5# NoSQL Security Cheat Sheet67## Introduction89NoSQL databases (MongoDB, CouchDB, Cassandra etc.) power many modern applications with flexible schemas and horizontal scale. But their different query models and deployment patterns create **more security risks** compared with relational databases.10This cheat sheet summarizes guidance to reduce risk when using NoSQL systems.1112## Threats & Common Failure Modes1314- **NoSQL Injection** — Unsafe construction of query objects or query strings from untrusted input.15- **Exposed Management Interfaces** — Admin GUIs, database ports or REST endpoints exposed to the internet.16- **Weak/No Authentication & Authorization** — Default open access or excessive privileges for clients.17- **Insecure Network Exposure** — No TLS, open ports, insufficient network segmentation.18- **Insecure Defaults** — Default admin accounts, default passwords, unsecured configs.19- **Poor Access Control Models** — Coarse roles allowing lateral abuse.20- **Insecure Serialization / Deserialization** — Remote code execution via unsafe object deserialization.21- **Misconfigured CORS / Public APIs** — APIs accidentally allow cross-origin requests or wide access.22- **Credential & Secret Leaks** — Hardcoded DB credentials in code, images, CI logs.23- **Unsafe Backup Exposure** — Backups left unencrypted or publicly accessible.24- **Supply-chain / Dependency Risks** — Vulnerable drivers, ORMs/ODMs, or plugins.2526## Secure-by-Design Principles2728- **Treat all input as untrusted** — validate, sanitize, and normalize.29- **Use least privilege** — narrow roles for users, services, and operators.30- **Defense in depth** — combine network controls, auth, input validation, and monitoring.31- **Secure defaults** — change default ports/accounts, enable auth and TLS by default.32- **Automate secrets & rotation** — vaults and short-lived credentials.33- **Monitor & audit** — log access and detect anomalies.3435## Practical Defenses & Examples3637### Prevent NoSQL Injection3839**Unsafe (string-based filter building — Node.js / MongoDB):**4041```js42// DANGEROUS: building query from untrusted input43const q = "{ name: '" + req.query.name + "' }";44const filter = eval("(" + q + ")"); // NEVER do this45db.collection('users').find(filter)46```4748**Safe (use driver query objects / parameterization):**4950```js51// SAFE: let driver handle query structure52const filter = { name: req.query.name };53db.collection('users').find(filter)54```5556**Safe (whitelisting for operators):**5758```js59// Reject operator injection by disallowing $ in keys or operator values60if (JSON.stringify(req.body).includes('"$')) throw Error("Invalid input");61```6263Notes:6465- Do **not** accept raw JSON fragments from the client to execute as queries.66- Disallow client-controlled query operators (like `$where`, `$regex`, or `$expr`) unless strictly required and validated.67- For text-based search parameters, use safe driver APIs (e.g., `$text` with controlled input).6869### Use Secure Driver / ODM Patterns7071- Prefer high-level APIs (ODM/ORM) that build queries safely (e.g., Mongoose, Spring Data, Datastax driver patterns).72- Avoid `.eval()`-like functionality and raw query execution from untrusted data.73- Sanitize and validate any raw expressions before passing to the DB.7475#### Example — PyMongo safe usage7677```python78from pymongo import MongoClient79client = MongoClient(uri, tls=True)80collection = client.mydb.users81user = collection.find_one({"email": email_input})82```8384### Authentication & Authorization8586- **Enable authentication** (do not run databases unauthenticated).87- Use **role-based access control (RBAC)**, least privilege for service accounts.88- Use **separate users** for admin/backup/readonly/application.89- Use identity federation or short-lived credentials when supported (e.g., AWS IAM -> DynamoDB).9091For more information please check following cheat sheets:9293[Authentication Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Authentication_Cheat_Sheet.html)9495[Authorization Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Authorization_Cheat_Sheet.html)9697### Network & Transport Security9899- **Bind services to internal interfaces**, not `0.0.0.0`.100- Use **network segmentation / private subnets** and security groups.101- **Enforce TLS** (in transit encryption) for driver connections and admin consoles.102- Turn off remote management or restrict it to admin networks/VPNs.103104### Configuration Hardening105106- Change default ports and disable sample/demo users.107- Turn off or restrict features that execute code on the server (e.g., MongoDB `db.eval`, server-side scripting).108- Require TLS for internal replication links where supported.109110### Secrets Management111112- Do **not** hardcode DB credentials — use a secret manager (Vault, AWS Secrets Manager, Azure Key Vault).113- Avoid baking credentials into container images or environment variables in CI logs.114- Rotate credentials regularly and use ephemeral tokens when possible.115116### Logging, Monitoring & Auditing117118- Enable audit logging (connection attempts, admin actions, failed auth).119- Send logs to a tamper-evident SIEM.120- Alert on anomalous patterns (spike in queries, slow queries, large data exports).121- Monitor for suspicious commands (e.g., admin actions, `$where`, map-reduce jobs).122123### Backups & Snapshots124125- Encrypt backups at rest and during transfer.126- Restrict access to backup storage.127- Sanitize backups for PII as required by policy.128- Validate restore procedures regularly.129130## Quick NoSQL Security Checklist131132- Enable authentication & RBAC133- Enforce TLS for client and node communication134- Bind DB to internal IPs / use private networks135- Use least privilege service accounts136- Disallow client-controlled query operators unless validated137- Avoid raw query execution / eval on server138- Store credentials in secret manager & rotate them139- Harden configs (disable unsafe defaults)140- Encrypt and secure backups141- Monitor/audit DB access and admin actions142- Keep DB and drivers patched143144## Do’s and Don’ts145146**Do**:147148- Use driver query objects rather than building query strings.149- Validate and whitelist user-supplied fields (columns/keys).150- Restrict management interfaces and require MFA for admin access.151- Automate security testing in CI/CD pipelines.152153**Don’t**:154155- Expose DB ports/admin consoles to the public Internet.156- Accept raw JSON queries from clients or eval untrusted strings.157- Use root/admin DB accounts for application connections.158- Rely only on network controls to protect badly written queries.159160## Examples of Dangerous Patterns (brief)161162- Allowing client to submit `{ "$where": "this.balance > 0" }` → remote code execution or heavy CPU.163- Concatenating user input into query language strings or shell commands for DB tools.164- Leaving MongoDB unsecured (no auth) listening on public IP.165166## References167168- [MongoDB Security Official Document](https://www.mongodb.com/docs/manual/security/)169- [Security best practices for Amazon DynamoDB](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/best-practices-security.html)170- [WSTG - Testing for NoSQL Injection](https://owasp.org/www-project-web-security-testing-guide/latest/4-Web_Application_Security_Testing/07-Input_Validation_Testing/05.6-Testing_for_NoSQL_Injection)