Skip to content

Commit eefbd75

Browse files
committed
ci(osv-scanner): fix subshell issue
1 parent 6c87111 commit eefbd75

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,29 @@ jobs:
4040
run: |
4141
set -euo pipefail
4242
IFS=$'\n\t'
43+
# The scan results JSON can be very large (potentially exceeding the
44+
# command-line / ARG_MAX length limit if passed as an argument or as
45+
# an env var expanded on the command line), so the workflow-level
46+
# expansion is written straight to a file via a quoted heredoc.
47+
# The quoted delimiter prevents any runtime shell expansion of
48+
# the JSON contents.
4349
cat > "${RUNNER_TEMP}/new_results.json" <<'__OSV_NEW_RESULTS_EOF__'
4450
${{ needs.osv-scanner-pr.outputs.new-results }}
4551
__OSV_NEW_RESULTS_EOF__
4652
cat > "${RUNNER_TEMP}/old_results.json" <<'__OSV_OLD_RESULTS_EOF__'
4753
${{ needs.osv-scanner-pr.outputs.old-results }}
4854
__OSV_OLD_RESULTS_EOF__
55+
# Fail if any vulnerability present in the new results is absent from
56+
# the old results, i.e. the PR introduces it. Read via process
57+
# substitution rather than a pipe so the rc assignment is not lost in
58+
# a subshell, and use a fixed-string match for the exact vuln id.
4959
rc=0
50-
jq -r '.results[].packages[].vulnerabilities[].id' "${RUNNER_TEMP}/new_results.json" | while read -r vid; do
51-
if grep -q -L -e "\"${vid}\"" "${RUNNER_TEMP}/old_results.json"; then
52-
rc=$((rc+1))
53-
>&2 echo "error: PR introduces new vulnerabilities ${vid} (see step 'scan > osv-scanner-pr > Run osv-scanner-reporter' for details)"
60+
while read -r vid; do
61+
if ! grep -qF -e "\"${vid}\"" "${RUNNER_TEMP}/old_results.json"; then
62+
rc=1
63+
>&2 echo "error: PR introduces new vulnerability ${vid} (see step 'scan > osv-scanner-pr > Run osv-scanner-reporter' for details)"
5464
fi
55-
done
56-
if [ "${rc}" -gt 0 ]; then
57-
exit "${rc}"
65+
done < <(jq -r '.results[]?.packages[]?.vulnerabilities[]?.id' "${RUNNER_TEMP}/new_results.json")
66+
if [ "${rc}" -ne 0 ]; then
67+
exit 1
5868
fi

.github/workflows/osv-scanner.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# For more examples and options, including how to ignore specific vulnerabilities,
1111
# see https://google.github.io/osv-scanner/github-action/
1212

13-
name: OSV Scanner
13+
name: "OSV Scanner"
1414

1515
on:
1616
push:
@@ -42,6 +42,12 @@ jobs:
4242
run: |
4343
set -euo pipefail
4444
IFS=$'\n\t'
45+
# The scan results JSON can be very large (potentially exceeding the
46+
# command-line / ARG_MAX length limit if passed as an argument or as
47+
# an env var expanded on the command line), so the workflow-level
48+
# expansion is written straight to a file via a quoted heredoc.
49+
# The quoted delimiter prevents any runtime shell expansion of
50+
# the JSON contents.
4551
cat > "${RUNNER_TEMP}/results.json" <<'__OSV_RESULTS_EOF__'
4652
${{ needs.osv-scanner.outputs.results }}
4753
__OSV_RESULTS_EOF__

0 commit comments

Comments
 (0)