Skip to content

Commit 776f40d

Browse files
author
Quoc Truong
committed
Fix a bug where avoidusernameandpasswordparam is too noisy
1 parent 5dfe070 commit 776f40d

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

Rules/AvoidUserNameAndPasswordParams.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,21 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
6161

6262
String paramName = paramAst.Name.VariablePath.ToString();
6363

64-
// if this is pscredential type with credential attribute where pscredential type comes first
65-
if (psCredentialType != null && credentialAttribute != null && psCredentialType.Extent.EndOffset < credentialAttribute.Extent.StartOffset)
66-
{
67-
continue;
68-
}
69-
64+
// check that the type is securestring
65+
var secureStringType = paramAst.Attributes.FirstOrDefault(paramAttribute =>
66+
(paramAttribute.TypeName.IsArray && (paramAttribute.TypeName as ArrayTypeName).ElementType.GetReflectionType() == typeof (System.Security.SecureString))
67+
|| paramAttribute.TypeName.GetReflectionType() == typeof(System.Security.SecureString));
68+
7069
foreach (String password in passwords)
7170
{
7271
if (paramName.IndexOf(password, StringComparison.OrdinalIgnoreCase) != -1)
7372
{
73+
// if this is a secure string, pscredential or credential attribute, don't count
74+
if (secureStringType != null || credentialAttribute != null || psCredentialType != null)
75+
{
76+
continue;
77+
}
78+
7479
hasPwd = true;
7580
break;
7681
}

Tests/Rules/AvoidUserNameAndPasswordParams.tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
Import-Module PSScriptAnalyzer
22

3-
$violationMessage = "Function 'Verb-Noun' has both username and password parameters. A credential parameter of type PSCredential with a CredentialAttribute where PSCredential comes before CredentialAttribute should be used."
3+
$violationMessage = "Function 'TestFunction' has both username and password parameters. A credential parameter of type PSCredential with a CredentialAttribute where PSCredential comes before CredentialAttribute should be used."
44
$violationName = "PSAvoidUsingUserNameAndPasswordParams"
55
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
66
$violations = Invoke-ScriptAnalyzer $directory\AvoidUserNameAndPasswordParams.ps1 | Where-Object {$_.RuleName -eq $violationName}
77
$noViolations = Invoke-ScriptAnalyzer $directory\AvoidUserNameAndPasswordParamsNoViolations.ps1 | Where-Object {$_.RuleName -eq $violationName}
88

99
Describe "AvoidUserNameAndPasswordParams" {
1010
Context "When there are violations" {
11-
It "has 3 avoid username and password parameter violations" {
12-
$violations.Count | Should Be 3
11+
It "has 1 avoid username and password parameter violations" {
12+
$violations.Count | Should Be 1
1313
}
1414

1515
It "has the correct violation message" {

0 commit comments

Comments
 (0)