Injection through API fields: JSON bodies reaching SQL, NoSQL, and shells
Why JSON APIs are still injection targets: operator injection in MongoDB-style queries, typed fields that are never type-checked, and filter parameters passed to query builders. Includes review searches and schema validation.
Diagram
A JSON filter reaching a query builder
Step 1 of 4
Expected
The client sends a plain string.
POST /api/login
{"username":"ana","password":"correct horse"}The example on the open step is one illustration, or the fix you can paste into an application you own.
What this page is made of
3 pieces. Each one says what fails, who is affected, what you would notice, and what you change.
Operator injection
- What fails
- Document databases treat keys beginning with $ as operators.
- Who is affected
- Accounts and records behind queries built from request objects.
- What you would notice
- Request objects passed into find, update, or aggregate filters.
- What you change
- Validate types and strip or reject keys that begin with $.
Filter and sort parameters
- What fails
- APIs expose flexible filtering that is concatenated into SQL or search queries.
- Who is affected
- Every row the query role can read.
- What you would notice
- Query-builder raw() calls with filter strings from the URL.
- What you change
- Parse filters into a fixed grammar and allow-list field names.
Fields reaching other interpreters
- What fails
- A filename, hostname, or template field lands in a shell, template, or LDAP query.
- Who is affected
- The host running the command.
- What you would notice
- API fields used in subprocess calls or template rendering.
- What you change
- Pass arguments as arrays and avoid shells entirely.
Review searches
Search for request bodies flowing into query calls without a validation step. A schema library at the router (Zod, Joi, Pydantic, Bean Validation) makes the safe path the default.
$ rg -n "find(One)?\(req\.body|\.raw\(.*req\.(query|body)|subprocess\..*shell=True" src/ app/
Authorized testing
In a test environment, send each field with the wrong JSON type (object, array, number) and watch for changed behavior or server errors. A login that succeeds with an object password, or a 500 that names the query engine, is the finding.
Check yourself
Recognition and the fix. Nothing here is a procedure.
A login accepts {"password":{"$ne":""}}. What is the root cause?
Sibling techniques
Pages at the same depth, plus a lab and a glossary card for this one.
- 01Second-order SQL injectionText that was stored safely can still change a later query if that job concatenates the stored value into SQL.
- 02Query builders that still concatenateAn ORM or a query builder is safe only when the SQL text stays under your control.