4 ms·
JSONP has numerous problems, first it's not JSON -- the object literal is created by executing JS, so you're back to square one with validating your input, the
by olliej 15y ago
JSONP has numerous problems, first it's not JSON -- the object literal is created by executing JS, so you're back to square one with validating your input, the second and more significant problem is that if you ever use JSONP to transmit private data then you've essentially lost the cross origin protection of data present in the browser.
Now all an attacker needs to do is get one of your users with an active session to load a page that does <script src="usersprivatedata.jsonp"></script> and they can gather that data.
- jhaglund 15y agoI was doing something similar to this article (feeding json directly to JS functions) with PHP. I think this is better for the browser. server side, it's heavier: <?php //inject json //this puts the json in a div //then the js gets the div's innerText and parses it as JSON //so there's an object in js (global) with all the JSON data, with jsonInject's key name if(isset($jsonInject) && count($jsonInject) > 0){ foreach($jsonInject as $i=>$json){ echo "<div style='display:none;' id='jsonInject_{$i}'>{$json}</div>" echo "<script>var {$i} = JSON.parse( $('#jsonInject_{$i}')[0].innerText ); </script>"; } } ?> fill the associative $jsonInject array with json strings in your controller. then this goes in your templater. it makes a hidden div, puts the json string in there. then jquery pulls the innerText value, it gets parsed as json and stored in a js var. depending on use, i'll sometimes just pass the results of JSON.parse to an initialize method. I like the proxy caching. I think apache's mod_proxy and mod_cache could do this but with more config (and resources per request).
- tlrobinson 15y agothe second and more significant problem is that if you ever use JSONP to transmit private data then you've essentially lost the cross origin protection of data present in the browser. That's assuming the API is using HTTP cookies for authentication, right? If it's not then theres no way for a 3rd party to construct an authenticated request without knowing the credentials. The real problem here is that a JSONP proxy allows 3rd party sites to access the API from your IP address (and API key if you use the suggestion of an API key proxy). Rate limiting and restricting to specific HTTP Referrers would be prudent.