-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpsscriptanalyzer-sarif-reports.yml
More file actions
349 lines (302 loc) · 12.6 KB
/
psscriptanalyzer-sarif-reports.yml
File metadata and controls
349 lines (302 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
name: PowerShell Corporate Lint (PSScriptAnalyzer + SARIF + Reports) [Report-Only]
on:
push:
branches: [main, develop]
paths:
- "**/*.ps1"
- "**/*.psm1"
- "**/*.psd1"
- ".psscriptanalyzer.psd1"
- ".github/workflows/psscriptanalyzer-sarif-reports.yml"
pull_request:
branches: [main, develop]
paths:
- "**/*.ps1"
- "**/*.psm1"
- "**/*.psd1"
- ".psscriptanalyzer.psd1"
- ".github/workflows/psscriptanalyzer-sarif-reports.yml"
workflow_dispatch:
concurrency:
group: powershell-lint-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
security-events: write
jobs:
psa-analyze:
name: PSScriptAnalyzer Report-Only (SARIF + Artifacts)
runs-on: windows-latest
timeout-minutes: 35
env:
PSA_VERSION: "1.24.0"
SETTINGS_FILE: ".psscriptanalyzer.psd1"
OUT_DIR: "lint-reports"
OUT_JSON: "psa-results.json"
OUT_CSV: "psa-results.csv"
OUT_MD: "psa-results.md"
OUT_SARIF: "psa-results.sarif"
OUT_FAILTXT: "psa-invocation-failed.txt"
OUT_SUMMARY: "psa-summary.txt"
ANALYZE_ROOTS: >-
BlueTeam-Tools
Core-ScriptLibrary
ITSM-Templates-SVR
ITSM-Templates-WKS
ProSuite-Hub
SysAdmin-Tools
EXCLUDE_SUBTREE: "SysAdmin-Tools/GroupPolicyObjects-Templates"
PRUNE_DIR_REGEX: '(?i)[\\/](\.git|node_modules|dist|build|artifacts|\.next)[\\/]'
SARIF_CATEGORY: "powershell/psscriptanalyzer"
steps:
- name: Checkout (sparse)
uses: actions/checkout@v4
with:
fetch-depth: 0
sparse-checkout-cone-mode: false
sparse-checkout: |
BlueTeam-Tools
Core-ScriptLibrary
ITSM-Templates-SVR
ITSM-Templates-WKS
ProSuite-Hub
SysAdmin-Tools
.psscriptanalyzer.psd1
!SysAdmin-Tools/GroupPolicyObjects-Templates
- name: Git long paths
shell: pwsh
run: |
git config --global core.longpaths true
- name: Prepare output folder
shell: pwsh
run: |
$outDir = Join-Path $env:GITHUB_WORKSPACE $env:OUT_DIR
New-Item -ItemType Directory -Force -Path $outDir | Out-Null
- name: Initialize SARIF baseline (always create file)
if: always()
shell: pwsh
run: |
$ErrorActionPreference = 'Continue'
$outDir = Join-Path $env:GITHUB_WORKSPACE $env:OUT_DIR
$sarifOut = Join-Path $outDir $env:OUT_SARIF
New-Item -ItemType Directory -Force -Path $outDir | Out-Null
$baseline = @{
'$schema' = 'https://json.schemastore.org/sarif-2.1.0.json'
version = '2.1.0'
runs = @(@{ tool = @{ driver = @{ name = 'PSScriptAnalyzer'; version = '0' } }; results = @() })
}
$baseline | ConvertTo-Json -Depth 8 | Set-Content -Encoding UTF8 $sarifOut
Write-Host "Baseline SARIF created: $sarifOut"
- name: Ensure module cache directories exist
shell: pwsh
run: |
New-Item -ItemType Directory -Force -Path "$HOME\Documents\PowerShell\Modules" | Out-Null
New-Item -ItemType Directory -Force -Path "$HOME\Documents\WindowsPowerShell\Modules" | Out-Null
- name: Cache PowerShell modules
uses: actions/cache@v4
with:
path: |
~\Documents\PowerShell\Modules
~\Documents\WindowsPowerShell\Modules
key: psmodules-${{ runner.os }}-psa-${{ env.PSA_VERSION }}
- name: Install PSScriptAnalyzer
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
$v = $env:PSA_VERSION
$have = Get-Module -ListAvailable PSScriptAnalyzer |
Sort-Object Version -Descending |
Select-Object -First 1
if (-not $have -or $have.Version.ToString() -ne $v) {
Install-Module PSScriptAnalyzer -RequiredVersion $v -Force -Scope CurrentUser -AllowClobber
}
Import-Module PSScriptAnalyzer -RequiredVersion $v -Force
Write-Host "PSScriptAnalyzer version loaded: $((Get-Module PSScriptAnalyzer).Version)"
- name: Run PSScriptAnalyzer + Build Reports (REPORT-ONLY)
if: always()
shell: pwsh
run: |
$ErrorActionPreference = 'Continue'
$outDir = Join-Path $env:GITHUB_WORKSPACE $env:OUT_DIR
New-Item -ItemType Directory -Force -Path $outDir | Out-Null
$jsonOut = Join-Path $outDir $env:OUT_JSON
$csvOut = Join-Path $outDir $env:OUT_CSV
$mdOut = Join-Path $outDir $env:OUT_MD
$sarifOut = Join-Path $outDir $env:OUT_SARIF
$failOut = Join-Path $outDir $env:OUT_FAILTXT
$sumOut = Join-Path $outDir $env:OUT_SUMMARY
$root = $env:GITHUB_WORKSPACE
$prune = [regex]::new($env:PRUNE_DIR_REGEX)
$excludeSub = ($env:EXCLUDE_SUBTREE -replace '/', [IO.Path]::DirectorySeparatorChar)
$excludeSubEsc = [regex]::Escape($excludeSub)
$roots = @()
foreach ($token in ($env:ANALYZE_ROOTS -split '\s+')) {
if ([string]::IsNullOrWhiteSpace($token)) { continue }
$p = Join-Path $root $token
if (Test-Path $p) { $roots += $p }
}
$files = @()
if ($roots.Count -gt 0) {
$files = foreach ($r in $roots) {
Get-ChildItem -Path $r -Recurse -File -Include *.ps1,*.psm1,*.psd1 -ErrorAction SilentlyContinue |
Where-Object {
($_.FullName -notmatch $prune) -and
($_.FullName -notmatch $excludeSubEsc)
}
}
}
$files = @($files | Sort-Object FullName -Unique)
$count = @($files).Count
$settingsPath = Join-Path $env:GITHUB_WORKSPACE $env:SETTINGS_FILE
$useSettings = (Test-Path $settingsPath)
$settingsArgs = @{}
if ($useSettings) { $settingsArgs['Settings'] = $settingsPath }
$results = @()
try {
if ($count -gt 0) {
# IMPORTANT: analyze one file at a time to avoid Path binding issues on runners
$paths = @($files | ForEach-Object { [string]$_.FullName })
$all = New-Object System.Collections.Generic.List[object]
foreach ($p in $paths) {
if ([string]::IsNullOrWhiteSpace($p)) { continue }
try {
$r = Invoke-ScriptAnalyzer -Path $p -Recurse:$false @settingsArgs
if ($r) { foreach ($item in @($r)) { $all.Add($item) } }
} catch {
# keep going; record failure details once
$_ | Out-String | Add-Content -Encoding UTF8 $failOut
}
}
$results = @($all)
} else {
"No PowerShell files found to analyze." | Set-Content -Encoding UTF8 $sumOut
}
}
catch {
$_ | Out-String | Set-Content -Encoding UTF8 $failOut
$results = @()
}
if (-not $results) { $results = @() }
# Always write reports (even if empty)
$results | ConvertTo-Json -Depth 8 | Set-Content -Encoding UTF8 $jsonOut
$results |
Select-Object RuleName, Severity, Message, ScriptName, Line, Column |
Export-Csv -Path $csvOut -NoTypeInformation -Encoding UTF8
$bySev = $results | Group-Object Severity | Sort-Object Name
$byRule = $results | Group-Object RuleName | Sort-Object Count -Descending
$md = New-Object System.Collections.Generic.List[string]
$md.Add("# PSScriptAnalyzer Report (Report-Only)")
$md.Add("")
$md.Add("**Files scanned:** $count")
$md.Add("**Settings file used:** $useSettings ($($env:SETTINGS_FILE))")
$md.Add("**Total findings:** $(@($results).Count)")
$md.Add("")
$md.Add("## Findings by Severity")
if ($bySev.Count -eq 0) { $md.Add("- None") }
else { foreach ($g in $bySev) { $md.Add("- **$($g.Name)**: $($g.Count)") } }
$md.Add("")
$md.Add("## Top Rules")
$top = $byRule | Select-Object -First 20
if ($top.Count -eq 0) { $md.Add("- None") }
else { foreach ($g in $top) { $md.Add("- **$($g.Name)**: $($g.Count)") } }
$md.Add("")
$md.Add("> Note: This workflow is report-only. Findings do not fail the job. Artifacts are uploaded when present.")
$md | Set-Content -Encoding UTF8 $mdOut
$summary = New-Object System.Collections.Generic.List[string]
$summary.Add("PSScriptAnalyzer (Report-Only)")
$summary.Add("Files scanned: $count")
$summary.Add("Settings file used: $useSettings ($($env:SETTINGS_FILE))")
$summary.Add("Total findings: $(@($results).Count)")
foreach ($g in $bySev) { $summary.Add(" $($g.Name): $($g.Count)") }
$summary | Set-Content -Encoding UTF8 $sumOut
function Get-SarifLevel([string]$sev) {
switch -Regex ($sev) {
'Error' { 'error' }
'Warning' { 'warning' }
default { 'note' }
}
}
# Update SARIF file (baseline exists already)
try {
$sarif = @{
'$schema' = 'https://json.schemastore.org/sarif-2.1.0.json'
version = '2.1.0'
runs = @(
@{
tool = @{
driver = @{
name = 'PSScriptAnalyzer'
informationUri = 'https://github.com/PowerShell/PSScriptAnalyzer'
version = (Get-Module PSScriptAnalyzer).Version.ToString()
}
}
results = @()
}
)
}
foreach ($r in $results) {
$scriptRel = [string]$r.ScriptName
if (-not [string]::IsNullOrWhiteSpace($scriptRel)) {
$scriptRel = $scriptRel.Replace("$env:GITHUB_WORKSPACE\", '').Replace("$env:GITHUB_WORKSPACE/", '')
}
$sarif.runs[0].results += @{
ruleId = [string]$r.RuleName
level = (Get-SarifLevel ([string]$r.Severity))
message = @{ text = [string]$r.Message }
locations = @(
@{
physicalLocation = @{
artifactLocation = @{ uri = $scriptRel }
region = @{
startLine = [int]$r.Line
startColumn = [int]$r.Column
}
}
}
)
}
}
$sarif | ConvertTo-Json -Depth 12 | Set-Content -Encoding UTF8 $sarifOut
}
catch {
$_ | Out-String | Add-Content -Encoding UTF8 $failOut
}
exit 0
- name: Pre-upload diagnostics (ensure SARIF exists)
if: always()
shell: pwsh
run: |
$ErrorActionPreference = 'Continue'
$outDir = Join-Path $env:GITHUB_WORKSPACE $env:OUT_DIR
$sarifOut = Join-Path $outDir $env:OUT_SARIF
Write-Host "GITHUB_WORKSPACE=$env:GITHUB_WORKSPACE"
Write-Host "OUT_DIR=$outDir"
if (Test-Path $outDir) {
Get-ChildItem -Recurse -Force $outDir | Format-Table FullName, Length
} else {
Write-Host "Output dir missing; creating."
New-Item -ItemType Directory -Force -Path $outDir | Out-Null
}
if (-not (Test-Path $sarifOut)) {
Write-Host "SARIF missing — creating baseline now."
$baseline = @{
'$schema' = 'https://json.schemastore.org/sarif-2.1.0.json'
version = '2.1.0'
runs = @(@{ tool = @{ driver = @{ name = 'PSScriptAnalyzer'; version = '0' } }; results = @() })
}
$baseline | ConvertTo-Json -Depth 8 | Set-Content -Encoding UTF8 $sarifOut
}
- name: Upload lint artifacts (never fail if empty)
if: always()
uses: actions/upload-artifact@v4
with:
name: powershell-lint-reports
path: ${{ env.OUT_DIR }}/**
if-no-files-found: warn
retention-days: 30
- name: Upload SARIF to GitHub Code Scanning (report-only)
if: always() && hashFiles(format('{0}/{1}', env.OUT_DIR, env.OUT_SARIF)) != ''
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: ${{ env.OUT_DIR }}/${{ env.OUT_SARIF }}
category: ${{ env.SARIF_CATEGORY }}