Skip to content

Commit 8a02fa7

Browse files
author
Kapil Borle
committed
Add correction extent to AvoidUsingPlainTextForPassword rule
1 parent b0a7c57 commit 8a02fa7

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

Rules/AvoidUsingPlainTextForPassword.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,30 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
6060
{
6161
yield return new DiagnosticRecord(
6262
String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPlainTextForPasswordError, paramAst.Name),
63-
paramAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
63+
paramAst.Extent,
64+
GetName(),
65+
DiagnosticSeverity.Warning,
66+
fileName,
67+
suggestedCorrections: GetCorrectionExtent(paramAst));
6468
}
6569
}
6670
}
6771

72+
private List<CorrectionExtent> GetCorrectionExtent(ParameterAst paramAst)
73+
{
74+
IScriptExtent ext = paramAst.Extent;
75+
var corrections = new List<CorrectionExtent>();
76+
string correctionText = string.Format("{0} {1}", "[SecureString]", paramAst.Name.Extent.Text);
77+
corrections.Add(new CorrectionExtent(
78+
ext.StartLineNumber,
79+
ext.EndLineNumber,
80+
ext.StartColumnNumber,
81+
ext.EndColumnNumber,
82+
correctionText,
83+
ext.File));
84+
return corrections;
85+
}
86+
6887
/// <summary>
6988
/// GetName: Retrieves the name of this rule.
7089
/// </summary>

Tests/Rules/AvoidUsingPlainTextForPassword.tests.ps1

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
$violationMessage = [regex]::Escape("Parameter '`$password' should use SecureString, otherwise this will expose sensitive information. See ConvertTo-SecureString for more information.")
44
$violationName = "PSAvoidUsingPlainTextForPassword"
55
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
6-
$violations = Invoke-ScriptAnalyzer $directory\AvoidUsingPlainTextForPassword.ps1 | Where-Object {$_.RuleName -eq $violationName}
6+
$violationsFilepath = Join-Path $directory 'AvoidUsingPlainTextForPassword.ps1'
7+
$violations = Invoke-ScriptAnalyzer $violationsFilepath | Where-Object {$_.RuleName -eq $violationName}
78
$noViolations = Invoke-ScriptAnalyzer $directory\AvoidUsingPlainTextForPasswordNoViolations.ps1 | Where-Object {$_.RuleName -eq $violationName}
89

910
Describe "AvoidUsingPlainTextForPassword" {
@@ -12,6 +13,22 @@ Describe "AvoidUsingPlainTextForPassword" {
1213
$violations.Count | Should Be 4
1314
}
1415

16+
It "suggests corrections" {
17+
Import-Module .\PSScriptAnalyzerTestHelper.psm1
18+
Function Test-Extent($idx, $violationText, $correctionText)
19+
{
20+
$violation = $violations[$idx]
21+
$violation.SuggestedCorrections.Count | Should Be 1
22+
Get-ExtentText $violation.SuggestedCorrections[0] $violationsFilepath | Should Be $violationText
23+
$violation.SuggestedCorrections[0].Text | Should Be $correctionText
24+
}
25+
26+
Test-Extent 0 '$passphrases' '[SecureString] $passphrases'
27+
Test-Extent 1 '$passwordparam' '[SecureString] $passwordparam'
28+
Test-Extent 2 '$credential' '[SecureString] $credential'
29+
Test-Extent 3 '$password' '[SecureString] $password'
30+
}
31+
1532
It "has the correct violation message" {
1633
$violations[3].Message | Should Match $violationMessage
1734
}

0 commit comments

Comments
 (0)