Course / 09 Web security / Lesson 13 of 28

NoSQL injection

Estimated time: 18 min reading · 12 min activity

Not every database receives SQL text. A NoSQL document store may receive a structured query containing fields and operators. A document is a record; an operator tells the database how to compare or transform values. The exact query language depends on the database.

JSON represents values such as strings, objects and arrays. A quoted value is a string; braces enclose an object with named properties. A schema states which types and fields an application accepts. Here username must be a string, so a well-formed object is still the wrong input type.

Before you begin

Estimated time
18 min reading, 12 min activity. Setup and optional practice may take longer.
Where you work
Read the original fictional requests, source fragments and state tables in a current desktop or mobile browser. The required activity needs no server, database, account or installed security tool. Do not send the printed requests to any service.
Without a lab
Use the complete printed evidence to trace the data or state on paper. Write the decision, the fact supporting it and one limit of the model, then compare with the activity rubric. This does not establish live service behavior. Reading works without JavaScript. Checking requires JavaScript and a secure browser context; saving also requires usable browser storage.

After this lesson you can:

  • Tell a JSON string from a JSON object.
  • Accept the activity username lane and refuse the operator-shaped object.

01Example

Read the worked username and schema, then classify the fresh activity values in the supplied JSON. No database is required.

Schema: username must be a string
Worked value: "ada"
JSON type: string
Decision: meets this type requirement
This does not establish identity or permission.

02Type before meaning

The card accepts ada and lane as strings and rejects the object containing kind. An application that merges arbitrary caller objects into a query may let their structure change query behavior. These cards do not use a real database operator; they isolate the type boundary so you can explain why valid JSON alone is insufficient.

Validate types and allowed keys on the server, build the intended query through the database's supported API, and keep caller values from becoming query structure. This schema check addresses the demonstrated type confusion; complete prevention also depends on the chosen database and how its query is constructed.

Accept a username only when it is a string, and refuse an object even when the JSON is well formed.Model. A teaching model drawn by hand. It shows the idea, not real tool output.

Read the full explanation below.

Worked value "ada"Separate the identity question
Username schema

Schema: username must be a string

met · JSON type: string.
Identity or permission

This does not establish identity or permission.

unknown · No identity evidence supplied.
Apply the stated rule
Meets type requirement

The supplied ada string meets this schema line only.

Well-formed data and a matching type do not authenticate a user or prove a safe full query.

The supplied ada string meets this schema line only.

Follow the explanation

  1. The supplied ada string meets this schema line only.
  2. The model does not turn a schema pass into authentication or permission.
Read every state and value

1. The supplied ada string meets this schema line only.

Case
type
Conditions inspected
type · identity
Decision shown
Yes

2. The model does not turn a schema pass into authentication or permission.

Case
identity
Conditions inspected
type · identity
Decision shown
Yes
Well-formed data and a matching type do not authenticate a user or prove a safe full query.

03Classify the activity values

Read the evidence before comparing your reasoning with the activity rubric. Do not start a database.

{
  "schema": {"username": "string"},
  "worked": {"username": "ada"},
  "activity_string": {"username": "lane"},
  "activity_object": {"username": {"kind": "operator"}},
  "field_note": "The question uses two other requests, A and B."
}

Download the request card

JSON types on this card.

A value in quotes is a string.
A value in braces is an object.
An object can be well-formed JSON and still violate a schema that requires a string.

worked username ada: string. Meets the schema.
activity username lane: string. Meets the schema.
activity username object with kind: object. Does not meet the schema.

This card does not use a database product's operator names.
The server rejects the object because the type is wrong.
A check that runs only in the browser is not this decision.

Download the type table

  1. Write the JSON type of username lane and the type of the value that contains kind.
  2. Compare each with the schema line.
  3. Check: lane meets the string schema. The object does not. ada is the worked string.
  4. Stop. Do not send these bodies to a real store.

04Optional challenge

Explain why a check that runs only in the browser cannot enforce this schema. The rubric is: the caller can send the object without that browser check, and the server still has to refuse the type. About ten minutes.

05Fresh cases for the checks

Use these fictional cases for the checks below. They have different inputs from the worked examples. Read them here or make notes; no installation or external service is needed.

JSON case A: fresh valuesOwned fixture. An original file made for this course. Follow the activity's instructions in your own lab.

P: "reed"
Q: {"kind":"filter"}
R: ["reed"]
A quoted value is a string; braces enclose an object; brackets enclose an array.

JSON case B: independent requestsOwned fixture. An original file made for this course. Follow the activity's instructions in your own lab.

Server schema: username must be a string. No coercion or query-structure merge is allowed.
M: {"username":"fern"}
N: {"username":{"kind":"filter"}}
O: {"username":["fern"]}
All are syntactically valid JSON; no database is involved.

06Explain your evidence

Optional written self-check: explain one decision using the supplied case. No tool run is required.

Check your explanation: classify the complete value and the username field separately; apply the string schema on the server; distinguish valid JSON, valid application input and authenticated identity; avoid merging caller structure into queries.

07Common mistakes

If you accept the object because it is valid JSON, you skipped the schema. The type is wrong.

If you look for the letters A and B in the file, those labels are only in the question.

If you invent a product operator name, you left the card. The card refuses the object by type.

References