Skip to content

Merge BugFixes to Master #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Apr 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8ef6b7d
Merge branch 'Bugfixes' into NewRule
yutingc Apr 17, 2015
767a438
Merge branch 'NewRule' into BugFixes
yutingc Apr 17, 2015
1eee6c5
Fix AvoidUsingInternalURLs throw warnings at SDDL
yutingc Apr 20, 2015
52f882c
Change count of ":" to more than 3
yutingc Apr 20, 2015
eb7c565
Change column count to 3 and 4.
yutingc Apr 20, 2015
9cb7241
Merge pull request #46 from PowerShell/issueFi
yutingc Apr 20, 2015
f0e44b2
Add wildcard support when include/exclude rules
yutingc Apr 20, 2015
0703994
Add tests and correct typo
yutingc Apr 21, 2015
42b28c8
Change the regular expression.
yutingc Apr 21, 2015
2545bfa
Merge pull request #47 from PowerShell/wildcard
yutingc Apr 21, 2015
6e831b1
Rule for presence of deprecated WMIObject cmdlets
raghushantha Apr 21, 2015
3ee3ddd
Add rule severity
yutingc Apr 22, 2015
a509548
Add RuleSeverity for AvoidUsingWMIObject rule
yutingc Apr 22, 2015
7a7b4ca
Added test for -Severity parameter in Get-ScriptAnalyzerRule
yutingc Apr 22, 2015
9f70999
Merge pull request #52 from PowerShell/ruleseverity
yutingc Apr 22, 2015
027fa41
Added modified project file
yutingc Apr 22, 2015
1bf5b9c
Merge pull request #54 from PowerShell/ruleseverity
yutingc Apr 22, 2015
5705d81
Fix test failure issue
yutingc Apr 22, 2015
61e0494
Update README.md
yutingc Apr 22, 2015
bdaa53d
Update README.md
yutingc Apr 22, 2015
e10ff30
Merge pull request #57 from PowerShell/TestFix
yutingc Apr 23, 2015
92abd36
Fix VariableAnalysis error with outer scope
Apr 23, 2015
11f8a30
Remove AstVisitor2 from SkipTypeDefinition
Apr 23, 2015
d728a50
Merge pull request #60 from PowerShell/FixVariableAnalysisScope
Apr 23, 2015
62ba3c0
Merge pull request #61 from PowerShell/RemoveAstVisitor2
Apr 23, 2015
a01b048
Change property display name to be the same as property name
yutingc Apr 23, 2015
ad6350d
Merge pull request #63 from PowerShell/propertyName
yutingc Apr 24, 2015
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
31 changes: 25 additions & 6 deletions Engine/Commands/GetScriptAnalyzerRuleCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@
// THE SOFTWARE.
//

using Microsoft.PowerShell.Commands;
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Management.Automation;
using System.Resources;
using System.Threading;
using System.Reflection;

namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Commands
{
Expand Down Expand Up @@ -50,12 +47,27 @@ public string[] CustomizedRulePath
[Parameter(Mandatory = false)]
[ValidateNotNullOrEmpty]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public string[] Name
public string[] RuleName
{
get { return name; }
set { name = value; }
}
private string[] name;

/// <summary>
/// Severity: Array of the severity types to be enabled.
/// </summary>
/// </summary>
[ValidateSet("Warning", "Error", "Information", IgnoreCase = true)]
[Parameter(Mandatory = false)]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public string[] Severity
{
get { return severity; }
set { severity = value; }
}
private string[] severity;

#endregion Parameters

#region Private Members
Expand Down Expand Up @@ -128,9 +140,16 @@ protected override void ProcessRecord()
}
else
{
if (severity != null)
{
var ruleSeverity = severity.Select(item => Enum.Parse(typeof (RuleSeverity), item));
rules = rules.Where(item => ruleSeverity.Contains(item.GetSeverity())).ToList();
}

foreach (IRule rule in rules)
{
WriteObject(new RuleInfo(rule.GetName(), rule.GetCommonName(), rule.GetDescription(), rule.GetSourceType(), rule.GetSourceName()));
WriteObject(new RuleInfo(rule.GetName(), rule.GetCommonName(), rule.GetDescription(),
rule.GetSourceType(), rule.GetSourceName(), rule.GetSeverity()));
}
}
}
Expand Down
134 changes: 118 additions & 16 deletions Engine/Commands/InvokeScriptAnalyzerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// THE SOFTWARE.
//

