From ced45f4f7f8f66ff5b2efe3a340712c0fec750d5 Mon Sep 17 00:00:00 2001 From: Andrei Merlescu Date: Wed, 18 Nov 2026 08:51:10 -0500 Subject: [PATCH] Updated pipeline for vulnerability checking to release gate on failing RUSTSEC noticies until they are patched Signed-off-by: Andrei Merlescu --- .github/advisories.toml | 26 +++ .github/workflows/ci.yml | 17 +- .github/workflows/release.yml | 14 +- .github/workflows/vulnerabilities.yml | 87 +++++++ packaging/advisory_gate.py | 319 ++++++++++++++++++++++++++ packaging/test_advisory_gate.py | 162 +++++++++++++ 6 files changed, 615 insertions(+), 10 deletions(-) create mode 100644 .github/advisories.toml create mode 100644 .github/workflows/vulnerabilities.yml create mode 100644 packaging/advisory_gate.py create mode 100644 packaging/test_advisory_gate.py diff --git a/.github/advisories.toml b/.github/advisories.toml new file mode 100644 index 00000000..7d5345c5 --- /dev/null +++ b/.github/advisories.toml @@ -0,0 +1,26 @@ +# Vulnerability advisories the maintainers have looked at and decided to ship +# with for now (packaging/advisory_gate.py, .github/workflows/vulnerabilities.yml). +# +# An accepted advisory is still reported (a notice in every run and a row in +# the job summary) but never fails the check, until its review_by date. After +# that date it counts as unreviewed again, so an acceptance cannot outlive the +# reasons for it unnoticed. Accept an advisory only with a reason a reviewer can +# check: why Graphicle is not exposed (the affected function is never called, +# the feature is off) or why shipping now is still the safer choice, and what +# would end the acceptance (a fixed release of the dependency). +# +# Each entry: +# id the RustSec id +# reason why it is acceptable, and what ends the acceptance +# review_by a date (no quotes) within about three months +# crates optional: only these crates (graphicle, graphicle-graph, +# graphicle-memory); all three when absent +# +# [[accept]] +# id = "RUSTSEC-YYYY-NNNN" +# reason = "..." +# review_by = 2027-02-15 +# crates = ["graphicle-graph"] +# +# Advisories the crates' deny.toml files ignore (the zstd and the unmaintained +# entries) never reach this check; move them here to give them review dates. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1c416cd2..0783c0d2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,7 +5,8 @@ # formatted, linted and tested on macOS (Apple silicon) and Linux (x86-64), in # its own job: the three build folders together would not fit a runner's disk. # The windows job builds, lints and tests each crate on Windows (x86-64, -# preview); that job says which crates' tests gate. +# preview); that job says which crates' tests gate. Vulnerability advisories +# have their own workflow, vulnerabilities.yml. name: ci on: @@ -198,13 +199,15 @@ jobs: run: | cargo install --locked cargo-deny@0.20.2 cargo install --locked --features cli cargo-about@0.9.2 - - name: Engine dependencies (licences, advisories, sources) - working-directory: crates/graphicle-graph - run: cargo deny check - - name: Memory library and program dependency licences + # Licences, banned crates and allowed sources gate every crate here. + # Vulnerability advisories are not a licence question: the + # vulnerabilities workflow ranks them by severity and decides pass, warn + # or fail (.github/workflows/vulnerabilities.yml). + - name: Dependency licences, bans and sources run: | - (cd crates/graphicle-memory && cargo deny check licenses) - (cd crates/graphicle && cargo deny check licenses) + (cd crates/graphicle-graph && cargo deny check bans licenses sources) + (cd crates/graphicle-memory && cargo deny check bans licenses sources) + (cd crates/graphicle && cargo deny check bans licenses sources) - name: Adapted-file headers working-directory: crates/graphicle-graph run: python3 tools/mark_adapted_files.py --check diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 28e98cf5..1791840d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -225,10 +225,18 @@ jobs: id: done run: echo "signed=true" >> "$GITHUB_OUTPUT" + # Before anything is published: no high or critical vulnerability that is + # neither fixed nor accepted in .github/advisories.toml. + vulnerabilities: + uses: ./.github/workflows/vulnerabilities.yml + with: + mode: release + release: - needs: [build, sign-windows] - # Goes ahead after a successful build whatever became of the signing. - if: ${{ !cancelled() && needs.build.result == 'success' }} + needs: [build, sign-windows, vulnerabilities] + # Goes ahead after a successful build and vulnerability check, whatever + # became of the signing. + if: ${{ !cancelled() && needs.build.result == 'success' && needs.vulnerabilities.result == 'success' }} runs-on: ubuntu-latest permissions: contents: write diff --git a/.github/workflows/vulnerabilities.yml b/.github/workflows/vulnerabilities.yml new file mode 100644 index 00000000..22671054 --- /dev/null +++ b/.github/workflows/vulnerabilities.yml @@ -0,0 +1,87 @@ +# Known vulnerabilities in each crate's dependencies: RustSec advisories, found +# by cargo-deny in the crate's own Cargo.lock and ranked by +# packaging/advisory_gate.py, which decides pass, warn or fail. +# +# A vulnerability is ranked by its CVSS score, or, when RustSec has none (about +# half have none), by its category. Unmaintained, unsound, notice and yanked +# findings are only ever warnings. What fails depends on what is checked: +# +# pull request a high or critical vulnerability the pull request brings in. +# One already on the base branch is a warning: the pull request +# did not cause it, and its author should not have to fix it. +# main, daily a high or critical vulnerability with a fixed version (a +# `cargo update -p` fixes it). Without a fixed version, a +# warning: nobody here can fix it, and it must not stop work. +# release every high or critical vulnerability that is not fixed or +# accepted in .github/advisories.toml (release.yml calls this +# workflow before it publishes). +# +# An advisory accepted in .github/advisories.toml, with a reason and a review +# date, is reported but never fails until that date. +name: vulnerabilities + +on: + push: + branches: [main] + pull_request: + # Daily, so that an advisory published against main is seen within a day, + # not at the next push. + schedule: + - cron: "23 5 * * *" + workflow_dispatch: + inputs: + mode: + description: "pr, main or release" + default: main + workflow_call: + inputs: + mode: + type: string + default: release + +permissions: + contents: read + +env: + RUST_TOOLCHAIN: "1.95.0" + CARGO_DENY_VERSION: "0.20.2" + +jobs: + advisories: + name: vulnerabilities (${{ matrix.crate }}) + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + crate: [graphicle, graphicle-graph, graphicle-memory] + steps: + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 + with: + persist-credentials: false + # A pull request is compared with its base branch's lockfile. + fetch-depth: 0 + - name: Toolchain + run: rustup toolchain install "$RUST_TOOLCHAIN" --profile minimal && rustup default "$RUST_TOOLCHAIN" + - name: Cache cargo-deny + id: deny_cache + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/.cargo/bin/cargo-deny + key: cargo-deny-${{ env.CARGO_DENY_VERSION }}-${{ runner.os }} + - name: cargo-deny + if: steps.deny_cache.outputs.cache-hit != 'true' + run: cargo install --locked "cargo-deny@$CARGO_DENY_VERSION" + - name: Gate tests + if: matrix.crate == 'graphicle' + run: python3 packaging/test_advisory_gate.py + - name: Advisories + env: + CRATE: ${{ matrix.crate }} + MODE: ${{ inputs.mode || (github.event_name == 'pull_request' && 'pr') || 'main' }} + BASE_REF: ${{ github.base_ref }} + run: | + if [ "$MODE" = pr ]; then + python3 packaging/advisory_gate.py --crate "crates/$CRATE" --mode pr --base "origin/$BASE_REF" + else + python3 packaging/advisory_gate.py --crate "crates/$CRATE" --mode "$MODE" + fi diff --git a/packaging/advisory_gate.py b/packaging/advisory_gate.py new file mode 100644 index 00000000..324dd2be --- /dev/null +++ b/packaging/advisory_gate.py @@ -0,0 +1,319 @@ +#!/usr/bin/env python3 +"""Rank the RustSec advisories that cargo-deny finds in one crate's lockfile +and decide pass, warn or fail, by severity, by whether a fixed version +exists, and by what is being checked (a pull request, main, or a release). + + python3 packaging/advisory_gate.py --crate crates/graphicle --mode pr --base origin/main + python3 packaging/advisory_gate.py --crate crates/graphicle --mode main + python3 packaging/advisory_gate.py --crate crates/graphicle --mode release + +Policy (.github/workflows/vulnerabilities.yml says the same): + + A vulnerability is ranked critical, high, medium or low by its CVSS score. + Nearly half of RustSec's vulnerabilities have no score; those are ranked + by category (code execution, memory corruption, privilege escalation: + high; anything else: medium). Unmaintained, unsound, notice and yanked + findings are never more than a warning. + + pr fails only for a high or critical vulnerability the pull request + brings in (not in the base branch's lockfile). One already on the + base branch is a warning: the pull request did not cause it. + main fails for a high or critical vulnerability that has a fixed + version (`cargo update -p` fixes it). Without one, a warning: an + upstream problem nobody here can fix must not stop the project. + release fails for every high or critical vulnerability that is neither + fixed nor accepted. + + .github/advisories.toml lists advisories the maintainers looked at and + decided to ship with, each with a reason and a review_by date. An accepted + advisory is reported, never fails, until its review_by date passes; then + it counts as unreviewed again. + +Every finding is printed as a GitHub annotation and as a row of the step +summary. Exit status: 0 pass or warn, 1 fail, 2 the check itself could not +run (cargo-deny or its advisory database failed): that is not a verdict. +""" + +from __future__ import annotations + +import argparse +import datetime as dt +import json +import math +import os +import subprocess +import sys +import tempfile +import tomllib +from dataclasses import dataclass, field +from pathlib import Path + +RANK = {"none": 0, "low": 1, "medium": 2, "high": 3, "critical": 4} +# Categories that, without a CVSS score, rank a vulnerability high. +SEVERE_CATEGORIES = {"code-execution", "memory-corruption", "privilege-escalation"} + + +# ---------------------------------------------------------------- severity + + +def _roundup(value: float) -> float: + """CVSS 3.1 Roundup: the smallest number with one decimal >= value.""" + whole = round(value * 100000) + if whole % 10000 == 0: + return whole / 100000.0 + return (math.floor(whole / 10000) + 1) / 10.0 + + +def cvss3_score(vector: str) -> float: + """The CVSS 3.0/3.1 base score of a vector string.""" + m = dict(part.split(":", 1) for part in vector.split("/")[1:]) + changed = m["S"] == "C" + av = {"N": 0.85, "A": 0.62, "L": 0.55, "P": 0.2}[m["AV"]] + ac = {"L": 0.77, "H": 0.44}[m["AC"]] + pr = {"N": 0.85, "L": 0.68 if changed else 0.62, "H": 0.5 if changed else 0.27}[m["PR"]] + ui = {"N": 0.85, "R": 0.62}[m["UI"]] + cia = {"H": 0.56, "L": 0.22, "N": 0.0} + iss = 1 - (1 - cia[m["C"]]) * (1 - cia[m["I"]]) * (1 - cia[m["A"]]) + impact = 7.52 * (iss - 0.029) - 3.25 * (iss - 0.02) ** 15 if changed else 6.42 * iss + if impact <= 0: + return 0.0 + exploitability = 8.22 * av * ac * pr * ui + total = impact + exploitability + return _roundup(min(1.08 * total if changed else total, 10)) + + +def cvss4_rank(vector: str) -> str: + """A CVSS 4.0 vector's rank, approximated without the specification's + lookup tables: by the impact on the vulnerable and subsequent systems, + raised to critical when it needs no access, attack requirement, privilege + or user. Errs high, never low.""" + m = dict(part.split(":", 1) for part in vector.split("/")[1:]) + impacts = [m.get(k, "N") for k in ("VC", "VI", "VA", "SC", "SI", "SA")] + if "H" in impacts: + easy = (m.get("AV"), m.get("AT"), m.get("PR"), m.get("UI")) == ("N", "N", "N", "N") + return "critical" if easy and impacts[:3].count("H") >= 2 else "high" + return "medium" if "L" in impacts else "low" + + +def score_rank(score: float) -> str: + if score >= 9.0: + return "critical" + if score >= 7.0: + return "high" + if score >= 4.0: + return "medium" + return "low" if score > 0 else "none" + + +def severity(advisory: dict) -> tuple[str, str]: + """(rank, how it was ranked) of a vulnerability advisory.""" + vector = advisory.get("cvss") or "" + if vector.startswith("CVSS:3"): + score = cvss3_score(vector) + return score_rank(score), f"CVSS {score}" + if vector.startswith("CVSS:4"): + return cvss4_rank(vector), "CVSS 4.0 (approximate)" + categories = set(advisory.get("categories") or []) + if categories & SEVERE_CATEGORIES: + return "high", "no CVSS; category " + ", ".join(sorted(categories & SEVERE_CATEGORIES)) + return "medium", "no CVSS" + (f"; category {', '.join(sorted(categories))}" if categories else "") + + +# ---------------------------------------------------------------- findings + + +@dataclass +class Finding: + id: str + kind: str # vulnerability, unsound, unmaintained, notice, yanked, ... + package: str + version: str + title: str + fix: str | None # "Upgrade to >=1.1.33", or None when no fixed version exists + rank: str = "none" + ranked_by: str = "" + introduced: bool = True + decision: str = "pass" + why: str = "" + accepted: dict | None = field(default=None, repr=False) + + @property + def key(self) -> tuple[str, str]: + return (self.id, self.package) + + +def run_deny(crate: Path) -> list[str]: + """cargo-deny's JSON lines for the advisories check of one crate.""" + proc = subprocess.run( + ["cargo", "deny", "--format", "json", "check", "advisories"], + cwd=crate, capture_output=True, text=True, + ) + lines = proc.stderr.splitlines() + proc.stdout.splitlines() + if not any('"type":"summary"' in line.replace(" ", "") for line in lines): + sys.stderr.write(proc.stderr[-4000:]) + raise RuntimeError(f"cargo deny produced no summary in {crate} (exit {proc.returncode})") + return lines + + +def parse(lines: list[str]) -> list[Finding]: + found: dict[tuple[str, str, str], Finding] = {} + for line in lines: + try: + event = json.loads(line) + except json.JSONDecodeError: + continue + if event.get("type") != "diagnostic": + continue + f = event.get("fields", {}) + code = f.get("code", "") + advisory = f.get("advisory") or {} + krate = ((f.get("graphs") or [{}])[0].get("Krate")) or {} + package = krate.get("name") or advisory.get("package") or "?" + version = krate.get("version") or "?" + notes = f.get("notes") or [] + fix = next((n.removeprefix("Solution: ") for n in notes if n.startswith("Solution: Upgrade")), None) + ident = advisory.get("id") or code + finding = Finding(ident, code, package, version, advisory.get("title") or f.get("message", ""), fix) + if code == "vulnerability": + finding.rank, finding.ranked_by = severity(advisory) + found[(ident, package, version)] = finding + return list(found.values()) + + +def base_findings(crate: Path, base: str) -> set[tuple[str, str]] | None: + """The (advisory, package) pairs cargo-deny finds on the base ref, from a + temporary worktree; None when the crate does not exist there.""" + repo = Path(subprocess.check_output(["git", "rev-parse", "--show-toplevel"], cwd=crate, text=True).strip()) + relative = crate.resolve().relative_to(repo) + with tempfile.TemporaryDirectory() as tmp: + tree = Path(tmp) / "base" + subprocess.run(["git", "worktree", "add", "--detach", "--quiet", str(tree), base], cwd=repo, check=True) + try: + if not (tree / relative / "Cargo.lock").exists(): + return None + return {f.key for f in parse(run_deny(tree / relative))} + finally: + subprocess.run(["git", "worktree", "remove", "--force", str(tree)], cwd=repo, check=False) + + +# ------------------------------------------------------------------ policy + + +def load_policy(path: Path) -> list[dict]: + if not path.exists(): + return [] + entries = tomllib.loads(path.read_text()).get("accept", []) + for entry in entries: + missing = {"id", "reason", "review_by"} - entry.keys() + if missing: + raise ValueError(f"{path}: entry {entry.get('id', '?')} lacks {', '.join(sorted(missing))}") + if not isinstance(entry["review_by"], dt.date): + raise ValueError(f"{path}: {entry['id']}: review_by must be a date (2027-01-31)") + return entries + + +def decide(finding: Finding, mode: str, crate_name: str, policy: list[dict], today: dt.date) -> None: + entry = next( + (e for e in policy if e["id"] == finding.id and crate_name in e.get("crates", [crate_name])), + None, + ) + if entry and entry["review_by"] >= today: + finding.accepted = entry + finding.decision = "accepted" + finding.why = f"accepted until {entry['review_by']}: {entry['reason']}" + return + expired = f" (its acceptance lapsed on {entry['review_by']})" if entry else "" + + if finding.kind != "vulnerability": + finding.decision = "warn" + finding.why = f"{finding.kind}: reported, never blocking" + expired + return + severe = RANK[finding.rank] >= RANK["high"] + if not severe: + finding.decision = "warn" if finding.rank == "medium" else "notice" + finding.why = f"{finding.rank}: reported, not blocking" + expired + return + action = f"fix: {finding.fix}" if finding.fix else "no fixed version yet; accept it in .github/advisories.toml to ship" + if mode == "pr": + if finding.introduced: + finding.decision, finding.why = "fail", f"this pull request brings it in; {action}" + else: + finding.decision, finding.why = "warn", "already on the base branch, not caused by this pull request" + elif mode == "main": + if finding.fix: + finding.decision, finding.why = "fail", action + else: + finding.decision, finding.why = "warn", "no fixed version exists yet; not blocking" + else: # release + finding.decision, finding.why = "fail", action + finding.why += expired + + +# ------------------------------------------------------------------ report + +LEVEL = {"fail": "error", "warn": "warning", "accepted": "notice", "notice": "notice"} + + +def report(crate_name: str, mode: str, findings: list[Finding]) -> None: + order = {"fail": 0, "warn": 1, "accepted": 2, "notice": 3} + findings.sort(key=lambda f: (order.get(f.decision, 4), -RANK.get(f.rank, 0), f.id)) + for f in findings: + title = f"{f.id} {f.package} {f.version}" + text = f"{f.title} [{f.kind}, {f.rank if f.kind == 'vulnerability' else '-'}] {f.why}" + print(f"::{LEVEL.get(f.decision, 'warning')} title={crate_name}: {title}::{text}") + rows = [ + f"| {f.decision} | [{f.id}](https://rustsec.org/advisories/{f.id}) | {f.package} {f.version} | " + f"{f.kind} | {f.rank if f.kind == 'vulnerability' else ''} {f.ranked_by} | {f.fix or 'none'} | {f.why} |" + for f in findings + ] + failed = sum(f.decision == "fail" for f in findings) + verdict = "fail" if failed else ("warn" if any(f.decision == "warn" for f in findings) else "pass") + summary = [ + f"### Vulnerabilities: `{crate_name}` ({mode}): **{verdict}**", + "", + "| decision | advisory | package | kind | severity | fixed version | why |", + "|---|---|---|---|---|---|---|", + *rows, + ] if findings else [f"### Vulnerabilities: `{crate_name}` ({mode}): **pass**", "", "No advisories."] + text = "\n".join(summary) + "\n" + print(text) + if os.environ.get("GITHUB_STEP_SUMMARY"): + with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as out: + out.write(text + "\n") + + +def main(argv: list[str] | None = None) -> int: + parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) + parser.add_argument("--crate", required=True, type=Path, help="the crate folder, e.g. crates/graphicle") + parser.add_argument("--mode", required=True, choices=["pr", "main", "release"]) + parser.add_argument("--base", help="pr mode: the base ref to compare with, e.g. origin/main") + parser.add_argument("--policy", type=Path, default=Path(".github/advisories.toml")) + parser.add_argument("--today", type=dt.date.fromisoformat, default=dt.date.today()) + parser.add_argument("--deny-json", type=Path, help="tests: cargo-deny output to use instead of running it") + parser.add_argument("--base-json", type=Path, help="tests: the base ref's cargo-deny output") + args = parser.parse_args(argv) + crate_name = args.crate.resolve().name + try: + policy = load_policy(args.policy) + lines = args.deny_json.read_text().splitlines() if args.deny_json else run_deny(args.crate) + findings = parse(lines) + if args.mode == "pr": + if args.base_json: + base = {f.key for f in parse(args.base_json.read_text().splitlines())} + elif args.base: + base = base_findings(args.crate, args.base) + else: + parser.error("--mode pr needs --base") + for f in findings: + f.introduced = base is None or f.key not in base + except (RuntimeError, ValueError, subprocess.CalledProcessError) as error: + print(f"::error title={crate_name}: advisory check could not run::{error}") + return 2 + for f in findings: + decide(f, args.mode, crate_name, policy, args.today) + report(crate_name, args.mode, findings) + return 1 if any(f.decision == "fail" for f in findings) else 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/packaging/test_advisory_gate.py b/packaging/test_advisory_gate.py new file mode 100644 index 00000000..453744a8 --- /dev/null +++ b/packaging/test_advisory_gate.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Tests of packaging/advisory_gate.py: severity ranking, the pass, warn and +fail decisions of each mode, and the acceptance policy. + + python3 packaging/test_advisory_gate.py +""" + +import datetime as dt +import json +import sys +import tempfile +import unittest +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import advisory_gate as gate # noqa: E402 + +TODAY = dt.date(2026, 11, 18) + + +def diagnostic(ident, code="vulnerability", package="libz-sys", version="1.1.31", + cvss=None, categories=(), fix=">=1.1.33"): + notes = [f"ID: {ident}"] + notes.append(f"Solution: Upgrade to {fix}" if fix else "Solution: No safe upgrade is available!") + return json.dumps({"type": "diagnostic", "fields": { + "severity": "error", "code": code, "message": f"{package} advisory", "notes": notes, + "graphs": [{"Krate": {"name": package, "version": version}}], + "advisory": {"id": ident, "package": package, "title": f"{package} advisory", + "cvss": cvss, "categories": list(categories), + "informational": None if code == "vulnerability" else code}, + }}) + + +def decided(lines, mode, base=None, policy=(), today=TODAY, crate="graphicle"): + findings = gate.parse(lines) + keys = {f.key for f in gate.parse(base)} if base is not None else None + for f in findings: + if mode == "pr": + f.introduced = keys is None or f.key not in keys + gate.decide(f, mode, crate, list(policy), today) + return {f.id: f for f in findings} + + +class Severity(unittest.TestCase): + def test_cvss31_scores_match_the_specification(self): + for vector, score in [ + ("CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H", 9.8), + ("CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:C/C:H/I:H/A:H", 10.0), + ("CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:N/A:N", 5.5), + ("CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:N/A:H", 5.9), + ("CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:C/C:L/I:L/A:N", 6.1), + ("CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H", 7.5), + ("CVSS:3.1/AV:P/AC:H/PR:H/UI:R/S:U/C:N/I:N/A:N", 0.0), + ]: + self.assertEqual(gate.cvss3_score(vector), score, vector) + + def test_ranks(self): + self.assertEqual(gate.severity({"cvss": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H"})[0], "critical") + self.assertEqual(gate.severity({"cvss": "CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H"})[0], "high") + self.assertEqual(gate.severity({"cvss": "CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:N/A:N"})[0], "medium") + # CVSS 4.0, approximated: never below what its impacts say. + v4 = "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:N/SC:N/SI:N/SA:N" + self.assertEqual(gate.severity({"cvss": v4})[0], "critical") + self.assertEqual(gate.severity({"cvss": v4.replace("PR:N", "PR:L")})[0], "high") + self.assertEqual(gate.severity({"cvss": "CVSS:4.0/AV:L/AC:L/AT:N/PR:L/UI:N/VC:L/VI:N/VA:N/SC:N/SI:N/SA:N"})[0], "medium") + + def test_without_cvss_the_category_decides(self): + self.assertEqual(gate.severity({"categories": ["memory-corruption"]})[0], "high") + self.assertEqual(gate.severity({"categories": ["code-execution", "denial-of-service"]})[0], "high") + self.assertEqual(gate.severity({"categories": ["denial-of-service"]})[0], "medium") + self.assertEqual(gate.severity({})[0], "medium") + + +class Decisions(unittest.TestCase): + high_fixed = diagnostic("RUSTSEC-1", categories=["memory-corruption"]) + high_unfixed = diagnostic("RUSTSEC-2", package="zstd", categories=["memory-corruption"], fix=None) + medium = diagnostic("RUSTSEC-3", package="h2", categories=["denial-of-service"]) + low = diagnostic("RUSTSEC-4", package="idna", cvss="CVSS:3.1/AV:L/AC:H/PR:H/UI:R/S:U/C:L/I:N/A:N") + unmaintained = diagnostic("RUSTSEC-5", code="unmaintained", package="chrono", fix=None) + + def lines(self): + return [self.high_fixed, self.high_unfixed, self.medium, self.low, self.unmaintained] + + def test_main_fails_only_for_a_severe_vulnerability_it_can_fix(self): + d = decided(self.lines(), "main") + self.assertEqual( + {i: f.decision for i, f in d.items()}, + {"RUSTSEC-1": "fail", "RUSTSEC-2": "warn", "RUSTSEC-3": "warn", "RUSTSEC-4": "notice", "RUSTSEC-5": "warn"}, + ) + + def test_a_pull_request_fails_only_for_what_it_brings_in(self): + already = decided(self.lines(), "pr", base=self.lines()) + self.assertNotIn("fail", {f.decision for f in already.values()}) + brought = decided(self.lines(), "pr", base=[self.medium, self.unmaintained]) + self.assertEqual(brought["RUSTSEC-1"].decision, "fail") + self.assertEqual(brought["RUSTSEC-2"].decision, "fail") # a new dependency with a known, unfixed hole + self.assertEqual(brought["RUSTSEC-3"].decision, "warn") + + def test_the_same_advisory_at_another_version_is_not_new(self): + bumped = diagnostic("RUSTSEC-1", version="1.1.32", categories=["memory-corruption"]) + self.assertEqual(decided([bumped], "pr", base=[self.high_fixed])["RUSTSEC-1"].decision, "warn") + + def test_a_release_fails_for_every_severe_vulnerability_not_accepted(self): + d = decided(self.lines(), "release") + self.assertEqual(d["RUSTSEC-1"].decision, "fail") + self.assertEqual(d["RUSTSEC-2"].decision, "fail") + self.assertIn("accept it in .github/advisories.toml", d["RUSTSEC-2"].why) + self.assertEqual(d["RUSTSEC-5"].decision, "warn") + + def test_an_acceptance_unblocks_until_its_review_date(self): + policy = [{"id": "RUSTSEC-2", "reason": "unreachable", "review_by": dt.date(2026, 12, 31)}] + self.assertEqual(decided(self.lines(), "release", policy=policy)["RUSTSEC-2"].decision, "accepted") + lapsed = decided(self.lines(), "release", policy=policy, today=dt.date(2027, 1, 1))["RUSTSEC-2"] + self.assertEqual(lapsed.decision, "fail") + self.assertIn("lapsed on 2026-12-31", lapsed.why) + + def test_an_acceptance_can_be_limited_to_crates(self): + policy = [{"id": "RUSTSEC-2", "reason": "r", "review_by": dt.date(2026, 12, 31), "crates": ["graphicle-graph"]}] + self.assertEqual(decided(self.lines(), "release", policy=policy)["RUSTSEC-2"].decision, "fail") + self.assertEqual( + decided(self.lines(), "release", policy=policy, crate="graphicle-graph")["RUSTSEC-2"].decision, "accepted" + ) + + +class Policy(unittest.TestCase): + def write(self, text): + path = Path(tempfile.mkdtemp()) / "advisories.toml" + path.write_text(text) + return path + + def test_entries_need_a_reason_and_a_review_date(self): + with self.assertRaisesRegex(ValueError, "lacks reason, review_by"): + gate.load_policy(self.write('[[accept]]\nid = "RUSTSEC-1"\n')) + with self.assertRaisesRegex(ValueError, "must be a date"): + gate.load_policy(self.write('[[accept]]\nid = "R"\nreason = "r"\nreview_by = "soon"\n')) + + def test_the_committed_policy_parses(self): + root = Path(__file__).resolve().parent.parent + gate.load_policy(root / ".github" / "advisories.toml") + + +class Command(unittest.TestCase): + def test_exit_status_and_summary(self): + tmp = Path(tempfile.mkdtemp()) + head, base, summary = tmp / "head.json", tmp / "base.json", tmp / "summary.md" + head.write_text("\n".join([Decisions.high_fixed, Decisions.unmaintained]) + "\n") + base.write_text(Decisions.unmaintained + "\n") + args = ["--crate", "crates/graphicle", "--deny-json", str(head), "--today", "2026-11-18", + "--policy", str(tmp / "none.toml")] + import os + os.environ["GITHUB_STEP_SUMMARY"] = str(summary) + try: + self.assertEqual(gate.main(args + ["--mode", "main"]), 1) + self.assertEqual(gate.main(args + ["--mode", "pr", "--base-json", str(head)]), 0) + self.assertEqual(gate.main(args + ["--mode", "pr", "--base-json", str(base)]), 1) + finally: + del os.environ["GITHUB_STEP_SUMMARY"] + self.assertIn("| fail | [RUSTSEC-1]", summary.read_text()) + + +if __name__ == "__main__": + unittest.main()