|
| 1 | +--- |
| 2 | +# This workflow uses actions that are not certified by GitHub. |
| 3 | +# They are provided by a third-party and are governed by |
| 4 | +# separate terms of service, privacy policy, and support |
| 5 | +# documentation. |
| 6 | + |
| 7 | +# A sample workflow which sets up periodic OSV-Scanner scanning for vulnerabilities, |
| 8 | +# in addition to a PR check which fails if new vulnerabilities are introduced. |
| 9 | +# |
| 10 | +# For more examples and options, including how to ignore specific vulnerabilities, |
| 11 | +# see https://google.github.io/osv-scanner/github-action/ |
| 12 | + |
| 13 | +name: "OSV Scanner - PR" |
| 14 | + |
| 15 | +on: |
| 16 | + pull_request: |
| 17 | + branches: ["main"] |
| 18 | + workflow_dispatch: |
| 19 | + |
| 20 | +permissions: {} |
| 21 | + |
| 22 | +jobs: |
| 23 | + osv-scanner-pr: |
| 24 | + permissions: |
| 25 | + # Require writing security events to upload SARIF file to security tab |
| 26 | + security-events: write |
| 27 | + # Read commit contents |
| 28 | + contents: read |
| 29 | + # Actions read-only |
| 30 | + actions: read |
| 31 | + uses: "google/osv-scanner-action/.github/workflows/osv-scanner-reusable-pr.yml@8deb546fdb875b9996d27d4950be7312dac076a1" # v2.5.0 |
| 32 | + with: |
| 33 | + fail-on-vuln: false |
| 34 | + check: |
| 35 | + runs-on: ubuntu-latest |
| 36 | + needs: osv-scanner-pr |
| 37 | + steps: |
| 38 | + - name: Check OSV scan results |
| 39 | + shell: bash |
| 40 | + env: |
| 41 | + # Passed through the environment rather than interpolated into the |
| 42 | + # script body. The runner injects these out-of-band, so the scanner |
| 43 | + # JSON can never break out of the surrounding shell context no matter |
| 44 | + # what it contains or how large it is. |
| 45 | + NEW_RESULTS: ${{ needs.osv-scanner-pr.outputs.new-results }} |
| 46 | + OLD_RESULTS: ${{ needs.osv-scanner-pr.outputs.old-results }} |
| 47 | + run: | |
| 48 | + set -euo pipefail |
| 49 | + IFS=$'\n\t' |
| 50 | + # printf is a bash builtin, so the (potentially very large) values are |
| 51 | + # written to disk without hitting the ARG_MAX command-line limit. |
| 52 | + printf '%s' "${NEW_RESULTS}" > "${RUNNER_TEMP}/new_results.json" |
| 53 | + printf '%s' "${OLD_RESULTS}" > "${RUNNER_TEMP}/old_results.json" |
| 54 | + # Fail if any vulnerability present in the new results is absent from |
| 55 | + # the old results, i.e. the PR introduces it. Read via process |
| 56 | + # substitution rather than a pipe so the rc assignment is not lost in |
| 57 | + # a subshell, and use a fixed-string match for the exact vuln id. |
| 58 | + rc=0 |
| 59 | + while read -r vid; do |
| 60 | + if ! grep -qF -e "\"${vid}\"" "${RUNNER_TEMP}/old_results.json"; then |
| 61 | + rc=1 |
| 62 | + >&2 echo "error: PR introduces new vulnerability ${vid} (see step 'scan > osv-scanner-pr > Run osv-scanner-reporter' for details)" |
| 63 | + fi |
| 64 | + done < <(jq -r '.results[]?.packages[]?.vulnerabilities[]?.id' "${RUNNER_TEMP}/new_results.json") |
| 65 | + if [ "${rc}" -ne 0 ]; then |
| 66 | + exit 1 |
| 67 | + fi |
0 commit comments