WSTG - Latest
JSON Parser Differentials
| ID |
|---|
| WSTG-INJT-26 |
Summary
A JSON request often passes through more than one parser before it is acted on. An API gateway or web application firewall may inspect it, a service may check it against a schema or verify a signature over it, and a different service, perhaps written in another language, may then process it. If these parsers read the same document differently, a request can pass the check with one value and be processed with another. This is the JSON counterpart of HTTP Parameter Pollution, which only touches on duplicate keys in JSON bodies. This test covers the wider set of ways that components can read the same JSON differently. Disagreements about where the HTTP request itself starts and ends, such as conflicting Content-Length and Transfer-Encoding handling, are covered in HTTP Request Smuggling. This page assumes both parsers already agree on the request body.
RFC 8259 only says that object names should be unique, and that when they are not, the behavior of the software that receives the object is unpredictable. In practice, parsers differ in several ways:
- Duplicate keys. Given
{"role": "user", "role": "admin"}, some parsers keep the first value, many keep the last (Python’sjsonmodule and JavaScript’sJSON.parseboth do), and some reject the document. - Key matching. Some parsers match keys to fields loosely. For example, Go’s
encoding/jsonmaps an object key to a struct field case-insensitively when there is no exact match, so"Role"and"ROLE"can both set theRolefield, while a check written in another language only looks for"role". - Escapes and invalid characters in keys. A key can be written with escapes, so
"\u0072ole"and"role"are the same name once decoded, but a component that compares the raw text sees two. Unpaired surrogates such as\ud800are valid under the JSON grammar, but RFC 8259 says the behavior of software that receives them is unpredictable. Invalid escape sequences and raw control characters are not valid JSON at all. Parsers handle these cases differently: some reject them, some replace them with U+FFFD, and some drop them, so"role\ud800"can be a separate key in one parser and collide with"role"in another. - Non-standard syntax. Lenient parsers accept comments, trailing commas, single quotes or unquoted keys. A strict parser rejects the document, and two lenient parsers may disagree on where a comment ends.
- Numbers. Large integers and special values are decoded differently.
9007199254740993is decoded exactly by Python but becomes9007199254740992in JavaScript, which stores all numbers as 64-bit floating point. Some parsers acceptNaNandInfinity, which RFC 8259 does not allow, and others turn out-of-range numbers into the largest representable value or into zero. - Trailing data. Python’s
jsonmodule and JavaScript’sJSON.parsereject{"role": "user"} {"role": "admin"}because of the extra data after the first value. A streaming decoder, such as Go’sjson.Decoder, which many gateways and middleware use, reads one value perDecode()call, so the rest is ignored unless the application decodes again or checks for the end of the input.
Where these differences sit between a security decision and the action it protects, they can be used to bypass authorization checks, input validation, rate limits, price or quantity checks, and signature or schema validation.
Test Objectives
- Identify places where the same JSON document is parsed by more than one component.
- Determine whether those components disagree on duplicate keys, key matching, invalid characters, non-standard syntax, numbers or trailing data.
- Assess whether a disagreement lets a request pass a check with one value and be processed with another.
How to Test
Map the Parsers
Look for evidence that a JSON request crosses more than one component:
- Response headers, error messages and timing that point to an API gateway, a web application firewall or a service mesh in front of the application.
- Different error formats for different kinds of invalid input, which often come from different layers. Compare the response to a syntax error, a schema violation and a business rule violation.
- Architecture documents and API definitions that describe a separate validation, authorization or signing step.
- Requests that are forwarded or queued for another service, such as webhooks, background jobs and batch endpoints.
Fingerprinting the parsers helps choose probes. Send the requests below one at a time and note whether each is accepted, rejected, or changes the result. A parser that accepts comments or trailing commas, or that rejects duplicate keys, is often identifiable from these behaviors alone.
Probe for Differences
For each field that affects a security decision, such as a role, user or account identifier, price, quantity, amount, or a flag that skips a check, send variations where the checked value and the used value can differ. Use a value the checks should accept and one they should reject, and try both orders:
{"role": "user", "role": "admin"}
{"role": "admin", "role": "user"}
Then try the other differences:
{"role": "user", "Role": "admin"}
{"role": "user", "\u0072ole": "admin"}
{"user": {"role": "user", "role": "admin"}}
{"role": "user", "role\ud800": "admin"}
{"role": "user", /* "role": "admin", */ "x": 1}
{"amount": 9007199254740993}
{"amount": 1.0}
{"amount": 1E2}
{"amount": -0}
{"amount": 1e400}
{"amount": NaN}
{"role": "user"} {"role": "admin"}
A bypass needs the checking parser to accept the document and the acting parser to derive the dangerous value from it. Probes that a strict parser rejects outright, such as NaN, Infinity, comments, trailing commas and single quotes, only lead somewhere when the checking layer is the lenient one. Against a strict front layer they return an error, so note which layer is strict before ruling a probe out.
Look for a result that matches neither a clean accept nor a clean reject, for example:
- The request is accepted, but the stored or returned object holds the value the check should have rejected.
- A validation error mentions one value while the action uses another.
- The amount charged, the quantity shipped or the identifier acted on differs from the one in the request.
- An error from a deeper layer appears for a request that the front layer let through.
When a response echoes the parsed object, or the result can be read back with another request, compare it with the value that was sent. When nothing is echoed, compare the effect of the request, such as which account’s data is returned or what was recorded, between a request with the variation and one without it.
Signatures, Schemas and Stored Documents
Some designs check a JSON document once and use it later, for example a signed webhook, a JSON Web Token, a document validated against a schema before it is stored, or a message placed on a queue. If the check and the later use are done by different parsers, apply the same probes to the document. A signature over the raw bytes only protects those bytes, so a document whose meaning depends on which parser reads it can carry a valid signature and still be interpreted in more than one way. A signature over a canonicalized form, such as the JSON Canonicalization Scheme (RFC 8785), only helps if the verifier and the consumer both work from that canonical form rather than re-parsing the original text.
Related Test Cases
Remediation
- Parse each request once, at the edge or in the service that makes the decision, and pass the parsed result on instead of the raw document.
- Configure parsers to reject duplicate keys, invalid escape sequences, non-standard syntax and numbers outside the range the application supports. The I-JSON profile (RFC 7493) describes these restrictions.
- Match object keys to fields exactly, including case.
- Where a document has to be checked by one component and used by another, have both use the same parser and the same settings, or have the checking component pass on a normalized copy.
- Represent large or exact numbers, such as identifiers and amounts, as strings.