---
layout: post
title: "Accessing cross-site data using JSONP"
thumbnail: padded-door-480.jpg
date: 2019-01-02
---
JSON with padding or JSONP is a method to access JSON data from another web site. This can lead to security vulnerabilities if the JSON data contains sensitive information.
## Same origin policy and JSON
JSON is a data format widely used in web applications that uses JavaScript notation to describe objects and values. By default, the same origin policy prevents reading JSON cross site. That is, the site https://attacker.com/ can not read https://my.webapp/data.json. There are two common ways to enable that, so that the data can be read cross-site:
* [Cross-origin resource sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
* JSON with padding or JSONP
## How JSONP works
JSONP is a little bit of a hack. It wraps the data in a function call. If you have this JSON:
{"hello": "world"}
JSONP will call some function with this data:
somefunction({"hello": "world"})
So the padding here is `somefunction(...)` and the original JSON is passed into it. This makes it possible to read the JSON cross-site, by including the JSONP resource as a script: