Fix CsrfProtectionMiddleware perf degradations - #67488
Merged
DeagleGross merged 6 commits intoJul 2, 2026
Merged
Conversation
PR dotnet#67119 introduced an unconditional context.Items[MiddlewareInvokedKeys.CsrfProtection] = ... write in CsrfProtectionMiddleware.InvokeAsync, which forces the lazy HttpContext.Items dictionary to allocate on every request. This blew up allocations on hot non-antiforgery paths (~264 B/request extra; ~1.3 GB/s at TechEmpower plaintext throughput). See PR dotnet#67119 comment 4835979504. Gate the marker write on either endpoint == null (re-execute into an antiforgery-required page must still find the marker) or the matched endpoint carrying any IAntiforgeryMetadata (both DisableAntiforgery() for re-execute and the FormFeature backstop for RequiresValidation:true). For endpoints with no antiforgery metadata at all - the hot non-antiforgery path - the marker has no consumer and is now skipped. Adds CsrfProtection_HotPath_NoEndpointMetadata_DoesNotAllocateItemsDictionary as a regression guard using GC.GetAllocatedBytesForCurrentThread(). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes the auto-injected CsrfProtectionMiddleware hot path by avoiding unnecessary HttpContext.Items initialization when an endpoint has no antiforgery metadata, while preserving the marker behavior needed for reroute/re-execute scenarios and antiforgery/disable-antiforgery endpoints.
Changes:
- Conditionally stamps the
MiddlewareInvokedKeys.CsrfProtectionmarker only whenendpointisnullor whenIAntiforgeryMetadatais present. - Updates integration tests to assert the marker is not set for endpoints without antiforgery metadata.
Show a summary per file
| File | Description |
|---|---|
| src/DefaultBuilder/src/Internal/CsrfProtectionMiddleware.cs | Avoids HttpContext.Items allocation by not stamping the marker on the no-antiforgery-metadata path. |
| src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/CsrfProtectionIntegrationTests.cs | Adjusts assertions to reflect that the marker is not set when the endpoint has no antiforgery metadata. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 1
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
DeagleGross
force-pushed
the
deaglegross-dmkorolev-csrf-perf-no-items
branch
from
July 1, 2026 11:59
ec17a1d to
556d8f9
Compare
halter73
reviewed
Jul 1, 2026
javiercn
approved these changes
Jul 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes degradation noticed by @BrennanConroy (thanks!) #67119 (comment)
There are several problems:
CsrfProtectionMiddlewarewas unconditionally writingMiddlewareInvokedKeys.CsrfProtectiontocontext.Items. The hot-path where writing tocontext.Itemsis useless is when endpoint is not null, but there is no antiforgeryMetadata. In such case CSRF just skips since endpoint is intentionally not wired with Antiforgery validation mechanisms.CsrfProtectionMiddlewareis useless without Routing (because there is nocontext.GetEndpoint()and obviosly antiforgery metadata. However, we were always registeringCsrfProtectionMiddlewarein the pipeline, when Routing was injected only if_builtApplication.DataSources.Count > 0is true. So I applied same check there.Testing
For current
maincode:and for updated code: