Skip to content

Fix check for auth services in middleware auto-registration #43205

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

Merged
merged 1 commit into from
Aug 11, 2022
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
1 change: 1 addition & 0 deletions src/DefaultBuilder/src/Microsoft.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

<ItemGroup>
<InternalsVisibleTo Include="Microsoft.AspNetCore.Authentication.Test" />
<InternalsVisibleTo Include="Microsoft.AspNetCore.Authorization.Test" />
</ItemGroup>

</Project>
5 changes: 3 additions & 2 deletions src/DefaultBuilder/src/WebApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui

// Process authorization and authentication middlewares independently to avoid
// registering middlewares for services that do not exist
if (_builtApplication.Services.GetService<IAuthenticationSchemeProvider>() is not null)
var serviceProviderIsService = _builtApplication.Services.GetService<IServiceProviderIsService>();
if (serviceProviderIsService?.IsService(typeof(IAuthenticationSchemeProvider)) is true)
{
// Don't add more than one instance of the middleware
if (!_builtApplication.Properties.ContainsKey(AuthenticationMiddlewareSetKey))
Expand All @@ -184,7 +185,7 @@ private void ConfigureApplication(WebHostBuilderContext context, IApplicationBui
}
}

if (_builtApplication.Services.GetService<IAuthorizationHandlerProvider>() is not null)
if (serviceProviderIsService?.IsService(typeof(IAuthorizationHandlerProvider)) is true)
{
if (!_builtApplication.Properties.ContainsKey(AuthorizationMiddlewareSetKey))
{
Expand Down
29 changes: 29 additions & 0 deletions src/Security/Authorization/test/AuthorizationMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization.Policy;
using Microsoft.AspNetCore.Authorization.Test.TestObjects;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Moq;

namespace Microsoft.AspNetCore.Authorization.Test;
Expand Down Expand Up @@ -658,6 +661,27 @@ public async Task IAuthenticateResultFeature_UsesExistingFeatureAndResult_Withou
Assert.Same(authenticateResult, authenticateResultFeature.AuthenticateResult);
}

// Validation for https://github.com/dotnet/aspnetcore/issues/43188
[Fact]
public async Task WebApplicationBuilder_CanRegisterAuthzMiddlewareWithScopedService()
{
var builder = WebApplication.CreateBuilder(new WebApplicationOptions()
{
EnvironmentName = Environments.Development
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent scopes on resolved services only get validated in Development so we set the environment name explicitly here to validate the broken behavior.

});
builder.Services.AddAuthorization();
builder.Services.AddScoped<IAuthorizationHandler, TestAuthorizationHandler>();
using var app = builder.Build();

Assert.False(app.Properties.ContainsKey("__AuthenticationMiddlewareSet"));
Assert.False(app.Properties.ContainsKey("__AuthorizationMiddlewareSet"));

await app.StartAsync();

Assert.False(app.Properties.ContainsKey("__AuthenticationMiddlewareSet"));
Assert.True(app.Properties.ContainsKey("__AuthorizationMiddlewareSet"));
}

private AuthorizationMiddleware CreateMiddleware(RequestDelegate requestDelegate = null, IAuthorizationPolicyProvider policyProvider = null)
{
requestDelegate = requestDelegate ?? ((context) => Task.CompletedTask);
Expand Down Expand Up @@ -724,6 +748,11 @@ private HttpContext GetHttpContext(
return httpContext;
}

public class TestAuthorizationHandler : IAuthorizationHandler
{
public Task HandleAsync(AuthorizationHandlerContext context) => Task.CompletedTask;
}

private class TestRequestDelegate
{
private readonly int _statusCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<Reference Include="Microsoft.AspNetCore" />
<Reference Include="Microsoft.AspNetCore.Authorization" />
<Reference Include="Microsoft.AspNetCore.Authorization.Policy" />
<Reference Include="Microsoft.AspNetCore.Http" />
Expand Down