Course contentsShow
← Security
Lesson 596 of 3,05213. Web Security - Injection AttacksFree lesson

JSON Injection and Type Confusion

Manipulating JSON input to inject NoSQL operators through associative arrays and exploiting weak type handling.

What You'll Learn

You'll learn how attackers manipulate JSON input to inject NoSQL operators by exploiting weak type handling in applications that trust client-provided data structures.

The Core Problem

Many NoSQL databases (especially MongoDB) accept queries as JSON objects. When an application blindly converts user input into JSON without proper validation, attackers can inject associative arrays (objects) instead of simple strings or numbers. This causes the application to interpret user data as database operators rather than literal values.

How Type Confusion Works

Imagine you expect a user to provide a password as a string like "password123". The backend builds a query:

{"username": "alice", "password": "password123"}

But what if the attacker sends the password field as an object instead?

{"username": "alice", "password": {"$ne": null}}

Now the query means: "Find a user named alice whose password is not equal to null" — bypassing authentication entirely!

The Analogy

Think of it like a vending machine that expects coins. You're supposed to insert quarters (strings), but the machine naively accepts anything inserted. An attacker inserts a command card (an object with operators) instead, and the machine executes the command rather than checking for valid payment.

Why It Happens

  • Weak typing: Languages like JavaScript and PHP don't enforce strict types
  • Automatic parsing: Frameworks convert JSON directly into objects without validation
  • Trust in structure: Developers assume users will only send primitive values (strings, numbers)

Key Takeaway

JSON injection exploits occur when applications accept complex JSON structures from users without validating types, allowing attackers to inject NoSQL operators like $ne, $gt, or $regex that alter query logic and bypass security checks.