|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft Corporation. |
| 3 | +// |
| 4 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 5 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 6 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 7 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 8 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 9 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 10 | +// THE SOFTWARE. |
| 11 | +// |
| 12 | + |
| 13 | +using System; |
| 14 | +using System.Linq; |
| 15 | +using System.Collections.Generic; |
| 16 | +using System.Management.Automation.Language; |
| 17 | +using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; |
| 18 | +#if !CORECLR |
| 19 | +using System.ComponentModel.Composition; |
| 20 | +#endif |
| 21 | +using System.Globalization; |
| 22 | + |
| 23 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// AvoidAssignmentToAutomaticVariable: |
| 27 | + /// </summary> |
| 28 | +#if !CORECLR |
| 29 | +[Export(typeof(IScriptRule))] |
| 30 | +#endif |
| 31 | + public class AvoidAssignmentToAutomaticVariable : IScriptRule |
| 32 | + { |
| 33 | + private static readonly IList<string> _readOnlyAutomaticVariables = new List<string>() |
| 34 | + { |
| 35 | + // Attempting to assign to any of those read-only variable would result in an error at runtime |
| 36 | + "?", "true", "false", "Host", "PSCulture", "Error", "ExecutionContext", "Home", "PID", "PSEdition", "PSHome", "PSUICulture", "PSVersionTable", "ShellId" |
| 37 | + }; |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Checks for assignment to automatic variables. |
| 41 | + /// <param name="ast">The script's ast</param> |
| 42 | + /// <param name="fileName">The script's file name</param> |
| 43 | + /// <returns>The diagnostic results of this rule</returns> |
| 44 | + /// </summary> |
| 45 | + public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) |
| 46 | + { |
| 47 | + if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); |
| 48 | + |
| 49 | + IEnumerable<Ast> assignmentStatementAsts = ast.FindAll(testAst => testAst is AssignmentStatementAst, searchNestedScriptBlocks: true); |
| 50 | + foreach (var assignmentStatementAst in assignmentStatementAsts) |
| 51 | + { |
| 52 | + var variableExpressionAst = assignmentStatementAst.Find(testAst => testAst is VariableExpressionAst, searchNestedScriptBlocks: false) as VariableExpressionAst; |
| 53 | + var variableName = variableExpressionAst.VariablePath.UserPath; |
| 54 | + if (_readOnlyAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase)) |
| 55 | + { |
| 56 | + yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableError, variableName), |
| 57 | + variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Error, fileName); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + IEnumerable<Ast> parameterAsts = ast.FindAll(testAst => testAst is ParameterAst, searchNestedScriptBlocks: true); |
| 62 | + foreach (ParameterAst parameterAst in parameterAsts) |
| 63 | + { |
| 64 | + var variableExpressionAst = parameterAst.Find(testAst => testAst is VariableExpressionAst, searchNestedScriptBlocks: false) as VariableExpressionAst; |
| 65 | + var variableName = variableExpressionAst.VariablePath.UserPath; |
| 66 | + // also check the parent to exclude parameter attributes such as '[Parameter(Mandatory=$true)]' where the read-only variable $true appears. |
| 67 | + if (_readOnlyAutomaticVariables.Contains(variableName, StringComparer.OrdinalIgnoreCase) && !(variableExpressionAst.Parent is NamedAttributeArgumentAst)) |
| 68 | + { |
| 69 | + yield return new DiagnosticRecord(DiagnosticRecordHelper.FormatError(Strings.AvoidAssignmentToReadOnlyAutomaticVariableError, variableName), |
| 70 | + variableExpressionAst.Extent, GetName(), DiagnosticSeverity.Error, fileName); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// GetName: Retrieves the name of this rule. |
| 77 | + /// </summary> |
| 78 | + /// <returns>The name of this rule</returns> |
| 79 | + public string GetName() |
| 80 | + { |
| 81 | + return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidAssignmentToAutomaticVariableName); |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// GetCommonName: Retrieves the common name of this rule. |
| 86 | + /// </summary> |
| 87 | + /// <returns>The common name of this rule</returns> |
| 88 | + public string GetCommonName() |
| 89 | + { |
| 90 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidAssignmentToReadOnlyAutomaticVariableCommonName); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// GetDescription: Retrieves the description of this rule. |
| 95 | + /// </summary> |
| 96 | + /// <returns>The description of this rule</returns> |
| 97 | + public string GetDescription() |
| 98 | + { |
| 99 | + return string.Format(CultureInfo.CurrentCulture, Strings.AvoidAssignmentToReadOnlyAutomaticVariableDescription); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// GetSourceType: Retrieves the type of the rule: builtin, managed or module. |
| 104 | + /// </summary> |
| 105 | + public SourceType GetSourceType() |
| 106 | + { |
| 107 | + return SourceType.Builtin; |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// GetSeverity: Retrieves the severity of the rule: error, warning of information. |
| 112 | + /// </summary> |
| 113 | + /// <returns></returns> |
| 114 | + public RuleSeverity GetSeverity() |
| 115 | + { |
| 116 | + return RuleSeverity.Warning; |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// GetSourceName: Retrieves the module/assembly name the rule is from. |
| 121 | + /// </summary> |
| 122 | + public string GetSourceName() |
| 123 | + { |
| 124 | + return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | +} |
0 commit comments