9 ms·
Cloudflare doesn't obey the Accept header on anything other than image content types. This means if you have an endpoint that returns HTML or JSON depending on
by simonw 3y ago
Cloudflare doesn't obey the Accept header on anything other than image content types.
This means if you have an endpoint that returns HTML or JSON depending on the requested content type and you try to serve it from behind Cloudflare you risk serving cached JSON to HTML user agents or vice-versa.
I dropped the idea of supporting content negotiation from my Datasette project because of this.
(And because I personally don't like that kind of endpoint - I want to be able to know if a specific URL is going to return JSON or HTML).
- ForkMeOnTinder 3y agoThat's even with the proper Vary header, right? Seems like a cloudflare bug if that's true. Maybe even a security bug if you find a service that supports text/plain.
- zoogeny 3y agoI haven't dealt with Cloudflare specifically, but I did deal with a number of big CDNs for large amounts of traffic. They were pretty adamant about NOT supporting arbitrary Vary header values. It broke some logic on a few of our systems and we eventually just decided to work around it instead of pushing our case. Interestingly, one of the big CDN providers did have controls in their UI for explicitly allowing/disallowing Vary header entries but they disabled it for us at some point (e.g. it was still in the UI but greyed out). I assumed once we hit a certain level of traffic it was too computationally expensive? Ever since, I've avoided any kind of fancy header/response variance in APIs just in case I end up in the same situation. It is rarely a necessity. IIRC, the only thing they continued to support variance wise was gzip (e.g. content-encoding). It's also worth noting they were extremely conservative with query parameters too. Also to reiterate, this was very high traffic and high volume with expectations of low latency, so probably not applicable to most people using CDNs for static website assets.
- derefr 3y ago> I assumed once we hit a certain level of traffic it was too computationally expensive? Seems strange; AFAIK in e.g. Varnish, Vary just means you get more "stuff" tacked onto the buffer that gets built from the request and then hashed to create the cache key. And actually, come to think of it, if memory for N concurrent in-flight requests is the concern, then you don't even need an actual (dynamically allocated) buffer, either; presuming you're using a streaming hash, you can feed each constituent field directly into the hasher, with only the hasher's (probably stack-allocated) internal static buffer for blockwise hashing required. (Which you're gonna need regardless of whether you're doing any Vary-ing.) So it's really just a question of how many CPU cycles are being spent hashing. And it's likely just going to be a difference between hashing 300 bytes (base request — hostname, path, headers that are always implicitly Varied upon) and 350 bytes (those things, plus whatever you explicitly Varied) per request. Doesn't seem like too much of a win... (especially when hardware-accelerated hashing ops operate on blocks anyway, such that you only get stepwise cost increases for every e.g. 128 bytes.) I wonder why they bothered?
- zoogeny 3y agoTo be clear, I'm not trying to make their argument for them since we spent probably 1 day working around it. I'm just passing along an anecdote. One day, Vary header stopped working on one CDN and we had to fix it. When I spoke to our account rep (I literally had a weekly call with them due to our usage) he said they were phasing it out for performance reasons. Not long after we got notice from another CDN asking for similar consideration. I have no inside knowledge as to their infrastructure or systems that made this a requirement. I very much doubt it was the cost to hash, maybe more likely something to do with their network topology and how requests were routed from origin to regional tiers to PoPs? I'm totally speculating here. If this had been a necessity then I would have probably dug into the request more deeply. It was a "pick your battles" kind of thing. Extremely low cost on our side to change, no reason to bother if they claimed it would decrease problems on their side.
- deleted 3y ago[deleted]
- phanimahesh 3y ago
- wbl 3y agoThe problem with Vary is that it massively expands footprints and reduces cache efficiency when overused. In a CDN this can create noisy neighbor like issues.
- shawn-butler 3y agoSerious problem if true
- Gigachad 3y agoMeh. Too many ways to solve the same problem results in bugs and security issues. There is no case where you need to use the Accepts header, you can just put that info in the URL instead. Some effort to clean up the useless and duplicate features would be good.
- stephenr 3y agoHow exactly do you specify weighted preferences for several possible types, in the url, in a way that is standardised?
- JoshTriplett 3y agoOther than for images and video, when in practice do you want to do that? If you're talking to an API, you should know what that API can produce. And if you're talking to a webpage, this isn't an issue.
- stephenr 3y ago> when in practice do you want to do that? Well as you're quite interested in APIs: the accept header is a fantastic way to version a REST API, and even has a built in way to handle clients that can talk to multiple versions of the same API. Your app could request just 'application/vnd.foo.v1+json' from an API and if it's a centralised service that may be fine. If the API your app talks to is something that's deployed to customers, or rolls out to regions incrementally or whatever, and thus can be at different versions, you need a way to handle that: the Accept header has you covered.
- Gigachad 3y agoEveryone does this by putting /v1/ in the api url. It's massively more visible, gets logged properly, and isn't annoying to request from tools like curl. The Accept header does nothing that URLs can not.
- judge2020 3y agoI think you mean for CDN-cached content, right? The accept header is passed along, so your server can respond however it wants for dynamic content/not cached by CDN.
- acdha 3y agoYes - this can be a big problem if it means that users get a cached response which isn’t compatible with their browser. It can also mean that you lose cache efficiency if you don’t get a ton of traffic - one site I worked on would’ve gotten much slower if they used WebP because it would have increased your odds of not getting a CDN-cached response, and the ~10-15% byte size savings just wasn’t worth that.
- LeonB 3y agoA wisened old web developer named Rik told me that in a wiki he wrote long ago he performed his own cache-busting form of content negotiation by having urls ends with a file type — E.g. /fred.html - returns html /fred.md - returns markdown /fred.json - returns json (I’m guessing that also — /fred - defaults to html ) Though he was a strict fundamentalist restafarian in other ways he held that this had the benefits of 1. Working 2. Being readily understood I’ve seen worse ideas. Only downside is that the urls look a bit ugly.
- euroderf 3y agoBut doesn't << /fred.md >> beat the heck out of something like << /fred?format=md >>?
- incrudible 3y agoFrom a URL aesthetics perspective, yes. Otherwise it just adds yet another dimension of how to pass a parameter.
- capableweb 3y agoDepends. Including it in the location part of the URL could make it harder to understand if you have multiple parameters, instead of just one. Using query parameters makes it very explicit, which can be good or bad depending on your use case, while sticking it into the URL makes it implicit. Together with that, you can also go for putting it in the `hash` part of the URL, if you want to keep it a secret from the server, as the server doesn't receive those parts, `/fred/#format=md`. In this case, it doesn't make much sense, but useful to know for other things, like keys and whatnot.
- matt-attack 3y agoIs don’t think I that hash method would work since that part of the URL isn’t sent to the server. It’s strictly used by the client to decide which part of the response we to show. Server has no need for it.
- 3y ago
- VWWHFSfQ 3y agoAre you certain that it is actually not supported? I suspect the CDN cache was not configured to vary cached responses via the Vary header. That header will usually at minimum look like: Vary: Cookie, Authorization, Accept, Accept-Language
- simonw 3y agoI mistyped: it's that Vary header that Cloudflare doesn't support (for non-image resources) - see https://developers.cloudflare.com/cache/concepts/cache-control/#other https://developers.cloudflare.com/cache/concepts/cache-contr...
- 4death4 3y agoThis sounds like a misconfiguration. I haven't used Cloudflare, but I've used other CDNs, and they need to generate a cache key with as few header values as possible to maximize the cache hit rate. The headers used in generating the cache key is configurable, so if you want to use the accept header in your application, then you're free to do so, but you need to tell Cloudflare that's an important part of your application.
- simonw 3y agoThat's the problem: they don't respect the Vary header for HTML or JSON documents, just for images. It's a documented decision that they've made: https://developers.cloudflare.com/cache/concepts/cache-control/#other https://developers.cloudflare.com/cache/concepts/cache-contr...
- simonw 3y agoHere's where Cloudflare document this: https://developers.cloudflare.com/cache/concepts/cache-control/#other https://developers.cloudflare.com/cache/concepts/cache-contr... > vary — Cloudflare does not consider vary values in caching decisions. Nevertheless, vary values are respected when Vary for images is configured and when the vary header is vary: accept-encoding.
- laurencerowe 3y agoI'm undecided about whether the Semantic Web is generally useful for not, but in certain domains it does seem to have some worth. When building a public scientific database I really want the URL identifying the item in the database to return the page for that item when I enter it in a browser but to return the appropriately structured data for that item when requested with "Accept: application/ld+json" or "application/rdf+xml" by a linked data library. So it's unfortunate that there's no good way to support this with common CDNs. Of course I always make it so that appending "?type=json" or "?type=xml" gets you the appropriate document.
- mariusor 3y ago> Cloudflare doesn't obey the Accept header That's not true. They do, but you have to add it to the Vary header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Vary https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Va... This way user-agents know that the Accept header is involved in the final form of the response. (As another example, Firefox also doesn't take Accept into consideration when caching locally by default, and it does with Varry: Accept)
- mariusor 3y agoAfter looking some more into it, it seems that parent was right, and the examples I had in mind are not cached at all, that being the reason why it looked like it was working. Their documentation specifically mentions Vary as non-functional[1] outside a paid plan, and even then only for images[2]. [1] https://developers.cloudflare.com/cache/concepts/cache-control/#other https://developers.cloudflare.com/cache/concepts/cache-contr... [2] https://developers.cloudflare.com/cache/advanced-configuration/vary-for-images/ https://developers.cloudflare.com/cache/advanced-configurati...
- Joker_vD 3y ago"HTTP/1.1 is a delightfully simple protocol, if you ignore most of it". A Vary field containing a list of field names has two purposes: 1. To inform cache recipients that they MUST NOT use this response to satisfy a later request unless the later request has the same values for the listed header fields as the original request (Section 4.1 of [CACHING]) or reuse of the response has been validated by the origin server. In other words, Vary expands the cache key required to match a new request to the stored cache entry. [...] When a cache receives a request that can be satisfied by a stored response and that stored response contains a Vary header field (Section 12.5.5 of [HTTP]), the cache MUST NOT use that stored response without revalidation unless all the presented request header fields nominated by that Vary field value match those fields in the original request (i.e., the request that caused the cached response to be stored). So, Cloudfare doesn't actually implement HTTP/1.1: you can't just decide to ignore parts of the standard and still claim you implement it, that's only allowed for "SHOULD [NOT]" and "MAY" parts, not for the "MUST [NOT]" ones.
- lucasyvas 3y agoIt seems crazy to me that Cloudflare of all companies doesn't work as expected with a content header. This is a bug even if it's deliberate.
- SergeAx 3y agoAre you adding `Vary: Accept` header to your response, including 304 response code?