using System.Text.RegularExpressions;
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -272,7 +273,28 @@ private void AnalyzeFile(string filePath)
IEnumerable<Ast> funcDefAsts;

// Use a List of KVP rather than dictionary, since for a script containing inline functions with same signature, keys clash
List<KeyValuePair<CommandInfo, IScriptExtent>> cmdInfoTable = new List<KeyValuePair<CommandInfo, IScriptExtent>>();
List<KeyValuePair<CommandInfo, IScriptExtent>> cmdInfoTable = new List<KeyValuePair<CommandInfo, IScriptExtent>>();

//Check wild card input for the Include/ExcludeRules and create regex match patterns
List<Regex> includeRegexList = new List<Regex>();
List<Regex> excludeRegexList = new List<Regex>();
if (includeRule != null)
{
foreach (string rule in includeRule)
{
Regex includeRegex = new Regex(String.Format("^{0}$", Regex.Escape(rule).Replace(@"\*", ".*")), RegexOptions.IgnoreCase);
includeRegexList.Add(includeRegex);
}
}
if (excludeRule != null)
{
foreach (string rule in excludeRule)
{
Regex excludeRegex = new Regex(String.Format("^{0}$", Regex.Escape(rule).Replace(@"\*", ".*")), RegexOptions.IgnoreCase);
excludeRegexList.Add(excludeRegex);
}
}


//Parse the file
if (File.Exists(filePath))
Expand Down Expand Up @@ -316,12 +338,30 @@ private void AnalyzeFile(string filePath)
#region Run ScriptRules
//Trim down to the leaf element of the filePath and pass it to Diagnostic Record
string fileName = System.IO.Path.GetFileName(filePath);

