124 lines
5.1 KiB
PowerShell
124 lines
5.1 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
[string]$Gradlew = '.\gradlew.bat',
|
|
[string]$Configuration = 'runtimeClasspath',
|
|
[string]$OsvEndpoint = 'https://api.osv.dev/v1/querybatch',
|
|
[string]$JsonPath = '',
|
|
[switch]$FailOnHigh
|
|
)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
$projectRoot = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path
|
|
Set-Location $projectRoot
|
|
if (-not (Test-Path -LiteralPath $Gradlew)) { throw "Missing Gradle wrapper: $Gradlew" }
|
|
|
|
function Get-ResolvedCoordinates([string]$Text) {
|
|
$resolved = @{}
|
|
foreach ($line in ($Text -split "`r?`n")) {
|
|
if ($line -match '^\s*[|+\\ -]*[+\\]---\s+([A-Za-z0-9_.-]+):([A-Za-z0-9_.-]+)(?::([0-9][A-Za-z0-9_.+-]*))?') {
|
|
$group = $Matches[1]
|
|
$artifact = $Matches[2]
|
|
$version = $Matches[3]
|
|
$remainder = $line.Substring($Matches[0].Length)
|
|
if ($remainder -match '->\s*([A-Za-z0-9_.+-]+)') { $version = $Matches[1] }
|
|
if ($version) { $resolved["$group`:$artifact"] = $version }
|
|
}
|
|
}
|
|
return $resolved
|
|
}
|
|
|
|
function Invoke-OsvBatch([array]$Queries) {
|
|
$body = @{ queries = $Queries } | ConvertTo-Json -Depth 8 -Compress
|
|
$response = Invoke-RestMethod -Method Post -Uri $OsvEndpoint -Body $body `
|
|
-ContentType 'application/json' -TimeoutSec 90
|
|
if ($null -eq $response.results) { throw 'OSV batch response has no results' }
|
|
return $response.results
|
|
}
|
|
|
|
function Get-Severity($Vulnerability) {
|
|
if ($Vulnerability.database_specific -and $Vulnerability.database_specific.severity) {
|
|
return ([string]$Vulnerability.database_specific.severity).ToUpperInvariant()
|
|
}
|
|
return 'UNKNOWN'
|
|
}
|
|
|
|
function Get-FixedVersions($Vulnerability) {
|
|
$fixed = @()
|
|
foreach ($affected in @($Vulnerability.affected)) {
|
|
foreach ($range in @($affected.ranges)) {
|
|
foreach ($event in @($range.events)) {
|
|
if ($event.fixed) { $fixed += [string]$event.fixed }
|
|
}
|
|
}
|
|
}
|
|
return @($fixed | Sort-Object -Unique)
|
|
}
|
|
|
|
$dependencyOutput = & $Gradlew dependencies "--configuration=$Configuration" --no-daemon --console=plain 2>&1 | Out-String
|
|
if ($LASTEXITCODE -ne 0) { throw 'Gradle dependency resolution failed' }
|
|
$coordinates = Get-ResolvedCoordinates $dependencyOutput
|
|
$names = @($coordinates.Keys | Sort-Object)
|
|
if ($names.Count -eq 0) { throw 'No resolved dependencies were found' }
|
|
|
|
$queries = @($names | ForEach-Object {
|
|
@{ package = @{ name = $_; ecosystem = 'Maven' }; version = $coordinates[$_] }
|
|
})
|
|
$results = @()
|
|
for ($offset = 0; $offset -lt $queries.Count; $offset += 100) {
|
|
$slice = @($queries | Select-Object -Skip $offset -First 100)
|
|
$results += @(Invoke-OsvBatch $slice)
|
|
}
|
|
if ($results.Count -ne $queries.Count) { throw 'OSV returned an incomplete batch result' }
|
|
|
|
# querybatch returns only IDs; fetch each advisory once for severity/fix metadata.
|
|
$vulnerabilityIds = @()
|
|
foreach ($result in $results) {
|
|
if ($result.vulns) { $vulnerabilityIds += @($result.vulns | ForEach-Object { $_.id }) }
|
|
}
|
|
$vulnerabilityIds = @($vulnerabilityIds | Sort-Object -Unique)
|
|
$advisories = @{}
|
|
foreach ($id in $vulnerabilityIds) {
|
|
$advisories[$id] = Invoke-RestMethod -Uri "https://api.osv.dev/v1/vulns/$id" -TimeoutSec 60
|
|
}
|
|
|
|
$findings = @()
|
|
for ($index = 0; $index -lt $results.Count; $index++) {
|
|
if (-not $results[$index].vulns) { continue }
|
|
foreach ($reference in $results[$index].vulns) {
|
|
$vulnerability = $advisories[$reference.id]
|
|
if (-not $vulnerability) { throw "OSV advisory detail is missing for $($reference.id)" }
|
|
$aliases = @($vulnerability.aliases | Where-Object { $_ -like 'CVE-*' })
|
|
$findings += [pscustomobject]@{
|
|
package = $names[$index]
|
|
version = $coordinates[$names[$index]]
|
|
id = $vulnerability.id
|
|
cve = if ($aliases.Count) { $aliases[0] } else { '' }
|
|
severity = Get-Severity $vulnerability
|
|
fixed = @(Get-FixedVersions $vulnerability) -join ','
|
|
summary = [string]$vulnerability.summary
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($JsonPath) {
|
|
[System.IO.File]::WriteAllText($JsonPath, ($findings | ConvertTo-Json -Depth 6),
|
|
[System.Text.UTF8Encoding]::new($false))
|
|
}
|
|
|
|
$severityCounts = @{}
|
|
foreach ($finding in $findings) {
|
|
$key = $finding.severity
|
|
if (-not $severityCounts.ContainsKey($key)) { $severityCounts[$key] = 0 }
|
|
$severityCounts[$key]++
|
|
}
|
|
Write-Host "Resolved OSV Maven dependencies: $($names.Count)"
|
|
Write-Host "Packages with findings: $(@($findings | Select-Object -ExpandProperty package -Unique).Count)"
|
|
Write-Host "Findings by severity: $(($severityCounts.GetEnumerator() | Sort-Object Name | ForEach-Object { "$($_.Name)=$($_.Value)" }) -join ' ')"
|
|
foreach ($finding in @($findings | Where-Object { $_.severity -in @('HIGH','CRITICAL') } | Sort-Object package, id)) {
|
|
Write-Host "HIGH_OR_CRITICAL $($finding.id) $($finding.cve) $($finding.package)@$($finding.version) fixed=$($finding.fixed)"
|
|
Write-Host " $($finding.summary)"
|
|
}
|
|
if ($FailOnHigh -and @($findings | Where-Object { $_.severity -in @('HIGH','CRITICAL') }).Count -gt 0) {
|
|
throw 'High or critical dependency findings require triage before this check can pass'
|
|
}
|