-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2178 from LingxiaChen/DoNotDisableRequestValidation
Do not disable request validation
- Loading branch information
Showing
17 changed files
with
435 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/Microsoft.NetCore.Analyzers/Core/Security/DoNotDisableRequestValidation.cs
This file contains 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,85 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. 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; | ||
|
||
namespace Microsoft.NetCore.Analyzers.Security | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] | ||
public sealed class DoNotDisableRequestValidation : DiagnosticAnalyzer | ||
{ | ||
internal const string DiagnosticId = "CA5363"; | ||
private static readonly LocalizableString s_Title = new LocalizableResourceString( | ||
nameof(SystemSecurityCryptographyResources.DoNotDisableRequestValidation), | ||
SystemSecurityCryptographyResources.ResourceManager, | ||
typeof(SystemSecurityCryptographyResources)); | ||
private static readonly LocalizableString s_Message = new LocalizableResourceString( | ||
nameof(SystemSecurityCryptographyResources.DoNotDisableRequestValidationMessage), | ||
SystemSecurityCryptographyResources.ResourceManager, | ||
typeof(SystemSecurityCryptographyResources)); | ||
private static readonly LocalizableString s_Description = new LocalizableResourceString( | ||
nameof(SystemSecurityCryptographyResources.DoNotDisableRequestValidationDescription), | ||
SystemSecurityCryptographyResources.ResourceManager, | ||
typeof(SystemSecurityCryptographyResources)); | ||
|
||
internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor( | ||
DiagnosticId, | ||
s_Title, | ||
s_Message, | ||
DiagnosticCategory.Security, | ||
DiagnosticHelpers.DefaultDiagnosticSeverity, | ||
isEnabledByDefault: DiagnosticHelpers.EnabledByDefaultIfNotBuildingVSIX, | ||
description: s_Description, | ||
helpLinkUri: null, | ||
customTags: WellKnownDiagnosticTags.Telemetry); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
|
||
// Security analyzer - analyze and report diagnostics on generated code. | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); | ||
|
||
context.RegisterCompilationStartAction( | ||
(CompilationStartAnalysisContext compilationStartAnalysisContext) => | ||
{ | ||
var validateInputAttributeTypeSymbol = compilationStartAnalysisContext.Compilation.GetTypeByMetadataName(WellKnownTypeNames.SystemWebMvcValidateInputAttribute); | ||
if (validateInputAttributeTypeSymbol == null) | ||
{ | ||
return; | ||
} | ||
compilationStartAnalysisContext.RegisterSymbolAction( | ||
(SymbolAnalysisContext symbolAnalysisContext) => | ||
{ | ||
var symbol = symbolAnalysisContext.Symbol; | ||
var attr = symbol.GetAttributes().FirstOrDefault(s => s.AttributeClass.Equals(validateInputAttributeTypeSymbol)); | ||
if (attr == null) | ||
{ | ||
return; | ||
} | ||
var constructorArguments = attr.ConstructorArguments; | ||
if (constructorArguments.Length == 1 && | ||
constructorArguments[0].Kind == TypedConstantKind.Primitive && | ||
constructorArguments[0].Value.Equals(false)) | ||
{ | ||
symbolAnalysisContext.ReportDiagnostic( | ||
symbol.CreateDiagnostic( | ||
Rule, | ||
symbol.Name)); | ||
} | ||
}, SymbolKind.Method, SymbolKind.NamedType); | ||
}); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 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.