if (ScriptAnalyzer.Instance.ScriptRules != null)
{
foreach (IScriptRule scriptRule in ScriptAnalyzer.Instance.ScriptRules)
{
if ((includeRule == null || includeRule.Contains(scriptRule.GetName(), StringComparer.OrdinalIgnoreCase)) &&
(excludeRule == null || !excludeRule.Contains(scriptRule.GetName(), StringComparer.OrdinalIgnoreCase)))
bool includeRegexMatch = false;
bool excludeRegexMatch = false;
foreach (Regex include in includeRegexList)
{
if (include.IsMatch(scriptRule.GetName()))
{
includeRegexMatch = true;
break;
}
}
foreach (Regex exclude in excludeRegexList)
{
if (exclude.IsMatch(scriptRule.GetName()))
{
excludeRegexMatch = true;
break;
}
}
if ((includeRule == null || includeRegexMatch) && (excludeRule == null || !excludeRegexMatch))
{
WriteVerbose(string.Format(CultureInfo.CurrentCulture, Strings.VerboseRunningMessage, scriptRule.GetName()));

Expand All @@ -334,7 +374,6 @@ private void AnalyzeFile(string filePath)
catch (Exception scriptRuleException)
{
WriteError(new ErrorRecord(scriptRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, filePath));
continue;
}
}
}
Expand Down Expand Up @@ -379,8 +418,25 @@ private void AnalyzeFile(string filePath)
{
foreach (ICommandRule commandRule in ScriptAnalyzer.Instance.CommandRules)
{
if ((includeRule == null || includeRule.Contains(commandRule.GetName(), StringComparer.OrdinalIgnoreCase)) &&
(excludeRule == null || !excludeRule.Contains(commandRule.GetName(), StringComparer.OrdinalIgnoreCase)))
bool includeRegexMatch = false;
bool excludeRegexMatch = false;
foreach (Regex include in includeRegexList)
{
if (include.IsMatch(commandRule.GetName()))
{
includeRegexMatch = true;
break;
}
}
foreach (Regex exclude in excludeRegexList)
{
if (exclude.IsMatch(commandRule.GetName()))
{
excludeRegexMatch = true;
break;
}
}
if ((includeRule == null || includeRegexMatch) && (excludeRule == null || !excludeRegexMatch))
{
foreach (KeyValuePair<CommandInfo, IScriptExtent> commandInfo in cmdInfoTable)
{
Expand All @@ -395,7 +451,6 @@ private void AnalyzeFile(string filePath)
catch (Exception commandRuleException)
{
WriteError(new ErrorRecord(commandRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, fileName));
continue;
}
}
}
Expand All @@ -410,8 +465,25 @@ private void AnalyzeFile(string filePath)
{
foreach (ITokenRule tokenRule in ScriptAnalyzer.Instance.TokenRules)
{
if ((includeRule == null || includeRule.Contains(tokenRule.GetName(), StringComparer.OrdinalIgnoreCase)) &&
(excludeRule == null || !excludeRule.Contains(tokenRule.GetName(), StringComparer.OrdinalIgnoreCase)))
bool includeRegexMatch = false;
bool excludeRegexMatch = false;
foreach (Regex include in includeRegexList)
{
if (include.IsMatch(tokenRule.GetName()))
{
includeRegexMatch = true;
break;
}
}
foreach (Regex exclude in excludeRegexList)
{
if (exclude.IsMatch(tokenRule.GetName()))
{
excludeRegexMatch = true;
break;
}
}
if ((includeRule == null || includeRegexMatch) && (excludeRule == null || !excludeRegexMatch))
{
WriteVerbose(string.Format(CultureInfo.CurrentCulture, Strings.VerboseRunningMessage, tokenRule.GetName()));

Expand All @@ -424,7 +496,6 @@ private void AnalyzeFile(string filePath)
catch (Exception tokenRuleException)
{
WriteError(new ErrorRecord(tokenRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, fileName));
continue;
}
}
}
Expand All @@ -438,8 +509,25 @@ private void AnalyzeFile(string filePath)
// Run DSC Class rule
foreach (IDSCResourceRule dscResourceRule in ScriptAnalyzer.Instance.DSCResourceRules)
{
if ((includeRule == null || includeRule.Contains(dscResourceRule.GetName(), StringComparer.OrdinalIgnoreCase)) &&
(excludeRule == null || !excludeRule.Contains(dscResourceRule.GetName(), StringComparer.OrdinalIgnoreCase)))
bool includeRegexMatch = false;
bool excludeRegexMatch = false;
foreach (Regex include in includeRegexList)
{
if (include.IsMatch(dscResourceRule.GetName()))
{
includeRegexMatch = true;
break;
}
}
foreach (Regex exclude in excludeRegexList)
{
if (exclude.IsMatch(dscResourceRule.GetName()))
{
excludeRegexMatch = true;
break;
}
}
if ((includeRule == null || includeRegexMatch) && (excludeRule == null || excludeRegexMatch))
{
WriteVerbose(string.Format(CultureInfo.CurrentCulture, Strings.VerboseRunningMessage, dscResourceRule.GetName()));

Expand All @@ -452,7 +540,6 @@ private void AnalyzeFile(string filePath)
catch (Exception dscResourceRuleException)
{
WriteError(new ErrorRecord(dscResourceRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, filePath));
continue;
}
}
}
Expand Down Expand Up @@ -480,8 +567,24 @@ private void AnalyzeFile(string filePath)
// Run all DSC Rules
foreach (IDSCResourceRule dscResourceRule in ScriptAnalyzer.Instance.DSCResourceRules)
{
if ((includeRule == null || includeRule.Contains(dscResourceRule.GetName(), StringComparer.OrdinalIgnoreCase)) &&
(excludeRule == null || !excludeRule.Contains(dscResourceRule.GetName(), StringComparer.OrdinalIgnoreCase)))
bool includeRegexMatch = false;
bool excludeRegexMatch = false;
foreach (Regex include in includeRegexList)
{
if (include.IsMatch(dscResourceRule.GetName()))
{
includeRegexMatch = true;
break;
}
}
foreach (Regex exclude in excludeRegexList)
{
if (exclude.IsMatch(dscResourceRule.GetName()))
{
excludeRegexMatch = true;
}
}
if ((includeRule == null || includeRegexMatch) && (excludeRule == null || !excludeRegexMatch))
{
WriteVerbose(string.Format(CultureInfo.CurrentCulture, Strings.VerboseRunningMessage, dscResourceRule.GetName()));

Expand All @@ -494,7 +597,6 @@ private void AnalyzeFile(string filePath)
catch (Exception dscResourceRuleException)
{
WriteError(new ErrorRecord(dscResourceRuleException, Strings.RuleError, ErrorCategory.InvalidOperation, filePath));
continue;
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions Engine/Generic/AvoidCmdletGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,11 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
/// </summary>
/// <returns>The source type of the rule.</returns>
public abstract SourceType GetSourceType();

/// <summary>
/// GetSeverity: Retrieves the severity of the rule: error, warning of information.
/// </summary>
/// <returns></returns>
public abstract RuleSeverity GetSeverity();
}
}
2 changes: 2 additions & 0 deletions Engine/Generic/AvoidParameterGeneric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,7 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
/// </summary>
/// <returns>The source type of the rule.</returns>
public abstract SourceType GetSourceType();

public abstract RuleSeverity GetSeverity();
}
}
6 changes: 6 additions & 0 deletions Engine/Generic/ExternalRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public SourceType GetSourceType()
return SourceType.Module;
}

//Set the community rule level as warning as the current implementation does not require user to specify rule severity when defining their functions in PS scripts
public RuleSeverity GetSeverity()
{
return RuleSeverity.Warning;
}

public string GetSourceName()
{
return this.srcName;
Expand Down
13 changes: 7 additions & 6 deletions Engine/Generic/IRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
// THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Generic
{
/// <summary>
Expand Down Expand Up @@ -52,5 +46,12 @@ public interface IRule
/// </summary>
/// <returns>The source type of the rule.</returns>
SourceType GetSourceType();

/// <summary>
/// GetSeverity: Retrieves severity of the rule.
/// </summary>
/// <returns></returns>
RuleSeverity GetSeverity();

}
}
18 changes: 15 additions & 3 deletions Engine/Generic/RuleInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ public class RuleInfo
private string description;
private SourceType sourceType;
private string sourceName;
private RuleSeverity ruleSeverity;

/// <summary>
/// Name: The name of the rule.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public string Name
public string RuleName
{
get { return name; }
private set { name = value; }
Expand Down Expand Up @@ -81,6 +82,16 @@ public string SourceName
private set { sourceName = value; }
}

/// <summary>
/// Severity : The severity of the rule violation.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public RuleSeverity Severity
{
get { return ruleSeverity; }
private set { ruleSeverity = value; }
}

/// <summary>
/// Constructor for a RuleInfo.
/// </summary>
Expand All @@ -89,13 +100,14 @@ public string SourceName
/// <param name="description">Description of the rule.</param>
/// <param name="sourceType">Source type of the rule.</param>
/// <param name="sourceName">Source name of the rule.</param>
public RuleInfo(string name, string commonName, string description, SourceType sourceType, string sourceName)
public RuleInfo(string name, string commonName, string description, SourceType sourceType, string sourceName, RuleSeverity severity)
{
Name = name;
RuleName = name;
CommonName = commonName;
Description = description;
SourceType = sourceType;
SourceName = sourceName;
Severity = severity;
}
}
}
Loading