5 ms·
It occurred to me the other day that it should be pretty simple to write a script that gets loaded first on the page and removes all subsequent scripts, then lo
by tghw 13y ago
It occurred to me the other day that it should be pretty simple to write a script that gets loaded first on the page and removes all subsequent scripts, then loads them itself, checking the MD5/SHA1 of the script against a known good value, stored in that script's attributes.
<script src="scriptloader.js"></script>
<script src="jquery.min.js" data-md5="a1b2..."></script>
Then you could decide to not load scripts that do not match the correct hash. It could even ping the server to alert it to broken scripts.
- sdevlin 13y agoHow would this protect you against a malicious server?
- tghw 13y agoIf the server serving the HTML is pwnd, then it doesn't, but it doesn't really matter then, either. This protects against any external scripts being unexpectedly modified, e.g. someone MITM your jquery source.
- sdevlin 13y agoI guess that's true, but in practice XSS is a much bigger threat to your application than someone MITM'ing an HTTPS connection to Google's (or whomever's) CDN.
- sneak 13y agoIf you don't allow external scripts to be modified, why host them externally at all? Why not just wget them and host them locally alongside the checksum document and skip all this silliness? Oh, also, those scripts can themselves load in other scripts you haven't checksummed. This is madness you're suggesting.
- martin-adams 13y ago>> Why not just wget them and host them locally Because you might be using a CDN for performance and bandwidth benefits. If you relying on a third party piece of code that you allow to change at any time, then it is very difficult to do any release testing to give you a known set of conditions your application should work under.
- tghw 13y agoIn what scenario do you want external scripts to be modified? Why not take advantage of their ability to serve the scripts while also verifying that they are the same scripts you expected to have? You can also verify that those scripts do not load any other scripts in the version you have. Then, if it's changed later to load more scripts, you'll know about it. How is checking the validity of the scripts that run on your site madness??
- rictic 13y agoIt's... kinda madness. Just to be clear that we're talking about the same thing, here's the proposed process as I understand it: 1) load your loader script, which has the URLs, fingerprints of the scripts you want to run, and the necessary dependency information (jquery-ui must load after jquery for example). In the best case, this file is being served out by the same server that's hosting the HTML, that way at least you're not adding more attack vectors. 2) from the loader script, initiate ajax requests for each of the remote files you need 3) as you get each one back, validate that its signature matches those that are expected, raise an exception if it does not (ideally also displaying something to the user), and evaluating it if its signature matches and we've loaded all of its dependencies. So, why is this madness? 1) Most of the time the reason that you're letting a third party host these files is for speed. They've got a CDN, and hopefully the file will already be cached by your user. Grabbing resources with javascript that you could load directly in the html will slow down your page's loading time, as the browser's html parser isn't able to look ahead and fetch resources that are likely to be needed before the renderer has asked (HTML has a defined rendering order that can be kinda strict sometimes, this is the same reason why you don't put your <script> elements in the <head>). 2) Another reason for using a CDN for your JS libraries is convenience, which this process also wipes out. 3) The whole thing won't work at all unless the third party server sends back cooperative CORS headers, as you can't do an ajax request to a third party site without their cooperation. Finally though – and this is the big one – it's more convenient for the developers, strictly safer, and faster for the end user if you just compile all of the JS and serve from the same domain that's serving your HTML. As stated above, if that server is compromised, you're toast anyways (barring a browser extension or similar). If you really want some more security, look into SSL (and actually look into it, there's definitely much better and much worse ways of doing it).
- deleted 13y ago[deleted]