Skip to content

Commit 3dbfa81

Browse files
authored
Avoid re-running routing for implicit middlewares and remove implicit anti-forgery (#50864)
## Description Avoid running routing eagerly in implicit middlewares to prevents the `EndpointFeature` from being set and causing unexpected reactions in other middlewares, like the static file middleware. We also remove the implicit registration of the anti-forgery middleware to avoid unintended collisions with authentication in Blazor. Fixes #50818, #50815, #50844 ## Customer Impact Without this change, the anti-forgery middleware in Blazor apps runs too early and is not able to examine authentication state in the application. Requiring the middleware to be registered explicitly ensures that the correct ordering is applied. Without this change, users will run into difficult to resolve issues with building applications that include forms with Blazor web apps. ## Regression? - [X] Yes - [ ] No This is a regression that was introduced to middleware routing in .NET 8 Preview 7. ## Risk - [ ] High - [X] Medium - [ ] Low **Medium risk** because: - We are reverting a change that was originally applied to resolve #49654. This means that the original bug will impact users, specifically those who are calling `UseRouting` explicitly without calling `UseAuthentication` and `UseAuthorization` if they are not available. There is a workaround that we plan to document this behavior for users. - Apps deployed in .NET 8 RC 1 will break because we no longer automatically enable the anti-forgery middleware. Users will receive an exception at startup notifying them of the code changes to make in order to get things working correctly. ## Verification - [X] Manual (required) - [X] Automated ## Packaging changes reviewed? - [ ] Yes - [ ] No - [X] N/A
1 parent f269696 commit 3dbfa81

File tree

11 files changed

+7
-438
lines changed

11 files changed

+7
-438
lines changed

src/Antiforgery/src/AntiforgeryApplicationBuilderExtensions.cs

-15
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
using Microsoft.AspNetCore.Antiforgery;
55
using Microsoft.AspNetCore.Antiforgery.Internal;
6-
using Microsoft.AspNetCore.Routing;
7-
using Microsoft.Extensions.DependencyInjection;
86

97
namespace Microsoft.AspNetCore.Builder;
108

@@ -26,19 +24,6 @@ public static IApplicationBuilder UseAntiforgery(this IApplicationBuilder builde
2624
builder.VerifyAntiforgeryServicesAreRegistered();
2725

2826
builder.Properties[AntiforgeryMiddlewareSetKey] = true;
29-
30-
// The anti-forgery middleware adds annotations to HttpContext.Items to indicate that it has run
31-
// that will be validated by the EndpointsRoutingMiddleware later. To do this, we need to ensure
32-
// that routing has run and set the endpoint feature on the HttpContext associated with the request.
33-
if (builder.Properties.TryGetValue(RerouteHelper.GlobalRouteBuilderKey, out var routeBuilder) && routeBuilder is not null)
34-
{
35-
return builder.Use(next =>
36-
{
37-
var newNext = RerouteHelper.Reroute(builder, routeBuilder, next);
38-
var antiforgery = builder.ApplicationServices.GetRequiredService<IAntiforgery>();
39-
return new AntiforgeryMiddleware(antiforgery, newNext).Invoke;
40-
});
41-
}
4227
builder.UseMiddleware<AntiforgeryMiddleware>();
4328

4429
return builder;

src/Antiforgery/src/Microsoft.AspNetCore.Antiforgery.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,5 @@
2626

2727
<ItemGroup>
2828
<Compile Include="$(SharedSourceRoot)HttpExtensions.cs" LinkBase="Shared"/>
29-
<Compile Include="$(SharedSourceRoot)Reroute.cs" LinkBase="Shared"/>
3029
</ItemGroup>
3130
</Project>

src/DefaultBuilder/src/WebApplicationBuilder.cs

-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Diagnostics;
55
using System.Diagnostics.CodeAnalysis;
66
using System.Reflection;
7-
using Microsoft.AspNetCore.Antiforgery;
87
using Microsoft.AspNetCore.Authentication;
98
using Microsoft.AspNetCore.Authorization;
109
using Microsoft.AspNetCore.Hosting;
@@ -25,7 +24,6 @@ public sealed class WebApplicationBuilder : IHostApplicationBuilder
2524
private const string EndpointRouteBuilderKey = "__EndpointRouteBuilder";
2625
private const string AuthenticationMiddlewareSetKey = "__AuthenticationMiddlewareSet";
2726
private const string AuthorizationMiddlewareSetKey = "__AuthorizationMiddlewareSet";
28-
private const string AntiforgeryMiddlewareSetKey = "__AntiforgeryMiddlewareSet";
2927
private const string UseRoutingKey = "__UseRouting";
3028

3129
private readonly HostApplicationBuilder _hostApplicationBuilder;
@@ -453,15 +451,6 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui
453451
}
454452
}
455453

456-
if (serviceProviderIsService?.IsService(typeof(IAntiforgery)) is true)
457-
{
458-
if (!_builtApplication.Properties.ContainsKey(AntiforgeryMiddlewareSetKey))
459-
{
460-
_builtApplication.Properties[AntiforgeryMiddlewareSetKey] = true;
461-
app.UseAntiforgery();
462-
}
463-
}
464-
465454
// Wire the source pipeline to run in the destination pipeline
466455
var wireSourcePipeline = new WireSourcePipeline(_builtApplication);
467456
app.Use(wireSourcePipeline.CreateMiddleware);

0 commit comments

Comments
 (0)