Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions Rules/AvoidUserNameAndPasswordParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)

List<String> passwords = new List<String>() {"Password", "Passphrase"};
List<String> usernames = new List<String>() { "Username", "User"};
Type[] typeWhiteList = {typeof(CredentialAttribute),
typeof(PSCredential),
typeof(System.Security.SecureString),
typeof(SwitchParameter),
typeof(Boolean)};

foreach (FunctionDefinitionAst funcAst in functionAsts)
{
Expand All @@ -50,32 +55,18 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)

// Finds all ParamAsts.
IEnumerable<Ast> paramAsts = funcAst.FindAll(testAst => testAst is ParameterAst, true);

ParameterAst usernameAst = null;
ParameterAst passwordAst = null;
// Iterates all ParamAsts and check if their names are on the list.
foreach (ParameterAst paramAst in paramAsts)
{
// this will be null if there is no [pscredential] attached to the parameter
var psCredentialType = paramAst.Attributes.FirstOrDefault(paramAttribute =>
(paramAttribute.TypeName.IsArray && (paramAttribute.TypeName as ArrayTypeName).ElementType.GetReflectionType() == typeof(PSCredential))
|| paramAttribute.TypeName.GetReflectionType() == typeof(PSCredential));

// this will be null if there are no [credential()] attribute attached
var credentialAttribute = paramAst.Attributes.FirstOrDefault(paramAttribute => paramAttribute.TypeName.GetReflectionType() == typeof(CredentialAttribute));

// this will be null if there are no [securestring] attached to the parameter
var secureStringType = paramAst.Attributes.FirstOrDefault(paramAttribute =>
(paramAttribute.TypeName.IsArray && (paramAttribute.TypeName as ArrayTypeName).ElementType.GetReflectionType() == typeof (System.Security.SecureString))
|| paramAttribute.TypeName.GetReflectionType() == typeof(System.Security.SecureString));

var attributes = typeWhiteList.Select(x => GetAttributeOfType(paramAst.Attributes, x));
String paramName = paramAst.Name.VariablePath.ToString();
foreach (String password in passwords)
{
if (paramName.IndexOf(password, StringComparison.OrdinalIgnoreCase) != -1)
{
// if this is a secure string, pscredential or credential attribute, don't count
if (secureStringType != null || credentialAttribute != null || psCredentialType != null)
if (attributes.Any(x => x != null))
{
continue;
}
Expand Down Expand Up @@ -106,6 +97,20 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
}
}

private AttributeBaseAst GetAttributeOfType(IEnumerable<AttributeBaseAst> attributeAsts, Type type)
{
return attributeAsts.FirstOrDefault(x => IsAttributeOfType(x, type));
}

private bool IsAttributeOfType(AttributeBaseAst attributeAst, Type type)
{
var arrayType = attributeAst.TypeName as ArrayTypeName;
if (arrayType != null)
{
return arrayType.ElementType.GetReflectionType() == type;
}
return attributeAst.TypeName.GetReflectionType() == type;
}
/// <summary>
/// Returns script extent of username and password parameters
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Tests/Engine/RuleSuppression.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function SuppressUserAndPwdRule()
param
(
[System.String] $username,
[System.Boolean] $password
[System.String] $password
)
}
'@
Expand Down
16 changes: 16 additions & 0 deletions Tests/Rules/AvoidUserNameAndPasswordParamsNoViolations.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,19 @@ function MyFunction3
$Password
)
}

function MyFunction3
{
param(
[string] $Username,
[switch] $HidePassword
)
}

function MyFunction4
{
param(
[string] $Username,
[bool] $HidePassword
)
}