Sub topic 12
Uploading SARIF Results from Other Tools
This chapter assumes you can read GitHub Actions workflows and know how CodeQL produces code scanning alerts. You have seen alerts in the Security tab. Here you will send findings from a different static analysis tool into that same view.
Why upload SARIF
SARIF (Static Analysis Results Interchange Format) is a JSON format for static analysis findings. CodeQL writes its results as SARIF. Code scanning accepts SARIF from any tool, so alerts from a third-party scanner can appear next to CodeQL alerts. You get one place to review, triage and gate pull requests.
The tool you use must produce a SARIF file. Many scanners can do this with an output flag. The upload action then sends that file to GitHub.
The upload step
The upload-sarif action takes a SARIF file and submits it to code scanning.
- name: Upload SARIF results
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: results.sarif
category: third-party-scannerThe job that runs this step needs the security-events: write permission. Add contents: read as well if the job also checks out the repository.
A complete example
The workflow below runs a scanner, then uploads its output. The scanner command is a placeholder. Replace it with the real command for your tool.
name: Third-party scan
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
scan:
runs-on: ubuntu-latest
permissions:
security-events: write
contents: read
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Run scanner
run: ./run-scanner.sh --format sarif --output results.sarif
- name: Upload SARIF results
if: always()
uses: github/codeql-action/upload-sarif@v4
with:
sarif_file: results.sarif
category: third-party-scannerThe if: always() condition matters. Many scanners exit with a non-zero code when they find issues. Without always(), the job stops before the upload and you never see the alerts.
Categories keep results apart
The category input labels a set of results. GitHub uses it to decide which earlier results a new upload replaces. Two uploads with the same category for the same commit overwrite each other. Two with different categories coexist.
Give each tool, and each language or component within a tool, its own category. The CodeQL workflow earlier used /language:${{ matrix.language }} for this reason. A third-party scanner should use a distinct name such as third-party-scanner. If you reuse a category, one tool's results can replace another's.
How the flow works
Common errors
- Missing permission. The upload fails with an authorization error. Add
security-events: writeto the job. - File not found. The path in
sarif_fileis relative to the workspace. Check that the scanner wrote the file where you expect. - Upload skipped after a failed scan. Add
if: always()to the upload step. - Results disappear or replace each other. Two tools share one category. Give each its own.
- Upload rejected as invalid. The file is not valid SARIF or exceeds GitHub's size and result limits. Check the scanner's output format setting and the limits in GitHub's documentation.
- Pull requests from forks. Workflows triggered by a fork's pull request get a read-only token. The upload cannot write security events there.
What happens after upload
Uploaded alerts behave like CodeQL alerts. You can filter them by tool in the Security tab, dismiss them with a reason, and include them in required checks. Alert text and severity come from the SARIF file, so the quality of the message depends on the scanner.
Exercise
A scanner exits with code 1 whenever it finds a problem. Your upload step never runs, and no alerts appear. What change fixes this, and which other setting should you check so two tools do not overwrite each other?
Show answer
Add if: always() to the upload step so it runs even after the scanner step fails. Then give each tool a distinct category value in its upload step, so results from one tool do not replace results from another.