From 507924f227772187a297469752b335ba9120f308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Di=20Remigio=20Eik=C3=A5s?= Date: Thu, 19 Nov 2026 13:15:08 +0100 Subject: [PATCH 1/3] feat: add a dependency scan for organization rulesets dependency-scan.yml runs `uv audit` on every uv.lock that git tracks, looks up the severity of each finding in GitHub's advisory database, and fails on high, critical and unknown-severity findings that .github/vulnerability-exceptions.toml doesn't approve. An organization ruleset requires it on pull requests and in the merge queue; it runs in the target repository, so the script is the body of a step and the workflow is a single file. The tests load the script from the workflow, and lock small projects with known vulnerable packages to run it end to end. CI runs them with actionlint, zizmor and ruff; Dependabot updates the actions. Assisted-by: Pi:claude-opus-5-5 --- .github/dependabot.yml | 12 + .github/workflows/ci.yml | 61 ++++ .github/workflows/dependency-scan.yml | 320 ++++++++++++++++++ README.md | 94 +++++ ...pendency_scan.cpython-315-pytest-9.2.0.pyc | Bin 0 -> 34091 bytes tests/test_dependency_scan.py | 293 ++++++++++++++++ 6 files changed, 780 insertions(+) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/dependency-scan.yml create mode 100644 README.md create mode 100644 tests/__pycache__/test_dependency_scan.cpython-315-pytest-9.2.0.pyc create mode 100644 tests/test_dependency_scan.py diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..811996a --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,12 @@ +version: 2 +updates: + - package-ecosystem: github-actions + directory: / + schedule: + interval: weekly + # a new release waits a week, so a compromised one is likely found and pulled first + cooldown: + default-days: 7 + groups: + github-actions: + patterns: ["*"] diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..764d1b1 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,61 @@ +name: CI + +on: + pull_request: + push: + branches: [main] + +# nothing by default: each job asks for the permissions it needs +permissions: {} + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + lint: + name: Lint + runs-on: ubuntu-slim + permissions: + contents: read + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 + with: + enable-cache: false + + - name: Check the workflows with actionlint + run: uvx --from actionlint-py==1.7.12.25 actionlint + + - name: Audit the workflows with zizmor + run: uvx zizmor@1.32.0 --offline .github + + - name: Lint and check the format of the tests with ruff + run: | + uvx ruff@0.17.5 check tests + uvx ruff@0.17.5 format --check tests + + test: + name: Test + runs-on: ubuntu-slim + permissions: + contents: read + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 + with: + # the same uv as the scan, whose `uv audit` the tests run + version: "0.13.8" + enable-cache: false + + - name: Run the tests + env: + # the advisory database answers without it, with a lower rate limit + GITHUB_TOKEN: ${{ github.token }} + run: uv run --no-project --python 3.13 --with pytest==9.* --with pyyaml==6.* pytest -v diff --git a/.github/workflows/dependency-scan.yml b/.github/workflows/dependency-scan.yml new file mode 100644 index 0000000..ff4756d --- /dev/null +++ b/.github/workflows/dependency-scan.yml @@ -0,0 +1,320 @@ +name: Dependency scan + +# Checks the Python dependencies locked in uv.lock for 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. +on: + pull_request: + merge_group: + +permissions: + contents: read + +jobs: + scan: + name: Dependency scan + runs-on: ubuntu-slim + timeout-minutes: 15 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + # No cache: the job must not restore files that another run could have written. + - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 + with: + # `uv audit` is a preview feature: its output can change between releases, so + # the version is pinned and moved on purpose. + version: "0.13.8" + enable-cache: false + + - name: Scan uv.lock for known vulnerabilities + id: scan + env: + # reads the public advisory database: the token only raises the rate limit + GITHUB_TOKEN: ${{ github.token }} + EXCEPTIONS_FILE: .github/vulnerability-exceptions.toml + # The script is the body of this step, so the workflow is a single file: a ruleset + # runs this file in another repository, where no other file of this one exists. + shell: uv run --no-project --python 3.13 python {0} + run: | + """Scan the uv.lock files of the repository with `uv audit`. + + A finding blocks the merge when its advisory is high or critical, or when its + severity can't be found, unless .github/vulnerability-exceptions.toml lists it. + """ + + from __future__ import annotations + + import json + import os + import subprocess + import sys + import tomllib + import urllib.error + import urllib.request + from dataclasses import dataclass, field + from pathlib import Path + + BLOCKING = {"critical", "high"} + AUDIT = [ + "uv", + "audit", + "--frozen", + "--preview-features", + "audit-command,json-output", + "--output-format", + "json", + ] + + + class ScanError(Exception): + """A problem that stops the scan, as opposed to a finding.""" + + + @dataclass + class Approval: + id: str + issue: str + reason: str + used: bool = False + + + @dataclass + class Finding: + lock: str + package: str + version: str + id: str + aliases: list[str] + summary: str + fix_versions: list[str] + ghsa: str | None = None + severity: str = "unknown" + withdrawn: bool = False + exception: Approval | None = None + ids: set[str] = field(default_factory=set) + + @property + def blocking(self) -> bool: + if self.withdrawn or self.exception is not None: + return False + return self.severity not in {"low", "medium", "moderate"} + + + def load_exceptions(path: Path) -> list[Approval]: + """Read the approved exceptions: an `[[exception]]` table per vulnerability.""" + if not path.is_file(): + return [] + try: + data = tomllib.loads(path.read_text(encoding="utf-8")) + except tomllib.TOMLDecodeError as error: + raise ScanError(f"{path} is not valid TOML: {error}") from error + unknown = set(data) - {"exception"} + if unknown: + raise ScanError(f"{path}: unknown keys {sorted(unknown)}, only [[exception]] tables") + exceptions = [] + for number, entry in enumerate(data.get("exception", []), start=1): + where = f"{path}, exception {number}" + if not isinstance(entry, dict): + raise ScanError(f"{where}: must be an [[exception]] table") + extra = set(entry) - {"id", "issue", "reason"} + if extra: + raise ScanError(f"{where}: unknown keys {sorted(extra)}") + for key in ("id", "issue", "reason"): + value = entry.get(key) + if not isinstance(value, str) or not value.strip(): + raise ScanError(f"{where}: `{key}` is required and can't be empty") + if not entry["issue"].startswith("https://"): + raise ScanError(f"{where}: `issue` must be the link to the issue that approves it") + exceptions.append(Approval(entry["id"].strip(), entry["issue"], entry["reason"])) + return exceptions + + + def lock_files(root: Path) -> list[Path]: + """The uv.lock files tracked by git, so generated or ignored ones don't count.""" + output = subprocess.run( + ["git", "ls-files", "-z", "--", "uv.lock", "*/uv.lock"], + cwd=root, + check=True, + capture_output=True, + text=True, + ).stdout + return sorted(root / name for name in output.split("\0") if name) + + + def audit(lock: Path, root: Path) -> tuple[list[Finding], list[dict]]: + """Run `uv audit` next to LOCK and return its findings and adverse statuses.""" + result = subprocess.run(AUDIT, cwd=lock.parent, capture_output=True, text=True, check=False) + # uv exits with 1 both for findings and for errors: the JSON tells them apart + try: + report = json.loads(result.stdout) + vulnerabilities = report["vulnerabilities"] + except (json.JSONDecodeError, KeyError, TypeError): + raise ScanError( + f"`uv audit` failed for {lock.relative_to(root)}:\n{result.stderr.strip()}" + ) from None + name = str(lock.relative_to(root)) + findings = [ + Finding( + lock=name, + package=item["dependency"]["name"], + version=item["dependency"]["version"], + id=item["id"], + aliases=list(item.get("aliases") or []), + summary=item.get("summary") or "", + fix_versions=list(item.get("fix_versions") or []), + ) + for item in vulnerabilities + ] + return findings, list(report.get("adverse_statuses") or []) + + + def fetch_advisory(ghsa: str) -> dict: + """Read one advisory from GitHub's database.""" + request = urllib.request.Request( + f"https://api.github.com/advisories/{ghsa}", + headers={ + "Accept": "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", + **( + {"Authorization": f"Bearer {os.environ['GITHUB_TOKEN']}"} + if os.environ.get("GITHUB_TOKEN") + else {} + ), + }, + ) + with urllib.request.urlopen(request, timeout=30) as response: + return json.load(response) + + + def classify(findings: list[Finding], exceptions: list[Approval], fetch=fetch_advisory) -> None: + """Fill in the severity and the exception of each finding.""" + cache: dict[str, dict | None] = {} + for finding in findings: + finding.ids = {finding.id, *finding.aliases} + finding.ghsa = next( + (i for i in [finding.id, *finding.aliases] if i.startswith("GHSA-")), None + ) + if finding.ghsa is not None: + if finding.ghsa not in cache: + try: + cache[finding.ghsa] = fetch(finding.ghsa) + except (urllib.error.URLError, TimeoutError, ValueError) as error: + print(f"::warning::Could not read {finding.ghsa}: {error}") + cache[finding.ghsa] = None + advisory = cache[finding.ghsa] + if advisory is not None: + finding.severity = (advisory.get("severity") or "unknown").lower() + finding.withdrawn = advisory.get("withdrawn_at") is not None + for exception in exceptions: + if exception.id in finding.ids: + finding.exception = exception + exception.used = True + break + + + def command(text: str) -> str: + """Escape TEXT for the message of a workflow command (::error:: and the like).""" + return text.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A") + + + def cell(text: str) -> str: + return text.replace("|", "\\|").replace("\n", " ") + + + def report( + findings: list[Finding], exceptions: list[Approval], statuses: list[str], locks: list[str] + ) -> str: + """The job summary, in Markdown.""" + blocking = [f for f in findings if f.blocking] + lines = ["## Dependency scan", ""] + lines.append(f"Scanned {', '.join(f'`{lock}`' for lock in locks)} with `uv audit`.") + lines.append("") + if not findings: + lines.append("No known vulnerabilities.") + else: + lines.append( + f"{len(findings)} finding(s), {len(blocking)} blocking: high, critical or of " + "unknown severity, and not in `.github/vulnerability-exceptions.toml`." + ) + lines += [ + "", + "| | Package | Advisory | Severity | Fixed in | Summary |", + "|---|---|---|---|---|---|", + ] + order = {"critical": 0, "high": 1, "unknown": 2, "moderate": 3, "medium": 3, "low": 4} + for f in sorted( + findings, key=lambda f: (not f.blocking, order.get(f.severity, 2), f.package) + ): + if f.blocking: + status = "❌ blocking" + elif f.withdrawn: + status = "withdrawn" + elif f.exception is not None: + status = f"[excepted]({f.exception.issue})" + else: + status = "reported" + advisory = f.ghsa or f.id + link = f"[{advisory}](https://github.com/advisories/{advisory})" if f.ghsa else advisory + lines.append( + f"| {status} | {cell(f.package)} {cell(f.version)} (`{f.lock}`) | {link} | " + f"{f.severity} | {cell(', '.join(f.fix_versions) or 'no fix yet')} | {cell(f.summary)} |" + ) + unused = [e for e in exceptions if not e.used] + if unused: + lines += ["", "Exceptions that match no finding any more (remove them):", ""] + lines += [f"- `{e.id}` ({e.issue})" for e in unused] + if statuses: + lines += ["", "Other problems that `uv audit` reports (not blocking):", ""] + lines += [f"- {cell(s)}" for s in statuses] + return "\n".join(lines) + "\n" + + + def main() -> int: + root = Path.cwd() + summary = os.environ.get("GITHUB_STEP_SUMMARY") + locks = lock_files(root) + if not locks: + print("::notice title=Dependency scan::No uv.lock in this repository: nothing to scan.") + if summary: + Path(summary).write_text("## Dependency scan\n\nNo `uv.lock`: nothing to scan.\n") + return 0 + try: + exceptions = load_exceptions( + root / os.environ.get("EXCEPTIONS_FILE", ".github/vulnerability-exceptions.toml") + ) + findings, statuses = [], [] + for lock in locks: + found, adverse = audit(lock, root) + findings += found + statuses += [f"`{lock.relative_to(root)}`: {json.dumps(s)}" for s in adverse] + except ScanError as error: + print(f"::error title=Dependency scan::{command(str(error))}") + return 1 + classify(findings, exceptions) + text = report(findings, exceptions, statuses, [str(lock.relative_to(root)) for lock in locks]) + print(text) + if summary: + Path(summary).write_text(text) + for f in findings: + if f.blocking: + fix = f"fixed in {', '.join(f.fix_versions)}" if f.fix_versions else "no fix yet" + print( + f"::error file={f.lock},title={f.severity} vulnerability in {f.package}::" + + command(f"{f.package} {f.version}: {f.ghsa or f.id} ({fix}). {f.summary}") + ) + for e in exceptions: + if not e.used: + print( + f"::warning title=Unused exception::{e.id} matches no finding: remove it ({e.issue})" + ) + return 1 if any(f.blocking for f in findings) else 0 + + + if __name__ == "__main__": + sys.exit(main()) diff --git a/README.md b/README.md new file mode 100644 index 0000000..d04e42c --- /dev/null +++ b/README.md @@ -0,0 +1,94 @@ +# security-workflows + +GitHub Actions workflows that organization rulesets require in Algorithmiq's repositories. +They are public so that one ruleset can run them in every repository, public, internal or +private: a ruleset can only run a workflow whose repository is at least as visible as the +repositories it targets. + +This repository holds generic workflow code only. It must never contain secrets, names of +private repositories or packages, or other organization-specific details: those come from the +repository the workflow runs in. + +## Dependency scan + +[`dependency-scan.yml`](.github/workflows/dependency-scan.yml) checks the Python dependencies +locked in `uv.lock` for known vulnerabilities, on every pull request and in the merge queue. + +### How it works + +A ruleset runs the workflow in the repository of the pull request, with that repository's +files and `GITHUB_TOKEN` (read-only), and ignores the filters of its triggers. The job: + +1. finds the `uv.lock` files that git tracks. With none, it passes with a notice; +2. runs [`uv audit`](https://docs.astral.sh/uv/) next to each one (`--frozen`: it reads the + lock file and installs nothing, so it needs no access to private package indexes). It + audits every package of the lock, including extras and dependency groups; +3. looks up the severity of each finding in + [GitHub's advisory database](https://github.com/advisories), by its GHSA identifier (`uv + audit` reports none); +4. 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 its advisory is **high** or **critical**, or when its +severity is unknown (no GHSA identifier, or the database can't be read): an unknown severity +never lets a vulnerability through. Low and medium findings are listed without failing the +job. A withdrawn advisory never blocks. + +### What leaves the repository + +`uv audit` sends the name and version of every locked package to the +[OSV](https://osv.dev) database, including packages from private indexes: OSV matches by name, +whatever the index. The job also sends the GHSA identifiers of the findings to GitHub's API. +Nothing else leaves the runner. 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. + +### Exceptions + +A finding that is acceptable for a while (no fix yet, code that is never reached, a false +positive) goes in `.github/vulnerability-exceptions.toml` of the repository, one table per +vulnerability: + +```toml +[[exception]] +id = "GHSA-xxxx-xxxx-xxxx" # the GHSA identifier, or any alias that the job summary shows +issue = "https://github.com///issues/" # the issue that approves it +reason = "The vulnerable function is only used by the test suite." +``` + +All three keys are required; any other key is an error. Exceptions are reviewed in pull +requests like any other change, and the history of the file shows who approved what. An +exception that no longer matches a finding gives a warning: remove it. + +### Setting it up (organization owners) + +1. **Settings → Repository → Rulesets → New branch ruleset**, enforcement **Evaluate** at first. +2. **Target repositories**: by custom property, for example the repositories that must have a + dependency scan. **Target branches**: the default branch. The rule blocks direct pushes, so + only target branches that change through pull requests. +3. Rule **Require workflows to pass before merging**: repository + `Algorithmiq/security-workflows`, workflow `.github/workflows/dependency-scan.yml`, and the + **full commit SHA** of a reviewed version as the reference, not a branch: a change here + reaches every targeted repository only when the ruleset moves to it. +4. Once the runs in Evaluate mode look right, switch the enforcement to **Active**. Pull + requests opened before the ruleset existed run the workflow on their next push. + +To move to a new version: review the change here, then update the SHA in the ruleset. + +### Updating uv + +`uv audit` is a preview feature of uv, and its output can change between releases. The +workflow pins uv (`version:` of `setup-uv`), and the tests run the same version. Dependabot +doesn't move that pin: update it by hand, run the tests, and update the ruleset. + +## Development + +The scan is the body of a workflow step (`shell: uv run … python {0}`), so the workflow is one +self-contained file. The tests load the script from the workflow: + +```sh +uv run --no-project --python 3.13 --with pytest --with pyyaml pytest +``` + +The end-to-end tests lock small projects with known vulnerable packages and need the network +(PyPI, OSV and GitHub's API); set `GITHUB_TOKEN` for a higher rate limit, and `OFFLINE=1` to +skip them. Changes need a pull request and a review: every targeted repository runs this code. diff --git a/tests/__pycache__/test_dependency_scan.cpython-315-pytest-9.2.0.pyc b/tests/__pycache__/test_dependency_scan.cpython-315-pytest-9.2.0.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ba3a9271fa0770b0735af900e18c0f573a931bcf GIT binary patch literal 34091 zcmeHwdvF}bdFRaT?0Xk40wh2XeBjg%_c&Rx#Fe?U_# zl~{F``+YsnT@C=sl44!T18n!#J>5M${rJAGzy7|SXUjrLv}O(?(rN6$^!GN54$snJM59Y+MTtspWSJZ1I%xi zgUsI|hnT-ZE@S>~xt#e=%athIapcJ1CGrxjj2^j~-8(JUys%^y_rlUnUaeJko9bKL zPA$;gZjDvp7x|6L%~-kag*wr>F<{1sL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1YaV6N~ky`|#0be=IgI!fc~s5(=f0 zmbK4(v9D|>busPZwW{hm4p?uM66|w`iqD8rJX##nFjLBm?{Kyf11G_Zm6P5lXFVRF z&RW9o(tdhiP(2B|mC3xNHz*25v6=HY9b00Q`b(6wf#SWWEPcl_Mh}$JgIMDKEyWAZ zc6XnaXcfag3#;JZ_AB&G>YT2j_%3o11=vY<>_{1Q&;Z!d3(4W+fa1D*K8ddrJftMF5K%&mQ;BIG5m(vOpVQq<9 z8*>6|MMs-)K={~&X&j8uvTr)DFXw&erf+o5`8kL8wUu982?J9;{JsNU6n^039INLD z0@^9MiEq+sHvv6dijRFh^wV)F554$S*W5}8hOeFCYiEUJzi@08bEUdeNcf<4a_Zv3=Jpk_$N=k)s?S5IxEy6kq3T z*Tk1^XY%#g!yG4Z$PHaW|d%k63E%~NB`MSMtdEZ_Kt2`DnwsN9w68Uzj z dict: + workflow = yaml.safe_load(WORKFLOW.read_text(encoding="utf-8")) + return next(s for s in workflow["jobs"]["scan"]["steps"] if s.get("id") == "scan") + + +@pytest.fixture(scope="session") +def script(tmp_path_factory: pytest.TempPathFactory) -> Path: + path = tmp_path_factory.mktemp("script") / "dependency_scan.py" + path.write_text(step()["run"], encoding="utf-8") + return path + + +@pytest.fixture(scope="session") +def scan(script: Path): + spec = importlib.util.spec_from_file_location("dependency_scan", script) + module = importlib.util.module_from_spec(spec) + sys.modules["dependency_scan"] = module + spec.loader.exec_module(module) + return module + + +# --- the exceptions file --------------------------------------------------------------- + + +def write(path: Path, text: str) -> Path: + path.write_text(textwrap.dedent(text), encoding="utf-8") + return path + + +def test_no_exceptions_file(scan, tmp_path: Path) -> None: + assert scan.load_exceptions(tmp_path / "missing.toml") == [] + + +def test_exceptions_are_read(scan, tmp_path: Path) -> None: + path = write( + tmp_path / "e.toml", + """ + [[exception]] + id = "GHSA-aaaa-bbbb-cccc" + issue = "https://github.com/example/project/issues/1" + reason = "Only the test suite imports it." + """, + ) + [approval] = scan.load_exceptions(path) + assert approval.id == "GHSA-aaaa-bbbb-cccc" + assert approval.issue.endswith("/issues/1") + + +@pytest.mark.parametrize( + ("text", "message"), + [ + ('[[exception]]\nid = "X"\nissue = "https://e/1"\n', "`reason` is required"), + ( + '[[exception]]\nid = ""\nissue = "https://e/1"\nreason = "r"\n', + "`id` is required", + ), + ('[[exception]]\nid = "X"\nissue = "#1"\nreason = "r"\n', "link to the issue"), + ( + '[[exception]]\nid = "X"\nissue = "https://e/1"\nreason = "r"\nuntil = "x"\n', + "unknown keys", + ), + ('[[exceptions]]\nid = "X"\n', "unknown keys"), + ("[[exception]\n", "not valid TOML"), + ], +) +def test_invalid_exceptions(scan, tmp_path: Path, text: str, message: str) -> None: + with pytest.raises(scan.ScanError, match=message): + scan.load_exceptions(write(tmp_path / "e.toml", text)) + + +# --- which findings block --------------------------------------------------------------- + + +def finding(scan, id: str, aliases: list[str] | None = None): + return scan.Finding( + lock="uv.lock", + package="pkg", + version="1.0", + id=id, + aliases=aliases or [], + summary="A problem", + fix_versions=["1.1"], + ) + + +ADVISORIES = { + "GHSA-crit-crit-crit": {"severity": "critical", "withdrawn_at": None}, + "GHSA-high-high-high": {"severity": "high", "withdrawn_at": None}, + "GHSA-medi-medi-medi": {"severity": "medium", "withdrawn_at": None}, + "GHSA-loww-loww-loww": {"severity": "low", "withdrawn_at": None}, + "GHSA-gone-gone-gone": { + "severity": "critical", + "withdrawn_at": "2025-01-01T00:00:00Z", + }, +} + + +def fetch(ghsa: str) -> dict: + if ghsa not in ADVISORIES: + raise urllib.error.URLError("not found") + return ADVISORIES[ghsa] + + +@pytest.mark.parametrize( + ("id", "aliases", "severity", "blocking"), + [ + ("GHSA-crit-crit-crit", [], "critical", True), + ("GHSA-high-high-high", [], "high", True), + ("GHSA-medi-medi-medi", [], "medium", False), + ("GHSA-loww-loww-loww", [], "low", False), + # uv shows the PYSEC id first for some advisories: the GHSA alias is used + ("PYSEC-2021-1", ["CVE-2021-1", "GHSA-medi-medi-medi"], "medium", False), + # no GHSA: the severity is unknown, which blocks + ("PYSEC-2021-2", ["CVE-2021-2"], "unknown", True), + # the advisory can't be read: unknown, which blocks + ("GHSA-miss-miss-miss", [], "unknown", True), + # a withdrawn advisory never blocks + ("GHSA-gone-gone-gone", [], "critical", False), + ], +) +def test_blocking( + scan, id: str, aliases: list[str], severity: str, blocking: bool +) -> None: + f = finding(scan, id, aliases) + scan.classify([f], [], fetch=fetch) + assert (f.severity, f.blocking) == (severity, blocking) + + +def test_an_exception_matches_an_alias(scan) -> None: + approval = scan.Approval("CVE-2021-2", "https://e/1", "reason") + f = finding(scan, "PYSEC-2021-2", ["CVE-2021-2"]) + scan.classify([f], [approval], fetch=fetch) + assert f.exception is approval + assert approval.used + assert not f.blocking + + +def test_the_report_lists_unused_exceptions(scan) -> None: + approval = scan.Approval("GHSA-none-none-none", "https://e/1", "reason") + f = finding(scan, "GHSA-high-high-high") + scan.classify([f], [approval], fetch=fetch) + text = scan.report([f], [approval], [], ["uv.lock"]) + assert "❌ blocking" in text + assert "match no finding" in text + assert "GHSA-none-none-none" in text + + +def test_workflow_command_messages_are_escaped(scan) -> None: + assert scan.command("50% done\nnext") == "50%25 done%0Anext" + + +# --- end to end: uv audit on real lock files --------------------------------------------- + +needs_network = pytest.mark.skipif( + shutil.which("uv") is None or os.environ.get("OFFLINE") == "1", + reason="needs uv and the network", +) + + +def git(cwd: Path, *args: str) -> None: + subprocess.run(["git", *args], cwd=cwd, check=True, capture_output=True) + + +def project(path: Path, dependencies: list[str]) -> Path: + """A git repository with a locked project that depends on DEPENDENCIES.""" + path.mkdir(parents=True) + quoted = ", ".join(f'"{d}"' for d in dependencies) + (path / "pyproject.toml").write_text( + f'[project]\nname = "demo"\nversion = "0"\nrequires-python = ">=3.11"\n' + f"dependencies = [{quoted}]\n", + encoding="utf-8", + ) + subprocess.run(["uv", "lock", "--quiet"], cwd=path, check=True) + git(path, "init", "--quiet") + git(path, "add", "--all") + return path + + +def run(script: Path, cwd: Path) -> subprocess.CompletedProcess: + summary = cwd.parent / f"{cwd.name}-summary.md" + env = { + **os.environ, + "GITHUB_STEP_SUMMARY": str(summary), + "EXCEPTIONS_FILE": ".github/vulnerability-exceptions.toml", + } + result = subprocess.run( + ["uv", "run", "--no-project", "--python", "3.13", "python", str(script)], + cwd=cwd, + env=env, + capture_output=True, + text=True, + check=False, + ) + result.summary = summary.read_text(encoding="utf-8") if summary.exists() else "" + return result + + +def test_nothing_to_scan(script: Path, tmp_path: Path) -> None: + repository = tmp_path / "empty" + repository.mkdir() + git(repository, "init", "--quiet") + result = run(script, repository) + assert result.returncode == 0, result.stdout + result.stderr + assert "nothing to scan" in result.stdout + + +@needs_network +def test_a_clean_lock_passes(script: Path, tmp_path: Path) -> None: + result = run(script, project(tmp_path / "clean", ["packaging>=25"])) + assert result.returncode == 0, result.stdout + result.stderr + assert "No known vulnerabilities." in result.summary + + +@needs_network +def test_a_vulnerable_lock_fails_until_excepted(script: Path, tmp_path: Path) -> None: + # jinja2 2.10 has high-severity advisories, e.g. a sandbox escape + repository = project(tmp_path / "vulnerable", ["jinja2==2.10"]) + result = run(script, repository) + assert result.returncode == 1, result.stdout + result.stderr + assert "::error file=uv.lock,title=high vulnerability in jinja2::" in result.stdout + assert "❌ blocking" in result.summary + + # approving every blocking advisory makes the scan pass, and lists them as excepted + ids = sorted( + { + line.split(": ", 1)[1].split(" ", 1)[0] + for line in result.stdout.splitlines() + if line.startswith("::error file=") + } + ) + assert ids + exceptions = repository / ".github" / "vulnerability-exceptions.toml" + exceptions.parent.mkdir() + exceptions.write_text( + "".join( + f'[[exception]]\nid = "{i}"\nissue = "https://github.com/example/demo/issues/1"\nreason = "Test."\n\n' + for i in ids + ), + encoding="utf-8", + ) + result = run(script, repository) + assert result.returncode == 0, result.stdout + result.stderr + assert "excepted" in result.summary + + +@needs_network +def test_every_tracked_lock_is_scanned(script: Path, tmp_path: Path) -> None: + repository = project(tmp_path / "monorepo", ["packaging>=25"]) + project(repository / "tools", ["jinja2==2.10"]) + shutil.rmtree(repository / "tools" / ".git") + git(repository, "add", "--all") + result = run(script, repository) + assert result.returncode == 1, result.stdout + result.stderr + assert "tools/uv.lock" in result.summary + + +@pytest.mark.skipif(shutil.which("uvx") is None, reason="needs uvx") +def test_the_script_passes_ruff(script: Path) -> None: + for command in (["check"], ["format", "--check"]): + result = subprocess.run( + ["uvx", "ruff@0.17.5", *command, "--line-length", "100", str(script)], + capture_output=True, + text=True, + check=False, + ) + assert result.returncode == 0, result.stdout + result.stderr From 2eaf91d5da44df1c78fffe878554bf64c17473ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Di=20Remigio=20Eik=C3=A5s?= Date: Thu, 19 Nov 2026 13:49:34 +0100 Subject: [PATCH 2/3] refactor: move the scan into scripts/dependency_scan.py The scan is now a script of its own, with inline metadata that uv run reads (Python 3.13, no dependencies). The workflow checks it out from job.workflow_repository at job.workflow_sha, the commit of the workflow file that the ruleset pins, so the script and the workflow can't diverge. uv runs it with --no-config: the uv settings of the scanned repository apply to `uv audit`, not to running the script. Also drops a .pyc file that was committed by mistake, and adds a .gitignore and ruff's line length. Assisted-by: Pi:claude-opus-5-5 --- .github/actionlint.yaml | 6 + .github/workflows/ci.yml | 6 +- .github/workflows/dependency-scan.yml | 300 ++---------------- .gitignore | 3 + README.md | 14 +- ruff.toml | 1 + scripts/dependency_scan.py | 280 ++++++++++++++++ ...pendency_scan.cpython-315-pytest-9.2.0.pyc | Bin 34091 -> 0 bytes tests/test_dependency_scan.py | 45 +-- 9 files changed, 337 insertions(+), 318 deletions(-) create mode 100644 .github/actionlint.yaml create mode 100644 .gitignore create mode 100644 ruff.toml create mode 100644 scripts/dependency_scan.py delete mode 100644 tests/__pycache__/test_dependency_scan.cpython-315-pytest-9.2.0.pyc diff --git a/.github/actionlint.yaml b/.github/actionlint.yaml new file mode 100644 index 0000000..638dac1 --- /dev/null +++ b/.github/actionlint.yaml @@ -0,0 +1,6 @@ +paths: + .github/workflows/dependency-scan.yml: + ignore: + # GitHub documents job.workflow_repository and job.workflow_sha (the repository and + # commit of the workflow file), but actionlint doesn't know them yet + - 'property "workflow_(repository|sha)" is not defined in object type' diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 764d1b1..54ff033 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,10 +33,10 @@ jobs: - name: Audit the workflows with zizmor run: uvx zizmor@1.32.0 --offline .github - - name: Lint and check the format of the tests with ruff + - name: Lint and check the format of the Python code with ruff run: | - uvx ruff@0.17.5 check tests - uvx ruff@0.17.5 format --check tests + uvx ruff@0.17.5 check scripts tests + uvx ruff@0.17.5 format --check scripts tests test: name: Test diff --git a/.github/workflows/dependency-scan.yml b/.github/workflows/dependency-scan.yml index ff4756d..da70a35 100644 --- a/.github/workflows/dependency-scan.yml +++ b/.github/workflows/dependency-scan.yml @@ -6,7 +6,8 @@ name: Dependency scan # 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. +# by itself whether there is anything to scan. The scan is scripts/dependency_scan.py of this +# repository, taken from the same commit as this file. on: pull_request: merge_group: @@ -20,10 +21,23 @@ jobs: runs-on: ubuntu-slim timeout-minutes: 15 steps: + # the repository of the pull request, which the scan reads - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false + # The script of the scan. With a ruleset, this workflow runs in another repository: + # `job.workflow_repository` and `job.workflow_sha` name this repository and the commit + # of this file, which the ruleset pins, so the script can't differ from the reviewed one. + # It goes next to the files of the pull request, untracked, so the scan doesn't see it. + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: ${{ job.workflow_repository }} + ref: ${{ job.workflow_sha }} + path: .dependency-scan + sparse-checkout: scripts + persist-credentials: false + # No cache: the job must not restore files that another run could have written. - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: @@ -33,288 +47,10 @@ jobs: enable-cache: false - name: Scan uv.lock for known vulnerabilities - id: scan env: # reads the public advisory database: the token only raises the rate limit GITHUB_TOKEN: ${{ github.token }} EXCEPTIONS_FILE: .github/vulnerability-exceptions.toml - # The script is the body of this step, so the workflow is a single file: a ruleset - # runs this file in another repository, where no other file of this one exists. - shell: uv run --no-project --python 3.13 python {0} - run: | - """Scan the uv.lock files of the repository with `uv audit`. - - A finding blocks the merge when its advisory is high or critical, or when its - severity can't be found, unless .github/vulnerability-exceptions.toml lists it. - """ - - from __future__ import annotations - - import json - import os - import subprocess - import sys - import tomllib - import urllib.error - import urllib.request - from dataclasses import dataclass, field - from pathlib import Path - - BLOCKING = {"critical", "high"} - AUDIT = [ - "uv", - "audit", - "--frozen", - "--preview-features", - "audit-command,json-output", - "--output-format", - "json", - ] - - - class ScanError(Exception): - """A problem that stops the scan, as opposed to a finding.""" - - - @dataclass - class Approval: - id: str - issue: str - reason: str - used: bool = False - - - @dataclass - class Finding: - lock: str - package: str - version: str - id: str - aliases: list[str] - summary: str - fix_versions: list[str] - ghsa: str | None = None - severity: str = "unknown" - withdrawn: bool = False - exception: Approval | None = None - ids: set[str] = field(default_factory=set) - - @property - def blocking(self) -> bool: - if self.withdrawn or self.exception is not None: - return False - return self.severity not in {"low", "medium", "moderate"} - - - def load_exceptions(path: Path) -> list[Approval]: - """Read the approved exceptions: an `[[exception]]` table per vulnerability.""" - if not path.is_file(): - return [] - try: - data = tomllib.loads(path.read_text(encoding="utf-8")) - except tomllib.TOMLDecodeError as error: - raise ScanError(f"{path} is not valid TOML: {error}") from error - unknown = set(data) - {"exception"} - if unknown: - raise ScanError(f"{path}: unknown keys {sorted(unknown)}, only [[exception]] tables") - exceptions = [] - for number, entry in enumerate(data.get("exception", []), start=1): - where = f"{path}, exception {number}" - if not isinstance(entry, dict): - raise ScanError(f"{where}: must be an [[exception]] table") - extra = set(entry) - {"id", "issue", "reason"} - if extra: - raise ScanError(f"{where}: unknown keys {sorted(extra)}") - for key in ("id", "issue", "reason"): - value = entry.get(key) - if not isinstance(value, str) or not value.strip(): - raise ScanError(f"{where}: `{key}` is required and can't be empty") - if not entry["issue"].startswith("https://"): - raise ScanError(f"{where}: `issue` must be the link to the issue that approves it") - exceptions.append(Approval(entry["id"].strip(), entry["issue"], entry["reason"])) - return exceptions - - - def lock_files(root: Path) -> list[Path]: - """The uv.lock files tracked by git, so generated or ignored ones don't count.""" - output = subprocess.run( - ["git", "ls-files", "-z", "--", "uv.lock", "*/uv.lock"], - cwd=root, - check=True, - capture_output=True, - text=True, - ).stdout - return sorted(root / name for name in output.split("\0") if name) - - - def audit(lock: Path, root: Path) -> tuple[list[Finding], list[dict]]: - """Run `uv audit` next to LOCK and return its findings and adverse statuses.""" - result = subprocess.run(AUDIT, cwd=lock.parent, capture_output=True, text=True, check=False) - # uv exits with 1 both for findings and for errors: the JSON tells them apart - try: - report = json.loads(result.stdout) - vulnerabilities = report["vulnerabilities"] - except (json.JSONDecodeError, KeyError, TypeError): - raise ScanError( - f"`uv audit` failed for {lock.relative_to(root)}:\n{result.stderr.strip()}" - ) from None - name = str(lock.relative_to(root)) - findings = [ - Finding( - lock=name, - package=item["dependency"]["name"], - version=item["dependency"]["version"], - id=item["id"], - aliases=list(item.get("aliases") or []), - summary=item.get("summary") or "", - fix_versions=list(item.get("fix_versions") or []), - ) - for item in vulnerabilities - ] - return findings, list(report.get("adverse_statuses") or []) - - - def fetch_advisory(ghsa: str) -> dict: - """Read one advisory from GitHub's database.""" - request = urllib.request.Request( - f"https://api.github.com/advisories/{ghsa}", - headers={ - "Accept": "application/vnd.github+json", - "X-GitHub-Api-Version": "2022-11-28", - **( - {"Authorization": f"Bearer {os.environ['GITHUB_TOKEN']}"} - if os.environ.get("GITHUB_TOKEN") - else {} - ), - }, - ) - with urllib.request.urlopen(request, timeout=30) as response: - return json.load(response) - - - def classify(findings: list[Finding], exceptions: list[Approval], fetch=fetch_advisory) -> None: - """Fill in the severity and the exception of each finding.""" - cache: dict[str, dict | None] = {} - for finding in findings: - finding.ids = {finding.id, *finding.aliases} - finding.ghsa = next( - (i for i in [finding.id, *finding.aliases] if i.startswith("GHSA-")), None - ) - if finding.ghsa is not None: - if finding.ghsa not in cache: - try: - cache[finding.ghsa] = fetch(finding.ghsa) - except (urllib.error.URLError, TimeoutError, ValueError) as error: - print(f"::warning::Could not read {finding.ghsa}: {error}") - cache[finding.ghsa] = None - advisory = cache[finding.ghsa] - if advisory is not None: - finding.severity = (advisory.get("severity") or "unknown").lower() - finding.withdrawn = advisory.get("withdrawn_at") is not None - for exception in exceptions: - if exception.id in finding.ids: - finding.exception = exception - exception.used = True - break - - - def command(text: str) -> str: - """Escape TEXT for the message of a workflow command (::error:: and the like).""" - return text.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A") - - - def cell(text: str) -> str: - return text.replace("|", "\\|").replace("\n", " ") - - - def report( - findings: list[Finding], exceptions: list[Approval], statuses: list[str], locks: list[str] - ) -> str: - """The job summary, in Markdown.""" - blocking = [f for f in findings if f.blocking] - lines = ["## Dependency scan", ""] - lines.append(f"Scanned {', '.join(f'`{lock}`' for lock in locks)} with `uv audit`.") - lines.append("") - if not findings: - lines.append("No known vulnerabilities.") - else: - lines.append( - f"{len(findings)} finding(s), {len(blocking)} blocking: high, critical or of " - "unknown severity, and not in `.github/vulnerability-exceptions.toml`." - ) - lines += [ - "", - "| | Package | Advisory | Severity | Fixed in | Summary |", - "|---|---|---|---|---|---|", - ] - order = {"critical": 0, "high": 1, "unknown": 2, "moderate": 3, "medium": 3, "low": 4} - for f in sorted( - findings, key=lambda f: (not f.blocking, order.get(f.severity, 2), f.package) - ): - if f.blocking: - status = "❌ blocking" - elif f.withdrawn: - status = "withdrawn" - elif f.exception is not None: - status = f"[excepted]({f.exception.issue})" - else: - status = "reported" - advisory = f.ghsa or f.id - link = f"[{advisory}](https://github.com/advisories/{advisory})" if f.ghsa else advisory - lines.append( - f"| {status} | {cell(f.package)} {cell(f.version)} (`{f.lock}`) | {link} | " - f"{f.severity} | {cell(', '.join(f.fix_versions) or 'no fix yet')} | {cell(f.summary)} |" - ) - unused = [e for e in exceptions if not e.used] - if unused: - lines += ["", "Exceptions that match no finding any more (remove them):", ""] - lines += [f"- `{e.id}` ({e.issue})" for e in unused] - if statuses: - lines += ["", "Other problems that `uv audit` reports (not blocking):", ""] - lines += [f"- {cell(s)}" for s in statuses] - return "\n".join(lines) + "\n" - - - def main() -> int: - root = Path.cwd() - summary = os.environ.get("GITHUB_STEP_SUMMARY") - locks = lock_files(root) - if not locks: - print("::notice title=Dependency scan::No uv.lock in this repository: nothing to scan.") - if summary: - Path(summary).write_text("## Dependency scan\n\nNo `uv.lock`: nothing to scan.\n") - return 0 - try: - exceptions = load_exceptions( - root / os.environ.get("EXCEPTIONS_FILE", ".github/vulnerability-exceptions.toml") - ) - findings, statuses = [], [] - for lock in locks: - found, adverse = audit(lock, root) - findings += found - statuses += [f"`{lock.relative_to(root)}`: {json.dumps(s)}" for s in adverse] - except ScanError as error: - print(f"::error title=Dependency scan::{command(str(error))}") - return 1 - classify(findings, exceptions) - text = report(findings, exceptions, statuses, [str(lock.relative_to(root)) for lock in locks]) - print(text) - if summary: - Path(summary).write_text(text) - for f in findings: - if f.blocking: - fix = f"fixed in {', '.join(f.fix_versions)}" if f.fix_versions else "no fix yet" - print( - f"::error file={f.lock},title={f.severity} vulnerability in {f.package}::" - + command(f"{f.package} {f.version}: {f.ghsa or f.id} ({fix}). {f.summary}") - ) - for e in exceptions: - if not e.used: - print( - f"::warning title=Unused exception::{e.id} matches no finding: remove it ({e.issue})" - ) - return 1 if any(f.blocking for f in findings) else 0 - - - if __name__ == "__main__": - sys.exit(main()) + # --no-config: the uv settings of the repository (uv.toml, [tool.uv]) apply to `uv + # audit`, which the script runs next to each uv.lock, but not to running the script + run: uv run --no-config --no-project .dependency-scan/scripts/dependency_scan.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b267296 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +.pytest_cache/ +.ruff_cache/ diff --git a/README.md b/README.md index d04e42c..d4f3a30 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,11 @@ locked in `uv.lock` for known vulnerabilities, on every pull request and in the ### How it works A ruleset runs the workflow in the repository of the pull request, with that repository's -files and `GITHUB_TOKEN` (read-only), and ignores the filters of its triggers. The job: +files and `GITHUB_TOKEN` (read-only), and ignores the filters of its triggers. The scan is +[`scripts/dependency_scan.py`](scripts/dependency_scan.py) of this repository: the job checks +it out from the commit of the workflow file (`job.workflow_repository`, `job.workflow_sha`), +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. runs [`uv audit`](https://docs.astral.sh/uv/) next to each one (`--frozen`: it reads the @@ -82,13 +86,13 @@ doesn't move that pin: update it by hand, run the tests, and update the ruleset. ## Development -The scan is the body of a workflow step (`shell: uv run … python {0}`), so the workflow is one -self-contained file. The tests load the script from the workflow: +The tests run `scripts/dependency_scan.py` as the workflow does: ```sh -uv run --no-project --python 3.13 --with pytest --with pyyaml pytest +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, OSV and GitHub's API); set `GITHUB_TOKEN` for a higher rate limit, and `OFFLINE=1` to -skip them. Changes need a pull request and a review: every targeted repository runs this code. +skip them. CI also runs ruff, actionlint and zizmor. Changes need a pull request and a review: +every targeted repository runs this code. diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 0000000..71de868 --- /dev/null +++ b/ruff.toml @@ -0,0 +1 @@ +line-length = 100 diff --git a/scripts/dependency_scan.py b/scripts/dependency_scan.py new file mode 100644 index 0000000..bfbe63c --- /dev/null +++ b/scripts/dependency_scan.py @@ -0,0 +1,280 @@ +# /// script +# requires-python = ">=3.13" +# dependencies = [] +# /// +"""Scan the uv.lock files of the repository with `uv audit`. + +A finding blocks the merge when its advisory is high or critical, or when its +severity can't be found, unless .github/vulnerability-exceptions.toml lists it. +""" + +from __future__ import annotations + +import json +import os +import subprocess +import sys +import tomllib +import urllib.error +import urllib.request +from dataclasses import dataclass, field +from pathlib import Path + +BLOCKING = {"critical", "high"} +AUDIT = [ + "uv", + "audit", + "--frozen", + "--preview-features", + "audit-command,json-output", + "--output-format", + "json", +] + + +class ScanError(Exception): + """A problem that stops the scan, as opposed to a finding.""" + + +@dataclass +class Approval: + id: str + issue: str + reason: str + used: bool = False + + +@dataclass +class Finding: + lock: str + package: str + version: str + id: str + aliases: list[str] + summary: str + fix_versions: list[str] + ghsa: str | None = None + severity: str = "unknown" + withdrawn: bool = False + exception: Approval | None = None + ids: set[str] = field(default_factory=set) + + @property + def blocking(self) -> bool: + if self.withdrawn or self.exception is not None: + return False + return self.severity not in {"low", "medium", "moderate"} + + +def load_exceptions(path: Path) -> list[Approval]: + """Read the approved exceptions: an `[[exception]]` table per vulnerability.""" + if not path.is_file(): + return [] + try: + data = tomllib.loads(path.read_text(encoding="utf-8")) + except tomllib.TOMLDecodeError as error: + raise ScanError(f"{path} is not valid TOML: {error}") from error + unknown = set(data) - {"exception"} + if unknown: + raise ScanError(f"{path}: unknown keys {sorted(unknown)}, only [[exception]] tables") + exceptions = [] + for number, entry in enumerate(data.get("exception", []), start=1): + where = f"{path}, exception {number}" + if not isinstance(entry, dict): + raise ScanError(f"{where}: must be an [[exception]] table") + extra = set(entry) - {"id", "issue", "reason"} + if extra: + raise ScanError(f"{where}: unknown keys {sorted(extra)}") + for key in ("id", "issue", "reason"): + value = entry.get(key) + if not isinstance(value, str) or not value.strip(): + raise ScanError(f"{where}: `{key}` is required and can't be empty") + if not entry["issue"].startswith("https://"): + raise ScanError(f"{where}: `issue` must be the link to the issue that approves it") + exceptions.append(Approval(entry["id"].strip(), entry["issue"], entry["reason"])) + return exceptions + + +def lock_files(root: Path) -> list[Path]: + """The uv.lock files tracked by git, so generated or ignored ones don't count.""" + output = subprocess.run( + ["git", "ls-files", "-z", "--", "uv.lock", "*/uv.lock"], + cwd=root, + check=True, + capture_output=True, + text=True, + ).stdout + return sorted(root / name for name in output.split("\0") if name) + + +def audit(lock: Path, root: Path) -> tuple[list[Finding], list[dict]]: + """Run `uv audit` next to LOCK and return its findings and adverse statuses.""" + result = subprocess.run(AUDIT, cwd=lock.parent, capture_output=True, text=True, check=False) + # uv exits with 1 both for findings and for errors: the JSON tells them apart + try: + report = json.loads(result.stdout) + vulnerabilities = report["vulnerabilities"] + except (json.JSONDecodeError, KeyError, TypeError): + raise ScanError( + f"`uv audit` failed for {lock.relative_to(root)}:\n{result.stderr.strip()}" + ) from None + name = str(lock.relative_to(root)) + findings = [ + Finding( + lock=name, + package=item["dependency"]["name"], + version=item["dependency"]["version"], + id=item["id"], + aliases=list(item.get("aliases") or []), + summary=item.get("summary") or "", + fix_versions=list(item.get("fix_versions") or []), + ) + for item in vulnerabilities + ] + return findings, list(report.get("adverse_statuses") or []) + + +def fetch_advisory(ghsa: str) -> dict: + """Read one advisory from GitHub's database.""" + request = urllib.request.Request( + f"https://api.github.com/advisories/{ghsa}", + headers={ + "Accept": "application/vnd.github+json", + "X-GitHub-Api-Version": "2022-11-28", + **( + {"Authorization": f"Bearer {os.environ['GITHUB_TOKEN']}"} + if os.environ.get("GITHUB_TOKEN") + else {} + ), + }, + ) + with urllib.request.urlopen(request, timeout=30) as response: + return json.load(response) + + +def classify(findings: list[Finding], exceptions: list[Approval], fetch=fetch_advisory) -> None: + """Fill in the severity and the exception of each finding.""" + cache: dict[str, dict | None] = {} + for finding in findings: + finding.ids = {finding.id, *finding.aliases} + finding.ghsa = next( + (i for i in [finding.id, *finding.aliases] if i.startswith("GHSA-")), None + ) + if finding.ghsa is not None: + if finding.ghsa not in cache: + try: + cache[finding.ghsa] = fetch(finding.ghsa) + except (urllib.error.URLError, TimeoutError, ValueError) as error: + print(f"::warning::Could not read {finding.ghsa}: {error}") + cache[finding.ghsa] = None + advisory = cache[finding.ghsa] + if advisory is not None: + finding.severity = (advisory.get("severity") or "unknown").lower() + finding.withdrawn = advisory.get("withdrawn_at") is not None + for exception in exceptions: + if exception.id in finding.ids: + finding.exception = exception + exception.used = True + break + + +def command(text: str) -> str: + """Escape TEXT for the message of a workflow command (::error:: and the like).""" + return text.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A") + + +def cell(text: str) -> str: + return text.replace("|", "\\|").replace("\n", " ") + + +def report( + findings: list[Finding], exceptions: list[Approval], statuses: list[str], locks: list[str] +) -> str: + """The job summary, in Markdown.""" + blocking = [f for f in findings if f.blocking] + lines = ["## Dependency scan", ""] + lines.append(f"Scanned {', '.join(f'`{lock}`' for lock in locks)} with `uv audit`.") + lines.append("") + if not findings: + lines.append("No known vulnerabilities.") + else: + lines.append( + f"{len(findings)} finding(s), {len(blocking)} blocking: high, critical or of " + "unknown severity, and not in `.github/vulnerability-exceptions.toml`." + ) + lines += [ + "", + "| | Package | Advisory | Severity | Fixed in | Summary |", + "|---|---|---|---|---|---|", + ] + order = {"critical": 0, "high": 1, "unknown": 2, "moderate": 3, "medium": 3, "low": 4} + for f in sorted( + findings, key=lambda f: (not f.blocking, order.get(f.severity, 2), f.package) + ): + if f.blocking: + status = "❌ blocking" + elif f.withdrawn: + status = "withdrawn" + elif f.exception is not None: + status = f"[excepted]({f.exception.issue})" + else: + status = "reported" + advisory = f.ghsa or f.id + link = f"[{advisory}](https://github.com/advisories/{advisory})" if f.ghsa else advisory + lines.append( + f"| {status} | {cell(f.package)} {cell(f.version)} (`{f.lock}`) | {link} | " + f"{f.severity} | {cell(', '.join(f.fix_versions) or 'no fix yet')} | {cell(f.summary)} |" + ) + unused = [e for e in exceptions if not e.used] + if unused: + lines += ["", "Exceptions that match no finding any more (remove them):", ""] + lines += [f"- `{e.id}` ({e.issue})" for e in unused] + if statuses: + lines += ["", "Other problems that `uv audit` reports (not blocking):", ""] + lines += [f"- {cell(s)}" for s in statuses] + return "\n".join(lines) + "\n" + + +def main() -> int: + root = Path.cwd() + summary = os.environ.get("GITHUB_STEP_SUMMARY") + locks = lock_files(root) + if not locks: + print("::notice title=Dependency scan::No uv.lock in this repository: nothing to scan.") + if summary: + Path(summary).write_text("## Dependency scan\n\nNo `uv.lock`: nothing to scan.\n") + return 0 + try: + exceptions = load_exceptions( + root / os.environ.get("EXCEPTIONS_FILE", ".github/vulnerability-exceptions.toml") + ) + findings, statuses = [], [] + for lock in locks: + found, adverse = audit(lock, root) + findings += found + statuses += [f"`{lock.relative_to(root)}`: {json.dumps(s)}" for s in adverse] + except ScanError as error: + print(f"::error title=Dependency scan::{command(str(error))}") + return 1 + classify(findings, exceptions) + text = report(findings, exceptions, statuses, [str(lock.relative_to(root)) for lock in locks]) + print(text) + if summary: + Path(summary).write_text(text) + for f in findings: + if f.blocking: + fix = f"fixed in {', '.join(f.fix_versions)}" if f.fix_versions else "no fix yet" + print( + f"::error file={f.lock},title={f.severity} vulnerability in {f.package}::" + + command(f"{f.package} {f.version}: {f.ghsa or f.id} ({fix}). {f.summary}") + ) + for e in exceptions: + if not e.used: + print( + f"::warning title=Unused exception::{e.id} matches no finding: remove it ({e.issue})" + ) + return 1 if any(f.blocking for f in findings) else 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/__pycache__/test_dependency_scan.cpython-315-pytest-9.2.0.pyc b/tests/__pycache__/test_dependency_scan.cpython-315-pytest-9.2.0.pyc deleted file mode 100644 index ba3a9271fa0770b0735af900e18c0f573a931bcf..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 34091 zcmeHwdvF}bdFRaT?0Xk40wh2XeBjg%_c&Rx#Fe?U_# zl~{F``+YsnT@C=sl44!T18n!#J>5M${rJAGzy7|SXUjrLv}O(?(rN6$^!GN54$snJM59Y+MTtspWSJZ1I%xi zgUsI|hnT-ZE@S>~xt#e=%athIapcJ1CGrxjj2^j~-8(JUys%^y_rlUnUaeJko9bKL zPA$;gZjDvp7x|6L%~-kag*wr>F<{1sLQ%+N4av(zcMT_cYPbs(a{UX{-8;;DI<$%t zy`pccRy)y;^Z`n**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?uM66|w`iqD8rJX##nFjLBm?{Kyf11G_Zm6P5lXFVRF z&RW9o(tdhiP(2B|mC3xNHz*25v6=HY9b00Q`b(6wf#SWWEPcl_Mh}$JgIMDKEyWAZ zc6XnaXcfag3#;JZ_AB&G>YT2j_%3o11=vY<>_{1Q&;Z!d3(4W+fa1D*K8ddrJftMF5K%&mQ;BIG5m(vOpVQq<9 z8*>6|MMs-)K={~&X&j8uvTr)DFXw&erf+o5`8kL8wUu982?J9;{JsNU6n^039INLD z0@^9MiEq+sHvv6dijRFh^wV)F554$S*W5}8hOeFCYiEUJzi@08bEUdeNcf<4a_Zv3=Jpk_$N=k)s?S5IxEy6kq3T z*Tk1^XY%#g!yG4Z$PHaW|d%k63E%~NB`MSMtdEZ_Kt2`DnwsN9w68Uzj z dict: - workflow = yaml.safe_load(WORKFLOW.read_text(encoding="utf-8")) - return next(s for s in workflow["jobs"]["scan"]["steps"] if s.get("id") == "scan") +@pytest.fixture(scope="session") +def script() -> Path: + return SCRIPT -@pytest.fixture(scope="session") -def script(tmp_path_factory: pytest.TempPathFactory) -> Path: - path = tmp_path_factory.mktemp("script") / "dependency_scan.py" - path.write_text(step()["run"], encoding="utf-8") - return path +def test_the_workflow_runs_the_script() -> None: + """The workflow checks out this repository's scripts/ and runs the scan from there.""" + steps = yaml.safe_load(WORKFLOW.read_text(encoding="utf-8"))["jobs"]["scan"]["steps"] + own = next( + s for s in steps if s.get("with", {}).get("repository") == "${{ job.workflow_repository }}" + ) + assert own["with"]["ref"] == "${{ job.workflow_sha }}" + path = own["with"]["path"] + run = steps[-1]["run"] + assert run.endswith(f"{path}/{SCRIPT.relative_to(ROOT)}") @pytest.fixture(scope="session") @@ -145,9 +148,7 @@ def fetch(ghsa: str) -> dict: ("GHSA-gone-gone-gone", [], "critical", False), ], ) -def test_blocking( - scan, id: str, aliases: list[str], severity: str, blocking: bool -) -> None: +def test_blocking(scan, id: str, aliases: list[str], severity: str, blocking: bool) -> None: f = finding(scan, id, aliases) scan.classify([f], [], fetch=fetch) assert (f.severity, f.blocking) == (severity, blocking) @@ -211,7 +212,7 @@ def run(script: Path, cwd: Path) -> subprocess.CompletedProcess: "EXCEPTIONS_FILE": ".github/vulnerability-exceptions.toml", } result = subprocess.run( - ["uv", "run", "--no-project", "--python", "3.13", "python", str(script)], + ["uv", "run", "--no-config", "--no-project", str(script)], cwd=cwd, env=env, capture_output=True, @@ -279,15 +280,3 @@ def test_every_tracked_lock_is_scanned(script: Path, tmp_path: Path) -> None: result = run(script, repository) assert result.returncode == 1, result.stdout + result.stderr assert "tools/uv.lock" in result.summary - - -@pytest.mark.skipif(shutil.which("uvx") is None, reason="needs uvx") -def test_the_script_passes_ruff(script: Path) -> None: - for command in (["check"], ["format", "--check"]): - result = subprocess.run( - ["uvx", "ruff@0.17.5", *command, "--line-length", "100", str(script)], - capture_output=True, - text=True, - check=False, - ) - assert result.returncode == 0, result.stdout + result.stderr From 6ebd9e80031d676d35684d0192d07ee7fe254558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberto=20Di=20Remigio=20Eik=C3=A5s?= Date: Thu, 19 Nov 2026 14:00:19 +0100 Subject: [PATCH 3/3] refactor: query OSV directly instead of running uv audit The script reads the packages of each uv.lock itself and asks OSV's batch API about their names and versions, with Python's default User-Agent: nothing else leaves the runner. The severity comes from the GHSA records that OSV mirrors, so GitHub's API and the token are no longer needed, and OSV's duplicate records of one vulnerability (GHSA, PYSEC) are merged, the GHSA one kept. This drops the dependency on `uv audit`, a preview feature whose output could change between uv releases, and on the uv user agent, which describes the runner. The results match `uv audit` on the test projects. Assisted-by: Pi:claude-opus-5-5 --- .github/workflows/ci.yml | 5 - .github/workflows/dependency-scan.yml | 18 +- README.md | 45 ++--- scripts/dependency_scan.py | 269 +++++++++++++++++--------- tests/test_dependency_scan.py | 172 +++++++++++----- 5 files changed, 327 insertions(+), 182 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 54ff033..a3e051d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,12 +50,7 @@ jobs: - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: - # the same uv as the scan, whose `uv audit` the tests run - version: "0.13.8" enable-cache: false - name: Run the tests - env: - # the advisory database answers without it, with a lower rate limit - GITHUB_TOKEN: ${{ github.token }} run: uv run --no-project --python 3.13 --with pytest==9.* --with pyyaml==6.* pytest -v diff --git a/.github/workflows/dependency-scan.yml b/.github/workflows/dependency-scan.yml index da70a35..3b45f59 100644 --- a/.github/workflows/dependency-scan.yml +++ b/.github/workflows/dependency-scan.yml @@ -1,7 +1,7 @@ name: Dependency scan -# Checks the Python dependencies locked in uv.lock for known vulnerabilities, on every pull -# request and in the merge queue. An organization ruleset requires it ("Require workflows to +# 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. # @@ -38,19 +38,15 @@ jobs: sparse-checkout: scripts persist-credentials: false - # No cache: the job must not restore files that another run could have written. + # runs the script with the Python of its inline metadata; no cache: the job must not + # restore files that another run could have written - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: - # `uv audit` is a preview feature: its output can change between releases, so - # the version is pinned and moved on purpose. - version: "0.13.8" enable-cache: false - - name: Scan uv.lock for known vulnerabilities + - name: Check uv.lock against OSV env: - # reads the public advisory database: the token only raises the rate limit - GITHUB_TOKEN: ${{ github.token }} EXCEPTIONS_FILE: .github/vulnerability-exceptions.toml - # --no-config: the uv settings of the repository (uv.toml, [tool.uv]) apply to `uv - # audit`, which the script runs next to each uv.lock, but not to running the script + # --no-config: the uv settings of the repository (uv.toml, [tool.uv]) are not for the + # script, and --no-project: the repository's project isn't needed to run it run: uv run --no-config --no-project .dependency-scan/scripts/dependency_scan.py diff --git a/README.md b/README.md index d4f3a30..22ea0e6 100644 --- a/README.md +++ b/README.md @@ -11,8 +11,9 @@ repository the workflow runs in. ## Dependency scan -[`dependency-scan.yml`](.github/workflows/dependency-scan.yml) checks the Python dependencies -locked in `uv.lock` for known vulnerabilities, on every pull request and in the merge queue. +[`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. ### How it works @@ -24,27 +25,28 @@ which the ruleset pins, and runs it with `uv run` (Python 3.13, standard library inline metadata says). The script: 1. finds the `uv.lock` files that git tracks. With none, it passes with a notice; -2. runs [`uv audit`](https://docs.astral.sh/uv/) next to each one (`--frozen`: it reads the - lock file and installs nothing, so it needs no access to private package indexes). It - audits every package of the lock, including extras and dependency groups; -3. looks up the severity of each finding in - [GitHub's advisory database](https://github.com/advisories), by its GHSA identifier (`uv - audit` reports none); +2. reads the packages of each one 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 that blocks. -A finding **blocks the merge** when its advisory is **high** or **critical**, or when its -severity is unknown (no GHSA identifier, or the database can't be read): an unknown severity -never lets a vulnerability through. Low and medium findings are listed without failing the -job. A withdrawn advisory never blocks. +A finding **blocks the merge** when its severity is **high** or **critical**, or unknown (no +GHSA record): an unknown severity never lets a vulnerability through. Low and moderate +findings are listed without failing the job. A withdrawn advisory never blocks. If OSV can't +be read, the job fails. ### What leaves the repository -`uv audit` sends the name and version of every locked package to the -[OSV](https://osv.dev) database, including packages from private indexes: OSV matches by name, -whatever the index. The job also sends the GHSA identifiers of the findings to GitHub's API. -Nothing else leaves the runner. 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`) 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. ### Exceptions @@ -78,12 +80,6 @@ exception that no longer matches a finding gives a warning: remove it. To move to a new version: review the change here, then update the SHA in the ruleset. -### Updating uv - -`uv audit` is a preview feature of uv, and its output can change between releases. The -workflow pins uv (`version:` of `setup-uv`), and the tests run the same version. Dependabot -doesn't move that pin: update it by hand, run the tests, and update the ruleset. - ## Development The tests run `scripts/dependency_scan.py` as the workflow does: @@ -93,6 +89,5 @@ 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, OSV and GitHub's API); set `GITHUB_TOKEN` for a higher rate limit, and `OFFLINE=1` to -skip them. CI also runs ruff, actionlint and zizmor. Changes need a pull request and a review: +(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. diff --git a/scripts/dependency_scan.py b/scripts/dependency_scan.py index bfbe63c..2f6d909 100644 --- a/scripts/dependency_scan.py +++ b/scripts/dependency_scan.py @@ -2,10 +2,12 @@ # requires-python = ">=3.13" # dependencies = [] # /// -"""Scan the uv.lock files of the repository with `uv audit`. +"""Check the packages locked in the uv.lock files of the repository against OSV. -A finding blocks the merge when its advisory is high or critical, or when its -severity can't be found, unless .github/vulnerability-exceptions.toml lists it. +Each package from a package index is looked up by name and version in the OSV database +(https://osv.dev), which is all that leaves the runner. A finding blocks the merge when its +severity is high or critical, or unknown, unless .github/vulnerability-exceptions.toml lists +it. The severity is GitHub's, from the GHSA records that OSV mirrors. """ from __future__ import annotations @@ -17,19 +19,14 @@ import tomllib import urllib.error import urllib.request +from collections.abc import Callable +from concurrent.futures import ThreadPoolExecutor from dataclasses import dataclass, field from pathlib import Path -BLOCKING = {"critical", "high"} -AUDIT = [ - "uv", - "audit", - "--frozen", - "--preview-features", - "audit-command,json-output", - "--output-format", - "json", -] +OSV = "https://api.osv.dev/v1" +# OSV answers at most this many queries per batch +BATCH = 1000 class ScanError(Exception): @@ -53,17 +50,84 @@ class Finding: aliases: list[str] summary: str fix_versions: list[str] - ghsa: str | None = None severity: str = "unknown" withdrawn: bool = False exception: Approval | None = None - ids: set[str] = field(default_factory=set) + + @property + def ids(self) -> set[str]: + return {self.id, *self.aliases} + + @property + def ghsa(self) -> str | None: + return next((i for i in [self.id, *self.aliases] if i.startswith("GHSA-")), None) @property def blocking(self) -> bool: if self.withdrawn or self.exception is not None: return False - return self.severity not in {"low", "medium", "moderate"} + return self.severity not in {"low", "moderate", "medium"} + + +@dataclass(frozen=True) +class Package: + lock: str + name: str + version: str + + +@dataclass +class Osv: + """OSV's API: the batch query, and the full records, read once each.""" + + post: Callable[[str, dict], dict] + get: Callable[[str], dict] + records: dict[str, dict] = field(default_factory=dict) + + def vulnerabilities(self, packages: list[Package]) -> list[list[str]]: + """The ids of the vulnerabilities of each package, in the order of PACKAGES.""" + ids: list[list[str]] = [[] for _ in packages] + pending = [(index, None) for index in range(len(packages))] + while pending: + batch, pending = pending[:BATCH], pending[BATCH:] + queries = [ + { + "package": {"name": packages[i].name, "ecosystem": "PyPI"}, + "version": packages[i].version, + **({"page_token": token} if token else {}), + } + for i, token in batch + ] + results = self.post(f"{OSV}/querybatch", {"queries": queries})["results"] + for (index, _), result in zip(batch, results, strict=True): + ids[index] += [vuln["id"] for vuln in result.get("vulns", [])] + if result.get("next_page_token"): + pending.append((index, result["next_page_token"])) + return ids + + def record(self, id: str) -> dict: + if id not in self.records: + self.records[id] = self.get(f"{OSV}/vulns/{id}") + return self.records[id] + + def prefetch(self, ids: set[str]) -> None: + missing = sorted(ids - set(self.records)) + with ThreadPoolExecutor(max_workers=8) as pool: + for id, record in zip(missing, pool.map(self.record, missing), strict=True): + self.records[id] = record + + +def http_get(url: str) -> dict: + with urllib.request.urlopen(url, timeout=30) as response: + return json.load(response) + + +def http_post(url: str, body: dict) -> dict: + request = urllib.request.Request( + url, data=json.dumps(body).encode(), headers={"Content-Type": "application/json"} + ) + with urllib.request.urlopen(request, timeout=60) as response: + return json.load(response) def load_exceptions(path: Path) -> list[Approval]: @@ -107,70 +171,88 @@ def lock_files(root: Path) -> list[Path]: return sorted(root / name for name in output.split("\0") if name) -def audit(lock: Path, root: Path) -> tuple[list[Finding], list[dict]]: - """Run `uv audit` next to LOCK and return its findings and adverse statuses.""" - result = subprocess.run(AUDIT, cwd=lock.parent, capture_output=True, text=True, check=False) - # uv exits with 1 both for findings and for errors: the JSON tells them apart +def locked_packages(lock: Path, root: Path) -> list[Package]: + """The packages of LOCK that come from a package index, with their versions. + + The project itself and the packages from git, a URL or a local path have no version on + PyPI to look up, so they're left out. + """ try: - report = json.loads(result.stdout) - vulnerabilities = report["vulnerabilities"] - except (json.JSONDecodeError, KeyError, TypeError): - raise ScanError( - f"`uv audit` failed for {lock.relative_to(root)}:\n{result.stderr.strip()}" - ) from None + data = tomllib.loads(lock.read_text(encoding="utf-8")) + except tomllib.TOMLDecodeError as error: + raise ScanError(f"{lock.relative_to(root)} is not valid TOML: {error}") from error name = str(lock.relative_to(root)) - findings = [ - Finding( - lock=name, - package=item["dependency"]["name"], - version=item["dependency"]["version"], - id=item["id"], - aliases=list(item.get("aliases") or []), - summary=item.get("summary") or "", - fix_versions=list(item.get("fix_versions") or []), - ) - for item in vulnerabilities - ] - return findings, list(report.get("adverse_statuses") or []) + return sorted( + { + Package(name, package["name"], package["version"]) + for package in data.get("package", []) + if "registry" in package.get("source", {}) and "version" in package + }, + key=lambda p: (p.name, p.version), + ) -def fetch_advisory(ghsa: str) -> dict: - """Read one advisory from GitHub's database.""" - request = urllib.request.Request( - f"https://api.github.com/advisories/{ghsa}", - headers={ - "Accept": "application/vnd.github+json", - "X-GitHub-Api-Version": "2022-11-28", - **( - {"Authorization": f"Bearer {os.environ['GITHUB_TOKEN']}"} - if os.environ.get("GITHUB_TOKEN") - else {} - ), - }, +def normalize(name: str) -> str: + return name.lower().replace("_", "-").replace(".", "-") + + +def fix_versions(record: dict, package: str) -> list[str]: + """The versions that fix RECORD for PACKAGE, from its affected ranges.""" + return sorted( + { + event["fixed"] + for affected in record.get("affected", []) + if normalize(affected.get("package", {}).get("name", "")) == normalize(package) + for range_ in affected.get("ranges", []) + for event in range_.get("events", []) + if "fixed" in event + } ) - with urllib.request.urlopen(request, timeout=30) as response: - return json.load(response) -def classify(findings: list[Finding], exceptions: list[Approval], fetch=fetch_advisory) -> None: +def find(packages: list[Package], osv: Osv) -> list[Finding]: + """The vulnerabilities of PACKAGES, one finding per vulnerability and package. + + OSV lists a vulnerability once per database (GHSA, PYSEC, …), the records naming each + other as aliases: the GHSA record is kept, as it has the severity. + """ + ids = osv.vulnerabilities(packages) + osv.prefetch({i for found in ids for i in found}) + findings = [] + for package, found in zip(packages, ids, strict=True): + seen: set[str] = set() + for id in sorted(found, key=lambda i: (not i.startswith("GHSA-"), i)): + record = osv.record(id) + aliases = [a for a in record.get("aliases", []) if a != id] + if seen & {id, *aliases}: + continue + seen |= {id, *aliases} + findings.append( + Finding( + lock=package.lock, + package=package.name, + version=package.version, + id=id, + aliases=aliases, + summary=record.get("summary") or record.get("details", "").split("\n")[0], + fix_versions=fix_versions(record, package.name), + withdrawn=record.get("withdrawn") is not None, + ) + ) + return findings + + +def classify(findings: list[Finding], exceptions: list[Approval], osv: Osv) -> None: """Fill in the severity and the exception of each finding.""" - cache: dict[str, dict | None] = {} for finding in findings: - finding.ids = {finding.id, *finding.aliases} - finding.ghsa = next( - (i for i in [finding.id, *finding.aliases] if i.startswith("GHSA-")), None - ) if finding.ghsa is not None: - if finding.ghsa not in cache: - try: - cache[finding.ghsa] = fetch(finding.ghsa) - except (urllib.error.URLError, TimeoutError, ValueError) as error: - print(f"::warning::Could not read {finding.ghsa}: {error}") - cache[finding.ghsa] = None - advisory = cache[finding.ghsa] - if advisory is not None: - finding.severity = (advisory.get("severity") or "unknown").lower() - finding.withdrawn = advisory.get("withdrawn_at") is not None + try: + record = osv.record(finding.ghsa) + except (urllib.error.URLError, TimeoutError, ValueError) as error: + print(f"::warning::Could not read {finding.ghsa}: {error}") + else: + severity = (record.get("database_specific") or {}).get("severity") + finding.severity = (severity or "unknown").lower() for exception in exceptions: if exception.id in finding.ids: finding.exception = exception @@ -187,13 +269,11 @@ def cell(text: str) -> str: return text.replace("|", "\\|").replace("\n", " ") -def report( - findings: list[Finding], exceptions: list[Approval], statuses: list[str], locks: list[str] -) -> str: +def report(findings: list[Finding], exceptions: list[Approval], locks: list[str]) -> str: """The job summary, in Markdown.""" blocking = [f for f in findings if f.blocking] lines = ["## Dependency scan", ""] - lines.append(f"Scanned {', '.join(f'`{lock}`' for lock in locks)} with `uv audit`.") + lines.append(f"Checked {', '.join(f'`{lock}`' for lock in locks)} against OSV.") lines.append("") if not findings: lines.append("No known vulnerabilities.") @@ -208,9 +288,11 @@ def report( "|---|---|---|---|---|---|", ] order = {"critical": 0, "high": 1, "unknown": 2, "moderate": 3, "medium": 3, "low": 4} - for f in sorted( - findings, key=lambda f: (not f.blocking, order.get(f.severity, 2), f.package) - ): + + def key(f: Finding) -> tuple: + return (not f.blocking, order.get(f.severity, 2), f.package) + + for f in sorted(findings, key=key): if f.blocking: status = "❌ blocking" elif f.withdrawn: @@ -219,23 +301,20 @@ def report( status = f"[excepted]({f.exception.issue})" else: status = "reported" - advisory = f.ghsa or f.id - link = f"[{advisory}](https://github.com/advisories/{advisory})" if f.ghsa else advisory + link = f"[{f.id}](https://osv.dev/vulnerability/{f.id})" + fixed = cell(", ".join(f.fix_versions) or "no fix yet") lines.append( f"| {status} | {cell(f.package)} {cell(f.version)} (`{f.lock}`) | {link} | " - f"{f.severity} | {cell(', '.join(f.fix_versions) or 'no fix yet')} | {cell(f.summary)} |" + f"{f.severity} | {fixed} | {cell(f.summary)} |" ) unused = [e for e in exceptions if not e.used] if unused: lines += ["", "Exceptions that match no finding any more (remove them):", ""] lines += [f"- `{e.id}` ({e.issue})" for e in unused] - if statuses: - lines += ["", "Other problems that `uv audit` reports (not blocking):", ""] - lines += [f"- {cell(s)}" for s in statuses] return "\n".join(lines) + "\n" -def main() -> int: +def main(osv: Osv | None = None) -> int: root = Path.cwd() summary = os.environ.get("GITHUB_STEP_SUMMARY") locks = lock_files(root) @@ -244,20 +323,20 @@ def main() -> int: if summary: Path(summary).write_text("## Dependency scan\n\nNo `uv.lock`: nothing to scan.\n") return 0 + osv = osv or Osv(post=http_post, get=http_get) + exceptions_file = os.environ.get("EXCEPTIONS_FILE", ".github/vulnerability-exceptions.toml") try: - exceptions = load_exceptions( - root / os.environ.get("EXCEPTIONS_FILE", ".github/vulnerability-exceptions.toml") - ) - findings, statuses = [], [] - for lock in locks: - found, adverse = audit(lock, root) - findings += found - statuses += [f"`{lock.relative_to(root)}`: {json.dumps(s)}" for s in adverse] + exceptions = load_exceptions(root / exceptions_file) + packages = [package for lock in locks for package in locked_packages(lock, root)] + findings = find(packages, osv) except ScanError as error: print(f"::error title=Dependency scan::{command(str(error))}") return 1 - classify(findings, exceptions) - text = report(findings, exceptions, statuses, [str(lock.relative_to(root)) for lock in locks]) + except (urllib.error.URLError, TimeoutError, ValueError, KeyError) as error: + print(f"::error title=Dependency scan::OSV could not be read: {command(str(error))}") + return 1 + classify(findings, exceptions, osv) + text = report(findings, exceptions, [str(lock.relative_to(root)) for lock in locks]) print(text) if summary: Path(summary).write_text(text) @@ -266,7 +345,7 @@ def main() -> int: fix = f"fixed in {', '.join(f.fix_versions)}" if f.fix_versions else "no fix yet" print( f"::error file={f.lock},title={f.severity} vulnerability in {f.package}::" - + command(f"{f.package} {f.version}: {f.ghsa or f.id} ({fix}). {f.summary}") + + command(f"{f.package} {f.version}: {f.id} ({fix}). {f.summary}") ) for e in exceptions: if not e.used: diff --git a/tests/test_dependency_scan.py b/tests/test_dependency_scan.py index e383752..41d1e38 100644 --- a/tests/test_dependency_scan.py +++ b/tests/test_dependency_scan.py @@ -1,7 +1,7 @@ """Tests of scripts/dependency_scan.py, the scan of .github/workflows/dependency-scan.yml. Run with: uv run --no-project --with pytest --with pyyaml pytest -The end-to-end tests need the network (PyPI, OSV and GitHub's advisory database). +The end-to-end tests need the network (PyPI and OSV). """ from __future__ import annotations @@ -101,63 +101,110 @@ def test_invalid_exceptions(scan, tmp_path: Path, text: str, message: str) -> No # --- which findings block --------------------------------------------------------------- -def finding(scan, id: str, aliases: list[str] | None = None): - return scan.Finding( - lock="uv.lock", - package="pkg", - version="1.0", - id=id, - aliases=aliases or [], - summary="A problem", - fix_versions=["1.1"], - ) +def osv_record(id: str, aliases: list[str], severity: str | None = None, **extra) -> dict: + record = { + "id": id, + "aliases": aliases, + "summary": f"Summary of {id}", + "affected": [ + { + "package": {"name": "Pkg", "ecosystem": "PyPI"}, + "ranges": [ + {"type": "ECOSYSTEM", "events": [{"introduced": "0"}, {"fixed": "1.1"}]} + ], + } + ], + **extra, + } + if severity: + record["database_specific"] = {"severity": severity} + return record + + +RECORDS = { + r["id"]: r + for r in [ + osv_record("GHSA-crit-crit-crit", [], "CRITICAL"), + osv_record("GHSA-high-high-high", ["PYSEC-2020-1"], "HIGH"), + osv_record("PYSEC-2020-1", ["GHSA-high-high-high"]), + osv_record("GHSA-medi-medi-medi", [], "MODERATE"), + osv_record("GHSA-loww-loww-loww", [], "LOW"), + osv_record("PYSEC-2021-2", ["CVE-2021-2"]), + osv_record("PYSEC-2021-3", ["GHSA-only-alia-sed1"]), + osv_record("GHSA-only-alia-sed1", ["PYSEC-2021-3"], "MODERATE"), + osv_record("GHSA-gone-gone-gone", [], "CRITICAL", withdrawn="2025-01-01T00:00:00Z"), + ] +} -ADVISORIES = { - "GHSA-crit-crit-crit": {"severity": "critical", "withdrawn_at": None}, - "GHSA-high-high-high": {"severity": "high", "withdrawn_at": None}, - "GHSA-medi-medi-medi": {"severity": "medium", "withdrawn_at": None}, - "GHSA-loww-loww-loww": {"severity": "low", "withdrawn_at": None}, - "GHSA-gone-gone-gone": { - "severity": "critical", - "withdrawn_at": "2025-01-01T00:00:00Z", - }, -} +def fake_osv(scan, found: dict[tuple[str, str], list[str]]): + """An OSV whose batch query answers FOUND, by (name, version), and with RECORDS.""" + queries = [] + def post(url: str, body: dict) -> dict: + assert url.endswith("/querybatch") + queries.extend(body["queries"]) + return { + "results": [ + {"vulns": [{"id": i} for i in found.get((q["package"]["name"], q["version"]), [])]} + for q in body["queries"] + ] + } -def fetch(ghsa: str) -> dict: - if ghsa not in ADVISORIES: - raise urllib.error.URLError("not found") - return ADVISORIES[ghsa] + def get(url: str) -> dict: + id = url.rsplit("/", 1)[1] + if id not in RECORDS: + raise urllib.error.URLError("not found") + return RECORDS[id] + + osv = scan.Osv(post=post, get=get) + osv.queries = queries + return osv + + +def scan_one(scan, ids: list[str], exceptions=()) -> list: + package = scan.Package("uv.lock", "pkg", "1.0") + osv = fake_osv(scan, {("pkg", "1.0"): ids}) + findings = scan.find([package], osv) + scan.classify(findings, list(exceptions), osv) + return findings @pytest.mark.parametrize( - ("id", "aliases", "severity", "blocking"), + ("ids", "severity", "blocking"), [ - ("GHSA-crit-crit-crit", [], "critical", True), - ("GHSA-high-high-high", [], "high", True), - ("GHSA-medi-medi-medi", [], "medium", False), - ("GHSA-loww-loww-loww", [], "low", False), - # uv shows the PYSEC id first for some advisories: the GHSA alias is used - ("PYSEC-2021-1", ["CVE-2021-1", "GHSA-medi-medi-medi"], "medium", False), + (["GHSA-crit-crit-crit"], "critical", True), + (["GHSA-high-high-high"], "high", True), + (["GHSA-medi-medi-medi"], "moderate", False), + (["GHSA-loww-loww-loww"], "low", False), + # only a PYSEC id, whose GHSA alias has the severity + (["PYSEC-2021-3"], "moderate", False), # no GHSA: the severity is unknown, which blocks - ("PYSEC-2021-2", ["CVE-2021-2"], "unknown", True), - # the advisory can't be read: unknown, which blocks - ("GHSA-miss-miss-miss", [], "unknown", True), + (["PYSEC-2021-2"], "unknown", True), # a withdrawn advisory never blocks - ("GHSA-gone-gone-gone", [], "critical", False), + (["GHSA-gone-gone-gone"], "critical", False), ], ) -def test_blocking(scan, id: str, aliases: list[str], severity: str, blocking: bool) -> None: - f = finding(scan, id, aliases) - scan.classify([f], [], fetch=fetch) +def test_blocking(scan, ids: list[str], severity: str, blocking: bool) -> None: + [f] = scan_one(scan, ids) assert (f.severity, f.blocking) == (severity, blocking) +def test_aliases_are_one_finding(scan) -> None: + """OSV lists a vulnerability once per database: the GHSA record is kept.""" + [f] = scan_one(scan, ["PYSEC-2020-1", "GHSA-high-high-high"]) + assert f.id == "GHSA-high-high-high" + assert f.fix_versions == ["1.1"] + + +def test_an_unreadable_record_stops_the_scan(scan) -> None: + with pytest.raises(urllib.error.URLError): + scan_one(scan, ["GHSA-miss-miss-miss"]) + + def test_an_exception_matches_an_alias(scan) -> None: approval = scan.Approval("CVE-2021-2", "https://e/1", "reason") - f = finding(scan, "PYSEC-2021-2", ["CVE-2021-2"]) - scan.classify([f], [approval], fetch=fetch) + [f] = scan_one(scan, ["PYSEC-2021-2"], [approval]) assert f.exception is approval assert approval.used assert not f.blocking @@ -165,23 +212,56 @@ def test_an_exception_matches_an_alias(scan) -> None: def test_the_report_lists_unused_exceptions(scan) -> None: approval = scan.Approval("GHSA-none-none-none", "https://e/1", "reason") - f = finding(scan, "GHSA-high-high-high") - scan.classify([f], [approval], fetch=fetch) - text = scan.report([f], [approval], [], ["uv.lock"]) + findings = scan_one(scan, ["GHSA-high-high-high"], [approval]) + text = scan.report(findings, [approval], ["uv.lock"]) assert "❌ blocking" in text assert "match no finding" in text assert "GHSA-none-none-none" in text +def test_only_packages_from_an_index_are_looked_up(scan, tmp_path: Path) -> None: + lock = write( + tmp_path / "uv.lock", + """ + version = 1 + [[package]] + name = "demo" + version = "0.1.0" + source = { editable = "." } + [[package]] + name = "pkg" + version = "1.0" + source = { registry = "https://pypi.org/simple" } + [[package]] + name = "private" + version = "2.0" + source = { registry = "https://packages.example.com/simple/" } + [[package]] + name = "fromgit" + version = "0.3" + source = { git = "https://github.com/example/fromgit?rev=abc#abc" } + """, + ) + packages = scan.locked_packages(lock, tmp_path) + assert [(p.name, p.version) for p in packages] == [("pkg", "1.0"), ("private", "2.0")] + osv = fake_osv(scan, {}) + scan.find(packages, osv) + # only the name, the ecosystem and the version are sent + assert osv.queries == [ + {"package": {"name": "pkg", "ecosystem": "PyPI"}, "version": "1.0"}, + {"package": {"name": "private", "ecosystem": "PyPI"}, "version": "2.0"}, + ] + + def test_workflow_command_messages_are_escaped(scan) -> None: assert scan.command("50% done\nnext") == "50%25 done%0Anext" -# --- end to end: uv audit on real lock files --------------------------------------------- +# --- end to end: real lock files and OSV -------------------------------------------------- needs_network = pytest.mark.skipif( shutil.which("uv") is None or os.environ.get("OFFLINE") == "1", - reason="needs uv and the network", + reason="needs uv (to lock the projects) and the network", )