Repository navigation
[Caching] Distinguish absent and empty cache key values - #69233
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
Only minor test-coverage nits remain; no blocking issues were identified.
Pull request overview
Updates Output Caching and Response Caching key serialization to distinguish absent and present-empty header/query values.
Changes:
- Encodes empty values distinctly.
- Applies encoding across header and configured/wildcard query paths.
- Adds regression coverage for key collision prevention.
File summaries
| File | Description |
|---|---|
src/Middleware/ResponseCaching/test/ResponseCachingKeyProviderTests.cs |
Tests header and query collision prevention. |
src/Middleware/ResponseCaching/src/ResponseCachingKeyProvider.cs |
Encodes empty values distinctly. |
src/Middleware/OutputCaching/test/OutputCacheKeyProviderTests.cs |
Tests header and query collision prevention. |
src/Middleware/OutputCaching/src/OutputCacheKeyProvider.cs |
Encodes empty values distinctly. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
| Assert.NotEqual(cacheKeyProvider.CreateStorageKey(absentContext), cacheKeyProvider.CreateStorageKey(emptyContext)); | ||
| } | ||
|
|
||
| [Fact] |
There was a problem hiding this comment.
add another test to lock in behavior with multiple empty-value keys? like ?HeaderA=fast&HeaderA= and ?HeaderA=&HeaderA=
[Fact]
public void OutputCachingKeyProvider_CreateStorageKey_EncodesMultipleEmptyHeaderValuesInSequence()
{
var cacheKeyProvider = TestUtils.CreateTestKeyProvider();
var context = TestUtils.CreateTestContext();
context.HttpContext.Request.Headers["HeaderA"] = string.Empty;
context.HttpContext.Request.Headers.Append("HeaderA", string.Empty);
context.CacheVaryByRules.HeaderNames = new string[] { "HeaderA" };
Assert.Equal($"{EmptyBaseKey}{KeyDelimiter}H{KeyDelimiter}HeaderA{KeyNameValueDelimiter}{KeyNameValueDelimiter}{KeySubDelimiter}{KeyNameValueDelimiter}",
cacheKeyProvider.CreateStorageKey(context));
}
Fixes #68943
Overview
Output Caching and Response Caching previously serialized an absent configured query/header value identically to a present empty value, even though application code can distinguish them with
ContainsKey. This changes the four affected configured paths and their shared wildcard/multi-value query serialization so cache keys represent presence and value boundaries injectively. The change is limited to the two internal key providers and focused tests; no public API changes.Design
None (no public contract change).
The existing ASCII group separator (
\x1d) remains the name/value delimiter and now also marks each empty value. This was preferred over introducing another reserved character: delimiter validation already prevents user values from containing\x1d, so the representation is unambiguous and composes with the existing record (\x1e) and unit (\x1f) separators.Name\x1dName\x1d\x1dName\x1d\x1d\x1fValueImplementation
Response Caching applies the same encoding through
AppendValue, preserving its existing behavior of throwingCacheKeyDelimiterExceptionfor unsafe values. The affected equivalence classes are:Existing non-empty serialization, original value ordering, wildcard query-key discovery/sorting, and delimiter rejection are unchanged.
Outcome
?QueryA=?QueryA=Each regression was observed failing for the expected identical-key reason before the production change and passing afterward. Additional focused coverage protects wildcard empty query values and empty elements in multi-value query/header sequences. Full validation passed:
Microsoft.AspNetCore.OutputCaching.Tests: 399 passedMicrosoft.AspNetCore.ResponseCaching.Tests: 283 passedCompatibility: absent and non-empty keys remain byte-for-byte unchanged. Present-empty values intentionally move to corrected keys, causing a one-time miss and safe cache repopulation. Existing distributed cache entries are not proactively purged and continue to expire or be evicted normally.