Run OSV (open-source vulnerabilities) scanner #368
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copyright 2025 Google LLC | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # https://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| # Zero-config modular workflow to run Open Source Vulnerabilities code scans. | |
| # | |
| # The OSV scanner is a dependency vulnerability scanner that identifies known | |
| # vulnerabilities in a project's dependencies. It supports C/C++, Python, Java, | |
| # JavaScript, and others. The findings are reported in the repo's code-scanning | |
| # results page, https://github.com/quantumlib/REPO/security/code-scanning/. | |
| # | |
| # Note: the OSV project provides a workflow you can reference as a step with | |
| # uses: google/osv-scanner-action/.github/workflows/osv-scanner-reusable-pr.yml. | |
| # Unfortunately, that workflow hardcodes some behaviors (such as uploading the | |
| # SARIF file to the workflow Actions tab, which we have never needed). The | |
| # workflow in this file is basically a heavily modified version of theirs. | |
| # | |
| # For more OSV scanner examples and options, including how to ignore specific | |
| # vulnerabilities, see https://google.github.io/osv-scanner/github-action/. | |
| # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
| name: OSV known vulnerabilities scanner | |
| run-name: Run OSV (open-source vulnerabilities) scanner ${{inputs.reason}} | |
| on: | |
| pull_request: | |
| types: [opened, synchronize] | |
| branches: | |
| - main | |
| # Support merge queues. | |
| merge_group: | |
| types: | |
| - checks_requested | |
| # Allow calling from other workflows. | |
| workflow_call: | |
| inputs: | |
| reason: | |
| description: 'Append text to workflow run name:' | |
| type: string | |
| debug: | |
| description: 'Run with debugging options' | |
| type: boolean | |
| default: false | |
| # Allow manual invocation. | |
| workflow_dispatch: | |
| inputs: | |
| debug: | |
| description: 'Run with debugging options' | |
| type: boolean | |
| default: true | |
| # Declare default workflow permissions as read only. | |
| permissions: read-all | |
| concurrency: | |
| # Cancel any previously-started but still active runs on the same branch. | |
| cancel-in-progress: true | |
| group: ${{github.workflow}}-${{github.event.pull_request.number||github.ref}} | |
| jobs: | |
| osv-scan: | |
| if: github.repository_owner == 'quantumlib' | |
| name: Run OSV scanner | |
| runs-on: ubuntu-24.04 | |
| timeout-minutes: 15 | |
| permissions: | |
| # Needed to read commit contents: | |
| actions: read | |
| # Needed to upload the results to code-scanning dashboard: | |
| security-events: write | |
| # Needed to upload SARIF file to CodeQL. | |
| contents: read | |
| env: | |
| # Setting Bash SHELLOPTS here takes effect for all shell commands below. | |
| SHELLOPTS: ${{inputs.debug && 'xtrace' || '' }} | |
| steps: | |
| - name: Check out a copy of the git repository | |
| uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 | |
| with: | |
| fetch-depth: 0 | |
| - name: Check out the target branch | |
| run: | | |
| git checkout ${{github.base_ref || github.ref_name}} | |
| git submodule update --recursive | |
| - name: Run OSV scanner on existing code | |
| # yamllint disable rule:line-length | |
| uses: google/osv-scanner-action/osv-scanner-action@e92b5d07338d4f0ba0981dffed17c48976ca4730 # v2.2.3 | |
| continue-on-error: true | |
| with: | |
| scan-args: |- | |
| --include-git-root | |
| --format=json | |
| --output=old-results.json | |
| --recursive | |
| ./ | |
| - name: Check out current branch | |
| # Use -f in case any changes were made by osv-scanner. | |
| run: | | |
| git checkout -f "$GITHUB_SHA" | |
| git submodule update --recursive | |
| - name: Run OSV scanner on new code | |
| # yamllint disable rule:line-length | |
| uses: google/osv-scanner-action/osv-scanner-action@e92b5d07338d4f0ba0981dffed17c48976ca4730 # v2.2.3 | |
| continue-on-error: true | |
| with: | |
| scan-args: |- | |
| --include-git-root | |
| --format=json | |
| --output=new-results.json | |
| --recursive | |
| ./ | |
| - name: Run the OSV scanner reporter | |
| # yamllint disable rule:line-length | |
| uses: google/osv-scanner-action/osv-reporter-action@e92b5d07338d4f0ba0981dffed17c48976ca4730 # v2.2.3 | |
| with: | |
| scan-args: |- | |
| --output=osv-results.sarif | |
| --old=old-results.json | |
| --new=new-results.json | |
| --gh-annotations=true | |
| --fail-on-vuln=true | |
| - name: Upload results to the repository's code-scanning results dashboard | |
| id: upload_artifact | |
| # yamllint disable rule:line-length | |
| uses: github/codeql-action/upload-sarif@f443b600d91635bebf5b0d9ebc620189c0d6fba5 # v3.29.5 | |
| with: | |
| sarif_file: osv-results.sarif | |
| - if: github.event.inputs.debug == true | |
| name: Upload results as artifacts to the workflow Summary page | |
| uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 | |
| with: | |
| name: SARIF file | |
| path: results.sarif | |
| retention-days: 5 | |
| - name: Error troubleshooter | |
| if: ${{always() && steps.upload_artifact.outcome == 'failure'}} | |
| run: echo '::error::Artifact upload failed. Check the workflow logs.' |