Skip to content

Avoid caching responses for authenticated users - #67110

Merged
wtgodbe merged 1 commit into
mainfrom
wtgodbe/avoid
Jun 10, 2026
Merged

wtgodbe merged 1 commit into
mainfrom
wtgodbe/avoid

Conversation

@wtgodbe

@wtgodbe wtgodbe commented Jun 10, 2026

Copy link
Copy Markdown
Member

No description provided.

If auth middleware was after the output caching middleware and some other auth was used besides the authorization header, then apps could end up caching a page meant for a specific user and then serve the cached page for anonymous or other users.
@wtgodbe
wtgodbe requested a review from BrennanConroy as a code owner June 10, 2026 00:20
Copilot AI review requested due to automatic review settings June 10, 2026 00:20
@github-actions github-actions Bot added the area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares label Jun 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates the OutputCaching default behavior to ensure responses generated for authenticated users are not stored in the output cache, including scenarios where authentication happens after the output cache middleware in the pipeline.

Changes:

  • Update DefaultPolicy to disable cache storage when the response is produced under an authenticated HttpContext.User.
  • Add tests verifying authenticated requests are not cached regardless of whether auth middleware runs before/after output caching.
  • Add a negative test demonstrating that placing auth middleware after output caching can result in auth not running on cache hits.

Reviewed changes

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

File Description
src/Middleware/OutputCaching/test/OutputCacheTests.cs Adds coverage for “authenticated responses are not cached” and a negative ordering test for auth-after-cache behavior.
src/Middleware/OutputCaching/src/Policies/DefaultPolicy.cs Prevents cache storage for responses when the request ends up authenticated.

Comment on lines +53 to +57
if (context.HttpContext.User?.Identity?.IsAuthenticated == true)
{
context.AllowCacheStorage = false;
return ValueTask.CompletedTask;
}
Comment on lines +1049 to +1053
{
services.AddOutputCache(outputCachingOptions =>
{
outputCachingOptions.BasePolicies = [new OutputCachePolicyBuilder().Build()];
});
Comment on lines +1148 to +1153
.ConfigureServices(services =>
{
services.AddOutputCache(outputCachingOptions =>
{
outputCachingOptions.BasePolicies = [new OutputCachePolicyBuilder().Build()];
});
@wtgodbe

wtgodbe commented Jun 10, 2026

Copy link
Copy Markdown
Member Author

/ba-g unrelated mac failure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-middleware Includes: URL rewrite, redirect, response cache/compression, session, and other general middlewares

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants