Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .github/workflows/dependency-scan.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: Dependency scan

# Checks the Python packages locked in uv.lock against the OSV database of known
# vulnerabilities, on every pull request and in the merge queue. An organization ruleset requires it ("Require workflows to
# pass before merging"), so it runs in the repository of the pull request, with that
# repository's files and token: see README.md.
# Checks the Python packages locked in uv.lock and the Rust crates locked in Cargo.lock
# against the OSV database of known vulnerabilities, on every pull request and in the merge
# queue. An organization ruleset requires it ("Require workflows to pass before merging"), so
# it runs in the repository of the pull request, with that repository's files and token: see
# README.md.
#
# A ruleset ignores the filters of the triggers (branches, paths, types), so the job checks
# by itself whether there is anything to scan. The scan is scripts/dependency_scan.py of this
Expand Down Expand Up @@ -44,7 +45,7 @@ jobs:
with:
enable-cache: false

- name: Check uv.lock against OSV
- name: Check uv.lock and Cargo.lock against OSV
env:
EXCEPTIONS_FILE: .github/vulnerability-exceptions.toml
# --no-config: the uv settings of the repository (uv.toml, [tool.uv]) are not for the
Expand Down
79 changes: 56 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ repository the workflow runs in.
## Dependency scan

[`dependency-scan.yml`](.github/workflows/dependency-scan.yml) checks the Python packages
locked in `uv.lock` against the [OSV](https://osv.dev) database of known vulnerabilities, on
every pull request and in the merge queue.
locked in `uv.lock`, and the Rust crates locked in `Cargo.lock`, against the
[OSV](https://osv.dev) database of known vulnerabilities, on every pull request and in the
merge queue.

### How it works

Expand All @@ -24,14 +25,19 @@ it out from the commit of the workflow file (`job.workflow_repository`, `job.wor
which the ruleset pins, and runs it with `uv run` (Python 3.13, standard library only, as its
inline metadata says). The script:

1. finds the `uv.lock` files that git tracks. With none, it passes with a notice;
2. reads the packages of each one that come from a package index, with their versions:
1. finds the `uv.lock` and `Cargo.lock` files that git tracks, in the root and in any
directory below it. With neither, it passes with a notice; with one kind, it scans that
kind;
2. reads the packages of each `uv.lock` that come from a package index, with their versions:
every extra and dependency group, whatever the index. The project itself and packages from
git, a URL or a local path are left out: they have no version on PyPI to look up;
3. asks OSV which vulnerabilities affect each name and version, and reads their records. OSV
lists a vulnerability once per database (GHSA, PYSEC, …): the GHSA record is kept, as it
carries GitHub's severity;
4. writes a table of the findings to the job summary, and an error annotation for each one
3. reads the crates of each `Cargo.lock` that come from crates.io, with their versions. The
packages of the workspace and the crates from git, a path or another registry are left
out. Versions 3 and 4 of the lock have the same `[[package]]` tables, and both are read;
4. asks OSV which vulnerabilities affect each name and version in its ecosystem (`PyPI` or
`crates.io`), and reads their records. OSV lists a vulnerability once per database (GHSA,
PYSEC, RUSTSEC, …): the GHSA record is kept, as it carries GitHub's severity;
5. writes a table of the findings to the job summary, and an error annotation for each one
that blocks.

A finding **blocks the merge** when it is in a **runtime dependency** and its severity is
Expand All @@ -41,20 +47,46 @@ are findings in packages that only the dependency groups need (`dev`, `test`, `d
which don't ship with the project. A withdrawn advisory never blocks. If OSV can't be read,
the job fails.

The runtime dependencies are those of the projects of the lock (its editable or virtual
packages) and of their extras, followed through the lock, with the extras each dependency
asks for. Whatever a project lists in `[project] dependencies` counts, even tools that only
its developers use: move those to a dependency group. A lock without a project counts every
package as a runtime dependency.
The runtime dependencies of a `uv.lock` are those of the projects of the lock (its editable
or virtual packages) and of their extras, followed through the lock, with the extras each
dependency asks for. Whatever a project lists in `[project] dependencies` counts, even tools
that only its developers use: move those to a dependency group. A lock without a project
counts every package as a runtime dependency.

### Rust crates

OSV imports the [RustSec advisory database](https://rustsec.org/) and GitHub's advisories for
crates.io, so a crate is checked with the same database, severity and rules as a Python
package. The differences:

- **Every crate counts as a runtime dependency.** `Cargo.lock` doesn't record which crates
are dev-dependencies or build-dependencies, nor which features are on: only cargo knows
that, when it resolves the manifest. A finding in a crate that only the tests or the build
script use blocks like any other, so use an exception for it, with the reason.
- **Informational advisories don't block.** RustSec also publishes notices that aren't
vulnerabilities: an `unmaintained` or `unsound` crate, or a `notice`. They have no GHSA
record, hence no severity. The scan lists them with the kind as the severity
(`informational: unmaintained`) and never blocks on them. If GitHub has an advisory for the
same issue, GitHub's severity applies instead.
- **A RustSec vulnerability that GitHub doesn't know blocks**, as its severity is unknown:
the severity is GitHub's, not the CVSS score of RustSec.
- **Yanked crates aren't reported.** A yanked version isn't an advisory, and OSV doesn't list
it.
- Crate names are compared exactly: cargo treats `foo-bar` and `foo_bar` as different crates,
where Python names ignore the case and the separators.

### What leaves the repository

Only the name, the ecosystem (`PyPI`) and the version of each locked package, sent to
[OSV's API](https://google.github.io/osv.dev/post-v1-querybatch/) with Python's default
User-Agent; then the identifiers of the vulnerabilities found, to read their records. That
includes packages from private indexes: OSV matches by name, whatever the index. If a private
package shares its name with a public one that has advisories, the findings of the public one
are reported: record them as exceptions.
Only the name, the ecosystem (`PyPI` or `crates.io`) and the version of each locked package,
sent to [OSV's API](https://google.github.io/osv.dev/post-v1-querybatch/) with Python's
default User-Agent; then the identifiers of the vulnerabilities found, to read their records.
For Python, that includes packages from private indexes: OSV matches by name, whatever the
index. If a private package shares its name with a public one that has advisories, the
findings of the public one are reported: record them as exceptions. For Rust, only the crates
of crates.io are sent (`registry+https://github.com/rust-lang/crates.io-index` or
`sparse+https://index.crates.io/` as the source in `Cargo.lock`): those names are public.
Crates from git, a path or another registry are never sent, and neither are the names of the
packages of the workspace.

### Exceptions

Expand All @@ -64,7 +96,7 @@ vulnerability:

```toml
[[exception]]
id = "GHSA-xxxx-xxxx-xxxx" # the GHSA identifier, or any alias that the job summary shows
id = "GHSA-xxxx-xxxx-xxxx" # the GHSA identifier, or any alias (CVE-…, RUSTSEC-…, PYSEC-…)
issue = "https://github.com/<owner>/<repository>/issues/<number>" # the issue that approves it
reason = "The vulnerable function is only used by the test suite."
```
Expand Down Expand Up @@ -96,6 +128,7 @@ The tests run `scripts/dependency_scan.py` as the workflow does:
uv run --no-project --with pytest --with pyyaml pytest
```

The end-to-end tests lock small projects with known vulnerable packages and need the network
(PyPI and OSV); set `OFFLINE=1` to skip them. CI also runs ruff, actionlint and zizmor. Changes need a pull request and a review:
every targeted repository runs this code.
The end-to-end tests lock small Python projects with known vulnerable packages, and use
hand-written `Cargo.lock` files with known vulnerable crates (no cargo needed). They need the
network (PyPI and OSV); set `OFFLINE=1` to skip them. CI also runs ruff, actionlint and
zizmor. Changes need a pull request and a review: every targeted repository runs this code.
Loading
Loading