-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
OSOE-472: Prevent the usage of PowerShell aliases
- Loading branch information
Showing
10 changed files
with
149 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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,38 @@ | ||
name: Static Code Analysis | ||
|
||
# Runs for PRs opened for any branch, and pushes to the dev branch. | ||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- dev | ||
|
||
defaults: | ||
run: | ||
shell: pwsh | ||
|
||
jobs: | ||
powershell-static-code-analysis: | ||
name: PowerShell Static Code Analysis | ||
strategy: | ||
matrix: | ||
machine-type: [ubuntu-latest, windows-latest] | ||
runs-on: ${{ matrix.machine-type }} | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: 'recursive' | ||
|
||
- name: Windows PowerShell Static Code Analysis | ||
run: | | ||
if ($Env:RUNNER_OS -ne 'Windows') | ||
{ | ||
Write-Output "This step is only for Windows. The current runner is $Env:RUNNER_OS so it's skipped." | ||
exit 0 | ||
} | ||
powershell .\Lombiq.Analyzers.PowerShell\Invoke-Analyzer.ps1 -ForGitHubActions | ||
- name: PowerShell 7+ Static Code Analysis | ||
if: always() # Even if the above fails, let's check both. | ||
run: .\Lombiq.Analyzers.PowerShell\Invoke-Analyzer.ps1 -ForGitHubActions |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
68 changes: 68 additions & 0 deletions
68
...yzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1
This file contains 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,68 @@ | ||
<# | ||
.SYNOPSIS | ||
Detects the usages of the alias $_ of the automatic variable $PSItem and suggests to correct them. | ||
.DESCRIPTION | ||
The full name of the automatic variable $PSItem should be used instead of its alias $_ for consistency. | ||
.EXAMPLE | ||
Measure-AutomaticVariableAlias -Token $Token | ||
.INPUTS | ||
[System.Management.Automation.Language.Token[]] | ||
.OUTPUTS | ||
[Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]] | ||
.NOTES | ||
Copied (and modified) version of | ||
https://github.com/PowerShell/PSScriptAnalyzer/blob/master/Tests/Engine/CommunityAnalyzerRules/CommunityAnalyzerRules.psm1#L613. | ||
#> | ||
function Measure-AutomaticVariableAlias | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] | ||
Param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[ValidateNotNullOrEmpty()] | ||
[System.Management.Automation.Language.Token[]] | ||
$Token | ||
) | ||
|
||
Process | ||
{ | ||
$results = @() | ||
|
||
try | ||
{ | ||
# Filter down tokens to just variable tokens with the name "_". | ||
foreach ($automaticVariableAliasToken in $Token | Where-Object { $PSItem.GetType().Name -eq "VariableToken" -and $PSItem.Name -eq "_" }) | ||
{ | ||
$correctionTypeName = "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent" | ||
$correctionExtent = New-Object -TypeName $correctionTypeName -ArgumentList @( | ||
$automaticVariableAliasToken.Extent.StartLineNumber | ||
$automaticVariableAliasToken.Extent.EndLineNumber | ||
$automaticVariableAliasToken.Extent.StartColumnNumber | ||
$automaticVariableAliasToken.Extent.EndColumnNumber | ||
'$PSItem' | ||
'Replaced the usage of the alias of the automatic variable ''$_'' with its full name ''$PSItem''.' | ||
) | ||
|
||
$suggestedCorrections = New-Object System.Collections.ObjectModel.Collection[$correctionTypeName] | ||
$suggestedCorrections.add($correctionExtent) | Out-Null | ||
|
||
$results += [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ | ||
"Extent" = $automaticVariableAliasToken.Extent | ||
"Message" = '''$_'' is an alias of the automatic variable ''$PSItem''. Please consider using the full name of this' + | ||
' variable for consistency.' | ||
"RuleName" = "PSAvoidUsingAutomaticVariableAlias" | ||
"RuleSuppressionID" = "PSAvoidUsingAutomaticVariableAlias" | ||
"Severity" = "Warning" | ||
"SuggestedCorrections" = $suggestedCorrections | ||
} | ||
} | ||
|
||
return $results | ||
} | ||
catch | ||
{ | ||
$PSCmdlet.ThrowTerminatingError($PSItem) | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
function Violate-Analyzers() | ||
{ | ||
"This file is intended made to verify that PSScriptAnalyzer works and contains intentionally bad code." | ||
"This file is intended to verify that PSScriptAnalyzer works and contains intentionally bad code." | ||
} | ||
|
||
try { echo Violate-Analyzers } catch { } | ||
try { Violate-Analyzers } catch { } | ||
|
||
Get-ChildItem . | % { "This is permitted by our settings file." } | ||
Get-ChildItem . | % { $_ } |