Repository navigation
fix: include the effective host in the cache key - #9
Merged
Merged
Conversation
Both cache middlewares keyed responses on the URL only, so requests differing solely by Request.Host shared a cache entry and a response cached for one virtual host was served for another one. The key now carries the effective host, Request.Host when set and the URL host otherwise, and every part goes in as a length-prefixed field so a value of one part can't be read as another one.
umputun
approved these changes
Aug 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Both cache middlewares built the key from
req.URLonly.Request.Hostoverrides the URL host on the wire, so two requests to the same upstream URL but different virtual hosts shared one cache entry, and a response cached for one host was served for the other:The key now carries the effective host,
Request.Hostwhen set and the URL host otherwise, in bothmiddleware.Cacheand themiddleware/cacheadapter.While in the key builders, every part now goes in as a length-prefixed field. Concatenating raw values let one part be read as another one, so a header value could be crafted to produce the key of a different set of headers, and with
CacheWithBody/KeyWithBodya body could shift the host field. Header records are length-prefixed per name, per value and as a whole record, so a multi-valued header cannot look like several headers.Behaviour change worth noting on release: every default key changes,
KeyFunckeys do not.middleware.Cacheis in-memory and only loses entries for the lifetime of the process, but a persistent or shared cache behindmiddleware/cache(lcw and friends) starts cold after the upgrade, and instances on mixed versions use separate keyspaces until all of them are updated. Entries cached before the upgrade are unreachable rather than served, which is the point of the change.README documents the host as part of the key composition for both middlewares.