Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid re-running routing for implicit middlewares and remove implicit anti-forgery #50864

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions src/Antiforgery/src/AntiforgeryApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Antiforgery.Internal;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.Builder;

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

builder.Properties[AntiforgeryMiddlewareSetKey] = true;

// The anti-forgery middleware adds annotations to HttpContext.Items to indicate that it has run
// that will be validated by the EndpointsRoutingMiddleware later. To do this, we need to ensure
// that routing has run and set the endpoint feature on the HttpContext associated with the request.
if (builder.Properties.TryGetValue(RerouteHelper.GlobalRouteBuilderKey, out var routeBuilder) && routeBuilder is not null)
{
return builder.Use(next =>
{
var newNext = RerouteHelper.Reroute(builder, routeBuilder, next);
var antiforgery = builder.ApplicationServices.GetRequiredService<IAntiforgery>();
return new AntiforgeryMiddleware(antiforgery, newNext).Invoke;
});
}
builder.UseMiddleware<AntiforgeryMiddleware>();

return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@

<ItemGroup>
<Compile Include="$(SharedSourceRoot)HttpExtensions.cs" LinkBase="Shared"/>
<Compile Include="$(SharedSourceRoot)Reroute.cs" LinkBase="Shared"/>
</ItemGroup>
</Project>
11 changes: 0 additions & 11 deletions src/DefaultBuilder/src/WebApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -25,7 +24,6 @@ public sealed class WebApplicationBuilder : IHostApplicationBuilder
private const string EndpointRouteBuilderKey = "__EndpointRouteBuilder";
private const string AuthenticationMiddlewareSetKey = "__AuthenticationMiddlewareSet";
private const string AuthorizationMiddlewareSetKey = "__AuthorizationMiddlewareSet";
private const string AntiforgeryMiddlewareSetKey = "__AntiforgeryMiddlewareSet";
private const string UseRoutingKey = "__UseRouting";

private readonly HostApplicationBuilder _hostApplicationBuilder;
Expand Down Expand Up @@ -453,15 +451,6 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui
}
}

if (serviceProviderIsService?.IsService(typeof(IAntiforgery)) is true)
{
if (!_builtApplication.Properties.ContainsKey(AntiforgeryMiddlewareSetKey))
{
_builtApplication.Properties[AntiforgeryMiddlewareSetKey] = true;
app.UseAntiforgery();
}
}

// Wire the source pipeline to run in the destination pipeline
var wireSourcePipeline = new WireSourcePipeline(_builtApplication);
app.Use(wireSourcePipeline.CreateMiddleware);
Expand Down
Loading
Loading