Javascript – 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?

Best Solution

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 page, your browser sends your cookie data to 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, 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:

Related Question