Skip to content

Commit 63ac5f0

Browse files
committed
Changes per PR comments
1 parent 479c8a9 commit 63ac5f0

File tree

6 files changed

+96
-18
lines changed

6 files changed

+96
-18
lines changed

src/Http/HttpAbstractions.sln

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Server
113113
EndProject
114114
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.WebUtilities.Performance", "WebUtilities\perf\Microsoft.AspNetCore.WebUtilities.Performance\Microsoft.AspNetCore.WebUtilities.Performance.csproj", "{21AC56E7-4E77-4B0E-B63E-C8E836E4D14E}"
115115
EndProject
116+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authorization.Policy", "..\Security\Authorization\Policy\src\Microsoft.AspNetCore.Authorization.Policy.csproj", "{8BCAA9EC-0ACD-435C-BF8A-8C843499FF7B}"
117+
EndProject
118+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Cors", "..\Middleware\CORS\src\Microsoft.AspNetCore.Cors.csproj", "{09168958-FD5B-4D25-8FBF-75E2C80D903B}"
119+
EndProject
116120
Global
117121
GlobalSection(SolutionConfigurationPlatforms) = preSolution
118122
Debug|Any CPU = Debug|Any CPU
@@ -603,6 +607,30 @@ Global
603607
{21AC56E7-4E77-4B0E-B63E-C8E836E4D14E}.Release|x64.Build.0 = Release|Any CPU
604608
{21AC56E7-4E77-4B0E-B63E-C8E836E4D14E}.Release|x86.ActiveCfg = Release|Any CPU
605609
{21AC56E7-4E77-4B0E-B63E-C8E836E4D14E}.Release|x86.Build.0 = Release|Any CPU
610+
{8BCAA9EC-0ACD-435C-BF8A-8C843499FF7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
611+
{8BCAA9EC-0ACD-435C-BF8A-8C843499FF7B}.Debug|Any CPU.Build.0 = Debug|Any CPU
612+
{8BCAA9EC-0ACD-435C-BF8A-8C843499FF7B}.Debug|x64.ActiveCfg = Debug|Any CPU
613+
{8BCAA9EC-0ACD-435C-BF8A-8C843499FF7B}.Debug|x64.Build.0 = Debug|Any CPU
614+
{8BCAA9EC-0ACD-435C-BF8A-8C843499FF7B}.Debug|x86.ActiveCfg = Debug|Any CPU
615+
{8BCAA9EC-0ACD-435C-BF8A-8C843499FF7B}.Debug|x86.Build.0 = Debug|Any CPU
616+
{8BCAA9EC-0ACD-435C-BF8A-8C843499FF7B}.Release|Any CPU.ActiveCfg = Release|Any CPU
617+
{8BCAA9EC-0ACD-435C-BF8A-8C843499FF7B}.Release|Any CPU.Build.0 = Release|Any CPU
618+
{8BCAA9EC-0ACD-435C-BF8A-8C843499FF7B}.Release|x64.ActiveCfg = Release|Any CPU
619+
{8BCAA9EC-0ACD-435C-BF8A-8C843499FF7B}.Release|x64.Build.0 = Release|Any CPU
620+
{8BCAA9EC-0ACD-435C-BF8A-8C843499FF7B}.Release|x86.ActiveCfg = Release|Any CPU
621+
{8BCAA9EC-0ACD-435C-BF8A-8C843499FF7B}.Release|x86.Build.0 = Release|Any CPU
622+
{09168958-FD5B-4D25-8FBF-75E2C80D903B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
623+
{09168958-FD5B-4D25-8FBF-75E2C80D903B}.Debug|Any CPU.Build.0 = Debug|Any CPU
624+
{09168958-FD5B-4D25-8FBF-75E2C80D903B}.Debug|x64.ActiveCfg = Debug|Any CPU
625+
{09168958-FD5B-4D25-8FBF-75E2C80D903B}.Debug|x64.Build.0 = Debug|Any CPU
626+
{09168958-FD5B-4D25-8FBF-75E2C80D903B}.Debug|x86.ActiveCfg = Debug|Any CPU
627+
{09168958-FD5B-4D25-8FBF-75E2C80D903B}.Debug|x86.Build.0 = Debug|Any CPU
628+
{09168958-FD5B-4D25-8FBF-75E2C80D903B}.Release|Any CPU.ActiveCfg = Release|Any CPU
629+
{09168958-FD5B-4D25-8FBF-75E2C80D903B}.Release|Any CPU.Build.0 = Release|Any CPU
630+
{09168958-FD5B-4D25-8FBF-75E2C80D903B}.Release|x64.ActiveCfg = Release|Any CPU
631+
{09168958-FD5B-4D25-8FBF-75E2C80D903B}.Release|x64.Build.0 = Release|Any CPU
632+
{09168958-FD5B-4D25-8FBF-75E2C80D903B}.Release|x86.ActiveCfg = Release|Any CPU
633+
{09168958-FD5B-4D25-8FBF-75E2C80D903B}.Release|x86.Build.0 = Release|Any CPU
606634
EndGlobalSection
607635
GlobalSection(SolutionProperties) = preSolution
608636
HideSolutionNode = FALSE
@@ -651,6 +679,8 @@ Global
651679
{611794D2-EF3A-422A-A077-23E61C7ADE49} = {793FFE24-138A-4C3D-81AB-18D625E36230}
652680
{1062FCDE-E145-40EC-B175-FDBCAA0C59A0} = {793FFE24-138A-4C3D-81AB-18D625E36230}
653681
{21AC56E7-4E77-4B0E-B63E-C8E836E4D14E} = {80A090C8-ED02-4DE3-875A-30DCCDBD84BA}
682+
{8BCAA9EC-0ACD-435C-BF8A-8C843499FF7B} = {793FFE24-138A-4C3D-81AB-18D625E36230}
683+
{09168958-FD5B-4D25-8FBF-75E2C80D903B} = {793FFE24-138A-4C3D-81AB-18D625E36230}
654684
EndGlobalSection
655685
GlobalSection(ExtensibilityGlobals) = postSolution
656686
SolutionGuid = {85B5E151-2E9D-419C-83DD-0DDCF446C83A}

src/Http/Routing/src/EndpointMiddleware.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
using Microsoft.AspNetCore.Authorization;
77
using Microsoft.AspNetCore.Cors.Infrastructure;
88
using Microsoft.AspNetCore.Http;
9-
using Microsoft.AspNetCore.Http.Features;
109
using Microsoft.Extensions.Logging;
1110
using Microsoft.Extensions.Options;
1211

1312
namespace Microsoft.AspNetCore.Routing
1413
{
1514
internal sealed class EndpointMiddleware
1615
{
17-
internal const string AuthorizationMiddlewareInvokedKey = "__AuthorizationMiddlewareInvoked";
18-
internal const string CorsMiddlewareInvokedKey = "__CorsMiddlewareInvoked";
16+
internal const string AuthorizationMiddlewareInvokedKey = "__AuthorizationMiddlewareWithEndpointInvoked";
17+
internal const string CorsMiddlewareInvokedKey = "__CorsMiddlewareWithEndpointInvoked";
1918

2019
private readonly ILogger _logger;
2120
private readonly RequestDelegate _next;

src/Http/Routing/test/FunctionalTests/EndpointRoutingIntegrationTest.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Net;
56
using System.Threading.Tasks;
67
using Microsoft.AspNetCore.Authorization;
78
using Microsoft.AspNetCore.Builder;
@@ -51,6 +52,31 @@ public async Task AuthorizationMiddleware_WhenNoAuthMetadataIsConfigured()
5152
response.EnsureSuccessStatusCode();
5253
}
5354

55+
[Fact]
56+
public async Task AuthorizationMiddleware_WhenEndpointIsNotFound()
57+
{
58+
// Arrange
59+
var builder = new WebHostBuilder();
60+
builder.Configure(app =>
61+
{
62+
app.UseRouting();
63+
app.UseAuthorization();
64+
app.UseEndpoints(b => b.Map("/", TestDelegate));
65+
66+
})
67+
.ConfigureServices(services =>
68+
{
69+
services.AddAuthorization();
70+
services.AddRouting();
71+
});
72+
73+
using var server = new TestServer(builder);
74+
75+
var response = await server.CreateRequest("/not-found").SendAsync("GET");
76+
77+
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
78+
}
79+
5480
[Fact]
5581
public async Task AuthorizationMiddleware_WithAuthorizedEndpoint()
5682
{
@@ -99,6 +125,29 @@ public async Task AuthorizationMiddleware_NotConfigured_Throws()
99125
Assert.Equal(AuthErrorMessage, ex.Message);
100126
}
101127

128+
[Fact]
129+
public async Task AuthorizationMiddleware_NotConfigured_WhenEndpointIsNotFound()
130+
{
131+
// Arrange
132+
var builder = new WebHostBuilder();
133+
builder.Configure(app =>
134+
{
135+
app.UseRouting();
136+
app.UseEndpoints(b => b.Map("/", TestDelegate).RequireAuthorization());
137+
138+
})
139+
.ConfigureServices(services =>
140+
{
141+
services.AddRouting();
142+
});
143+
144+
using var server = new TestServer(builder);
145+
146+
var response = await server.CreateRequest("/not-found").SendAsync("GET");
147+
148+
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
149+
}
150+
102151
[Fact]
103152
public async Task AuthorizationMiddleware_ConfiguredBeforeRouting_Throws()
104153
{

src/Http/Routing/test/UnitTests/EndpointMiddlewareTest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ public async Task Invoke_WithEndpoint_ThrowsIfAuthAttributesWereFound_ButAuthMid
101101
// Arrange
102102
var expected = "Endpoint Test contains authorization metadata, but a middleware was not found that supports authorization." +
103103
Environment.NewLine +
104-
"Configure your application startup by adding app.UseAuthorization() inside the call to Configure(..) in the application startup code.";
104+
"Configure your application startup by adding app.UseAuthorization() inside the call to Configure(..) in the application startup code. " +
105+
"The call to app.UseAuthorization() must appear between app.UseRouting() and app.UseEndpoints(...).";
105106
var httpContext = new DefaultHttpContext
106107
{
107108
RequestServices = new ServiceProvider()
@@ -197,7 +198,8 @@ public async Task Invoke_WithEndpoint_ThrowsIfCorsMetadataWasFound_ButCorsMiddle
197198
// Arrange
198199
var expected = "Endpoint Test contains CORS metadata, but a middleware was not found that supports CORS." +
199200
Environment.NewLine +
200-
"Configure your application startup by adding app.UseCors() inside the call to Configure(..) in the application startup code.";
201+
"Configure your application startup by adding app.UseCors() inside the call to Configure(..) in the application startup code. " +
202+
"The call to app.UseAuthorization() must appear between app.UseRouting() and app.UseEndpoints(...).";
201203
var httpContext = new DefaultHttpContext
202204
{
203205
RequestServices = new ServiceProvider()

src/Middleware/CORS/src/Infrastructure/CorsMiddleware.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
1414
public class CorsMiddleware
1515
{
1616
// Property key is used by other systems, e.g. MVC, to check if CORS middleware has run
17-
private const string CorsMiddlewareInvokedKey = "__CorsMiddlewareInvoked";
18-
private static readonly object CorsMiddlewareInvokedValue = new object();
17+
private const string CorsMiddlewareWithEndpointInvokedKey = "__CorsMiddlewareWithEndpointInvoked";
18+
private static readonly object CorsMiddlewareWithEndpointInvokedValue = new object();
1919

2020
private readonly Func<object, Task> OnResponseStartingDelegate = OnResponseStarting;
2121
private readonly RequestDelegate _next;
@@ -127,10 +127,9 @@ public Task Invoke(HttpContext context, ICorsPolicyProvider corsPolicyProvider)
127127

128128
if (endpoint != null)
129129
{
130-
// Flag to indicate to the system that the middleware was run in the context of endpoint routing.
131-
// Setting this flag allows a check in EndpointRoutingMiddleware that verifies if the middleware
132-
// pipeline is wired correctly to succeed.
133-
context.Items[CorsMiddlewareInvokedKey] = CorsMiddlewareInvokedValue;
130+
// EndpointRoutingMiddleware uses this flag to check if the CORS middleware processed CORS metadata on the endpoint.
131+
// The CORS middleware can only make this claim if it observes an actual endpoint.
132+
context.Items[CorsMiddlewareWithEndpointInvokedKey] = CorsMiddlewareWithEndpointInvokedValue;
134133
}
135134

136135
if (!context.Request.Headers.ContainsKey(CorsConstants.Origin))

src/Security/Authorization/Policy/src/AuthorizationMiddleware.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ namespace Microsoft.AspNetCore.Authorization
1313
{
1414
public class AuthorizationMiddleware
1515
{
16-
// Property key is used by other systems, e.g. MVC, to check if authorization middleware has run
17-
private const string AuthorizationMiddlewareInvokedKey = "__AuthorizationMiddlewareInvoked";
18-
private static readonly object AuthorizationMiddlewareInvokedValue = new object();
16+
// Property key is used by Endpoint routing to determine if Authorization has run
17+
private const string AuthorizationMiddlewareInvokedWithEndpointKey = "__AuthorizationMiddlewareWithEndpointInvoked";
18+
private static readonly object AuthorizationMiddlewareWithEndpointInvokedValue = new object();
1919

2020
private readonly RequestDelegate _next;
2121
private readonly IAuthorizationPolicyProvider _policyProvider;
@@ -37,10 +37,9 @@ public async Task Invoke(HttpContext context)
3737

3838
if (endpoint != null)
3939
{
40-
// Flag to indicate to the system that the middleware was run in the context of endpoint routing.
41-
// Setting this flag allows a check in EndpointRoutingMiddleware that verifies if the middleware
42-
// pipeline is wired correctly to succeed.
43-
context.Items[AuthorizationMiddlewareInvokedKey] = AuthorizationMiddlewareInvokedValue;
40+
// EndpointRoutingMiddleware uses this flag to check if the Authorization middleware processed auth metadata on the endpoint.
41+
// The Authorization middleware can only make this claim if it observes an actual endpoint.
42+
context.Items[AuthorizationMiddlewareInvokedWithEndpointKey] = AuthorizationMiddlewareWithEndpointInvokedValue;
4443
}
4544

4645
// IMPORTANT: Changes to authorization logic should be mirrored in MVC's AuthorizeFilter

0 commit comments

Comments
 (0)