News for hackers. Stuff that matters.
Re:Using Immutable Caching to Speed Up the Web by achairapart on Thursday January 26, 2017 @03:30PM (#13494997)
Maybe it's time for browsers to go beyond the cache concept and implement a common standard package manager. Download once, stay forever. True immutable.

As developers, we try everyday to squeeze till the last byte and optimize things. We all know how performance is important.

So why download for every website the same asset: React, jQuery, libraries, CSS utils, you-name-it? What a waste!

Re:Using Immutable Caching to Speed Up the Web by benjaminjackman on Thursday January 26, 2017 @06:43PM (#13496473)
We don't need to have anything as complex as package manager. It would be much easier to just link to these libraries (and any other resource) by the hash of their content. If you do that you would just have to link to resources by their content hash.

I'm not really sure why this isn't already in place.

edit: The reason I'm not sure why because it sure seems to me that multiple threads on the post are all suggesting basically the same simple idea, the ability to serve a file by it's hash (either just by it's hash alone, or by a url + the hash). Personally, I think whatever form these url's take i think it ought to be backward compatible which I think is possible.

Re:Using Immutable Caching to Speed Up the Web by Perseids on Friday January 27, 2017 @09:33AM (#13499984)
> I'm not really sure why this isn't already in place.

Everything we need is already in place, except for a tweak in the caching strategy of the browsers[1]. With Subresource Integrity [2] you provide a cryptographic hash for the file you include, e.g.

  <script src="https://example.com/example-framework.js"
          integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"
          crossorigin="anonymous"></script>
As it is, browsers first download the file and then verify it. But you can also switch this around and build a content addressable cache in the browser where it retrieves files by their hash and only issue a request to the network as a fallback option, should the file not already be in cache. Combine this with a CDN which also serves their files via https://domain.com/$hash.js [3] and you have everything you need for a pretty nice browserify alternative, without any new web standardization necessary.

[1] And lot's of optimization to minimize cache eviction and handle privacy concerns, but that are different questions.

[2] https://developer.mozilla.org/en-US/docs/Web/Security/Subres...

[3] Imagine if some CDN would work together with NPM, so every package in NPM would already be present in the CDN.

Re:Using Immutable Caching to Speed Up the Web by bugmen0t on Friday January 27, 2017 @11:43AM (#13500795)
Folks in W3C webappsec are interested, but the cross-origin security problems are hard. We'd love feedback from developers as to what is still useful without breaking the web. Read this doc and reach out! https://hillbrad.github.io/sri-addressable-caching/sri-addre...
Re:Using Immutable Caching to Speed Up the Web by benjaminjackman on Friday January 27, 2017 @12:24PM (#13501104)
What's the best way to reach out?

I think that using the integrity attribute is great because if it happens it's going to have to work through a lot of tricky implementation details (e.g. things like origin laundering) of moving to an internet of content by hash rather than content by location.

However beyond just having an integrity attribute added to html I am interested in the question of how do we encode an immutable url as well as the content-hash for what it points to (as well as additionally required attributes) into a `canonical hash-url` (i.e. encode all these attributes) that is backward compatible with all the current browsers / devices, and which browsers can use in the future to locate an item by hash and/or by location.

The driving reason for this encoding is make sharing of links to resources more resilient, and backwards compatible. Eventually the browsers could parse apart the `canonical hash-url`s and use their own stores for serving the data, but not until the issues (and likely other unthought of ones) listed in the sri addressable caching document you linked are worked through.

Re:Using Immutable Caching to Speed Up the Web by EE84M3i on Friday January 27, 2017 @12:32PM (#13501168)
These problems are really hairy. Thankfully, all the privacy issues are only one-bit leakages (and there are TONS of one-bit leakages in web browsers), but the CSP bypass with SRI attack is really cool.

One thing that I've found incredibly disappointing about SRI is that it requires CORS. There's some more information here: https://github.com/w3c/webappsec/issues/418 but it essentially means that you can't SRI-pin content on a sketchy/untrustworthy CDN without them putting in work to enable CORS (which, if they're sketchy and untrustworthy, they probably won't do).

The attack that the authors lay out for SRI requiring CORS is legitimate, but incredibly silly - a site could use SRI as an oracle to check the hash value of cross-domain content. You could theoretically use this to brute force secrets on pages, but this is kind of silly because SRI only works with CSS and JavaScript anyway.

Re:Using Immutable Caching to Speed Up the Web by bugmen0t on Friday January 27, 2017 @01:39PM (#13501599)
I, as someone who worked on the SRI spec find this incredibly disappointing as well. We've tried to reduce this to "must be publicly cachable", but attacks have proven us wrong.

And unfortunately, there are too many hosts that make the attack you mention credibly silly:

It is not uncommon that the JavaScript served by home routers contains dynamically inserted credentials. And the JSON response from your API is valid JavaScript.

Re:Using Immutable Caching to Speed Up the Web by bugmen0t on Friday January 27, 2017 @01:36PM (#13501568)
Addendum

To be completely honest: Only reach out if you have solutions for any of the problems or can reduce what you want down to something that is solvable with these problems in mind.

If your solution does not live on the web, you'll have a hard time finding allies in the standards bodies that work on the web :)

You'll have a hard time convincing spec editors and browser vendors already. The working group mailing list is https://lists.w3.org/Archives/Public/public-webappsec/

If you have minimal edits to the spec, we can take it straight to Github. SRI spec contains a link to the repo.

Re:Using Immutable Caching to Speed Up the Web by drinkjuice on Thursday January 26, 2017 @10:29PM (#13497473)
Well, HTTP does have etag ( https://en.wikipedia.org/wiki/HTTP_ETag ) but of course that would still require a request sending the known etag, to either get 304 not modified or the content. So how about a way to put the etags of assets into the header of the document loading them or something? Then the browser can decide if it wants to make that request.

And I also have the feeling that surely, just this exist, I just don't know about it ^^

Linking things by hash, other than being so very ugly, consider this case: you have this asset that changes every minute, and is several MB. If a users users your site too much, you will flush out out all other cached stuff with old versions of that file that will never get referenced again. That just strikes me as extremely wasteful, that is, you get a short term boost but even worse performance overall. If other sites do it too much, it will mean your own stuff will not even be cached when visitors come back.

Re:Using Immutable Caching to Speed Up the Web by viraptor on Friday January 27, 2017 @05:50AM (#13499001)
It's called subresource integrity. The spec is at: https://www.w3.org/TR/SRI/

Mozilla already implements it: https://hacks.mozilla.org/2015/09/subresource-integrity-in-f...

Re:Using Immutable Caching to Speed Up the Web by drinkjuice on Friday January 27, 2017 @10:23AM (#13500272)
Does this make the browser avoid the request if it already has a cached script with that hash? If so, that would indeed be exactly what I was hoping for, except it would need to be extended for all things, not just javascript. Anyway, thanks!
Re:Using Immutable Caching to Speed Up the Web by achairapart on Thursday January 26, 2017 @07:16PM (#13496663)
> We don't need to have anything as complex as package manager.

Maybe you're right. But still, today we need very complex build tools and silly server hacks like setting expire headers in 10 years.

I wish instead it was as simple as deploying my 3kb app.js and tell the browser: "Hello there, here's my manifest with all the dependecies I need to run my app. Thank you.".

Re:Using Immutable Caching to Speed Up the Web by amelius on Friday January 27, 2017 @07:28AM (#13499339)
> It would be much easier to just link to these libraries (and any other resource) by the hash of their content.

That sounds like IPFS, [1].

[1] https://ipfs.io/#how

Re:Using Immutable Caching to Speed Up the Web by koalalorenzo on Friday January 27, 2017 @08:34AM (#13499657)
I hope that one day all the browsers will implement IPFS for this!
Re:Using Immutable Caching to Speed Up the Web by paulddraper on Thursday January 26, 2017 @10:20PM (#13497435)
> It would be much easier to just link to these libraries (and any other resource) by the hash of their content.

And that's exactly what Nix and Guix do. Elegant solutions to age old problems.

Re:Using Immutable Caching to Speed Up the Web by taeric on Thursday January 26, 2017 @07:50PM (#13496834)
Linking by hash to all common resources would be bad. You do want fixes and updates, after all.
Re:Using Immutable Caching to Speed Up the Web by jacobsenscott on Thursday January 26, 2017 @08:00PM (#13496876)
Links are already tied to some specific version. Nobody serves "jquery.js" that points to a constantly updated version of jquery. Nobody that's sane anyway.
Re:Using Immutable Caching to Speed Up the Web by taeric on Thursday January 26, 2017 @08:56PM (#13497125)
Fair. My point was more that for many resources, you probably do appreciate this. Certainly package managers do this. Not sure why web would be different.
Re:Using Immutable Caching to Speed Up the Web by rwmj on Thursday January 26, 2017 @03:52PM (#13495157)
Powered by a content-addressable cache, where each object is identified by the hash of its content only: https://rwmj.wordpress.com/2013/09/09/half-baked-idea-conten...
Re:Using Immutable Caching to Speed Up the Web by niftich on Thursday January 26, 2017 @04:39PM (#13495482)
In a browser's current usage model, this is vulnerable to a 'cache origin confusion attack'. See this thread [1]. It's a bit hard to follow, so perhaps see these posts [2][3], which state the problem succinctly. Let me adapt the text from [3]:

The problem is that www.victim.com/evil.js doesn't exist, and never did, but your browser won't know that if it's in the cache -- this gives you a way of faking files existing on other servers at the URL of your choice, and as long as they're in the cache you'll get away with it.

[1] https://news.ycombinator.com/item?id=10310594 [2] https://news.ycombinator.com/item?id=10311555 [3] https://news.ycombinator.com/item?id=10312333

Re:Using Immutable Caching to Speed Up the Web by btrask on Thursday January 26, 2017 @06:45PM (#13496488)
This issue is discussed here, with what I think is a workable solution: https://github.com/w3c/webappsec-subresource-integrity/issue...
Re:Using Immutable Caching to Speed Up the Web by wanderr on Friday January 27, 2017 @04:24AM (#13498683)
I think the best way to fully quell the objections involves subresource integrity but I think the ideal way to deal would be to add a new attribute, sameas="https://canonical jquery link" or something along those lines. Then if the browser already has canonical jquery link AND the subresource integrity checks out it'll use that one. Otherwise it will load from wherever the developer specified and not be stored in a shared cache associated with the canonical jquery url in any way. Browsers can track references to sameas URLs that they keep getting cache misses on and go fetch them in the background for future use, possibly even going back and deleting the duplicates it had from before. This would allow developers to have a much greater chance that their libraries load from cache without adding more dns lookups and dependencies on cdns or servers they can't control.
Re:Using Immutable Caching to Speed Up the Web by saurik on Thursday January 26, 2017 @06:35PM (#13496431)
From that thread, "CSP already has a mechanism for hash-based whitelisting - if this is the only limitation, it'd be just as easy to allow cache-sharing whenever CSP is absent and/or the specific hash is explicitly white-listed." <- I totally understand the attack, but fail to see why it would be a problem if CSP actually does, in fact, have the ability to whitelist hashes (and looking at the spec, it does seem to, though I have not paid attention to CSP before and admit I could be misinterpreting the spec based on this prompt).
Re:Using Immutable Caching to Speed Up the Web by tetrep on Thursday January 26, 2017 @06:10PM (#13496246)
This is fixed by relying on CORS and using a key of (domain, hash) rather than just the hash.
Re:Using Immutable Caching to Speed Up the Web by woogley on Thursday January 26, 2017 @06:12PM (#13496261)
But then you're still downloading jQuery (or whatever) again for every unique domain
Re:Using Immutable Caching to Speed Up the Web by d33 on Thursday January 26, 2017 @06:34PM (#13496423)
Not if it points to a CDN.
Re:Using Immutable Caching to Speed Up the Web by woogley on Thursday January 26, 2017 @07:25PM (#13496699)
This brings us back to current status, as it defeats the purpose of normalizing arbitrary CDNs with the hash
Re:Using Immutable Caching to Speed Up the Web by elviejo on Thursday January 26, 2017 @04:40PM (#13495484)
And then using bit torrent to do colaborative transfer. If we are in the same LAN and I have a part of the file you are requesting, I'll feed it to you... so decrease network traffic in 90%
Re:Using Immutable Caching to Speed Up the Web by david-given on Thursday January 26, 2017 @04:50PM (#13495558)
Putting my devil's-advocate hat on:

What sort of failure modes are there when something inevitably goes wrong, either through malice, incompetence or sheer plain accident, and you end up serving different content from the same hash?

Re:Using Immutable Caching to Speed Up the Web by hueving on Thursday January 26, 2017 @05:09PM (#13495712)
If you've discovered a way to generate hash collisions with a modern crypto hashing algorithm, there is more glory to be had than screwing with people's browser caches.
Re:Using Immutable Caching to Speed Up the Web by cortesoft on Thursday January 26, 2017 @05:15PM (#13495760)
So lets skip the malice condition and go to the plain accident one; there are more chunks of content in the world than there are bits in any of the hashes we could use. We are eventually going to run into collisions.
Re:Using Immutable Caching to Speed Up the Web by Derander on Thursday January 26, 2017 @06:07PM (#13496222)
Arbitrary example: If you're using SHA256 you're emitting 256bit hashes. Hopefully the distribution of SHA256 outputs is indistinguishable from random (or close enough) or else we've got big problems in other domains.

If you're drawing at random from a pool of size k you need approximately sqrt(k) draws until you reach a ~50% chance of a collision[0].

With 256 bits, there are 2^256 possibilities, so following the rule-of-thumb you'd need 2^128 draws until you had a 50% chance of a collision.

2^128 > # of atoms in the universe.

If you adjust your risk tolerance you'll have different numbers come out, but the chance of a collision in any realistic scenario is negligible.

[0]: https://en.wikipedia.org/wiki/Birthday_problem

Re:Using Immutable Caching to Speed Up the Web by ycmbntrthrwaway on Thursday January 26, 2017 @05:48PM (#13496071)
> there are more chunks of content in the world than there are bits in any of the hashes we could use

The probability of collision is still negligible.

Re:Using Immutable Caching to Speed Up the Web by thinkloop on Thursday January 26, 2017 @05:52PM (#13496104)
Come on, you must know that hashes are astronomical, unpredictable and collision-free for any earthly purpose. If not, just add a bit.
Re:Using Immutable Caching to Speed Up the Web by jonchang on Thursday January 26, 2017 @06:02PM (#13496185)
Decentraleyes for Firefox helps with this issue, it's a script cache for CDN assets like jQuery: https://github.com/Synzvato/decentraleyes/wiki/Simple-Introd...
Re:Using Immutable Caching to Speed Up the Web by Orangeair on Thursday January 26, 2017 @03:41PM (#13495074)
Shouldn't using common CDNs solve this problem?
Re:Using Immutable Caching to Speed Up the Web by acdha on Thursday January 26, 2017 @03:52PM (#13495147)
Using CDNs lowers the hit rate because the average user's browser cache will have each library stored number of CDNs times the number of versions in widespread usage. A package manager could improve some of this by allowing you to say “Any version of jQuery between 1.0 and 2.0 is fine” when most sites don't depend on point releases.

The Subresource Integrity spec for strong content hashes could improve cache hits by allowing a different URL to be used if the hash matches but everyone wants to avoid that turning into a massive security / privacy hit — see e.g. https://hillbrad.github.io/sri-addressable-caching/sri-addre...

Re:Using Immutable Caching to Speed Up the Web by px1999 on Thursday January 26, 2017 @05:53PM (#13496111)
I understand the idea of supporting ranges of libraries from a development perspective, but when you're running something in production, is "use any of these configurations" ever desirable?

Presumably there's a reason that you'd _want_ them to use 1.12.4 over 1.0.0 (or you'd want to at least check that they didn't break anything that you rely on in 1.13 when that comes out)?

Re:Using Immutable Caching to Speed Up the Web by acdha on Thursday January 26, 2017 @09:50PM (#13497329)
It's desirable if you're really focused on performance – if you're not a site like Google/Facebook which people access all the time, it's safe to assume that none of your resources are cached and so you might find n milliseconds of load time a healthy gain (less so now that HTTP/2 is widespread). In many cases it's more likely that you'd know of a bug fixed in a new release – where you'd say libfoo >= 1.12.4 – than that 1.3 has a huge problem.
Re:Using Immutable Caching to Speed Up the Web by michaelmior on Thursday January 26, 2017 @04:46PM (#13495524)
Using a CDN doesn't lower the hit rate over not using a CDN. I assume you mean using a CDN has a potentially lower hit rate than using a package manager within the browser.
Re:Using Immutable Caching to Speed Up the Web by acdha on Thursday January 26, 2017 @09:53PM (#13497335)
One factor to consider: what's the cost of the extra DNS lookups and connection latency to a new CDN host vs. your main host, especially in the post-HTTP/2 era? If you had something like SRI where the browser might not even connect at all, that's a pure win but otherwise it's quite easy to find real pages where the extra 500-5000 milliseconds to connect to the CDN is greater than serving a modest-sized file.

(That's not an exaggeration: I've measured uncached latency for DNS + connection for a CDN host in the high end of that range, especially over cellular connections or outside of the U.S.)

Re:Using Immutable Caching to Speed Up the Web by kijin on Friday January 27, 2017 @12:23AM (#13497942)
South Korea here. Every time I find random asset-loading delays in my clients' websites, it's caused by one of the major CDNs such as Google web fonts and code.jquery.com.

International connectivity out of Korea is pretty congested, especially at peak hours. Tokyo is 30-40ms away in the morning but randomly jumps to 150ms+ in the evening. It can take more than 1 second for the DNS lookup, TCP and SSL handshakes, let alone the actual transfer. So unless the CDN in question has a physical presence in Korea, it is almost always faster to load assets directly from the web server.

I suspect that many regions outside of US/EU are in a similar situation. Using a POP in another country 2000km away does jack shit for local websites, and only harms companies that fall for aggressive CDN marketing.

Re:Using Immutable Caching to Speed Up the Web by michaelmior on Friday January 27, 2017 @11:03AM (#13500535)
Sure, if your target market is outside the area covered by your CDN's POPs then you're not going to get anything out of it. Although if you do get a CDN that's appropriately located it can be a huge win if you can avoid leaving certain country boundaries.
Re:Using Immutable Caching to Speed Up the Web by michaelmior on Friday January 27, 2017 @11:01AM (#13500522)
Fair point. It's true that hosting CDN assets on a separate domain at least requires more thought than it used to. Although there's also the fact that if you're using a common CDN the browser may have DNS cached. Plus it can still be a win if the CDN is well-located wrt to your target audience and you have a fair number of assets.
Re:Using Immutable Caching to Speed Up the Web by acdha on Friday January 27, 2017 @09:25PM (#13504466)
“may have DNS cached” was less common than I thought – even using some fairly popular CDN providers, the RUM DNS latency outliers are surprisingly high even for clients in areas with CDN edge nodes.
Re:Using Immutable Caching to Speed Up the Web by achairapart on Thursday January 26, 2017 @03:48PM (#13495114)
I think they are more like a band-aid than a solution.
Re:Using Immutable Caching to Speed Up the Web by notatoad on Thursday January 26, 2017 @11:46PM (#13497777)
How is your proposal different to what's happening here? If two websites call a library from the same CDN that implements immutable, then isn't that just a distributed package manager with URL-based namespacing?
Re:Using Immutable Caching to Speed Up the Web by achairapart on Friday January 27, 2017 @06:25AM (#13499115)
The problem will be that every website should use the same CDN. Not likely to happen and centralize things is never good.

You'll also miss other features: version range alone could save tons of bandwidth.

Re:Using Immutable Caching to Speed Up the Web by notatoad on Friday January 27, 2017 @11:52AM (#13500870)
>centralize things is never good.

but centralizing in the browser is?

Re:Using Immutable Caching to Speed Up the Web by gambler on Friday January 27, 2017 @10:22AM (#13500270)
The fact that this architecturally clueless comment is at the top for an article about caching makes me wheep for the future of the web.

Packages inevitably create a tangle of complex problems that page/document model avoids. That is one of the main reasons web apps replaced normal apps in so many domains. Not to mention that a package system will give a huge competitive advantage to already popular scripts, which will inevitably lead to technological stagnation. And it will remove the biggest disincentive to avoid bloat.

Re:Using Immutable Caching to Speed Up the Web by vvggff on Thursday January 26, 2017 @03:34PM (#13495025)
Yeah, you know it so much that I have to download 100s of MBs to view a text-only site.
Re:Using Immutable Caching to Speed Up the Web by achairapart on Thursday January 26, 2017 @03:53PM (#13495159)
Why that? The browser download only the assets you require.
Re:Using Immutable Caching to Speed Up the Web by grashalm01 on Thursday January 26, 2017 @05:04PM (#13495668)
Great. By compromising one package you can access their bank account...
Re:Using Immutable Caching to Speed Up the Web by hueving on Thursday January 26, 2017 @05:06PM (#13495687)
Same thing as compromising one of these packages loaded from a CDN...

If someone loads a bad JS library, it doesn't matter where it came from.

Re:Using Immutable Caching to Speed Up the Web by ComodoHacker on Thursday January 26, 2017 @07:38PM (#13496775)
I expect WebAssembly to handle this.