dotnet / dotnet/aspnetcore

Output and Response Caching treat absent and empty query/header values as the same cache key

Open
#68,943 1 comment 0 reactions 0 assignees View on GitHub
area-middleware feature-output-caching feature-response-caching
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

When Output Caching or Response Caching varies by a configured query key or header, its cache key does not distinguish between the key/header being absent and the key/header being present with an empty value (such as `?mode=`).

The application layer does distinguish these states through `Request.Query.ContainsKey("mode")` and `Request.Headers.ContainsKey("X-Mode")`.

Example:
```csharp
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOutputCache();
builder.Services.AddResponseCaching();
var app = builder.Build();
app.UseResponseCaching();
app.UseOutputCache();
var counter = 0;
app.MapGet("/output/query", (HttpContext context) =>
{
return new
{
Present = context.Request.Query.ContainsKey("mode"),
Value = context.Request.Query["mode"].ToString(),
Counter = Interlocked.Increment(ref counter),
GeneratedAt = DateTimeOffset.UtcNow
};
})
.CacheOutput(policy => policy
.Expire(TimeSpan.FromSeconds(20))
.SetVaryByQuery("mode"));
app.Run();
```

```text
curl "http://localhost:5000/output/query"
curl "http://localhost:5000/output/query?mode="
```

Contributor guide

Open the contributing guide

Research direction

Start by tracing how Output Caching and Response Caching construct keys for configured query and header variation. Use the provided /output/query example and curl requests as the reproduction, then add regression coverage showing that an absent value and an explicitly empty value produce distinct cache entries.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.