Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Security issue with dynamic script tags

This flickr blog post discusses the thought behind their latest improvements to the people selector autocomplete.

One problem they had to overcome was how to parse and otherwise handle so much data (i.e., all your contacts) client-side. They tried getting XML and JSON via AJAX, but found it too slow. They then had this to say about loading the data via a dynamically generated script tag (with callback function):

JSON and Dynamic Script Tags: Fast but Insecure

Working with the theory that large string manipulation was the problem with the last approach, we switched from using Ajax to instead fetching the data using a dynamically generated script tag. This means that the contact data was never treated as string, and was instead executed as soon as it was downloaded, just like any other JavaScript file. The difference in performance was shocking: 89ms to parse 10,000 contacts (a reduction of 3 orders of magnitude), while the smallest case of 172 contacts only took 6ms. The parse time per contact actually decreased the larger the list became. This approach looked perfect, except for one thing: in order for this JSON to be executed, we had to wrap it in a callback method. Since it’s executable code, any website in the world could use the same approach to download a Flickr member’s contact list. This was a deal breaker. (emphasis mine)

Could someone please go into the exact security risk here (perhaps with a sample exploit)? How is loading a given file via the "src" attribute in a script tag different from loading that file via an AJAX call?

like image 752
Tom Lehman Avatar asked Mar 27 '09 19:03

Tom Lehman


People also ask

Are script tags safe?

No, it is not secure. In fact, nothing in a web page is completely secure. In your particular example, here are some examples for how your myapp object can be manipulated: The end-user can open the browser console and type in a line of code to change that object.

Are script tags blocking?

Under normal circumstances, a script tag causes the browser to halt rendering, load a file, and run the code. The browser is blocked from doing other useful work because your JavaScript could write to the page, modify existing elements, or redirect to another URL.

What is the best practice to use script tag?

The best practice is to put JavaScript <script> tags just before the closing </body> tag rather than in the <head> section of your HTML. The reason for this is that HTML loads from top to bottom.

Can you put script tags anywhere?

You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is normally recommended that you should keep it within the <head> tags. The <script> tag alerts the browser program to start interpreting all the text between these tags as a script.


2 Answers

This is a good question and this exact sort of exploit was once used to steal contact lists from gmail.

Whenever a browser fetches data from a domain, it send across any cookie data that the site has set. This cookie data can then used to authenticate the user, and fetch any specific user data.

For example, when you load a new stackoverflow.com page, your browser sends your cookie data to stackoverflow.com. Stackoverflow uses that data to determine who you are, and shows the appropriate data for you.

The same is true for anything else that you load from a domain, including CSS and Javascript files.

The security vulnerability that Flickr faced was that any website could embed this javascript file hosted on Flickr's servers. Your Flickr cookie data would then be sent over as part of the request (since the javascript was hosted on flickr.com), and Flickr would generate a javascript document containing the sensitive data. The malicious site would then be able to get access to the data that was loaded.

Here is the exploit that was used to steal google contacts, which may make it more clear than my explanation above: http://blogs.zdnet.com/Google/?p=434

like image 166
Gdeglin Avatar answered Oct 05 '22 20:10

Gdeglin


If I was to put an HTML page on my website like this:

<script src="http://www.flickr.com/contacts.js"></script>
<script> // send the contact data to my server with AJAX </script>

Assuming contacts.js uses the session to know which contacts to send, I would now have a copy of your contacts.

However if the contacts are sent via JSON, I can't request them from my HTML page, because it would be a cross-domain AJAX request, which isn't allowed. I can't request the page from my server either, because I wouldn't have your session ID.

like image 44
Greg Avatar answered Oct 05 '22 19:10

Greg