Skip to content

Commit b8669a9

Browse files
committed
ci(osv-scanner): print vulnerabilities directly
1 parent 921ee96 commit b8669a9

2 files changed

Lines changed: 49 additions & 22 deletions

File tree

‎.github/workflows/osv-scanner-pr.yml‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,36 @@ jobs:
5151
set -euo pipefail
5252
IFS=$'\n\t'
5353
# Fail if any vulnerability present in the new results is absent from
54-
# the old results, i.e. the PR introduces it. Read via process
55-
# substitution rather than a pipe so the rc assignment is not lost in
56-
# a subshell, and use a fixed-string match for the exact vuln id.
57-
rc=0
58-
while read -r vid; do
59-
if ! grep -qF -e "\"${vid}\"" "${RUNNER_TEMP}/osv/old-results.json"; then
60-
rc=1
61-
>&2 echo "error: PR introduces new vulnerability ${vid} (see step 'scan > osv-scanner-pr > Run osv-scanner-reporter' for details)"
62-
fi
63-
done < <(jq -r '.results[]?.packages[]?.vulnerabilities[]?.id' "${RUNNER_TEMP}/osv/new-results.json")
64-
if [ "${rc}" -ne 0 ]; then
54+
# the old results (by id or alias), i.e. the PR introduces it, and
55+
# list each one with its severity and affected packages. A jq failure
56+
# (e.g. missing or malformed results) aborts the step via set -e
57+
# instead of being mistaken for "no new vulnerabilities".
58+
vulns="$(jq -r --slurpfile old "${RUNNER_TEMP}/osv/old-results.json" '
59+
([ $old[0].results[]?.packages[]?.vulnerabilities[]?
60+
| .id, .aliases[]? ]
61+
| map({(.): true}) | add // {}) as $known
62+
| [ .results[]?
63+
| .source.path as $path
64+
| .packages[]?
65+
| .package as $package
66+
| .groups as $groups
67+
| .vulnerabilities[]?
68+
| select($known[.id] | not)
69+
| .id as $id
70+
| { id,
71+
summary: (.summary // ""),
72+
severity: ([ $groups[]?
73+
| select(any(.ids[]?; . == $id))
74+
| .max_severity // empty
75+
| select(. != "") ]
76+
| first // "unknown"),
77+
package: "\($package.ecosystem)/\($package.name)@\($package.version) in \($path)" } ]
78+
| group_by(.id)[]
79+
| (.[0] | "\(.id)\(if .summary != "" then ": \(.summary)" else "" end) (severity \(.severity))"),
80+
(.[] | " \(.package)")
81+
' "${RUNNER_TEMP}/osv/new-results.json")"
82+
if [ -n "${vulns}" ]; then
83+
>&2 echo "error: PR introduces one or more new vulnerabilities:"
84+
>&2 printf '%s\n' "${vulns}"
6585
exit 1
6686
fi

‎.github/workflows/osv-scanner.yml‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,24 @@ jobs:
5252
set -euo pipefail
5353
IFS=$'\n\t'
5454
# Each rule in the SARIF report is a vulnerability found by the scan,
55-
# with its worst numeric CVSS score in the security-severity property.
56-
# A jq failure (e.g. missing or malformed report) aborts the step via
57-
# set -e instead of being mistaken for "no vulnerabilities".
58-
count="$(jq '
59-
[ .runs[].tool.driver.rules[]?
60-
| .properties["security-severity"] // empty
61-
| tonumber
62-
| select(. >= 4.0) ]
63-
| length
55+
# with its worst numeric CVSS score in the security-severity property,
56+
# and each result referencing that rule is an affected package. List
57+
# the medium or higher severity ones, most severe first. A jq failure
58+
# (e.g. missing or malformed report) aborts the step via set -e
59+
# instead of being mistaken for "no vulnerabilities".
60+
vulns="$(jq -r '
61+
.runs[] as $run
62+
| [ $run.tool.driver.rules[]?
63+
| (.properties["security-severity"] // empty | tonumber) as $severity
64+
| select($severity >= 4.0)
65+
| {id, $severity, summary: (.shortDescription.text // .id)} ]
66+
| sort_by(-.severity)[]
67+
| . as $vuln
68+
| "\($vuln.summary) (severity \($vuln.severity))",
69+
($run.results[]? | select(.ruleId == $vuln.id) | " \(.message.text)")
6470
' "${RUNNER_TEMP}/osv/results.sarif")"
65-
if [ "${count}" -gt 0 ]; then
66-
>&2 echo "error: found one or more vulnerabilities with a medium or higher severity (see step 'scan > osv-scanner > Run osv-scanner-reporter' for details)"
71+
if [ -n "${vulns}" ]; then
72+
>&2 echo "error: found one or more vulnerabilities with a medium or higher severity:"
73+
>&2 printf '%s\n' "${vulns}"
6774
exit 1
6875
fi

0 commit comments

Comments
 (0)