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.

Lesson 37 of 69 · 11 min read · practice only with authorization

Diagram

A JSON filter reaching a query builder

Follow the path. One example at a time.

Step 1 of 4

Expected

The client sends a plain string.

Illustration: Expected
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.

  1. 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 $.
  2. 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.
  3. 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.

1 / 3

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.

  1. MapSQL injection: how it happens, how testers confirm it, and the fix that holdsHow concatenated queries let input change SQL, the error, boolean, time, and out-of-band behaviors testers look for in an authorized test, what scanners like sqlmap report, and parameterization plus least privilege as the fix.
  1. 01Second-order SQL injectionText that was stored safely can still change a later query if that job concatenates the stored value into SQL.
  2. 02Query builders that still concatenateAn ORM or a query builder is safe only when the SQL text stays under your control.

Practice

  1. LabTerminal labRead the files on a scoped host and name the failure in a finding.

Glossary

  1. CardInjection through API fieldsA field the client sends is glued into a query, a filter, or a command.