JSONP
![]() JSONP, or JSON-P (JSON with Padding), is a historical JavaScript technique for requesting data by loading a JSONP is vulnerable to the data source replacing the innocuous function call with malicious code, which is why it has been superseded by CORS (cross-origin resource sharing, available since 2009[3]) in modern applications. FunctionalityThe HTML For example, a request to a foreign service {
"Name": "Clem",
"Id": 1234,
"Rank": 7
}
Without support for CORS, an attempt to use the data across domains results in a JavaScript error. <script type="application/javascript"
src="http://server.example.com/Users/1234">
</script>
The browser will download the In the JSONP usage pattern, the URL request pointed to by the The function call to parseResponse() is the "P" of JSONP—the "padding" or "prefix" around the pure JSON.[4] For JSONP to work, a server must reply with a response that includes the JSONP function. JSONP does not work with JSON-formatted results. The JSONP function invocation that gets sent back, and the payload that the function receives, must be agreed upon by the client and server. By convention, the server providing the JSON data offers the requesting website to name the JSONP function, typically using the name jsonp or callback as the named query-string parameter, in its request to the server: In this example, the received payload would be: parseResponse({"Name": "Clem", "Id": 1234, "Rank": 7});
Script element injectionJSONP makes sense only when used with a script element. For each new JSONP request, the browser must add a new An example of using jQuery to dynamically inject script element for a JSONP call looks like this: $.getScript("http://server.example.com/Users/192.168.73.96?callback=parseResponse");
After the element is injected, the browser evaluates the element, and performs an HTTP GET on the src URL, retrieving the content. Then the browser evaluates the return payload as JavaScript. This is typically a function invocation. In that way, the use of JSONP can allow browser pages to work around the same-origin policy via script element injection.[5] The script runs within the scope of the including page and, as such, is still subject to cross-domain restrictions relative to the domain of the including page. This means that a web page cannot, for example, load a library hosted on another site via JSONP and then make XMLHttpRequest requests to that site (unless cross-origin resource sharing (CORS) is supported), although one could use such a library to make XMLHttpRequests to one's own site. Security concernsUntrusted third-party codeIncluding script elements from remote servers allows the remote servers to inject any content into a website. If the remote servers have vulnerabilities that allow JavaScript injection, the page served from the original server is exposed to an increased risk. If an attacker can inject any JavaScript into the original web page, then that code can retrieve additional JavaScript from any domain, bypassing the same-origin policy.[6] The Content Security Policy HTTP Header lets web sites tell web browsers which domain scripts may be included from. An effort was undertaken around 2011 to define a safer strict subset definition for JSONP[1] that browsers would be able to enforce on script requests with a specific MIME type such as "application/json-p". If the response did not parse as strict JSONP, the browser could throw an error or just ignore the entire response. However, this approach was abandoned in favor of CORS, and the correct MIME type for JSONP remains Whitespace differencesJSONP carried the same problems as resolving JSON with Callback name manipulation and reflected file download attackUnsanitized callback names may be used to pass malicious data to clients, bypassing the restrictions associated with Insecure JSONP endpoints can be also injected with malicious data.[11] Cross-site request forgeryNaive deployments of JSONP are subject to cross-site request forgery (CSRF or XSRF) attacks.[12] Because the HTML Rosetta FlashRosetta Flash is an exploitation technique that allows an attacker to exploit servers with a vulnerable JSONP endpoint by causing Adobe Flash Player to believe that an attacker-specified Flash applet originated on the vulnerable server. Flash Player implements same-origin policy allowing one to make requests (with cookies) and receive responses from the hosting site. The applet can then send the retrieved data back to the attacker. This is a cross-origin exploit with an impact similar to embedding an arbitrary Flash applet in the vulnerable domain. The exploit uses an ActionScript payload compiled to an SWF file composed entirely of alphanumeric characters by crafting a zlib stream with a particular header and DEFLATE blocks with ad-hoc Huffman coding. The resulting alphanumeric-only SWF file is then used as the callback parameter of a JSONP call. High-profile sites such as Google, YouTube, Twitter, Yahoo!, Yandex, LinkedIn, eBay, GitHub, Instagram, and Tumblr were all vulnerable until July 2014.[13] This vulnerability was initially discovered by Erling and Alok Menghrajani, with a public presentation at a security conference. The exploitation of the vulnerability was subsequently improved by Gábor Molnár. Google security engineer Michele Spagnuolo coined the term[14] and has CVE-2014-4671[15] and CVE-2014-5333.[16] Adobe Flash Player release version 14.0.0.145, released on 8 July 2014, introduced stronger validation of Flash files,[17] and in version 14.0.0.176, released on 12 August 2014, finalized the fix,[18] preventing this exploit from working. Prior to Adobe's fix, websites could protect themselves by prepending an empty JavaScript comment (/**/) or even just a newline as the first bytes of the JSONP response. HistoryIn July 2005, George Jempty suggested an optional variable assignment be prepended to JSON.[19][20] The original proposal for JSONP, where the padding is a callback function, appears to have been made by Bob Ippolito in December 2005[21] and is now used by many Web 2.0 applications such as Dojo Toolkit and Google Web Toolkit. See alsoReferences
External links
|