11 ms·
I hear this argument a lot, and I very much disagree. Now you have browser vendors having to device which libraries are "popular" and shipping them in the initi
by kevincox 11y ago
I hear this argument a lot, and I very much disagree. Now you have browser vendors having to device which libraries are "popular" and shipping them in the initial download of the browser.
It turns out that this technology already exists in a much better form. It's called cache. The problem is that almost everyone hosts their own version of jQuery. If everyone simply linked the "canonical" version of jQuery (the CDN link is right on their site) then requiring jQuery will be effectively free because it will be in everyone's cache.
Also the cache is supported by all browsers with an elegant fallback. Instead of having to manually having to check if your user's browser has the resource you want preloaded you just like the URL and the best option will automatically be used.
TL;DR Rather then turning this into a political issue stop bundling resources, modern protocols and intelligent parallel loading allow using the cache to stove this problem.
- deleted 11y ago[deleted]
- Lawtonfogle 11y ago> If everyone simply linked the "canonical" version of jQuery (the CDN link is right on their site) then requiring jQuery will be effectively free because it will be in everyone's cache. So create one massive target that needs to be breached to access massive numbers of websites around the world? Imagine if every Windows PC ran code from a single web page on startup every time they started up. Now imagine if anything could be put in that code and it would be ran. How big of a target would that be? While there are cases where the performance is worth using a CDN, there are plenty of reasons to not want to run foreign code. (Now maybe we could add some security, like generating a hash of the code on the CDN and matching it with a value provided by the website and only running the code if the hashes matched. But there are still business risks even with that.)
- eertami 11y agoThe solution to this is including a checksum with the link to the file, and if the checksum doesn't match, don't load the file. See https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity https://developer.mozilla.org/en-US/docs/Web/Security/Subres... though it isn't universally supported yet.
- jonlucc 11y agoJust so I understand, I pull the file and make a checksum, then hardcode it into the link to the resource in my own code? Then, when the client pulls my code, follows the link, checks the checksum against the one I included in the link.
- dexterdog 11y agoYes. It's very simple. I don't know why more library providers don't have it in their copyable <script> snippets.
- d33 11y agoThe problem with caching is that you're sharing the referer with the canonical URL. Another problem is that you're using someone else's bandwidth. And if you combine the two, you can be sure that info about your visitors will be sold, which is why quite a lot of people would prefer to host their own versions of jQuery...
- lolive 11y agoCouldn't the HTTP cache of your ISP be a good in-betweener, in that case? Would he send the referer to the canonical URL?
- mdavidn 11y agoNot for sites using TLS. The only option for secure sites would be a CDN. That is, an HTTP cache in a relationship with the content publisher rather than the subscriber.
- kevincox 11y agoFor the referrer problem you can apply a referrer policy to prevent this but unfortunately the referrer policy isn't very granular. Also for my sites I have a fallback to a local copy of the script. This allows me to do completely local development and remain up if the public CDN goes down (or gets compromised). With small (usually) performance impact.
- newjersey 11y agoWhat if the checksum was the same and you accepted the cache hit if the checksum agrees and get your own copy if it doesn't? Maybe the application should get to declare a canonical URL for the js file instead of the browser? So something like <script src="jQuery-1.12.2.min.js" authoritative-cache-provider="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js" https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.m... sha-256="31be012d5df7152ae6495decff603040b3cfb949f1d5cf0bf5498e9fc117d546"></script> Would this cause more problems than it would solve? I'm assuming disk access is faster than network access. I'm concerned about people like me who use noscript selectively. How easy is it to create a malicious file that matches the checksum of a known file?
- AnkhMorporkian 11y ago> I'm concerned about people like me who use noscript selectively. How easy is it to create a malicious file that matches the checksum of a known file? SHA-256? Very, very, very, very hard. I don't believe there are any known attacks for collisions for SHA-256.
- jmiserez 11y agoI think even a collision (any collision) has yet to found.
- dexterdog 11y agoAnd if you worry about that you can always use 384. Plus a side benefit is that 384 is faster on a 64-bit processor.
- dinkumthinkum 11y agoPeople make too big a deal of this collision stuff, a lot of times these are very theoretical would require tremendous computation. Anyway, for this use case, even md5, how likely really to make a useful malicious that file collides with a particular known and widely used one? I dunno seems pretty unlikely.
- eertami 11y ago>How easy is it to create a malicious file that matches the checksum of a known file? I'd say not easy at all, practically impossible. https://en.wikipedia.org/wiki/Preimage_attack https://en.wikipedia.org/wiki/Preimage_attack
- dkopi 11y agoThe problem with hosting JS libraries on CDNs is that the cache has a network effect. You only gain performance if the browser already has a cached version of this specific version on this specific CDN. If you don't - you end up losing performance, because now an additional DNS lookup needs to be performed, and an additional TCP connection needs to be opened. Here are a few reasons people choose to avoid CDNized versions of JS libraries. http://www.sitepoint.com/7-reasons-not-to-use-a-cdn/ http://www.sitepoint.com/7-reasons-not-to-use-a-cdn/ This is a 6 year old post, but it raises some valid concerns: https://zoompf.com/blog/2010/01/should-you-use-javascript-library-cdns https://zoompf.com/blog/2010/01/should-you-use-javascript-li...
- kevincox 11y agoThis is why I recommended using the CDN recommended by the project, most do recommend a CDN, for example jQuery has it's own CDN. As for adoption, that is very much a chicken and egg problem.
- tracker1 11y agoEven then, different versions will have their own misses... not to mention 3rd party libraries on another CDN means another DNS hit. DNS resolution time is a pretty significant impact for a lot of sites.
- brianjackson 11y agoYa I would have to agree with you tracker. Ever 3rd party dependency is introducing another DNS lookup. The whole point behind using a CDN effectively, besides lowering latency, is to reduce your DNS lookups to a bare minimum. For example, I use https://www.keycdn https://www.keycdn. They support HTTP/2 and HPACK compression along with Huffman encoding which reduce the size of your headers. The benefits of hosting say Google Fonts, Font Awesome, jQuery, etc. all with KeyCDN is that I can take better advantage of parallelism if I have one single HTTP/2 connection. Not to mention I have full control over my assets to implement caching (cache-control), expire headers, etags, easier purging, and the ability to host my own scripts.
- hyperpape 11y agoI agree with you that putting JQuery in the browser is a bad idea, but the second part of the argument, that the browser will have the libraries in cache is not really that reliable. Here's some notes I collected on the subject: http://justinblank.com/notebooks/BrowserCacheEffectiveness.html http://justinblank.com/notebooks/BrowserCacheEffectiveness.h....
- kevincox 11y agoI was definitely speaking whimsically because it is a huge chicken and egg problem. But theoretically if every site that used jQuery referenced a version at https://code.jquery.com/ https://code.jquery.com/ there would be a very good hit ratio, even considering different versions. However we are a very, very far way away from that.
- throwaway_1011 11y agoThat may seem like a good solution for some sites, but the name of the page and the requestor's IP address and other information is also 'leaked' to jquery.com. This is not always welcomed. For example, a company has an acquisition tracking site (or other legal-related site) and the name of the targets are part of the page name (goobler, foxulus, etc.) which get sent as the referrer page and IP address to jquery.com or other third party sites/CDNs. While not a security threat, you may unwittingly be recommending an unwanted information leak.
- dexterdog 11y agoBut it's only leaked when you don't have it cached. Otherwise the client doesn't even make the request.
- sametmax 11y agoYes but no. - jquery.com becomes a central point of failure and attack; - jquery gets to be the biggest tracker of all time; - cache does not stay forever. Actually, with pages taking 3Mo everytime they load, after 100 click (not much), I invalidated 300 Mo of cache. If firefox allow 2go of cache (it's lot for only one app), then an the end of the day, all my cache has been busted.
- nostrademons 11y ago> If everyone simply linked the "canonical" version of jQuery (the CDN link is right on their site) then requiring jQuery will be effectively free because it will be in everyone's cache. It's not, though. I ran this experiment when I tried to get Google Search to adopt JQuery (back in 2010). About 13% of visits (then) hit Google with a clean cache. This is Google Search, which at the time was the most visited website in the world, and it was using the Google CDN version of JQuery, which at the time was what the JQuery homepage recommended. The situation is likely worse now, with the rise of mobile. When I did some testing on mobile browsing performance in early 2014, there were some instances where two pageviews was enough to make a page fall out of cache. I'd encourage you to go to chrome://view-http-cache/ and take a look at what's actually in your cache. Mine has about 18 hours worth of pages. The vast majority is filled up with ad-tracking garbage and Facebook videos. It also doesn't help that every Wordpress blog has its own copy of JQuery (WordPress is a significant fraction of the web), or for that matter that DoubleClick has a cache-busting parameter on all their JS so they can include the referer. There's sort of a cache-poisoning effect where every site that chooses not to use a CDN for JQuery etc. makes the CDN less effective for sites that do choose to. [On a side note, when I look at my cache entries I just wanna say "Doubleclick: Breaking the web since 2000". It was DoubleClick that finally got/forced me to switch from Netscape to Internet Explorer, because they served broken Javascript in an ad that hung Netscape for about 40% of the web. Grrrr....]
- PavlovsCat 11y ago> The vast majority is filled up with ad-tracking garbage and Facebook videos. There is the problem then, and the solution? I for one don't make bloated sites nilly-willy, I suck at what I do but at least I do love to fiddle and tweak for the sake of it, not because anyone else might even notice; and I like that in websites and prefer to visit those, too. Clean, no-BS, no hype "actual websites". So I'd be rather annoyed if my browser brought some more stuff I don't need along just because the web is now a marketing machine and people need to deploy their oh so important landing pages with stock photos and stock text and stock product in 3 seconds. It was fine before that, and I think a web with hardly any money to be made in it would still work fine, it would still develop. The main difference is that it would be mostly developed by people who you'd have to pay to stay away, instead of the other way around. I genuinely feel we're cheating ourselves out of the information age we could have, that is, one with informed humans.
- JoshTriplett 11y ago> The problem is that almost everyone hosts their own version of jQuery. Any site that expects users to trust it with sensitive data should not be pulling in any off-site JavaScript. As for checksumming, browser vendors don't need to pre-load popular JavaScript libraries (though they might choose to do so, especially if they already ship those libraries for use by their extensions and UI). But checksum-based caching means the cost still only gets paid by the first site, after which the browser has the file cached no matter who references it.
- j3097736 11y agoThere's the firefox addon Decentraleyes https://github.com/Synzvato/decentraleyes https://github.com/Synzvato/decentraleyes which is trying to solve those problems, currently only the most common versions of each library are bundled, but there are plans to create more flexible bundles. There's no reason to hit the webserver with an If-modified when the libraries already include their version in the path.
- Sanddancer 11y agoIf everyone linked the "canonical" version of jquery, then that location would be worth a considerable amount of money to any sort of malicious actor. Just getting your version there for a few minutes could rope in many thousands of machines. Look at China's attack on github earlier in the year for examples of the damage that that sort of decision could do.