| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using Microsoft.CodeAnalysis; |
| | | 3 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 4 | | using Microsoft.CodeAnalysis.Operations; |
| | | 5 | | |
| | | 6 | | namespace AsiBackbone.Analyzers; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Reports local-development signing providers that are wired inside a production environment branch, or wired |
| | | 10 | | /// without any environment guard. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// Local-development signing generates an in-process key that is never persisted, so it is suitable only for tests, |
| | | 14 | | /// samples, and local proof paths. Production hosts should use a host-owned key provider instead. |
| | | 15 | | /// </remarks> |
| | | 16 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 17 | | public sealed class LocalDevelopmentSigningProductionAnalyzer : DiagnosticAnalyzer |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Reports local-development signing used inside a production environment branch. |
| | | 21 | | /// </summary> |
| | | 22 | | public const string DiagnosticId = "ASIB002"; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Reports local-development signing wired with no environment guard at all. |
| | | 26 | | /// </summary> |
| | | 27 | | public const string UnguardedDiagnosticId = "ASIB003"; |
| | | 28 | | |
| | | 29 | | private const string LocalDevelopmentNamespace = "AsiBackbone.Signing.LocalDevelopment"; |
| | | 30 | | |
| | 1 | 31 | | private static readonly DiagnosticDescriptor Rule = new( |
| | 1 | 32 | | DiagnosticId, |
| | 1 | 33 | | "Do not wire local-development signing in production branches", |
| | 1 | 34 | | "Local-development signing type '{0}' is used inside a production environment branch; use a host-owned productio |
| | 1 | 35 | | "AsiBackbone.ProductionSafety", |
| | 1 | 36 | | DiagnosticSeverity.Warning, |
| | 1 | 37 | | isEnabledByDefault: true, |
| | 1 | 38 | | description: "LocalDevelopment signing providers generate in-process keys for tests, samples, and local proof pa |
| | | 39 | | |
| | | 40 | | /// <remarks> |
| | | 41 | | /// <see cref="Rule" /> only fires on a call the analyzer can see inside a production branch, so an unconditional |
| | | 42 | | /// registration — the shape that actually reaches production — was never reported. The runtime guard in |
| | | 43 | | /// <c>UseLocalDevelopmentSigning</c> throws in Production; this rule surfaces the same problem at build time. |
| | | 44 | | /// </remarks> |
| | 1 | 45 | | private static readonly DiagnosticDescriptor UnguardedRule = new( |
| | 1 | 46 | | UnguardedDiagnosticId, |
| | 1 | 47 | | "Guard local-development signing by environment", |
| | 1 | 48 | | "Local-development signing type '{0}' is wired without an environment guard; it will be registered in whichever |
| | 1 | 49 | | "AsiBackbone.ProductionSafety", |
| | 1 | 50 | | DiagnosticSeverity.Warning, |
| | 1 | 51 | | isEnabledByDefault: true, |
| | 1 | 52 | | description: "LocalDevelopment signing providers generate an in-process key that is never persisted, so artifact |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets the diagnostics supported by this analyzer. |
| | | 56 | | /// </summary> |
| | 32 | 57 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Rule, UnguardedRule]; |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Initializes the analyzer and registers operation analysis for invocations and object creations that reference |
| | | 61 | | /// local-development signing types. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="context">The analysis context used to register analyzer actions.</param> |
| | | 64 | | public override void Initialize(AnalysisContext context) |
| | | 65 | | { |
| | 32 | 66 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 32 | 67 | | context.EnableConcurrentExecution(); |
| | 32 | 68 | | context.RegisterOperationAction(AnalyzeInvocation, OperationKind.Invocation); |
| | 32 | 69 | | context.RegisterOperationAction(AnalyzeObjectCreation, OperationKind.ObjectCreation); |
| | 32 | 70 | | } |
| | | 71 | | |
| | | 72 | | private static void AnalyzeInvocation(OperationAnalysisContext context) |
| | | 73 | | { |
| | 47 | 74 | | var invocation = (IInvocationOperation)context.Operation; |
| | | 75 | | |
| | 47 | 76 | | if (IsSuppressedByHostMarker(context.ContainingSymbol) |
| | 47 | 77 | | || IsNestedInsideInvocationThatAlreadyReferencesLocalDevelopment(invocation)) |
| | | 78 | | { |
| | 7 | 79 | | return; |
| | | 80 | | } |
| | | 81 | | |
| | 40 | 82 | | bool insideProductionBranch = IsInsideProductionBranch(invocation); |
| | | 83 | | |
| | 40 | 84 | | if (!insideProductionBranch && IsInsideEnvironmentConditional(invocation)) |
| | | 85 | | { |
| | 11 | 86 | | return; |
| | | 87 | | } |
| | | 88 | | |
| | 29 | 89 | | ITypeSymbol? localDevelopmentType = FindReferencedLocalDevelopmentType(invocation); |
| | 29 | 90 | | if (localDevelopmentType is null) |
| | | 91 | | { |
| | 12 | 92 | | return; |
| | | 93 | | } |
| | | 94 | | |
| | 17 | 95 | | context.ReportDiagnostic( |
| | 17 | 96 | | Diagnostic.Create( |
| | 17 | 97 | | insideProductionBranch ? Rule : UnguardedRule, |
| | 17 | 98 | | invocation.Syntax.GetLocation(), |
| | 17 | 99 | | localDevelopmentType.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat))); |
| | 17 | 100 | | } |
| | | 101 | | |
| | | 102 | | private static void AnalyzeObjectCreation(OperationAnalysisContext context) |
| | | 103 | | { |
| | 71 | 104 | | var objectCreation = (IObjectCreationOperation)context.Operation; |
| | | 105 | | |
| | 71 | 106 | | if (IsSuppressedByHostMarker(context.ContainingSymbol) || !IsInsideProductionBranch(objectCreation)) |
| | | 107 | | { |
| | 69 | 108 | | return; |
| | | 109 | | } |
| | | 110 | | |
| | 2 | 111 | | ITypeSymbol? localDevelopmentType = FindLocalDevelopmentType(objectCreation.Type); |
| | 2 | 112 | | if (localDevelopmentType is null) |
| | | 113 | | { |
| | 1 | 114 | | return; |
| | | 115 | | } |
| | | 116 | | |
| | 1 | 117 | | context.ReportDiagnostic( |
| | 1 | 118 | | Diagnostic.Create( |
| | 1 | 119 | | Rule, |
| | 1 | 120 | | objectCreation.Syntax.GetLocation(), |
| | 1 | 121 | | localDevelopmentType.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat))); |
| | 1 | 122 | | } |
| | | 123 | | |
| | | 124 | | private static bool IsNestedInsideInvocationThatAlreadyReferencesLocalDevelopment(IInvocationOperation invocation) |
| | | 125 | | { |
| | 42 | 126 | | return invocation.Parent is IArgumentOperation { Parent: IInvocationOperation parentInvocation } |
| | 42 | 127 | | && FindReferencedLocalDevelopmentType(parentInvocation) is not null; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | private static ITypeSymbol? FindReferencedLocalDevelopmentType(IInvocationOperation invocation) |
| | | 131 | | { |
| | 31 | 132 | | ITypeSymbol? containingType = FindLocalDevelopmentType(invocation.TargetMethod.ContainingType); |
| | 31 | 133 | | if (containingType is not null) |
| | | 134 | | { |
| | 0 | 135 | | return containingType; |
| | | 136 | | } |
| | | 137 | | |
| | 84 | 138 | | foreach (ITypeSymbol typeArgument in invocation.TargetMethod.TypeArguments) |
| | | 139 | | { |
| | 20 | 140 | | ITypeSymbol? localDevelopmentType = FindLocalDevelopmentType(typeArgument); |
| | 20 | 141 | | if (localDevelopmentType is not null) |
| | | 142 | | { |
| | 18 | 143 | | return localDevelopmentType; |
| | | 144 | | } |
| | | 145 | | } |
| | | 146 | | |
| | 56 | 147 | | foreach (IArgumentOperation argument in invocation.Arguments) |
| | | 148 | | { |
| | 15 | 149 | | ITypeSymbol? localDevelopmentType = FindLocalDevelopmentType(argument.Value.Type); |
| | 15 | 150 | | if (localDevelopmentType is not null) |
| | | 151 | | { |
| | 0 | 152 | | return localDevelopmentType; |
| | | 153 | | } |
| | | 154 | | } |
| | | 155 | | |
| | 13 | 156 | | return FindLocalDevelopmentType(invocation.Type); |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | private static ITypeSymbol? FindLocalDevelopmentType(ITypeSymbol? type) |
| | | 160 | | { |
| | 83 | 161 | | if (type is null) |
| | | 162 | | { |
| | 0 | 163 | | return null; |
| | | 164 | | } |
| | | 165 | | |
| | 83 | 166 | | if (type is INamedTypeSymbol namedType && namedType.IsGenericType) |
| | | 167 | | { |
| | 7 | 168 | | foreach (ITypeSymbol typeArgument in namedType.TypeArguments) |
| | | 169 | | { |
| | 2 | 170 | | ITypeSymbol? localDevelopmentType = FindLocalDevelopmentType(typeArgument); |
| | 2 | 171 | | if (localDevelopmentType is not null) |
| | | 172 | | { |
| | 1 | 173 | | return localDevelopmentType; |
| | | 174 | | } |
| | | 175 | | } |
| | | 176 | | } |
| | | 177 | | |
| | 82 | 178 | | string namespaceName = type.ContainingNamespace?.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat) ? |
| | 82 | 179 | | return namespaceName.Equals(LocalDevelopmentNamespace, StringComparison.Ordinal) |
| | 82 | 180 | | ? type |
| | 82 | 181 | | : null; |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | private static bool IsInsideProductionBranch(IOperation operation) |
| | | 185 | | { |
| | 778 | 186 | | for (IOperation? current = operation.Parent; current is not null; current = current.Parent) |
| | | 187 | | { |
| | 300 | 188 | | if (current is IConditionalOperation conditionalOperation |
| | 300 | 189 | | && IsProductionLikeCondition(conditionalOperation.Condition)) |
| | | 190 | | { |
| | 18 | 191 | | return true; |
| | | 192 | | } |
| | | 193 | | } |
| | | 194 | | |
| | 89 | 195 | | return false; |
| | | 196 | | } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// Determines whether the operation sits inside any conditional that tests the host environment. |
| | | 200 | | /// </summary> |
| | | 201 | | /// <remarks> |
| | | 202 | | /// A registration guarded by an environment check — for either the production or the non-production side — is a |
| | | 203 | | /// deliberate choice the host has already made. Only registrations with no environment check at all are reported by |
| | | 204 | | /// <see cref="UnguardedRule" />. |
| | | 205 | | /// </remarks> |
| | | 206 | | private static bool IsInsideEnvironmentConditional(IOperation operation) |
| | | 207 | | { |
| | 178 | 208 | | for (IOperation? current = operation.Parent; current is not null; current = current.Parent) |
| | | 209 | | { |
| | 76 | 210 | | if (current is IConditionalOperation conditionalOperation |
| | 76 | 211 | | && ConditionReferencesEnvironment(conditionalOperation.Condition)) |
| | | 212 | | { |
| | 11 | 213 | | return true; |
| | | 214 | | } |
| | | 215 | | } |
| | | 216 | | |
| | 13 | 217 | | return false; |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | private static bool ConditionReferencesEnvironment(IOperation operation) |
| | | 221 | | { |
| | 24 | 222 | | operation = Unwrap(operation); |
| | | 223 | | |
| | 24 | 224 | | if (IsEnvironmentNameReference(operation)) |
| | | 225 | | { |
| | 2 | 226 | | return true; |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | switch (operation) |
| | | 230 | | { |
| | | 231 | | case IInvocationOperation invocationOperation: |
| | 12 | 232 | | IMethodSymbol method = invocationOperation.TargetMethod.ReducedFrom ?? invocationOperation.TargetMethod; |
| | 12 | 233 | | string namespaceName = method.ContainingNamespace?.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessag |
| | | 234 | | |
| | 12 | 235 | | return namespaceName.Equals("Microsoft.Extensions.Hosting", StringComparison.Ordinal) |
| | 12 | 236 | | || InvocationComparesEnvironmentNameToProduction(invocationOperation) |
| | 12 | 237 | | || IsEnvironmentNameReference(invocationOperation.Instance); |
| | | 238 | | |
| | | 239 | | case IBinaryOperation binaryOperation: |
| | 3 | 240 | | return ConditionReferencesEnvironment(binaryOperation.LeftOperand) |
| | 3 | 241 | | || ConditionReferencesEnvironment(binaryOperation.RightOperand); |
| | | 242 | | |
| | | 243 | | case IUnaryOperation unaryOperation: |
| | 2 | 244 | | return ConditionReferencesEnvironment(unaryOperation.Operand); |
| | | 245 | | |
| | | 246 | | case IPropertyReferenceOperation propertyReference: |
| | 3 | 247 | | return propertyReference.Property.Name.Contains("Environment", StringComparison.Ordinal); |
| | | 248 | | |
| | | 249 | | default: |
| | 2 | 250 | | return false; |
| | | 251 | | } |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | private static bool IsProductionLikeCondition(IOperation operation) |
| | | 255 | | { |
| | 36 | 256 | | operation = Unwrap(operation); |
| | | 257 | | |
| | 36 | 258 | | return operation switch |
| | 36 | 259 | | { |
| | 26 | 260 | | IInvocationOperation invocationOperation => IsProductionInvocation(invocationOperation) |
| | 26 | 261 | | || InvocationComparesEnvironmentNameToProduction(invocationOperation), |
| | 5 | 262 | | IBinaryOperation binaryOperation => BinaryComparesEnvironmentNameToProduction(binaryOperation), |
| | 5 | 263 | | _ => false |
| | 36 | 264 | | }; |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | private static bool IsProductionInvocation(IInvocationOperation invocation) |
| | | 268 | | { |
| | 26 | 269 | | IMethodSymbol method = invocation.TargetMethod.ReducedFrom ?? invocation.TargetMethod; |
| | 26 | 270 | | string namespaceName = method.ContainingNamespace?.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat) |
| | | 271 | | |
| | 26 | 272 | | return method.Name.Equals("IsProduction", StringComparison.Ordinal) |
| | 26 | 273 | | && namespaceName.Equals("Microsoft.Extensions.Hosting", StringComparison.Ordinal); |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | private static bool InvocationComparesEnvironmentNameToProduction(IInvocationOperation invocation) |
| | | 277 | | { |
| | 20 | 278 | | if (!invocation.TargetMethod.Name.Equals("Equals", StringComparison.Ordinal)) |
| | | 279 | | { |
| | 8 | 280 | | return false; |
| | | 281 | | } |
| | | 282 | | |
| | 12 | 283 | | bool hasEnvironmentName = IsEnvironmentNameReference(invocation.Instance); |
| | 12 | 284 | | bool hasProductionConstant = false; |
| | | 285 | | |
| | 56 | 286 | | foreach (IArgumentOperation argument in invocation.Arguments) |
| | | 287 | | { |
| | 16 | 288 | | IOperation value = Unwrap(argument.Value); |
| | 16 | 289 | | hasEnvironmentName = hasEnvironmentName || IsEnvironmentNameReference(value); |
| | 16 | 290 | | hasProductionConstant = hasProductionConstant || IsProductionConstant(value); |
| | | 291 | | } |
| | | 292 | | |
| | 12 | 293 | | return hasEnvironmentName && hasProductionConstant; |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | private static bool BinaryComparesEnvironmentNameToProduction(IBinaryOperation binaryOperation) |
| | | 297 | | { |
| | 5 | 298 | | if (binaryOperation.OperatorKind != BinaryOperatorKind.Equals) |
| | | 299 | | { |
| | 2 | 300 | | return false; |
| | | 301 | | } |
| | | 302 | | |
| | 3 | 303 | | IOperation left = Unwrap(binaryOperation.LeftOperand); |
| | 3 | 304 | | IOperation right = Unwrap(binaryOperation.RightOperand); |
| | | 305 | | |
| | 3 | 306 | | return (IsEnvironmentNameReference(left) && IsProductionConstant(right)) |
| | 3 | 307 | | || (IsEnvironmentNameReference(right) && IsProductionConstant(left)); |
| | | 308 | | } |
| | | 309 | | |
| | | 310 | | private static bool IsEnvironmentNameReference(IOperation? operation) |
| | | 311 | | { |
| | 53 | 312 | | operation = operation is null ? null : Unwrap(operation); |
| | | 313 | | |
| | 53 | 314 | | return operation is IPropertyReferenceOperation propertyReference |
| | 53 | 315 | | && propertyReference.Property.Name.Equals("EnvironmentName", StringComparison.Ordinal); |
| | | 316 | | } |
| | | 317 | | |
| | | 318 | | private static bool IsProductionConstant(IOperation operation) |
| | | 319 | | { |
| | 17 | 320 | | return operation.ConstantValue.HasValue |
| | 17 | 321 | | && operation.ConstantValue.Value is string value |
| | 17 | 322 | | && value.Equals("Production", StringComparison.OrdinalIgnoreCase); |
| | | 323 | | } |
| | | 324 | | |
| | | 325 | | private static IOperation Unwrap(IOperation operation) |
| | | 326 | | { |
| | 135 | 327 | | while (operation is IConversionOperation conversionOperation) |
| | | 328 | | { |
| | 4 | 329 | | operation = conversionOperation.Operand; |
| | 4 | 330 | | } |
| | | 331 | | |
| | 131 | 332 | | return operation; |
| | | 333 | | } |
| | | 334 | | |
| | | 335 | | private static bool IsSuppressedByHostMarker(ISymbol? symbol) |
| | | 336 | | { |
| | 396 | 337 | | for (ISymbol? current = symbol; current is not null; current = current.ContainingSymbol) |
| | | 338 | | { |
| | 469 | 339 | | foreach (AttributeData attribute in current.GetAttributes()) |
| | | 340 | | { |
| | 41 | 341 | | string? attributeName = attribute.AttributeClass?.Name; |
| | 41 | 342 | | if (attributeName is "AsiBackboneProductionConfigurationReviewed" or "AsiBackboneProductionConfiguration |
| | | 343 | | { |
| | 9 | 344 | | return true; |
| | | 345 | | } |
| | | 346 | | } |
| | | 347 | | |
| | 189 | 348 | | if (current is INamedTypeSymbol) |
| | | 349 | | { |
| | | 350 | | break; |
| | | 351 | | } |
| | | 352 | | } |
| | | 353 | | |
| | 109 | 354 | | return false; |
| | | 355 | | } |
| | | 356 | | } |