JSON Injection
JSON Injection
Table of Contents
JSON injection
JSON injection is a vulnerability that lets a malicious hacker inject malicious data
into JSON streams or use malicious JSON streams to modify application behavior.
There are two types of JSON injections, server-side and client-side:
Server-side JSON injection happens when data from an untrusted source is not
sanitized by the server and is written directly to a JSON stream.
Client-side JSON injection happens when data from an untrusted JSON source
is not sanitized and is parsed directly using the JavaScript eval function.
Worst-case Hi there!
full system ✋
What brings you
compromise
1
https://www.invicti.com/learn/json-injection/ 1/7
8/21/24, 11:58 AM JSON Injection
What is JSON?
Get a demo
JSON (JavaScript Object Notation) is a lightweight data interchange format used
for communication between applications. It performs a similar role to XML but is
simpler and better suited to processing in JavaScript.
1. The attacker creates a malicious website containing a script tag that references
a JSON data URL of the web application under attack and includes code to
hijack the JSON data.
2. A user logged into the targeted web application is tricked into visiting the
malicious website (usually using social engineering).
3. Since the same-origin policy (SOP) allows JavaScript from any website to be
included and executed in the context of any other site, the user’s web browser
loads the JSON data in the context of the malicious site.
4. The malicious website hijacks the JSON data.
https://www.invicti.com/learn/json-injection/ 2/7
8/21/24, 11:58 AM JSON Injection
1. The server stores user data as a JSON string, including the account type.
2. User name and password values are taken directly from user input parameters
without validation or sanitization.
3. The JSON string is built using simple concatenation:
1 $json_string = '{"accountType":"user","userName":"'.$_GET['userName'].'",
4. A malicious user appends data to their user name entered into an input form or
delivered in an HTTP header. This data is sent to the back-end unsanitized:
john%22,%22accountType%22:%22administrator%22
1 {
2 "accountType":"user",
3 "userName":"john",
4 "accountType":"administrator",
5 "pass":"password"
6 }
6. When reading the stored string, the JSON parser (json_decode) encounters
two accountType entries and accepts the last one, granting john administrator
privileges without any authentication. Note that, strictly speaking, the behavior
of json_decode is not incorrect – RFC-7159 for the JSON format states that
“the names within an object SHOULD be unique” but not that they must be
unique, leaving some room for interpretation.
https://www.invicti.com/learn/json-injection/ 3/7
8/21/24, 11:58 AM JSON Injection
user"});alert(document.cookie);({"accountType":"user
The consequences of JSON injection highly depend on the way JSON data is used
by the web application. However, in some cases, they may be quite severe:
While JSON injection on its own may not seem very dangerous, it is often only one
step in a longer chain of attacks, so in some cases it can have severe consequences,
up to and including full system compromise.
The best way to detect JSON injection vulnerabilities varies depending on whether
they are already known or unknown.
By using this website you agree with our use of cookies to improve its performance and enhance your experience.
More information in our Privacy Policy.
https://www.invicti.com/learn/json-injection/ 4/7
8/21/24, 11:58 AM JSON Injection
As with most vulnerabilities, the key to maintaining web application security and
preventing JSON injections is to sanitize data. This applies to both server-side and
client-side JSON injections.
The best way of preventing client-side JSON injections is never to use the eval
function to evaluate JSON data. Whenever you use the eval function with untrusted
data that contains JavaScript code, that code will be executed – and it could be
malicious. To eliminate this risk, use JSON.parse instead.
https://www.invicti.com/learn/json-injection/ 5/7
8/21/24, 11:58 AM JSON Injection
Classification ID
CAPEC 153
WASC 20
OWASP 2021 A3
Written by: Tomasz Andrzej Nidecki, reviewed by: Benjamin Daniel Mussler
By using this website you agree with our use of cookies to improve its performance and enhance your experience.
More information in our Privacy Policy.
https://www.invicti.com/learn/json-injection/ 6/7
8/21/24, 11:58 AM JSON Injection
Invicti Security Corp 1000 N Lamar Blvd Suite 300 Austin, TX 78703, US
COMPANY
About Us
Contact Us
Support
Careers
Resources
Partners
© Invicti 2024
Compliance
Legal
Privacy Policy
California Privacy Rights
Terms of Use
Accessibility
Sitemap
By using this website you agree with our use of cookies to improve its performance and enhance your experience.
More information in our Privacy Policy.
https://www.invicti.com/learn/json-injection/ 7/7