NoSQL Database Fundamentals and Attack Surface
You'll learn how NoSQL databases differ from traditional SQL databases and where injection vulnerabilities can appear in their flexible query structures.
What Are NoSQL Databases?
Unlike SQL databases with rigid tables and structured queries, NoSQL databases store data in flexible formats—think of them as filing cabinets organized in different ways rather than spreadsheets with fixed columns.
The main types you'll encounter:
- Document stores (MongoDB, CouchDB): Store data as JSON-like documents
- Key-value stores (Redis, DynamoDB): Simple pairs like a dictionary
- Column-family stores (Cassandra): Wide tables with flexible columns
- Graph databases (Neo4j): Data as connected nodes
The Injection Attack Surface
NoSQL databases don't use SQL syntax, but they're still vulnerable to injection attacks. The danger zones:
Query operators: In MongoDB, queries use JSON objects. An attacker might inject operators like $ne (not equal), $gt (greater than), or $where (arbitrary JavaScript):
// Vulnerable: user input directly in query
{"username": userInput, "password": passwordInput}
// Attack: {"$ne": null} bypasses authentication
{"username": {"$ne": null}, "password": {"$ne": null}}
Key-value manipulation: In Redis, commands are string-based. Injecting command separators can execute multiple operations.
JavaScript execution: Some NoSQL databases allow JavaScript in queries—a massive attack surface if user input isn't sanitized.
The core problem: NoSQL's flexibility means queries accept complex objects and expressions, not just strings. Developers often forget to validate the structure and type of input, only checking content.
Key Takeaway: NoSQL injection exploits flexible query structures—document operators, key-value commands, and embedded code execution—by manipulating input structure, not just content. Always validate both data type and structure.