|
17 | 17 | using System.ComponentModel.Composition;
|
18 | 18 | using System.Globalization;
|
19 | 19 | using System.Reflection;
|
| 20 | +using System.Text; |
20 | 21 |
|
21 | 22 | namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
|
22 | 23 | {
|
@@ -71,21 +72,53 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
|
71 | 72 |
|
72 | 73 | private List<CorrectionExtent> GetCorrectionExtent(ParameterAst paramAst)
|
73 | 74 | {
|
74 |
| - IScriptExtent ext = paramAst.Extent; |
75 |
| - var corrections = new List<CorrectionExtent>(); |
76 |
| - string correctionText = string.Format("[SecureString] {0}", paramAst.Name.Extent.Text); |
77 |
| - string description = string.Format("Set {0} type to SecureString", paramAst.Name.Extent.Text); |
| 75 | + //Find the parameter type extent and replace that with secure string |
| 76 | + IScriptExtent extent; |
| 77 | + var typeAttributeAst = GetTypeAttributeAst(paramAst); |
| 78 | + var corrections = new List<CorrectionExtent>(); |
| 79 | + string correctionText; |
| 80 | + if (typeAttributeAst == null) |
| 81 | + { |
| 82 | + // cannot find any type attribute |
| 83 | + extent = paramAst.Name.Extent; |
| 84 | + correctionText = string.Format("[SecureString] {0}", paramAst.Name.Extent.Text); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + // replace only the existing type with [SecureString] |
| 89 | + extent = typeAttributeAst.Extent; |
| 90 | + correctionText = typeAttributeAst.TypeName.IsArray ? "[SecureString[]]" : "[SecureString]"; |
| 91 | + } |
| 92 | + string description = string.Format( |
| 93 | + CultureInfo.CurrentCulture, |
| 94 | + Strings.AvoidUsingPlainTextForPasswordCorrectionDescription, |
| 95 | + paramAst.Name.Extent.Text); |
78 | 96 | corrections.Add(new CorrectionExtent(
|
79 |
| - ext.StartLineNumber, |
80 |
| - ext.EndLineNumber, |
81 |
| - ext.StartColumnNumber, |
82 |
| - ext.EndColumnNumber, |
83 |
| - correctionText, |
84 |
| - ext.File, |
| 97 | + extent.StartLineNumber, |
| 98 | + extent.EndLineNumber, |
| 99 | + extent.StartColumnNumber, |
| 100 | + extent.EndColumnNumber, |
| 101 | + correctionText.ToString(), |
| 102 | + paramAst.Extent.File, |
85 | 103 | description));
|
86 | 104 | return corrections;
|
87 | 105 | }
|
88 | 106 |
|
| 107 | + private TypeConstraintAst GetTypeAttributeAst(ParameterAst paramAst) |
| 108 | + { |
| 109 | + if (paramAst.Attributes != null) |
| 110 | + { |
| 111 | + foreach(var attr in paramAst.Attributes) |
| 112 | + { |
| 113 | + if (attr.GetType() == typeof(TypeConstraintAst)) |
| 114 | + { |
| 115 | + return attr as TypeConstraintAst; |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
89 | 122 | /// <summary>
|
90 | 123 | /// GetName: Retrieves the name of this rule.
|
91 | 124 | /// </summary>
|
|
0 commit comments