-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Throw when UseAuthorization is incorrectly configured #14401
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Diagnostics; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Microsoft.AspNetCore.Analyzers | ||
{ | ||
internal class UseAuthorizationAnalyzer | ||
{ | ||
private readonly StartupAnalysis _context; | ||
|
||
public UseAuthorizationAnalyzer(StartupAnalysis context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public void AnalyzeSymbol(SymbolAnalysisContext context) | ||
{ | ||
Debug.Assert(context.Symbol.Kind == SymbolKind.NamedType); | ||
Debug.Assert(StartupFacts.IsStartupClass(_context.StartupSymbols, (INamedTypeSymbol)context.Symbol)); | ||
|
||
var type = (INamedTypeSymbol)context.Symbol; | ||
|
||
foreach (var middlewareAnalysis in _context.GetRelatedAnalyses<MiddlewareAnalysis>(type)) | ||
{ | ||
MiddlewareItem? useAuthorizationItem = default; | ||
MiddlewareItem? useRoutingItem = default; | ||
|
||
var length = middlewareAnalysis.Middleware.Length; | ||
for (var i = length - 1; i >= 0; i-- ) | ||
{ | ||
var middlewareItem = middlewareAnalysis.Middleware[i]; | ||
var middleware = middlewareItem.UseMethod.Name; | ||
|
||
if (middleware == "UseAuthorization") | ||
{ | ||
if (useRoutingItem != null && useAuthorizationItem == null) | ||
{ | ||
// This looks like | ||
// | ||
// app.UseAuthorization(); | ||
// ... | ||
// app.UseRouting(); | ||
// app.UseEndpoints(...); | ||
|
||
context.ReportDiagnostic(Diagnostic.Create( | ||
StartupAnalyzer.Diagnostics.IncorrectlyConfiguredAuthorizationMiddleware, | ||
middlewareItem.Operation.Syntax.GetLocation(), | ||
middlewareItem.UseMethod.Name)); | ||
} | ||
|
||
useAuthorizationItem = middlewareItem; | ||
} | ||
else if (middleware == "UseEndpoints") | ||
{ | ||
if (useAuthorizationItem != null) | ||
{ | ||
// This configuration looks like | ||
// | ||
// app.UseRouting(); | ||
// app.UseEndpoints(...); | ||
// ... | ||
// app.UseAuthorization(); | ||
// | ||
|
||
context.ReportDiagnostic(Diagnostic.Create( | ||
StartupAnalyzer.Diagnostics.IncorrectlyConfiguredAuthorizationMiddleware, | ||
useAuthorizationItem.Operation.Syntax.GetLocation(), | ||
middlewareItem.UseMethod.Name)); | ||
} | ||
} | ||
else if (middleware == "UseRouting") | ||
{ | ||
useRoutingItem = middlewareItem; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
|
||
namespace Microsoft.AspNetCore.Analyzers.TestFiles.StartupAnalyzerTest | ||
{ | ||
public class UseAuthAfterUseEndpoints | ||
{ | ||
public void Configure(IApplicationBuilder app) | ||
{ | ||
app.UseRouting(); | ||
app.UseEndpoints(r => { }); | ||
/*MM*/app.UseAuthorization(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
|
||
namespace Microsoft.AspNetCore.Analyzers.TestFiles.StartupAnalyzerTest | ||
{ | ||
public class UseAuthBeforeUseRouting | ||
{ | ||
public void Configure(IApplicationBuilder app) | ||
{ | ||
app.UseFileServer(); | ||
/*MM*/app.UseAuthorization(); | ||
app.UseRouting(); | ||
app.UseEndpoints(r => { }); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
|
||
namespace Microsoft.AspNetCore.Analyzers.TestFiles.StartupAnalyzerTest | ||
{ | ||
public class UseAuthConfiguredCorrectly | ||
{ | ||
public void Configure(IApplicationBuilder app) | ||
{ | ||
app.UseRouting(); | ||
app.UseAuthorization(); | ||
app.UseEndpoints(r => { }); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
|
||
namespace Microsoft.AspNetCore.Analyzers.TestFiles.StartupAnalyzerTest | ||
{ | ||
public class UseAuthFallbackPolicy | ||
{ | ||
public void Configure(IApplicationBuilder app) | ||
{ | ||
// This sort of setup would be useful if the user wants to use Auth for non-endpoint content to be handled using the Fallback policy, while | ||
// using the second instance for regular endpoint routing based auth. We do not want to produce a warning in this case. | ||
app.UseAuthorization(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't super get this - it only makes sense if there's a terminal middleware between here and routing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll toss in a Static files to appear after this. |
||
app.UseStaticFiles(); | ||
|
||
app.UseRouting(); | ||
app.UseAuthorization(); | ||
app.UseEndpoints(r => { }); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
|
||
namespace Microsoft.AspNetCore.Analyzers.TestFiles.StartupAnalyzerTest | ||
{ | ||
public class UseAuthMultipleTimes | ||
{ | ||
public void Configure(IApplicationBuilder app) | ||
{ | ||
app.UseRouting(); | ||
app.UseAuthorization(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. File is called |
||
app.UseAuthorization(); | ||
app.UseEndpoints(r => { }); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
YSK: this doesn't do control-flow analysis - ie, it's trivial to cause false positives or false negatives when
.Map
is involved.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. We can always recommend turning the analyzer for users where this becomes problematic. Doing flow analysis for this sounds like a lot more effort