-
Notifications
You must be signed in to change notification settings - Fork 382
/
UsePSCredentialType.cs
224 lines (191 loc) · 8.98 KB
/
UsePSCredentialType.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Linq;
using System.Collections.Generic;
using System.Management.Automation;
using System.Management.Automation.Language;
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
#if !CORECLR
using System.ComponentModel.Composition;
#endif
using System.Globalization;
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{
/// <summary>
/// UsePSCredentialType: Checks if a parameter named Credential is of type PSCredential. Also checks if there is a credential transformation attribute defined after the PSCredential type attribute. The order between credential transformation attribute and PSCredential type attribute is applicable only to Poweshell 4.0 and earlier.
/// </summary>
#if !CORECLR
[Export(typeof(IScriptRule))]
#endif
public class UsePSCredentialType : IScriptRule
{
/// <summary>
/// AnalyzeScript: Analyzes the ast to check if a parameter named Credential is of type PSCredential. Also checks if there is a credential transformation attribute defined after the PSCredential type attribute. The order between the credential transformation attribute and PSCredential type attribute is applicable only to Poweshell 4.0 and earlier.
/// </summary>
/// <param name="ast">The script's ast</param>
/// <param name="fileName">The script's file name</param>
/// <returns>A List of diagnostic results of this rule</returns>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
var sbAst = ast as ScriptBlockAst;
var requiresTransformationAttribute = false;
var psVersion = Helper.Instance.GetPSVersion();
if (psVersion != null && psVersion.Major < 5)
{
requiresTransformationAttribute = true;
}
// do not run the rule if the script requires PS version 5
// but PSSA in invoked through PS version < 5
if (sbAst != null
&& sbAst.ScriptRequirements != null
&& sbAst.ScriptRequirements.RequiredPSVersion != null
&& sbAst.ScriptRequirements.RequiredPSVersion.Major == 5
&& requiresTransformationAttribute)
{
yield break;
}
IEnumerable<Ast> funcDefAsts = ast.FindAll(testAst => testAst is FunctionDefinitionAst, true);
IEnumerable<Ast> scriptBlockAsts = ast.FindAll(testAst => testAst is ScriptBlockAst, true);
List<DiagnosticRecord> diagnosticRecords = new List<DiagnosticRecord>();
foreach (FunctionDefinitionAst funcDefAst in funcDefAsts)
{
IEnumerable<ParameterAst> parameterAsts = null;
if (funcDefAst.Parameters != null)
{
parameterAsts = funcDefAst.Parameters;
}
if (funcDefAst.Body.ParamBlock != null
&& funcDefAst.Body.ParamBlock.Parameters != null)
{
parameterAsts = funcDefAst.Body.ParamBlock.Parameters;
}
if (parameterAsts != null)
{
diagnosticRecords.AddRange(GetViolations(
parameterAsts,
string.Format(CultureInfo.CurrentCulture, Strings.UsePSCredentialTypeError, funcDefAst.Name),
fileName,
requiresTransformationAttribute));
}
}
foreach (ScriptBlockAst scriptBlockAst in scriptBlockAsts)
{
// check for the case where it's parent is function, in that case we already processed above
if (scriptBlockAst.Parent != null && scriptBlockAst.Parent is FunctionDefinitionAst)
{
continue;
}
if (scriptBlockAst.ParamBlock != null && scriptBlockAst.ParamBlock.Parameters != null)
{
diagnosticRecords.AddRange(GetViolations(
scriptBlockAst.ParamBlock.Parameters,
string.Format(CultureInfo.CurrentCulture, Strings.UsePSCredentialTypeErrorSB),
fileName,
requiresTransformationAttribute));
}
}
foreach (var dr in diagnosticRecords)
{
yield return dr;
}
}
private IEnumerable<DiagnosticRecord> GetViolations(
IEnumerable<ParameterAst> parameterAsts,
string errorMessage,
string fileName,
bool requiresTransformationAttribute)
{
foreach (ParameterAst parameter in parameterAsts)
{
if (WrongCredentialUsage(parameter, requiresTransformationAttribute))
{
yield return new DiagnosticRecord(
errorMessage,
parameter.Extent,
GetName(),
DiagnosticSeverity.Warning,
fileName);
}
}
}
private bool WrongCredentialUsage(ParameterAst parameter, bool requiresTransformationAttribute)
{
if (parameter.Name.VariablePath.UserPath.Equals("Credential", StringComparison.OrdinalIgnoreCase))
{
var psCredentialType = parameter.Attributes.FirstOrDefault(paramAttribute => (paramAttribute.TypeName.IsArray && (paramAttribute.TypeName as ArrayTypeName).ElementType.GetReflectionType() == typeof(PSCredential))
|| paramAttribute.TypeName.GetReflectionType() == typeof(PSCredential));
if (psCredentialType == null)
{
return true;
}
if (!requiresTransformationAttribute && psCredentialType != null)
{
return false;
}
var credentialAttribute = parameter.Attributes.FirstOrDefault(
paramAttribute =>
paramAttribute.TypeName.GetReflectionType() == typeof(CredentialAttribute)
|| paramAttribute.TypeName.FullName.Equals(
"System.Management.Automation.Credential",
StringComparison.OrdinalIgnoreCase));
// check that both exists and pscredentialtype comes before credential attribute
if (psCredentialType != null
&& credentialAttribute != null
&& psCredentialType.Extent.EndOffset <= credentialAttribute.Extent.StartOffset)
{
return false;
}
return true;
}
return false;
}
/// <summary>
/// GetName: Retrieves the name of this rule.
/// </summary>
/// <returns>The name of this rule</returns>
public string GetName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UsePSCredentialTypeName);
}
/// <summary>
/// GetCommonName: Retrieves the common name of this rule.
/// </summary>
/// <returns>The common name of this rule</returns>
public string GetCommonName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.UsePSCredentialTypeCommonName);
}
/// <summary>
/// GetDescription: Retrieves the description of this rule.
/// </summary>
/// <returns>The description of this rule</returns>
public string GetDescription()
{
return string.Format(CultureInfo.CurrentCulture, Strings.UsePSCredentialTypeDescription);
}
/// <summary>
/// GetSourceType: Retrieves the type of the rule: builtin, managed or module.
/// </summary>
public SourceType GetSourceType()
{
return SourceType.Builtin;
}
/// <summary>
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
/// </summary>
/// <returns></returns>
public RuleSeverity GetSeverity()
{
return RuleSeverity.Warning;
}
/// <summary>
/// GetSourceName: Retrieves the module/assembly name the rule is from.
/// </summary>
public string GetSourceName()
{
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
}
}
}