Skip to content

Commit 371617f

Browse files
author
Kapil Borle
authored
Merge pull request #570 from PowerShell/kapilmb/FixRuleSuppressionPlainTextPwd
Fix rule suppression for PSAvoidUsingPlainTextForPassword rule
2 parents bc9a9eb + 69f9194 commit 371617f

File tree

3 files changed

+126
-5
lines changed

3 files changed

+126
-5
lines changed

Rules/AvoidUsingPlainTextForPassword.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
6565
GetName(),
6666
DiagnosticSeverity.Warning,
6767
fileName,
68+
paramName,
6869
suggestedCorrections: fileName == null ? null : GetCorrectionExtent(paramAst));
6970
}
7071
}

Tests/Engine/RuleSuppression.tests.ps1

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,25 @@ Describe "RuleSuppressionWithoutScope" {
8181
$suppression = $violationsUsingScriptDefinition | Where-Object {$_.RuleName -eq "PSAvoidDefaultValueForMandatoryParameter" }
8282
$suppression.Count | Should Be 1
8383
}
84+
85+
It "Suppresses PSAvoidUsingPlainTextForPassword violation for the given ID" {
86+
$ruleSuppressionIdAvoidPlainTextPassword = @'
87+
function SuppressPwdParam()
88+
{
89+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "password1")]
90+
param(
91+
[string] $password1,
92+
[string] $password2
93+
)
94+
}
95+
'@
96+
Invoke-ScriptAnalyzer `
97+
-ScriptDefinition $ruleSuppressionIdAvoidPlainTextPassword `
98+
-IncludeRule "PSAvoidUsingPlainTextForPassword" `
99+
-OutVariable ruleViolations `
100+
-SuppressedOnly
101+
$ruleViolations.Count | Should Be 1
102+
}
84103
}
85104

86105
if (($PSVersionTable.PSVersion -ge [Version]'5.0'))

build.ps1

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
[CmdletBinding()]
22
param(
3+
4+
[Parameter(ParameterSetName='Build')]
35
[ValidateSet('PSV3 Debug','PSV3 Release','Debug','Release')]
46
[string] $Configuration = 'Debug',
57

8+
[Parameter(ParameterSetName='Build')]
69
[switch] $BuildSolution = $false,
710

11+
[Parameter(ParameterSetName='Build')]
812
[switch] $CleanSolution = $false,
913

14+
[Parameter(ParameterSetName='Build')]
1015
[switch] $BuildDocs = $false,
1116

17+
[Parameter(ParameterSetName='Build')]
1218
[switch] $CleanOutput = $false,
1319

14-
[switch] $Install = $false
20+
[Parameter(ParameterSetName='Build')]
21+
[switch] $Install = $false,
22+
23+
[Parameter(ParameterSetName='Build')]
24+
[switch] $Uninstall = $false,
25+
26+
[Parameter(ParameterSetName='Test')]
27+
[switch] $Test = $false,
28+
29+
[Parameter(ParameterSetName='Test')]
30+
[switch] $Engine = $false,
31+
32+
[Parameter(ParameterSetName='Test')]
33+
[switch] $Rules = $false,
34+
35+
[Parameter(ParameterSetName='Test')]
36+
[switch] $RunInDifferentProcess = $false
1537
)
1638

1739
# Some cmdlets like copy-item do not respond to the $verbosepreference variable
@@ -88,16 +110,95 @@ if ($BuildDocs)
88110
New-ExternalHelp -Path $markdownDocsPath -OutputPath $outputDocsPath -Force -Verbose:$verbosity
89111
}
90112

113+
114+
$moduleRootPath = Join-Path (Split-Path $profile) 'Modules'
115+
$modulePSSAPath = Join-Path $moduleRootPath 'PSScriptAnalyzer'
91116
if ($Install)
92117
{
93-
$modulePath = Join-Path (Split-Path $profile) 'Modules'
94-
if (-not (Test-Path $modulePath))
118+
if (-not (Test-Path $moduleRootPath))
95119
{
96-
New-Item -Path $modulePath -ItemType Directory -Force -Verbose:$verbosity
120+
New-Item -Path $moduleRootPath -ItemType Directory -Force -Verbose:$verbosity
97121
}
98122
if (-not (Test-Path -Path $destinationPath))
99123
{
100124
throw "Please build the module first."
101125
}
102-
Copy-Item -Path $destinationPath -Destination $modulePath -Recurse -Verbose:$verbosity
126+
Copy-Item -Path $destinationPath -Destination $modulePSSAPath -Recurse -Verbose:$verbosity
127+
}
128+
129+
if ($Test)
130+
{
131+
Import-Module PSScriptAnalyzer -ErrorAction Stop
132+
Import-Module -Name Pester -RequiredVersion 3.4.0 -ErrorAction Stop
133+
134+
135+
Function GetTestRunnerScriptContent($testPath)
136+
{
137+
$x = @"
138+
cd $testPath
139+
Invoke-Pester
140+
"@
141+
return $x
142+
}
143+
144+
Function CreateTestRunnerScript($testPath)
145+
{
146+
$tmptmpFilePath = [System.IO.Path]::GetTempFileName()
147+
$tmpFilePath = $tmptmpFilePath + '.ps1'
148+
Move-Item $tmptmpFilePath $tmpFilePath -Verbose:$verbosity
149+
$content = GetTestRunnerScriptContent $testPath
150+
Set-Content -Path $tmpFilePath -Value $content -Verbose:$verbosity
151+
return $tmpFilePath
152+
}
153+
154+
Function GetTestPath($TestType)
155+
{
156+
if ($TestType -eq "engine")
157+
{
158+
$testPath = Join-Path $projectRoot "Tests/Engine"
159+
}
160+
else
161+
{
162+
$testPath = Join-Path $projectRoot "Tests/Rules"
163+
}
164+
return $testPath
165+
}
166+
167+
Function RunTest($TestType, [Boolean] $DifferentProcess)
168+
{
169+
$testPath = GetTestPath($TestType)
170+
if ($DifferentProcess)
171+
{
172+
$testScriptFilePath = CreateTestRunnerScript $testPath
173+
Start-Process powershell -ArgumentList "-NoExit","-File $testScriptFilePath" -Verb runas
174+
# clean up the test file
175+
}
176+
else
177+
{
178+
try
179+
{
180+
Push-Location .
181+
([scriptblock]::Create((GetTestRunnerScriptContent $testPath))).Invoke()
182+
}
183+
finally
184+
{
185+
Pop-Location
186+
187+
}
188+
}
189+
}
190+
191+
if ($Engine -or (-not ($Engine -or $Rules)))
192+
{
193+
RunTest 'engine' $RunInDifferentProcess
194+
}
195+
if ($Rules -or (-not ($Engine -or $Rules)))
196+
{
197+
RunTest 'rules' $RunInDifferentProcess
198+
}
103199
}
200+
201+
if ($Uninstall)
202+
{
203+
Remove-Item -Path $modulePSSAPath -Force -Verbose:$verbosity -Recurse
204+
}

0 commit comments

Comments
 (0)