Fix antiforgery/CSRF middleware lost on primary path when using re-execution#67667
Closed
oroztocil wants to merge 1 commit into
Closed
Fix antiforgery/CSRF middleware lost on primary path when using re-execution#67667oroztocil wants to merge 1 commit into
oroztocil wants to merge 1 commit into
Conversation
The Blazor Web template relies on the framework auto-injecting the antiforgery/CSRF middleware, which WebApplicationBuilder stores in a single-use post-routing slot consumed by EndpointRoutingMiddleware. RerouteHelper.Reroute (used by UseStatusCodePagesWithReExecute, UseExceptionHandler, UsePathBase and UseRewriter) builds a re-execution branch whose routing consumes that slot from the shared global route builder before the primary routing is built, stealing it from the primary request pipeline. A normal request to an antiforgery-required endpoint then throws 'a middleware was not found that supports anti-forgery' (HTTP 500). Capture and restore the post-routing slot around the re-execution branch build so both the primary and re-executed pipelines run the implicit middleware. Fixes #67628 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Summary
Fixes #67628. A newly created Blazor Web App (
dotnet new blazor+dotnet run) returns HTTP 500 on every page on 11.0 Preview 7 (regression from Preview 6), failing withEndpoint / (/) contains anti-forgery metadata, but a middleware was not found that supports anti-forgery. This also turned theTemplates.Mvc.Test.BlazorTemplateTestCI leg red on essentially every run.Analysis
The Blazor Web template no longer calls
app.UseAntiforgery()explicitly (removed in #67119); it relies on the framework auto-injecting the antiforgery/CSRF middleware.WebApplicationBuilderstores that implicit post-routing middleware (authentication/authorization/CSRF) in a single-use application-property slot,__Internal_PostRoutingPipeline, whichEndpointRoutingMiddlewareconsumes (and removes) so it runs right after routing and stamps the marker thatEndpointMiddlewarechecks for.The template also calls
app.UseStatusCodePagesWithReExecute(...). In theWebApplicationcase this goes throughRerouteHelper.Reroute, which builds a re-execution branch that itself callsUseRouting(). Because the user pipeline (containing the re-execution middleware) is composed before the primary routing middleware, that branch'sEndpointRoutingMiddlewareconsumes and removes the single-use slot from the shared global route builder first. The primary request pipeline's routing is then built without the block, so a normal request to an antiforgery-required endpoint (the Blazor root/) finds no antiforgery/CSRF marker andEndpointMiddlewarethrows.The bug affects all
RerouteHelper.Reroutecallers:UseStatusCodePagesWithReExecute,UseExceptionHandler,UsePathBase, andUseRewriter. Existing tests only exercised the re-executed path (which received the stolen block), so the normal-request regression went uncovered.Fix
Capture the
__Internal_PostRoutingPipelineslot from the shared global route builder before building the re-execution branch and restore it afterwards, inRerouteHelper.Reroute(src/Shared/Reroute.cs). This lets the re-execution branch keep its own copy of the implicit middleware (preserving CSRF on re-executed paths) while leaving the slot in place for the primary pipeline's routing. The single-use semantics for the main pipeline are unchanged, so repeatedUseRouting()calls still cannot double-apply the block.Tests
Added three regression tests to
CsrfProtectionIntegrationTestscovering the previously-untested shape (a normal request to an antiforgery-required endpoint when re-execution is configured), for both implicit and explicit routing, plus a guard that cross-origin POSTs are still rejected.Verified locally (all green):
CsrfProtectionIntegrationTests(61)WebApplicationTests(299)StatusCode/ExceptionHandler(54)UsePathBase(52)EndpointMiddleware/EndpointRoutingMiddleware(37)Notes
Draft for review. The
app.UseAntiforgery()workaround from the issue remains valid; this change removes the need for it.