What Are JSON Injections | Acunetix (2024)

What Are JSON Injections | Acunetix (1)

The term JSON injection may be used to describe two primary types of security issues:

  • Server-side JSON injection happens when data from an untrusted source is not sanitized by the server and written directly to a JSON stream.
  • Client-side JSON injection happens when data from an untrusted JSON source is not sanitized and parsed directly using the JavaScript eval function.

What Is JSON

JSON (JavaScript Object Notation) is a lightweight data interchange format used to communicate between applications. It is similar to XML but simpler and better suited to be processed by JavaScript.

Many web applications use this format to communicate between themselves and serialize/deserialize data. Some web applications also use JSON to store important information, for example, user data. JSON is commonly used in RESTful APIs and AJAX applications.

Examples of JSON Injection Attacks

A simple server-side JSON injection could be performed in PHP as follows:

  1. The server stores user data as a JSON string including the account type
  2. User and password are taken directly from user input without sanitization
  3. The JSON string is formed using simple concatenation: $json_string = '{"account":"user","user":"'.$_GET['user'].'","pass":"'.$_GET['pass'].'"}';
  4. A malicious user appends data to their user name: john%22,%22account%22:%22administrator%22
  5. The resultant JSON string is:
    { "account":"user", "user":"john", "account":"administrator", "pass":"password"}
  6. When reading the stored string, the JSON parser (json_decode) encounters two account entries and takes the last one, granting john administrator privileges. Note that the behavior of json_decode is not incorrect – RFC-7159 states that “the names within an object SHOULD be unique” (not MUST), leaving the door open to interpretation.

A simple client-side JSON injection could be performed as follows:

  1. The JSON string is the same as in the above example
  2. The server gets the JSON string from an untrusted source
  3. The client parses the JSON string using eval:
    var result = eval("(" + json_string + ")");document.getElementById("#account").innerText = result.account;document.getElementById("#user").innerText = result.name; document.getElementById("#pass").innerText = result.pass;
  4. The account value is: user"});alert(document.cookie);({"account":"user
  5. The eval function executes the alert.
  6. Parsing results in a Cross-site Scripting (XSS) attack (document.cookie is disclosed).

JSON Injection vs. JSON Hijacking

While JSON hijacking (a subset of Cross-site Script Inclusion – XSSI) also relates to the JSON notation, it is a slightly different attack, in some ways similar to Cross-site Request Forgery (CSRF). In the case of JSON hijacking, the attacker aims to intercept JSON data sent to the web application from the web server.

  1. The attacker creates a malicious website and embeds a script tag in its code that attempts to access JSON data from the attacked web application.
  2. The user, who is logged into the attacked web application, is lured to visit 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 website, the user accesses the JSON data.
  4. The malicious website hijacks the JSON data.

JSON Injection Prevention

As with most injection 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.

To prevent server-side JSON injections, sanitize all data before serializing it to JSON. For example, if you use Java, a good option to sanitize JSON data is to use the OWASP JSON Sanitizer.

The best method to prevent client-side JSON injections is never to use the eval function to evaluate JSON data. If you use the eval function and the untrusted data contains JavaScript code, that code will be executed. To avoid this, use JSON.parse. You can also enforce Content Security Policy, which by default prevents the use of eval.

Frequently asked questions

The term JSON injection may be used to describe two primary types of security issues. Server-side JSON injection happens when data from an untrusted source is not sanitized by the server and written directly to a JSON stream. Client-side JSON injection happens when data from an untrusted JSON source is not sanitized and parsed directly using the JavaScript eval function.

Read more about other types of injection attacks.

JSON hijacking is an attack in some ways similar to cross-site request forgery (CSRF). In the case of JSON hijacking, the attacker aims to intercept JSON data sent to the web application from the web server.

Read about cross-site request forgery (CSRF) attacks.

JSON injections are not very common and not as dangerous as many other vulnerabilities but they can lead to other dangerous attacks, such as cross-site scripting (XSS).

Read more about cross-site scripting (XSS).

To prevent server-side JSON injections, sanitize all data before serializing it to JSON. The best method to prevent client-side JSON injections is never to use the eval function to evaluate JSON data. If you use the eval function and the untrusted data contains JavaScript code, that code will be executed. To avoid this, use JSON.parse. You can also enforce Content Security Policy, which by default prevents the use of eval.

Read more about general best practices for secure coding.

Get the latest content on web security
in your inbox each week.

THE AUTHOR

What Are JSON Injections | Acunetix (2)

Tomasz Andrzej Nidecki
Principal Cybersecurity Writer
LinkedIn

Tomasz Andrzej Nidecki (also known as tonid) is a Primary Cybersecurity Writer at Invicti, focusing on Acunetix. A journalist, translator, and technical writer with 25 years of IT experience, Tomasz has been the Managing Editor of the hakin9 IT Security magazine in its early years and used to run a major technical blog dedicated to email security.

Related Posts:

What Are JSON Injections | Acunetix (2024)
Top Articles
End-of-Sale and End-of-Life Announcement for the Cisco Adaptive Security Appliance (ASA) Release 9.8(x), Adaptive Security Virtual Appliance (ASAv) Release 9.8(x) and Adaptive Security Device Manager (ASDM) Release 7.8(x)
Phantom privacy policy • Phantom
Navicent Human Resources Phone Number
Bank Of America Financial Center Irvington Photos
Lowe's Garden Fence Roll
Lamb Funeral Home Obituaries Columbus Ga
His Lost Lycan Luna Chapter 5
Napa Autocare Locator
Jeremy Corbell Twitter
Noaa Swell Forecast
Flights To Frankfort Kentucky
Radio Aleluya Dialogo Pastoral
Tracking Your Shipments with Maher Terminal
Shreveport Active 911
Craigslist Farm And Garden Cincinnati Ohio
Bad Moms 123Movies
Houses and Apartments For Rent in Maastricht
Epro Warrant Search
How Much You Should Be Tipping For Beauty Services - American Beauty Institute
Hollywood Bowl Section H
The Blind Showtimes Near Amc Merchants Crossing 16
Wbiw Weather Watchers
Dcf Training Number
Buying Cars from Craigslist: Tips for a Safe and Smart Purchase
Knock At The Cabin Showtimes Near Alamo Drafthouse Raleigh
Piedmont Healthstream Sign In
Tokyo Spa Memphis Reviews
Kitchen Exhaust Cleaning Companies Clearwater
Account Now Login In
Tactical Masters Price Guide
Maisons près d'une ville - Štanga - Location de vacances à proximité d'une ville - Štanga | Résultats 201
Kqelwaob
Hannah Jewell
Alima Becker
What does wym mean?
Craigslist Gigs Norfolk
Blackstone Launchpad Ucf
1400 Kg To Lb
Atlantic Broadband Email Login Pronto
The Boogeyman Showtimes Near Surf Cinemas
Case Funeral Home Obituaries
Planet Fitness Lebanon Nh
Bismarck Mandan Mugshots
How Big Is 776 000 Acres On A Map
Brown launches digital hub to expand community, career exploration for students, alumni
Scythe Banned Combos
Child care centers take steps to avoid COVID-19 shutdowns; some require masks for kids
Das schönste Comeback des Jahres: Warum die Vengaboys nie wieder gehen dürfen
Automatic Vehicle Accident Detection and Messageing System – IJERT
Craigslist Psl
March 2023 Wincalendar
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 5645

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.