Full name: Duplicate key, meaning a repeated member name, in a JavaScript Object Notation (JSON) object.
Definition
A duplicate JSON §12.26 key is a name that occurs more than once among the members of one object in a JSON (JavaScript Object Notation) text, as in {"limit":1,"limit":100}. JSON is a text format for structured data; an object is a list of name–value pairs, called members, written between braces. RFC 8259, the JSON standard, says names within an object should be unique and that software receiving repeated names behaves unpredictably. Parsers differ: many keep the last value, some the first, some reject the text.
The idea
A gateway checks each JSON order before forwarding it: it parses the text and refuses any order whose limit exceeds 10. It then forwards the original text to a back end that parses it again and performs limit operations. An attacker sends {"limit":1,"limit":100}. The gateway’s parser keeps the first value and sees 1; the back end’s parser keeps the last and sees 100. Each component follows its own rules correctly, and the system performs 100 operations although its policy allows 10.
The root cause is that one text has two meanings. Most programs turn a JSON object into a map, a data structure that holds at most one value per name. When a name repeats, the parser must drop all but one value, and the standard does not say which. Two parsers that choose differently disagree about the same bytes, so a check made by one and an action taken by the other concern different data. Such a disagreement between two parsers reading one input is called a parser differential. The cure is to remove the ambiguity where the text is parsed, before the information is lost.
How it works
From text to map.
The parser reads the members in order as pairs , where each is a name and each a value. It decodes escape sequences in names first:
\u0061stands for the letter a, so"a"and"\u0061"are the same name.The parser inserts each pair into a map. On a repeated name it follows one of four policies: last wins (overwrite), first wins (ignore the later pair), reject (fail with an error) or keep all (return every pair).
The program receives the map. From here on, nothing shows that a name was repeated or which values were dropped.
Known behavior. Python’s §12.23 json module and JavaScript’s JSON.parse keep the last value. Python’s module also accepts a function, passed as object_pairs_hook, that receives every pair in order and can refuse repeats. RFC 7493 defines I-JSON (Internet JSON), a restricted profile of JSON that forbids duplicate names [388]. RFC 8785, the JSON Canonicalization Scheme (JCS) that the Canonical JSON §12.28 entry describes, requires its input to follow I-JSON and so to have no duplicate names.
The defense.
Choose one duplicate-name policy for the protocol, normally reject, and write it down.
Enforce the policy in the step that turns text into a map, while all pairs are still visible, in every component that parses the text.
Better still, parse once: the component that checks the data hands the parsed value, or a fresh serialization §4.28 of it, to the component that acts, so both work on one representation.
When a digital signature §5.14 or a message authentication code §5.7 (MAC) protects the text, verify the tag over the exact bytes received, then parse those bytes strictly with duplicates refused. The tag proves who produced the bytes; strict parsing §1.48 guarantees that the bytes have one meaning.
Worked example
The text {"limit":1,"limit":100} is 23 bytes long: 2 braces, 2 quoted names "limit" of 7 bytes each, 2 colons, 1 comma, the digit 1 and the 3 digits of 100, so . The policy allows at most 10 operations.
A first-wins validator keeps 1. Its bound check is true, so it accepts the text.
A last-wins consumer keeps 100 and performs 100 operations, and .
A strict parser sees the name
limittwice and refuses the whole text, so nothing is performed.In Python,
json.loadsreturns the map{'limit': 100}; withobject_pairs_hook, the hook receives[('limit', 1), ('limit', 100)]and can refuse it.
Each parser’s own bound check is correct for the value it kept: for the first-wins validator and for the last-wins consumer. The vulnerability is the mismatch between the two interpretations.
Canonicalizing after the fact hides the problem. Serializing the last-wins map without spaces gives {"limit":100}, 13 bytes, a deterministic text with one member that no longer shows the value 1 the validator approved. A duplicate also need not repeat the same bytes: in {"a":1,"\u0061":2} the escape decodes to a, and Python’s hook receives [('a', 1), ('a', 2)]. A check that compares raw name bytes before decoding escapes misses that duplicate.
The mismatch has caused real vulnerabilities. In CVE-2017-12635, an entry in the Common Vulnerabilities and Exposures (CVE) list, a user record sent to the Apache CouchDB database carried two roles members. A validation function written in JavaScript saw the last value, an empty list, and accepted the record; the database’s own Erlang code used the first value, which granted the administrator role.
Stated precisely
Let an object’s members, after escape decoding, be , with names compared character for character and without Unicode normalization §15.19. The object has a duplicate name when for some . For a name that occurs, the first-wins value is for the smallest with , and the last-wins value is for the largest with . The two policies give the same map exactly when no name repeats with different values.
Let component check and component act on , where is the text and and are the parsers of and . The check by says something about the action of only if for every text that accepts. Two arrangements guarantee this for duplicate names: both parsers reject every text with a duplicate name, or never parses and receives itself. Two parsers that document the same policy agree only as long as both implementations really follow it, escape decoding included.
Limits and common mistakes
Duplicate names are one parser difference among several. Parsers also disagree about numbers too large for their number type (Python’s json module reads 1e400 as infinity; other parsers reject it), integers above , which a JavaScript number cannot hold exactly, invalid escapes, a byte order mark §12.48 at the start, and trailing commas or comments, which lenient parsers accept.
Case-insensitive matching creates duplicates by another route. Go’s encoding/json package matches object names to structure fields ignoring case when no exact match exists, so Limit and limit can fill one field while a case-sensitive validator sees two names.
Refusing duplicates does not authenticate the text, bound its size or nesting depth, or check application rules such as limit . A JSON Schema §12.27 validator checks the map that its parser produced, so a schema cannot detect a duplicate the parser has already removed.
A shared file extension does not imply a shared meaning: two programs that both accept .json files can read the same file differently.
Questions answered
Can a validator see one value while a later consumer sees another?
Yes. When a JSON (JavaScript Object Notation) object repeats a name, as in the text
{"limit":1,"limit":100},
a validator whose parser keeps the first value sees 1, and a consumer whose parser keeps the last value sees 100. RFC 8259, the JSON standard, leaves the outcome unpredictable, and real parsers differ. Each component’s check can be correct for the value it kept while the system acts on a value nobody approved. The same split arises when one parser decodes escapes such as \u0061 before comparing names and another does not, or when one compares names ignoring case.
Prevent it in one of two ways. Choose one duplicate policy, normally rejection, and enforce it in the step that turns text into a map, while all pairs are still visible, in every component that parses the text. Or parse once and hand the parsed value to the consumer, so that check and action use one representation. Canonicalizing the text after a parser has silently dropped a duplicate cannot recover the dropped value.
Related entries: JSON Schema (§12.27), Canonical JSON (§12.28), JSON (§12.26), Authentication coverage (§5.41), Unicode normalization (§15.19), Robustness principle and strict parsing (§1.48). Further reading: [29, 30, 124, 388].