Mindsack : User Experience Engineering : Dynodes: Cross-Domain Scripting using Dynamic Node Creation

Try it out: get a random number from a script hosted on a foreign domain.

(Note: clicking repeatedly won't give you multiple results; you need to mouse out and mouse back in again. Please read How It Works to find out why.)

Your random number will appear here.

What's This?

In certain edge cases, XMLHTTPRequest is impossible or unpalatable. It could be cross-domain security issues. It could also be that you've got a friendly script on the other end that supplies a JSON object with a callback, so you don't need to take the extra step of proxying it through a server on your domain.

Dynodes use CSS, unobtrusive Javascript, and the Document Object Model to create, import, run, and destroy foreign script nodes on demand, without the usual security restrictions faced by AJAX.

Questions? Comments?

Please leave 'em in the Mindsack. Thanks!

How Dynodes Work

Things To Do And Notice

Browser Compatibility

Directions for Future Development

Words Of Warning

I'm lucky enough to work with some of the leading voices in the field. One such alert reader is Douglas Crockford, who writes:

XMLHttpRequest suffers from a defective security mechanism that constrains it to connecting only with the server that delivered the base page. This renders XMLHttpRequest virtually useless for a large, exciting class of applications. Clearly an alternative is needed.

The dynamic <script> tag hack suffers from the opposite problem. It allows a page to access data from any server in the web, which is really useful. Unfortunately, the data is returned in the form of a script. That script can deliver the data, but it runs with the same authority as scripts on the base page, so it is able steal cookies or misuse the authorization of the user with the server. A rogue script can do destructive things to the relationship between the user and the base server.

It is safe in the particular example that Kent shows, but it is extremely dangerous in other patterns. Be extremely cautious in your own use, and even more cautious in teaching it outside. The unrestricted script tag hack is the last big security hole in browsers. It cannot be easily fixed because the whole advertising infrastructure depends on the hole. Be very cautious.

Thanks, Douglas. I thought twice about releasing dynodes into the wild; on balance, however, I believe that the technique is important enough--and working examples are few enough--that the benefits outweigh the risks.

That being said, I'm going to apply a really basic casual-bozo filter and not supply the source code for this example. If you know what you're doing with the Web, you know where to find it.

Acknowledgements

Some Rights Reserved

Article and code copyright © Kent Brewster, 2005. Please feel free to reprint or redistribute as long as you post a comment saying where you used it. Thanks!