|
| 1 | +using System.Collections.Immutable; |
| 2 | +using System.Linq; |
| 3 | +using Microsoft.CodeAnalysis; |
| 4 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 5 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 6 | +using Microsoft.CodeAnalysis.Operations; |
| 7 | + |
| 8 | +namespace Meziantou.Analyzer.Rules; |
| 9 | + |
| 10 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 11 | +public sealed class ProcessStartAnalyzer : DiagnosticAnalyzer |
| 12 | +{ |
| 13 | + private static readonly DiagnosticDescriptor UseShellExecuteMustBeExplicitlySet = new( |
| 14 | + RuleIdentifiers.UseShellExecuteMustBeSet, |
| 15 | + title: "UseShellExecute must be explicitly set", |
| 16 | + messageFormat: "UseShellExecute must be explicitly set when initializing a ProcessStartInfo", |
| 17 | + RuleCategories.Usage, |
| 18 | + DiagnosticSeverity.Info, |
| 19 | + isEnabledByDefault: false, |
| 20 | + description: "", |
| 21 | + helpLinkUri: RuleIdentifiers.GetHelpUri(RuleIdentifiers.UseShellExecuteMustBeSet)); |
| 22 | + |
| 23 | + private static readonly DiagnosticDescriptor UseProcessStartOverload = new( |
| 24 | + RuleIdentifiers.UseProcessStartOverload, |
| 25 | + title: "Use Process.Start overload with ProcessStartInfo", |
| 26 | + messageFormat: "Use an overload of Process.Start that has a ProcessStartInfo parameter", |
| 27 | + RuleCategories.Usage, |
| 28 | + DiagnosticSeverity.Info, |
| 29 | + isEnabledByDefault: false, |
| 30 | + description: "", |
| 31 | + helpLinkUri: RuleIdentifiers.GetHelpUri(RuleIdentifiers.UseProcessStartOverload)); |
| 32 | + |
| 33 | + private static readonly DiagnosticDescriptor SetToFalseWhenRedirectingOutput = new( |
| 34 | + RuleIdentifiers.UseShellExecuteMustBeFalse, |
| 35 | + title: "UseShellExecute must be false when redirecting standard input or output", |
| 36 | + messageFormat: "Set UseShellExecute to false when redirecting standard input or output", |
| 37 | + RuleCategories.Usage, |
| 38 | + DiagnosticSeverity.Warning, |
| 39 | + isEnabledByDefault: true, |
| 40 | + description: "", |
| 41 | + helpLinkUri: RuleIdentifiers.GetHelpUri(RuleIdentifiers.UseShellExecuteMustBeFalse)); |
| 42 | + |
| 43 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| 44 | + ImmutableArray.Create(UseShellExecuteMustBeExplicitlySet, SetToFalseWhenRedirectingOutput, UseProcessStartOverload); |
| 45 | + |
| 46 | + public override void Initialize(AnalysisContext context) |
| 47 | + { |
| 48 | + context.EnableConcurrentExecution(); |
| 49 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 50 | + |
| 51 | + context.RegisterCompilationStartAction(ctx => |
| 52 | + { |
| 53 | + var analyzerContext = new AnalyzerContext(ctx.Compilation); |
| 54 | + if (!analyzerContext.IsValid) |
| 55 | + return; |
| 56 | + |
| 57 | + ctx.RegisterOperationAction(analyzerContext.AnalyzeInvocation, OperationKind.Invocation); |
| 58 | + ctx.RegisterOperationAction(analyzerContext.AnalyzeObjectCreation, OperationKind.ObjectCreation); |
| 59 | + }); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + private sealed class AnalyzerContext(Compilation compilation) |
| 64 | + { |
| 65 | + private readonly INamedTypeSymbol? _processStartInfoSymbol = compilation.GetBestTypeByMetadataName("System.Diagnostics.ProcessStartInfo"); |
| 66 | + |
| 67 | + private readonly INamedTypeSymbol? _processSymbol = compilation.GetBestTypeByMetadataName("System.Diagnostics.Process"); |
| 68 | + |
| 69 | + public bool IsValid => _processStartInfoSymbol is not null; |
| 70 | + |
| 71 | + public void AnalyzeInvocation(OperationAnalysisContext context) |
| 72 | + { |
| 73 | + var operation = (IInvocationOperation)context.Operation; |
| 74 | + if (IsProcessStartInvocation(operation)) |
| 75 | + { |
| 76 | + if (!operation.Arguments.Any(IsProcessStartInfo)) |
| 77 | + { |
| 78 | + // Calling Process.Start without ProcessStartInfo |
| 79 | + context.ReportDiagnostic(UseProcessStartOverload, operation); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public void AnalyzeObjectCreation(OperationAnalysisContext context) |
| 85 | + { |
| 86 | + var operation = (IObjectCreationOperation)context.Operation; |
| 87 | + if (IsProcessStartInfoCreation(operation)) |
| 88 | + { |
| 89 | + if (operation is { Initializer: {} initializer } ) |
| 90 | + { |
| 91 | + var useShellExecuteInitializer = initializer.Initializers.OfType<ISimpleAssignmentOperation>() |
| 92 | + .FirstOrDefault(x => x.Target.Syntax is IdentifierNameSyntax { Identifier.Text: "UseShellExecute" }); |
| 93 | + |
| 94 | + if (useShellExecuteInitializer is null) |
| 95 | + { |
| 96 | + if (IsRedirectingInputOrOutput(operation.SemanticModel!, initializer)) |
| 97 | + { |
| 98 | + // Redirecting standard input or output while UseShellExecute is not explicitly set |
| 99 | + context.ReportDiagnostic(SetToFalseWhenRedirectingOutput, operation); |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + // Constructing ProcessStartInfo without setting UseShellExecute in the initializer |
| 104 | + context.ReportDiagnostic(UseShellExecuteMustBeExplicitlySet, operation); |
| 105 | + } |
| 106 | + } |
| 107 | + else if (IsInitializedToTrue(operation.SemanticModel!, useShellExecuteInitializer)) |
| 108 | + { |
| 109 | + if (IsRedirectingInputOrOutput(operation.SemanticModel!, initializer)) |
| 110 | + { |
| 111 | + // Redirecting standard input or output while UseShellExecute is set to true |
| 112 | + context.ReportDiagnostic(SetToFalseWhenRedirectingOutput, operation); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + // Constructing ProcessStartInfo with not initializer at all |
| 119 | + context.ReportDiagnostic(UseShellExecuteMustBeExplicitlySet, operation); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static bool IsInitializedToTrue(SemanticModel semanticModel, ISimpleAssignmentOperation simpleAssignmentOperation) |
| 125 | + => semanticModel.GetConstantValue(simpleAssignmentOperation.Value.Syntax) is { HasValue: true, Value: true }; |
| 126 | + |
| 127 | + private static bool IsRedirectingInputOrOutput(SemanticModel semanticModel, |
| 128 | + IObjectOrCollectionInitializerOperation initializer) => |
| 129 | + initializer.Initializers.OfType<ISimpleAssignmentOperation>() |
| 130 | + .Any(x => x.Target.Syntax is IdentifierNameSyntax { Identifier.Text: "RedirectStandardError" or "RedirectStandardInput" or "RedirectStandardOutput" } |
| 131 | + && IsInitializedToTrue(semanticModel, x)); |
| 132 | + |
| 133 | + private bool IsProcessStartInfo(IArgumentOperation operation) |
| 134 | + => operation.Value.Type.IsEqualTo(_processStartInfoSymbol); |
| 135 | + |
| 136 | + private bool IsProcessStartInfoCreation(IObjectCreationOperation operation) |
| 137 | + => operation.Type.IsEqualTo(_processStartInfoSymbol); |
| 138 | + |
| 139 | + private bool IsProcessStartInvocation(IInvocationOperation operation) |
| 140 | + => operation.TargetMethod.Name == "Start" |
| 141 | + && operation.TargetMethod.ContainingType.IsEqualTo(_processSymbol) |
| 142 | + && operation.TargetMethod.IsStatic; |
| 143 | + } |
| 144 | +} |
0 commit comments