| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Immutable; |
| | | 5 | | using System.Linq; |
| | | 6 | | using Microsoft.CodeAnalysis; |
| | | 7 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.BuildTools; |
| | | 10 | | |
| | | 11 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 12 | | public sealed class AuthorizationAttributesAnalyzer : DiagnosticAnalyzer |
| | | 13 | | { |
| | | 14 | | private const string AuthenticationSchemesPropertyName = "AuthenticationSchemes"; |
| | | 15 | | private const string RolesPropertyName = "Roles"; |
| | | 16 | | private const string AuthorizeDataTypeName = "Microsoft.AspNetCore.Authorization.IAuthorizeData"; |
| | | 17 | | private const string AllowAnonymousTypeName = "Microsoft.AspNetCore.Authorization.IAllowAnonymous"; |
| | | 18 | | private const string CoreWCFServiceContractAttributeTypeName = "CoreWCF.ServiceContractAttribute"; |
| | | 19 | | private const string CoreWCFOperationContractAttributeTypeName = "CoreWCF.OperationContractAttribute"; |
| | | 20 | | private const string SSMServiceContractAttributeTypeName = "System.ServiceModel.ServiceContractAttribute"; |
| | | 21 | | private const string SSMOperationContractAttributeTypeName = "System.ServiceModel.OperationContractAttribute"; |
| | | 22 | | private const string CoreWCFInjectedAttributeTypeName = "CoreWCF.InjectedAttribute"; |
| | | 23 | | private const string MVCFromServicesAttributeTypeName = "Microsoft.AspNetCore.Mvc.FromServicesAttribute"; |
| | | 24 | | |
| | 1 | 25 | | private static readonly ImmutableArray<DiagnosticDescriptor> s_supportedDiagnostics = |
| | 1 | 26 | | new[] |
| | 1 | 27 | | { |
| | 1 | 28 | | DiagnosticDescriptors.AuthorizationAttributesAnalyzer_02XX.AllowAnonymousAttributeIsNotSupported, |
| | 1 | 29 | | DiagnosticDescriptors.AuthorizationAttributesAnalyzer_02XX.AuthorizeDataAuthenticationSchemesPropertyIsN |
| | 1 | 30 | | DiagnosticDescriptors.AuthorizationAttributesAnalyzer_02XX.AuthorizeDataRolesPropertyIsNotSupported |
| | 1 | 31 | | } |
| | 1 | 32 | | .ToImmutableArray(); |
| | | 33 | | |
| | | 34 | | public override void Initialize(AnalysisContext context) |
| | | 35 | | { |
| | 36 | 36 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDia |
| | | 37 | | #if !DEBUG |
| | 36 | 38 | | context.EnableConcurrentExecution(); |
| | | 39 | | #endif |
| | 36 | 40 | | context.RegisterCompilationStartAction(compilationStartContext => |
| | 36 | 41 | | { |
| | 23 | 42 | | INamedTypeSymbol? allowAnonymous = compilationStartContext.Compilation.GetTypeByMetadataName(AllowAnonymousT |
| | 23 | 43 | | INamedTypeSymbol? authorizeData = compilationStartContext.Compilation.GetTypeByMetadataName(AuthorizeDataTyp |
| | 23 | 44 | | INamedTypeSymbol? ssmServiceContractAttribute = compilationStartContext.Compilation.GetTypeByMetadataName(SS |
| | 23 | 45 | | INamedTypeSymbol? coreWCFServiceContractAttribute = compilationStartContext.Compilation.GetTypeByMetadataNam |
| | 23 | 46 | | INamedTypeSymbol? ssmOperationContractAttribute = compilationStartContext.Compilation.GetTypeByMetadataName( |
| | 23 | 47 | | INamedTypeSymbol? coreWCFOperationContractAttribute = compilationStartContext.Compilation.GetTypeByMetadataN |
| | 23 | 48 | | INamedTypeSymbol? coreWCFInjectedAttribute = compilationStartContext.Compilation.GetTypeByMetadataName(CoreW |
| | 23 | 49 | | INamedTypeSymbol? mvcFromServicesAttribute = compilationStartContext.Compilation.GetTypeByMetadataName(MVCFr |
| | 36 | 50 | | |
| | 23 | 51 | | compilationStartContext.RegisterSymbolAction( |
| | 46 | 52 | | context => WarnWhenAllowAnonymousOnServiceContractImplementation(context, allowAnonymous, coreWCFService |
| | 23 | 53 | | SymbolKind.NamedType); |
| | 23 | 54 | | compilationStartContext.RegisterSymbolAction( |
| | 70 | 55 | | context => WarnWhenAllowAnonymousOnOperationContractImplementation(context, allowAnonymous, ssmServiceCo |
| | 23 | 56 | | SymbolKind.Method); |
| | 23 | 57 | | compilationStartContext.RegisterSymbolAction( |
| | 70 | 58 | | context => WarnWhenAuthorizeDataWithAuthenticationSchemesOrRolesPropertySetOnOperationContractImplementa |
| | 23 | 59 | | SymbolKind.Method); |
| | 59 | 60 | | }); |
| | 36 | 61 | | } |
| | | 62 | | |
| | 574 | 63 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => s_supportedDiagnostics; |
| | | 64 | | |
| | | 65 | | private static void WarnWhenAuthorizeDataWithAuthenticationSchemesOrRolesPropertySetOnOperationContractImplementatio |
| | | 66 | | { |
| | 70 | 67 | | IMethodSymbol methodSymbol = (IMethodSymbol)context.Symbol; |
| | 70 | 68 | | if (methodSymbol.IsGeneratedCode() == true) |
| | | 69 | | { |
| | 12 | 70 | | return; |
| | | 71 | | } |
| | | 72 | | |
| | 58 | 73 | | bool hasAuthorizeData = methodSymbol.HasOneAttributeInheritFrom(authorizeData); |
| | 58 | 74 | | if (!hasAuthorizeData) |
| | | 75 | | { |
| | 46 | 76 | | return; |
| | | 77 | | } |
| | | 78 | | |
| | 12 | 79 | | bool isOperationContractImplementation = IsOperationContractImplementation(methodSymbol, ssmServiceContractAttri |
| | 12 | 80 | | if (!isOperationContractImplementation) |
| | | 81 | | { |
| | 0 | 82 | | return; |
| | | 83 | | } |
| | | 84 | | |
| | 12 | 85 | | var arguments = from attribute in methodSymbol.GetAttributes() |
| | 12 | 86 | | where attribute.AttributeClass!.AllInterfaces.Any(@interface => |
| | 24 | 87 | | @interface.Equals(authorizeData, SymbolEqualityComparer.Default)) |
| | 12 | 88 | | from args in attribute.NamedArguments |
| | 20 | 89 | | select args; |
| | | 90 | | |
| | 40 | 91 | | foreach (var argument in arguments) |
| | | 92 | | { |
| | 8 | 93 | | if (argument.Key == AuthenticationSchemesPropertyName) |
| | | 94 | | { |
| | 4 | 95 | | context.ReportDiagnostic(DiagnosticDescriptors.AuthorizationAttributesAnalyzer_02XX.AuthorizeDataAuthent |
| | 4 | 96 | | continue; |
| | | 97 | | } |
| | 4 | 98 | | if (argument.Key == RolesPropertyName) |
| | | 99 | | { |
| | 4 | 100 | | context.ReportDiagnostic(DiagnosticDescriptors.AuthorizationAttributesAnalyzer_02XX.AuthorizeDataRolesPr |
| | | 101 | | } |
| | | 102 | | } |
| | 12 | 103 | | } |
| | | 104 | | |
| | | 105 | | private static void WarnWhenAllowAnonymousOnOperationContractImplementation(SymbolAnalysisContext context, INamedTyp |
| | | 106 | | { |
| | 70 | 107 | | IMethodSymbol methodSymbol = (IMethodSymbol)context.Symbol; |
| | 70 | 108 | | if (methodSymbol.IsGeneratedCode() == true) |
| | | 109 | | { |
| | 12 | 110 | | return; |
| | | 111 | | } |
| | | 112 | | |
| | 58 | 113 | | bool hasAllowAnonymous = methodSymbol.HasOneAttributeInheritFrom(allowAnonymous); |
| | 58 | 114 | | if (!hasAllowAnonymous) |
| | | 115 | | { |
| | 45 | 116 | | return; |
| | | 117 | | } |
| | | 118 | | |
| | 13 | 119 | | bool isOperationContractImplementation = IsOperationContractImplementation(methodSymbol, ssmServiceContractAttri |
| | | 120 | | |
| | 13 | 121 | | if (isOperationContractImplementation) |
| | | 122 | | { |
| | 12 | 123 | | context.ReportDiagnostic(DiagnosticDescriptors.AuthorizationAttributesAnalyzer_02XX.AllowAnonymousAttributeI |
| | | 124 | | } |
| | 13 | 125 | | } |
| | | 126 | | |
| | | 127 | | private static bool IsOperationContractImplementation(IMethodSymbol methodSymbol, INamedTypeSymbol? ssmServiceContra |
| | | 128 | | { |
| | 25 | 129 | | var serviceContracts = (from @interface in methodSymbol.ContainingType.AllInterfaces |
| | 28 | 130 | | where @interface.GetOneAttributeOf(ssmServiceContractAttribute, coreWCFServiceContractAttribute) is not null |
| | 25 | 131 | | select @interface).ToImmutableArray(); |
| | | 132 | | |
| | 25 | 133 | | if (serviceContracts.IsEmpty) |
| | | 134 | | { |
| | 1 | 135 | | return false; |
| | | 136 | | } |
| | | 137 | | |
| | 24 | 138 | | var operationContracts = (from serviceContract in serviceContracts |
| | 60 | 139 | | from method in serviceContract.GetMembers().OfType<IMethodSymbol>() |
| | 36 | 140 | | where method.Name == methodSymbol.Name |
| | 36 | 141 | | where method.GetOneAttributeOf(coreWCFOperationContractAttribute, ssmOperationContractAttribute) is not null |
| | 60 | 142 | | select method).ToImmutableArray(); |
| | | 143 | | |
| | 24 | 144 | | var implementedMethods = methodSymbol.ContainingType.GetMembers().OfType<IMethodSymbol>() |
| | 84 | 145 | | .Where(x => x.Name == methodSymbol.Name) |
| | 24 | 146 | | .ToImmutableArray(); |
| | | 147 | | |
| | 72 | 148 | | foreach (IMethodSymbol operationContract in operationContracts) |
| | | 149 | | { |
| | 24 | 150 | | if (implementedMethods.Any(implementedMethod => |
| | 72 | 151 | | implementedMethod.IsMatchingUserProvidedMethod(operationContract, coreWCFInjectedAttribute, |
| | 72 | 152 | | mvcFromServicesAttribute))) |
| | | 153 | | { |
| | 24 | 154 | | return true; |
| | | 155 | | } |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | return false; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | private static void WarnWhenAllowAnonymousOnServiceContractImplementation(SymbolAnalysisContext context, INamedTypeS |
| | | 162 | | { |
| | 46 | 163 | | INamedTypeSymbol namedTypeSymbol = (INamedTypeSymbol)context.Symbol; |
| | | 164 | | |
| | 46 | 165 | | bool hasAllowAnonymous = namedTypeSymbol.HasOneAttributeInheritFrom(allowAnonymous); |
| | 46 | 166 | | if (!hasAllowAnonymous) |
| | | 167 | | { |
| | 41 | 168 | | return; |
| | | 169 | | } |
| | | 170 | | |
| | 5 | 171 | | if (namedTypeSymbol.AllInterfaces.Any(x => |
| | 13 | 172 | | x.GetOneAttributeOf(coreWCFServiceContractAttribute, ssmServiceContractAttribute) is not null)) |
| | | 173 | | { |
| | 4 | 174 | | context.ReportDiagnostic(DiagnosticDescriptors.AuthorizationAttributesAnalyzer_02XX.AllowAnonymousAttributeI |
| | | 175 | | } |
| | 5 | 176 | | } |
| | | 177 | | } |