Skip to content

fix(proxy-cache): keep Vary variant keys out of reach of a crafted request - #13831

Open
nic-6443 wants to merge 4 commits into
apache:masterfrom
nic-6443:fix/proxy-cache-vary-key-collision
Open

fix(proxy-cache): keep Vary variant keys out of reach of a crafted request#13831
nic-6443 wants to merge 4 commits into
apache:masterfrom
nic-6443:fix/proxy-cache-vary-key-collision

Conversation

@nic-6443

@nic-6443 nic-6443 commented Aug 16, 2026

Copy link
Copy Markdown
Member

Description

The memory strategy of proxy-cache lays its shdict keys out as <cache key> for the plain entry, <cache key>::__vary for the variant index and <cache key>::<signature> for each variant, where the signature is md5() over the request's values for the varied headers.

The cache key is client controlled — cache_key defaults to $host and $request_uri — and the signature is offline computable, so a request to /<victim uri>::<md5 of the varied values> gets a cache key byte-identical to the victim's variant key. Nothing on the entry records which key it belongs to, so:

  • the crafted request is served the victim's cached variant (cross-request cache read), and
  • its own response is stored there, where the victim's next request looks the variant up (cache poisoning).

Same trick works against the ::__vary index key.

The fix keeps the derivation injective by prefixing the cache key with its length before appending anything: for two different cache keys either the length field or the key itself differs, so no crafted cache key can produce another one's storage key. CACHE_VERSION goes 2 → 3 alongside it, so an entry written under the old layout is bypassed and purged rather than served if a cache key happens to look like the new one.

Upgrade note: entries cached before the upgrade are unreachable under the new layout, so the first request per key is a MISS and a PURGE for a URL that only has pre-upgrade entries answers 404 — the old slots then sit in the shdict until their own TTL expires.

Not addressed here: base_key is still the plain concatenation of the cache_key components, so two different component tuples can produce the same base key (["a", "bc"] vs ["ab", "c"]). The same length-prefix trick would fix it in util.generate_complex_value; that is a separate change and a separate cache-key break.

t/plugin/proxy-cache/memory.t TEST 48 caches a Vary: Accept-Encoding response, then sends the crafted URI: on master the crafted request answers HIT with the victim's body, with this change it is a plain MISS with its own body and the victim's entry stays intact.

Which issue(s) this PR fixes:

N/A

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change — internal key layout, no documented behaviour changes
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

…quest

The memory strategy stores a Vary variant at `<cache key>::<signature>` and
the variant index at `<cache key>::__vary`. The cache key is client
controlled -- it defaults to `$host` and `$request_uri` -- and the signature
is the md5 of the request's values for the varied headers, so a request to
`<victim uri>::<md5 of the varied values>` produces a cache key equal to the
victim's variant key. Its response is stored where the victim's variant is
looked up, and the victim's cached response is served to it: cache poisoning
in one direction, a cross-request cache read in the other.

Derive all three keys from the cache key prefixed with its length instead,
which keeps the derivation injective, so no crafted cache key can be the
storage key of another one.
Copilot AI lite review requested due to automatic review settings August 16, 2026 14:47
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Aug 16, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the proxy-cache plugin’s in-memory (shdict) storage key derivation to prevent crafted requests from colliding with (and thus reading/poisoning) another request’s Vary-variant cache entries.

Changes:

  • Introduces length-prefixed base-key encoding (entry_key) and uses it consistently for base entries, Vary index keys, and variant keys.
  • Updates purge/update/lookup paths to use the new derived key helpers (entry_key, index_key, variant_key).
  • Adds a regression test that reproduces the crafted-URI collision scenario and asserts it no longer hits/overwrites the victim’s cached variant.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
apisix/plugins/proxy-cache/memory_handler.lua Switches shdict key derivation to a length-prefixed scheme for injective key construction across base/index/variant entries.
t/plugin/proxy-cache/memory.t Adds a regression test ensuring a crafted URI cannot collide with another request’s Vary variant key.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread t/plugin/proxy-cache/memory.t Outdated
Comment on lines +1597 to +1598
if not res then return nil, err end
return res.headers["Apisix-Cache-Status"], res.body:gsub("%s+$", "")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 112b6c9, it now guards the body like the other helpers in this file.

Comment on lines +46 to +50
-- Every shdict key for one cache key is derived from it: the entry itself,
-- the Vary index, and one entry per variant. The cache key is client
-- controlled (`cache_key` defaults to `$host` and `$request_uri`), so
-- deriving them by appending a suffix lets a crafted request produce a cache
-- key byte-identical to another request's variant key -- the attacker's

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point — a cache key that happens to look like the new layout could otherwise survive the upgrade. Bumped CACHE_VERSION to 3 in 112b6c9, so any entry written under the old layout is bypassed and purged on first access.

Comment on lines 136 to 140
-- Purge every variant entry referenced by the index, then the index itself,
-- and finally the legacy base-key entry (which may exist if the URL ever
-- cached a no-Vary response in the past). The index is read stale-tolerant:
-- it can outlive or be outlived by its variants (variant TTLs diverge when
-- cache_control derives them per response), and an expired index must still

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworded in 112b6c9 — it now describes the entry stored directly under the cache key instead of calling it the legacy base key.

An entry written under the old layout must never be served under the new
one, whatever the cache key looks like. Also guard the response body in the
new test and describe the purged base entry by its current key.
Exact-match routes on the same URI left over from earlier tests took
precedence over the prefix route, so the victim requests never reached it.
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Aug 16, 2026
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants