Skip to content

fix(cache): make VarianceBuilder cache-key encoding injective - #1097

Open
alisel5 wants to merge 1 commit into
cloudflare:mainfrom
alisel5:fix/variance-injective-key
Open

alisel5 wants to merge 1 commit into
cloudflare:mainfrom
alisel5:fix/variance-injective-key

Conversation

@alisel5

@alisel5 alisel5 commented Nov 1, 2026

Copy link
Copy Markdown

What

VarianceBuilder::finalize() serialized each (name, value) variance dimension into the hash input separated by a single 0x00 byte. That separator is ambiguous for any field that can itself contain 0x00: two different sets of dimensions can serialize to identical bytes and therefore hash to the same cache-variance key.

For example, these currently produce the same variance key:

  • { "a": "b", "c": "d" }
  • { "a": "b\0c\0d" }

Change

Length-prefix each field so the hashed bytes are an injective encoding of the (name, value) pairs — field boundaries become explicit rather than delimiter-based, so no field's contents can reproduce a boundary. Behavior is unchanged for all 0x00-free inputs (which includes every HTTP header value). Adds a regression test (test_field_boundaries_are_unambiguous).

This mirrors the reasoning already documented for the removed CacheKey::new namespace argument, whose concatenation "preserves its ambiguous component boundaries".

Compatibility

Variance hashes change, so a cache that stores entries carrying variance sees a one-time cold start for those entries. Entries without variance are unaffected (finalize() returns None and combined_bin() falls back to the primary key). This is the same migration caveat noted for the namespace removal.

Testing

cargo test -p pingora-cache --lib variance::

5/5 pass (4 existing + the new regression test).

VarianceBuilder::finalize() separated each (name, value) dimension with a
single 0x00 byte. That separator is ambiguous for any field that can itself
contain 0x00, so two distinct variance sets could serialize to identical bytes
and collide onto the same cache key (e.g. {a: "b", c: "d"} and {a: "b\0c\0d"}).

Length-prefix each field instead, so the hashed bytes are an injective encoding
of the (name, value) pairs regardless of their contents. Field boundaries are
then explicit rather than delimiter-based. Adds a regression test.

Note: this changes variance hashes, so a cache storing entries with variance
sees a one-time cold start for those entries (same migration caveat as the
earlier namespace removal).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant