-
Notifications
You must be signed in to change notification settings - Fork 481
Analyzer for misusage of MaxResponseHeadersLength #6796
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
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
284fe29
tests + unshipped markdown
amiru3f f30af52
conflict resolution
amiru3f 6017fc3
conflict removal
amiru3f 5e4490e
using Symbol comparison
amiru3f 7e4bec5
warning removal
amiru3f 1d1af89
remove conflicts
amiru3f f714877
Symbol checking in CompilationStart
amiru3f 3e130b6
warning message title
amiru3f 1e350ea
Unshipped.md update and cleanup
58b5170
rename
amiru3f dd2b34a
conflict fixture
amiru3f f77ff23
const rename
amiru3f 00eec51
avoid emitting warning in case of limit disabling
amiru3f 13cdf2e
supporting SocketsHttpHandler
amiru3f b456423
texts improved
amiru3f 974fb85
pack
amiru3f bb47cde
Binary operation fix
amiru3f b62ba45
fix: mistakenly generated analyzer release file
amiru3f 873acf1
Update src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNet…
amiru3f 6b9c383
regenerated stuff with msbuild pack
amiru3f File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
....NetCore.Analyzers/Usage/ProvideHttpClientHandlerMaxResponseHeaderLengthValueCorrectly.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
|
||
| using System.Collections.Immutable; | ||
| using System.Linq; | ||
| using Analyzer.Utilities; | ||
| using Analyzer.Utilities.Extensions; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using Microsoft.CodeAnalysis.Operations; | ||
|
|
||
| namespace Microsoft.NetCore.Analyzers.Usage | ||
| { | ||
| using static MicrosoftNetCoreAnalyzersResources; | ||
|
|
||
| /// <summary> | ||
| /// CA2262: <inheritdoc cref="ProvideHttpClientHandlerMaxResponseHeaderLengthValueCorrectlyTitle"/> | ||
| /// </summary> | ||
| [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] | ||
| public sealed class ProvideHttpClientHandlerMaxResponseHeaderLengthValueCorrectly : DiagnosticAnalyzer | ||
| { | ||
| private const string MaxResponseHeadersLengthPropertyName = "MaxResponseHeadersLength"; | ||
| private const int MaxLimitToReport = 128; | ||
| internal const string RuleId = "CA2262"; | ||
|
|
||
| internal static readonly DiagnosticDescriptor EnsureMaxResponseHeaderLengthRule = DiagnosticDescriptorHelper.Create( | ||
| RuleId, | ||
| CreateLocalizableResourceString(nameof(ProvideHttpClientHandlerMaxResponseHeaderLengthValueCorrectlyTitle)), | ||
| CreateLocalizableResourceString(nameof(ProvideHttpClientHandlerMaxResponseHeaderLengthValueCorrectlyMessage)), | ||
| DiagnosticCategory.Usage, | ||
| RuleLevel.IdeSuggestion, | ||
| description: CreateLocalizableResourceString(nameof(ProvideHttpClientHandlerMaxResponseHeaderLengthValueCorrectlyDescription)), | ||
| isPortedFxCopRule: false, | ||
| isDataflowRule: false); | ||
|
|
||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(EnsureMaxResponseHeaderLengthRule); | ||
|
|
||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.EnableConcurrentExecution(); | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
|
|
||
| context.RegisterCompilationStartAction(context => | ||
| { | ||
| var httpClientHandlerPropSymbol = context.Compilation | ||
| .GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNetHttpHttpClientHandler) | ||
| ?.GetMembers(MaxResponseHeadersLengthPropertyName) | ||
| .FirstOrDefault(); | ||
|
|
||
| var socketClientHandlerPropSymbol = context.Compilation | ||
| .GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNetHttpSocketsHttpHandler) | ||
| ?.GetMembers(MaxResponseHeadersLengthPropertyName) | ||
| .FirstOrDefault(); | ||
|
|
||
| if (httpClientHandlerPropSymbol is null || socketClientHandlerPropSymbol is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| ImmutableArray<ISymbol> symbols = ImmutableArray.Create(httpClientHandlerPropSymbol, socketClientHandlerPropSymbol); | ||
| context.RegisterOperationAction(context => AnalyzeSimpleAssignmentOperationAndCreateDiagnostic(context, symbols), OperationKind.SimpleAssignment); | ||
| }); | ||
| } | ||
|
|
||
| private static void AnalyzeSimpleAssignmentOperationAndCreateDiagnostic(OperationAnalysisContext context, ImmutableArray<ISymbol> propSymbols) | ||
| { | ||
| var assignmentOperation = (ISimpleAssignmentOperation)context.Operation; | ||
|
|
||
| if (!IsValidPropertyAssignmentOperation(assignmentOperation, propSymbols)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (assignmentOperation.Value is null || !assignmentOperation.Value.ConstantValue.HasValue || assignmentOperation.Value.ConstantValue.Value is not int propertyValue) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // If the user set the value to int.MaxValue, their intention is to disable the limit, and we shouldn't emit a warning. | ||
| if (propertyValue is > MaxLimitToReport and not int.MaxValue) | ||
| { | ||
| context.ReportDiagnostic(context.Operation.CreateDiagnostic(EnsureMaxResponseHeaderLengthRule, propertyValue)); | ||
| } | ||
| } | ||
|
|
||
| private static bool IsValidPropertyAssignmentOperation(ISimpleAssignmentOperation operation, ImmutableArray<ISymbol> propSymbols) | ||
| { | ||
| if (operation.Target is not IPropertyReferenceOperation propertyReferenceOperation) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (!propSymbols.Contains(propertyReferenceOperation.Member)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return operation.Value is IFieldReferenceOperation or ILiteralOperation or IBinaryOperation; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
These analyzers shipped with 8.0, looks the PR need to pull the latest and run
msbuild /t:packThere 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.
Weird, it was mistakenly (auto) generated by
msbuildpacking.Fixed.