You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Response headers vary: Accept-Encoding is stopping undici from caching the http request #3959
However, I noticed that the request is never cached.
Reproducible By
See code snippet above.
Expected Behavior
The request should be cached and the fetch 2 should happen "instantly".
Environment
Node.js v22.11.0 on Darwin
Additional context
I noticed that https://easylist.to/easylist/easylist.txt is returning the Vary: Accept-Encoding response header regardless of whether Accept-Encoding is included in the request header. This causes undici to panic because undici doesn't expect Accept-Encoding to be included in the cacheKey:
Many CDNs will always return the Vary: Accept-Encoding header whether the HTTP client is sending an Accept-Encoding request header or not. However undici.request doesn't include Accept-Encoding and gzip & br decompression.
Same for GitHub RAW. GitHub RAW always returns Vary: Authorization,Accept-Encoding,Origin header. However, the Authorization and Origin headers might not always exist. The cache should still work w/o those exists in the request headers
changed the title [-]Rresponse headers `vary: Accept-Encoding` is stopping undici from caching the http request[/-][+]Response headers `vary: Accept-Encoding` is stopping undici from caching the http request[/+]on Dec 18, 2024
Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.
I am not sure of the proper way to fix this:
We could simply ignore any missing request headers (those undici are not sending) when parsing the Vary response header, but I am afraid that later requests with those request headers presented will result in cache poisoning.
Or we could use a placeholder for those missing request headers, but the placeholder couldn't be a symbol because it can't be properly serialized for storing.
If (after any normalization that might take place) a header field [specified in the vary header] is absent from a request, it can only match another request if it is also absent there.
I don't think cache poisoning would be a problem, but it is noteworthy that this change would mean that these two requests considered the same:
GET / HTTP/1.1Host: localhostHTTP/1.1 200 OKDate: asdVary: aasd123
GET / HTTP/1.1Host: localhostHTTP/1.1 200 OKDate: asdasd123
I am afraid that later requests with those request headers presented will result in cache poisoning.
It shouldn't, cache stores get the result of the parsed vary header which maps the header's name to the header's value. The store should be using it to find the request to use. If we ignore a header, it won't be in this mapping meaning it shouldn't be taken into consideration when deciding which response to use.
Regardless, that is definitely something that needs be covered in a unit test if we go down this road.
So I think we can ignore it if it's present in the vary header but isn't actually present in the request. The fix would be removing this else block:
I don't think cache poisoning would be a problem, but it is noteworthy that this change would mean that these two requests considered the same:
It shouldn't, cache stores get the result of the parsed vary header which maps the header's name to the header's value. The store should be using it to find the request to use. If we ignore a header, it won't be in this mapping meaning it shouldn't be taken into consideration when deciding which response to use.
I am still not sure if this is the path we should take. I am not sure how to properly implement this now.
but the placeholder couldn't be a symbol because it can't be properly serialized for storing.
Wdyt of using null?
This should work as long as someone doesn't ignore the typescript definitions and passes a Record<string, string | null>.
Sorry for lateish response - looking a bit deeper into this and I think was wrong here:
So I think we can ignore it if it's present in the vary header but isn't actually present in the request.
We can still continue parsing the vary header if we get to one that's in vary but isn't in the request ofc, however, cache stores still need to use it when deciding if a request is cached or not.
My original suggested change wouldn't allow for this and would mean that these two request & responses are seen as the same:
GET / HTTP/1.1Host: localhostHTTP/1.1 200 OKDate: asdVary: aasd123
GET / HTTP/1.1Host: localhosta: somethingHTTP/1.1 200 OKDate: asdasd123
This goes against the spec. So we would need to do something similar to what @SukkaW mentioned here
Or we could use a placeholder for those missing request headers
I'm not really sure on the best way to do this however. The best thing I can think of right now is what I mentioned here #3959 (comment) but I don't particularly like it
Activity
SukkaW commentedon Dec 17, 2024
Many CDNs will always return the
Vary: Accept-Encodingheader whether the HTTP client is sending anAccept-Encodingrequest header or not. Howeverundici.requestdoesn't includeAccept-Encodingand gzip & br decompression.Same for GitHub RAW. GitHub RAW always returns
Vary: Authorization,Accept-Encoding,Originheader. However, theAuthorizationandOriginheaders might not always exist. The cache should still work w/o those exists in the request headersmcollina commentedon Dec 18, 2024
Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.
[-]Rresponse headers `vary: Accept-Encoding` is stopping undici from caching the http request[/-][+]Response headers `vary: Accept-Encoding` is stopping undici from caching the http request[/+]SukkaW commentedon Dec 18, 2024
I am not sure of the proper way to fix this:
We could simply ignore any missing request headers (those undici are not sending) when parsing the
Varyresponse header, but I am afraid that later requests with those request headers presented will result in cache poisoning.Or we could use a placeholder for those missing request headers, but the placeholder couldn't be a symbol because it can't be properly serialized for storing.
mcollina commentedon Dec 18, 2024
What does the standard say? cc @flakey5
flakey5 commentedon Dec 18, 2024
section 4.1-4
So I think we can ignore it if it's present in the vary header but isn't actually present in the request. The fix would be removing this else block:
undici/lib/util/cache.js
Lines 273 to 275 in 3eeeeb7
I don't think cache poisoning would be a problem, but it is noteworthy that this change would mean that these two requests considered the same:
It shouldn't, cache stores get the result of the parsed vary header which maps the header's name to the header's value. The store should be using it to find the request to use. If we ignore a header, it won't be in this mapping meaning it shouldn't be taken into consideration when deciding which response to use.
Regardless, that is definitely something that needs be covered in a unit test if we go down this road.
flakey5 commentedon Dec 18, 2024
Wdyt of using
null?SukkaW commentedon Dec 19, 2024
I am still not sure if this is the path we should take. I am not sure how to properly implement this now.
This should work as long as someone doesn't ignore the typescript definitions and passes a
Record<string, string | null>.SukkaW commentedon Jan 2, 2025
@flakey5 What's your suggestions on this?
flakey5 commentedon Jan 5, 2025
Sorry for lateish response - looking a bit deeper into this and I think was wrong here:
We can still continue parsing the vary header if we get to one that's in
varybut isn't in the request ofc, however, cache stores still need to use it when deciding if a request is cached or not.My original suggested change wouldn't allow for this and would mean that these two request & responses are seen as the same:
This goes against the spec. So we would need to do something similar to what @SukkaW mentioned here
I'm not really sure on the best way to do this however. The best thing I can think of right now is what I mentioned here #3959 (comment) but I don't particularly like it
Patch undici to workaround nodejs/undici#3959