|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Management.Automation.Language; |
| 6 | +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; |
| 7 | +#if !CORECLR |
| 8 | +using System.ComponentModel.Composition; |
| 9 | +#endif |
| 10 | +using System.Globalization; |
| 11 | +using System.Linq; |
| 12 | + |
| 13 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// AvoidUsingBrokenHashAlgorithms: Avoid using the broken algorithms MD5 or SHA-1. |
| 17 | + /// </summary> |
| 18 | +#if !CORECLR |
| 19 | +[Export(typeof(IScriptRule))] |
| 20 | +#endif |
| 21 | + public class AvoidUsingBrokenHashAlgorithms : AvoidParameterGeneric |
| 22 | + { |
| 23 | + private readonly string[] brokenAlgorithms = new string[] |
| 24 | + { |
| 25 | + "MD5", |
| 26 | + "SHA1", |
| 27 | + }; |
| 28 | + |
| 29 | + private string algorithm; |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Condition on the cmdlet that must be satisfied for the error to be raised |
| 33 | + /// </summary> |
| 34 | + /// <param name="CmdAst"></param> |
| 35 | + /// <returns></returns> |
| 36 | + public override bool CommandCondition(CommandAst CmdAst) |
| 37 | + { |
| 38 | + return true; |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Condition on the parameter that must be satisfied for the error to be raised. |
| 43 | + /// </summary> |
| 44 | + /// <param name="CmdAst"></param> |
| 45 | + /// <param name="CeAst"></param> |
| 46 | + /// <returns></returns> |
| 47 | + public override bool ParameterCondition(CommandAst CmdAst, CommandElementAst CeAst) |
| 48 | + { |
| 49 | + if (CeAst is CommandParameterAst) |
| 50 | + { |
| 51 | + CommandParameterAst cmdParamAst = CeAst as CommandParameterAst; |
| 52 | + |
| 53 | + if (String.Equals(cmdParamAst.ParameterName, "algorithm", StringComparison.OrdinalIgnoreCase)) |
| 54 | + { |
| 55 | + Ast hashAlgorithmArgument = cmdParamAst.Argument; |
| 56 | + if (hashAlgorithmArgument is null) |
| 57 | + { |
| 58 | + hashAlgorithmArgument = GetHashAlgorithmArg(CmdAst, cmdParamAst.Extent.StartOffset); |
| 59 | + if (hashAlgorithmArgument is null) |
| 60 | + { |
| 61 | + return false; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + var constExprAst = hashAlgorithmArgument as ConstantExpressionAst; |
| 66 | + if (constExprAst != null) |
| 67 | + { |
| 68 | + algorithm = constExprAst.Value as string; |
| 69 | + return IsBrokenAlgorithm(algorithm); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + private bool IsBrokenAlgorithm(string algorithm) |
| 78 | + { |
| 79 | + if (algorithm != null) |
| 80 | + { |
| 81 | + return brokenAlgorithms.Contains<string>( |
| 82 | + algorithm, |
| 83 | + StringComparer.OrdinalIgnoreCase); |
| 84 | + } |
| 85 | + |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + private Ast GetHashAlgorithmArg(CommandAst CmdAst, int StartIndex) |
| 90 | + { |
| 91 | + int small = int.MaxValue; |
| 92 | + Ast hashAlgorithmArg = null; |
| 93 | + foreach (Ast ast in CmdAst.CommandElements) |
| 94 | + { |
| 95 | + if (ast.Extent.StartOffset > StartIndex && ast.Extent.StartOffset < small) |
| 96 | + { |
| 97 | + hashAlgorithmArg = ast; |
| 98 | + small = ast.Extent.StartOffset; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return hashAlgorithmArg; |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Retrieves the error message |
| 107 | + /// </summary> |
| 108 | + /// <param name="FileName"></param> |
| 109 | + /// <param name="CmdAst"></param> |
| 110 | + /// <returns></returns> |
| 111 | + public override string GetError(string FileName, CommandAst CmdAst) |
| 112 | + { |
| 113 | + if (CmdAst is null) |
| 114 | + { |
| 115 | + return string.Empty; |
| 116 | + } |
| 117 | + |
| 118 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingBrokenHashAlgorithmsError, CmdAst.GetCommandName(), algorithm); |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// GetName: Retrieves the name of this rule. |
| 123 | + /// </summary> |
| 124 | + /// <returns>The name of this rule</returns> |
| 125 | + public override string GetName() |
| 126 | + { |
| 127 | + return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidUsingBrokenHashAlgorithmsName); |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// GetCommonName: Retrieves the common name of this rule. |
| 132 | + /// </summary> |
| 133 | + /// <returns>The common name of this rule</returns> |
| 134 | + public override string GetCommonName() |
| 135 | + { |
| 136 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingBrokenHashAlgorithmsCommonName); |
| 137 | + } |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// GetDescription: Retrieves the description of this rule. |
| 141 | + /// </summary> |
| 142 | + /// <returns>The description of this rule</returns> |
| 143 | + public override string GetDescription() |
| 144 | + { |
| 145 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingBrokenHashAlgorithmsDescription); |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. |
| 150 | + /// </summary> |
| 151 | + public override SourceType GetSourceType() |
| 152 | + { |
| 153 | + return SourceType.Builtin; |
| 154 | + } |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// GetSeverity: Retrieves the severity of the rule: error, warning or information. |
| 158 | + /// </summary> |
| 159 | + /// <returns></returns> |
| 160 | + public override RuleSeverity GetSeverity() |
| 161 | + { |
| 162 | + return RuleSeverity.Warning; |
| 163 | + } |
| 164 | + |
| 165 | + /// <summary> |
| 166 | + /// DiagnosticSeverity: Retrieves the severity of the rule of type DiagnosticSeverity: error, warning or information. |
| 167 | + /// </summary> |
| 168 | + /// <returns></returns> |
| 169 | + public override DiagnosticSeverity GetDiagnosticSeverity() |
| 170 | + { |
| 171 | + return DiagnosticSeverity.Warning; |
| 172 | + } |
| 173 | + |
| 174 | + /// <summary> |
| 175 | + /// GetSourceName: Retrieves the module/assembly name the rule is from. |
| 176 | + /// </summary> |
| 177 | + public override string GetSourceName() |
| 178 | + { |
| 179 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | + |
0 commit comments