-
Notifications
You must be signed in to change notification settings - Fork 394
Added customized rule documentation #94
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0c82a4a
Add documentation for customized rules
yutingc 296f091
Change code formatting
yutingc d2057f1
Modified some wording
yutingc 0d37d1f
Update the list order
yutingc 4267cae
Ordered list
yutingc b6c17b3
Update CustomizedRuleDocumentation.md
yutingc 11ced68
Update CustomizedRuleDocumentation.md
yutingc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
##Documentation for Customized Rules in PowerShell Scripts | ||
PSScriptAnalyzer uses MEF(Managed Extensibility Framework) to import all rules defined in the assembly. It can also consume rules written in PowerShell scripts. When calling Invoke-ScriptAnalyzer, users can pass customized rules using parameter -CustomizedRulePath to apply rule checkings on the scripts. | ||
|
||
This documentation serves as a basic guideline on how to define customized rules. | ||
|
||
###Basics | ||
- Functions should have comment-based help. Make sure .DESCRIPTION field is there, as it will be consumed as rule description for the customized rule. | ||
``` | ||
<# | ||
.SYNOPSIS | ||
Name of your rule. | ||
.DESCRIPTION | ||
This would be the description of your rule. Please refer to Rule Documentation for consistent rule messages. | ||
.EXAMPLE | ||
.INPUTS | ||
.OUTPUTS | ||
.NOTES | ||
#> | ||
``` | ||
|
||
- Output type should be DiagnosticRecord: | ||
``` | ||
[OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] | ||
``` | ||
|
||
- Make sure each function takes either a Token or an Ast as a parameter | ||
``` | ||
Param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.Management.Automation.Language.ScriptBlockAst] | ||
$testAst | ||
) | ||
``` | ||
|
||
- DiagnosticRecord should have four properties: Message, Extent, RuleName and Severity | ||
``` | ||
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]]@{"Message" = "This is a sample rule"; | ||
"Extent" = $ast.Extent; | ||
"RuleName" = $PSCmdlet.MyInvocation.InvocationName; | ||
"Severity" = "Warning"} | ||
``` | ||
|
||
- Make sure you export the function(s) at the end of the script using Export-ModuleMember | ||
``` | ||
Export-ModuleMember -Function (FunctionName) | ||
``` | ||
|
||
###Example | ||
``` | ||
<# | ||
.SYNOPSIS | ||
Uses #Requires -RunAsAdministrator instead of your own methods. | ||
.DESCRIPTION | ||
The #Requires statement prevents a script from running unless the Windows PowerShell version, modules, snap-ins, and module and snap-in version prerequisites are met. | ||
From Windows PowerShell 4.0, the #Requires statement let script developers require that sessions be run with elevated user rights (run as Administrator). | ||
Script developers does not need to write their own methods any more. | ||
To fix a violation of this rule, please consider to use #Requires -RunAsAdministrator instead of your own methods. | ||
.EXAMPLE | ||
Measure-RequiresRunAsAdministrator -ScriptBlockAst $ScriptBlockAst | ||
.INPUTS | ||
[System.Management.Automation.Language.ScriptBlockAst] | ||
.OUTPUTS | ||
[Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]] | ||
.NOTES | ||
None | ||
#> | ||
function Measure-RequiresRunAsAdministrator | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] | ||
Param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.Management.Automation.Language.ScriptBlockAst] | ||
$ScriptBlockAst | ||
) | ||
|
||
Process | ||
{ | ||
$results = @() | ||
try | ||
{ | ||
#region Define predicates to find ASTs. | ||
# Finds specific method, IsInRole. | ||
[ScriptBlock]$predicate1 = { | ||
param ([System.Management.Automation.Language.Ast]$Ast) | ||
[bool]$returnValue = $false | ||
if ($Ast -is [System.Management.Automation.Language.MemberExpressionAst]) | ||
{ | ||
[System.Management.Automation.Language.MemberExpressionAst]$meAst = $ast; | ||
if ($meAst.Member -is [System.Management.Automation.Language.StringConstantExpressionAst]) | ||
{ | ||
[System.Management.Automation.Language.StringConstantExpressionAst]$sceAst = $meAst.Member; | ||
if ($sceAst.Value -eq "isinrole") | ||
{ | ||
$returnValue = $true; | ||
} | ||
} | ||
} | ||
return $returnValue | ||
} | ||
|
||
# Finds specific value, [system.security.principal.windowsbuiltinrole]::administrator. | ||
[ScriptBlock]$predicate2 = { | ||
param ([System.Management.Automation.Language.Ast]$Ast) | ||
[bool]$returnValue = $false | ||
if ($ast -is [System.Management.Automation.Language.AssignmentStatementAst]) | ||
{ | ||
[System.Management.Automation.Language.AssignmentStatementAst]$asAst = $Ast; | ||
if ($asAst.Right.ToString().ToLower() -eq "[system.security.principal.windowsbuiltinrole]::administrator") | ||
{ | ||
$returnValue = $true | ||
} | ||
} | ||
return $returnValue | ||
} | ||
#endregion | ||
#region Finds ASTs that match the predicates. | ||
|
||
[System.Management.Automation.Language.Ast[]]$methodAst = $ScriptBlockAst.FindAll($predicate1, $true) | ||
[System.Management.Automation.Language.Ast[]]$assignmentAst = $ScriptBlockAst.FindAll($predicate2, $true) | ||
if ($null -ne $ScriptBlockAst.ScriptRequirements) | ||
{ | ||
if ((!$ScriptBlockAst.ScriptRequirements.IsElevationRequired) -and | ||
($methodAst.Count -ne 0) -and ($assignmentAst.Count -ne 0)) | ||
{ | ||
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureRequiresRunAsAdministrator; | ||
"Extent" = $assignmentAst.Extent; | ||
"RuleName" = $PSCmdlet.MyInvocation.InvocationName; | ||
"Severity" = "Information"} | ||
$results += $result | ||
} | ||
} | ||
else | ||
{ | ||
if (($methodAst.Count -ne 0) -and ($assignmentAst.Count -ne 0)) | ||
{ | ||
$result = [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{"Message" = $Messages.MeasureRequiresRunAsAdministrator; | ||
"Extent" = $assignmentAst.Extent; | ||
"RuleName" = $PSCmdlet.MyInvocation.InvocationName; | ||
"Severity" = "Information"} | ||
$results += $result | ||
} | ||
} | ||
return $results | ||
#endregion | ||
} | ||
catch | ||
{ | ||
$PSCmdlet.ThrowTerminatingError($PSItem) | ||
} | ||
} | ||
} | ||
``` | ||
More examples can be found in *Tests\Engine\CommunityRules* | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we must just call these as ScriptRules (not communityrules)
We can do this in the next commit. For now, changes here are good