Skip to content

Commit 710b43b

Browse files
committed
Merge pull request #390 from PowerShell/HardCodedComputerNameRuleFixBranch
Added localhost exceptions for HardCodedComputerName Rule
2 parents 7b1a13a + 86b936f commit 710b43b

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

Rules/AvoidUsingComputerNameHardcoded.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic;
1616
using System.ComponentModel.Composition;
1717
using System.Globalization;
18+
using System.Collections.Generic;
19+
using System.Linq;
1820

1921
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2022
{
@@ -48,7 +50,23 @@ public override bool ParameterCondition(CommandAst CmdAst, CommandElementAst CeA
4850

4951
if (String.Equals(cmdParamAst.ParameterName, "computername", StringComparison.OrdinalIgnoreCase))
5052
{
51-
return cmdParamAst.Argument is ConstantExpressionAst || GetComputerNameArg(CmdAst, cmdParamAst.Extent.StartOffset) is ConstantExpressionAst;
53+
List<string> localhostRepresentations = new List<string> { "localhost", ".", "::1", "127.0.0.1" };
54+
Ast computerNameArgument = GetComputerNameArg(CmdAst, cmdParamAst.Extent.StartOffset);
55+
56+
if (null != computerNameArgument)
57+
{
58+
if (!localhostRepresentations.Contains(computerNameArgument.Extent.Text.ToLower()))
59+
{
60+
return computerNameArgument is ConstantExpressionAst;
61+
}
62+
63+
return false;
64+
}
65+
66+
if (null != cmdParamAst.Argument && !localhostRepresentations.Contains(cmdParamAst.Argument.ToString().Replace("\"", "").Replace("'", "").ToLower()))
67+
{
68+
return cmdParamAst.Argument is ConstantExpressionAst;
69+
}
5270
}
5371
}
5472

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
Invoke-Command -ComputerName $comp
2-
Invoke-Command -ComputerName $env:COMPUTERNAME
2+
Invoke-Command -ComputerName $env:COMPUTERNAME
3+
Invoke-Command -ComputerName localhost
4+
Invoke-Command -ComputerName .
5+
Invoke-Command -ComputerName ::1
6+
Invoke-Command -ComputerName 127.0.0.1
7+
Invoke-Command -ComputerName:'localhost'
8+
Invoke-Command -ComputerName:"."
9+
Invoke-Command -ComputerName:'::1'
10+
Invoke-Command -ComputerName:"127.0.0.1"

0 commit comments

Comments
 (0)