diff --git a/.agents/skills/agentic-fuzzing/SKILL.md b/.agents/skills/agentic-fuzzing/SKILL.md index cca08a35..0d7429c2 100644 --- a/.agents/skills/agentic-fuzzing/SKILL.md +++ b/.agents/skills/agentic-fuzzing/SKILL.md @@ -91,7 +91,7 @@ routed to its own line's executor and silently runs another one). Lower it to `safe` when you need to be sure a divergence is the contract's and not a debug facility's — `safe` refuses both wall-clock exposure and the `:test` alias. Once `:test` no longer resolves the contract must name its runner by -hash, read out of `build/out/executor//data/latest.json`, e.g. +hash, the runner's `latest` in `build/out/executor//data/all.json`, e.g. `# { "Depends": "py-genlayer:9b8kjy…" }`. That hash changes whenever the SDK does, which is exactly why in-tree cases keep the alias and only probes give it up. diff --git a/.github/workflows/audit_nightly.yaml b/.github/workflows/audit_nightly.yaml new file mode 100644 index 00000000..a5bccb59 --- /dev/null +++ b/.github/workflows/audit_nightly.yaml @@ -0,0 +1,53 @@ +name: audit / nightly + +# Publishes `genvm-tool audit` findings to code scanning (Security → Code +# scanning, tool "genvm-tool audit"). An alert closes by itself once a run no +# longer reports it; dismissing one there is the ruling that it does not apply. +# +# GitHub only schedules workflows from the DEFAULT branch, so this file has to +# reach it before the cron fires. + +on: + schedule: + # Off the hour: a scheduled run on a busy minute is queued or dropped. + - cron: "37 4 * * *" + workflow_dispatch: + +permissions: + contents: read + security-events: write + +concurrency: + group: audit-nightly + cancel-in-progress: false + +defaults: + run: + shell: bash -exo pipefail {0} + +jobs: + audit: + runs-on: ubuntu-latest + timeout-minutes: 60 + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + # Lockfiles are tracked in the submodules; no third-party tree is read + - name: Get source + uses: ./.github/actions/get-src + with: + with_nix: "true" + third_party: none + github_token: ${{ secrets.GITHUB_TOKEN }} + nix_cache_pull_token: ${{ secrets.NIX_CACHE_PULL_TOKEN }} + - name: Audit + env: + # Optional: without it NVD is queried at its anonymous rate + NVD_API_KEY: ${{ secrets.NVD_API_KEY }} + run: >- + nix develop '.?submodules=1#audit' --command + genvm-tool audit --sarif build/audit.sarif + - name: Publish to code scanning + uses: github/codeql-action/upload-sarif@1fc58d04a4d4374f921bdac573c3811c240f9c26 # v4.39.2 + with: + sarif_file: build/audit.sarif + category: genvm-tool-audit diff --git a/.github/workflows/branch_wasmtime_watch.yaml b/.github/workflows/branch_wasmtime_watch.yaml deleted file mode 100644 index 4af27ce9..00000000 --- a/.github/workflows/branch_wasmtime_watch.yaml +++ /dev/null @@ -1,61 +0,0 @@ -name: branch / wasmtime watch - -# Daily sweep for published advisories against the upstream sources the manager -# vendors: each repo in an executor line's `manifest.json` by its pinned commit, -# and each crate that comes out of one by its locked version. -# -# It gates nothing and writes no branch: it keeps one `wasmtime-maintenance` -# issue in sync with what OSV currently says. Nothing PR-triggered could do this -# job — a pin becomes vulnerable while standing still, with no PR involved. -# -# Closing the issue is a human ruling on the findings it names; the tool never -# closes or reopens one. See support/ci/tools/wasmtime_watch.py. - -on: - schedule: - # Daily, off the hour: a scheduled run on a busy minute is queued or dropped. - - cron: "17 5 * * *" - workflow_dispatch: - inputs: - dry_run: - description: Report findings without creating or editing the issue - type: boolean - required: false - default: false - -permissions: - contents: read - issues: write - -# Never cancel: a cancelled sweep can leave the issue describing a state that no -# longer holds, and the next run is a day away. -concurrency: - group: wasmtime-watch - cancel-in-progress: false - -defaults: - run: - shell: bash -exo pipefail {0} - -jobs: - watch: - runs-on: ubuntu-latest - timeout-minutes: 30 - steps: - - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 - # Both files it reads are tracked inside the executor submodules, so this - # needs a checkout but not the materialized third-party trees — which is - # what lets it skip nix entirely. - - name: Get source - uses: ./.github/actions/get-src - with: - with_nix: "false" - third_party: none - github_token: ${{ secrets.GITHUB_TOKEN }} - - name: Sweep the vendored upstream sources - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - WASMTIME_REBASE_OWNER: ${{ vars.WASMTIME_REBASE_OWNER }} - run: >- - ./support/ci/run.sh tool wasmtime-watch - ${{ inputs.dry_run && '--dry-run' || '' }} diff --git a/docs/contributing/howto/committing/git-third-party.md b/docs/contributing/howto/committing/git-third-party.md index b24e42d6..5003b41e 100644 --- a/docs/contributing/howto/committing/git-third-party.md +++ b/docs/contributing/howto/committing/git-third-party.md @@ -59,30 +59,18 @@ the nested repo's commits and `save` ## Advisory Monitoring -`branch / wasmtime watch` checks every vendored source of every active line -against [OSV](https://osv.dev) daily and keeps one `wasmtime-maintenance` issue -in sync with the result. It gates nothing and writes no branch. Run it by hand -with `./support/ci/run.sh tool wasmtime-watch --dry-run` - -Two things are queried per line, because neither covers the other: +`audit / nightly` runs `genvm-tool audit` daily and publishes to Security → +Code scanning ([genvm-tool.md](../genvm-tool.md)). Vendored sources are checked +twice, because neither check covers the other: 1. each repo in `manifest.json`, by its pinned upstream commit 2. each crate that comes out of one, by its locked version — a vendored tree is dozens of crates (`cranelift-codegen`, `wasmparser`, `wiggle`), and that is where most advisories against this stack are actually published -A crate counts as vendored when `executor/Cargo.lock` gives it no `source` and -no tracked `Cargo.toml` in the executor repo declares it. Registry crates are -excluded because they are covered upstream; first-party crates, because they are -ours. A first-party crate with no committed manifest falls through and is -queried too — a dismissible false positive, chosen over a silent gap +A crate counts as vendored when `Cargo.lock` gives it no `source` and no tracked +`Cargo.toml` declares it OSV knows nothing about the features GenVM disables or what the patch series -changes, so a hit is a finding to rule on, not a proven exposure - -The sweep edits its issue in place and never closes or reopens one: closing it -is the ruling that its findings are handled. The advisory ids that issue covered -are recorded in its body marker, so later sweeps stay quiet until an id outside -that set appears, at which point a fresh issue supersedes it. -`WASMTIME_REBASE_OWNER` (an Actions variable) is the login the issue is assigned -to at creation; sweeps never reassign it +changes, so an alert is a finding to rule on, not a proven exposure. Dismissing +it in code scanning is that ruling diff --git a/docs/contributing/howto/extending/modify-runner.md b/docs/contributing/howto/extending/modify-runner.md index 25ee5ba5..d7c0a32d 100644 --- a/docs/contributing/howto/extending/modify-runner.md +++ b/docs/contributing/howto/extending/modify-runner.md @@ -15,3 +15,18 @@ Paths are relative to `executors/v0.3.x/`, the line that owns runner sources Step 4 in detail, and why a hash is never restored by hand: [committing/runners.md](../committing/runners.md) + +## Gating a Runner + +A pin in `current.nix` may set 2 optional attributes, which end up in the +line's `data/all.json`: + +1. `available-after = "2026-12-01T00:00:00Z";` — transactions timestamped + earlier resolve the runner as malformed, the same as on an executor that does + not ship it ([spec](../../../website/src/spec/02-execution-environment/04-runners.rst)). + Pick a time by which every validator runs the new executor. A runner new to + a released minor must carry one ([versioning](../../../website/src/spec/01-core-architecture/03-versioning.rst)) +2. `supersedes = "";` — the hash of the same id this one replaces. + The old hash must still be shipped, or evaluation fails + +`hash-updater.py` rewrites only `hash`, so both survive a re-pin diff --git a/docs/contributing/howto/genvm-tool.md b/docs/contributing/howto/genvm-tool.md index e5fab237..af666a0f 100644 --- a/docs/contributing/howto/genvm-tool.md +++ b/docs/contributing/howto/genvm-tool.md @@ -13,6 +13,7 @@ has no wrapper | `codegen` | render a data JSON into rust/python/rst/go | | `build-manifest` | write the manager's `data/manifest.yaml` | | `docs` | render the `docs/contributing/` indexes; `--write` splices them into `AGENTS.md` (`CLAUDE.md` is a symlink to it), `--check` fails when that copy is stale | +| `audit` | list published advisories against tracked lockfiles (osv-scanner), vendored trees by pinned commit (OSV) and the `cpes` of `libs/deps/dependency-urls.json` entries (NVD); `--markdown` writes a report, `--sarif` feeds the nightly `audit / nightly` upload to Security → Code scanning | | `fuzz-corpus` | seed the fuzz corpora from a finished run ([testing/fuzzing.md](testing/fuzzing.md)) | | `git` | `ls`, `list-repo`, `create-branches`, `check-for-push` across the manager and every executor submodule ([submodules.md](committing/submodules.md)) | diff --git a/docs/contributing/howto/testing/integration.md b/docs/contributing/howto/testing/integration.md index 98a2a1b2..b6ef5294 100644 --- a/docs/contributing/howto/testing/integration.md +++ b/docs/contributing/howto/testing/integration.md @@ -85,7 +85,7 @@ because `reroute_to` — what points a case at its own line's executor — is honored only from `safe` up. Below `unsafe` a contract must name its runner by hash instead of `:test`. Write `@RUNNER_LATEST__@` for that hash, for example `py-genlayer:@RUNNER_LATEST_v0.3_py-genlayer@`: the harness replaces -it with the uid in `build/out/executor//data/latest.json` before the +it with the runner's `latest` in `build/out/executor//data/all.json` before the code reaches an executor, so a runner change needs no test edit. A literal uid still works and goes stale diff --git a/docs/website/src/spec/01-core-architecture/03-versioning.rst b/docs/website/src/spec/01-core-architecture/03-versioning.rst index 8b8c6b29..06c675c8 100644 --- a/docs/website/src/spec/01-core-architecture/03-versioning.rst +++ b/docs/website/src/spec/01-core-architecture/03-versioning.rst @@ -22,7 +22,10 @@ Minor A minor release adds new features and is backwards-compatible. #. The :ref:`gvm-def-execution-hash` of a transaction may change. -#. New :term:`Runner`\s may be added; existing runners MUST NOT be removed. +#. New :term:`Runner`\s may be added; each MUST carry an + :ref:`availability time `, so executors of the + previous minor agree with it until then. Existing runners MUST NOT be + removed. #. New ``gl_call`` methods and new parameters of existing methods may be added. #. Old code continues to work, unless it deliberately tried to be future-incompatible (e.g. by observing additions permitted by rule 3). diff --git a/docs/website/src/spec/02-execution-environment/04-runners.rst b/docs/website/src/spec/02-execution-environment/04-runners.rst index ea027225..3246c69f 100644 --- a/docs/website/src/spec/02-execution-environment/04-runners.rst +++ b/docs/website/src/spec/02-execution-environment/04-runners.rst @@ -53,6 +53,20 @@ A ``chain:`` id is code, but its resolution is an ordinary A resolution that finds no code, or code that is not a valid :term:`runner`, fails like any other malformed runner rather than falling back to another view. +.. _gvm-def-runner-availability: + +Packaged Runner Availability +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +An executor ships a fixed set of packaged runners. Each may carry an +availability time: + +#. A ``:`` the executor does not ship fails as a + malformed runner +#. One whose availability time is later than the transaction timestamp fails + the same way, so executors that ship it agree with those that do not until + that time + Hash Format ~~~~~~~~~~~ diff --git a/docs/website/src/spec/changelog.rst b/docs/website/src/spec/changelog.rst index b33d1625..bc960e64 100644 --- a/docs/website/src/spec/changelog.rst +++ b/docs/website/src/spec/changelog.rst @@ -134,3 +134,6 @@ Changed :term:`Storage Slot` length instead of by ``u32`` overflow, so a slot's final octet is addressable; every access naming it was previously refused. See :doc:`02-execution-environment/03-wasi_genlayer_sdk/01-functions` +#. A packaged runner may carry an availability time; a transaction timestamped + before it resolves the runner as malformed, as if it were not shipped. See + :ref:`gvm-def-runner-availability` diff --git a/executors/v0.3.x b/executors/v0.3.x index 2e094c6f..536c58ff 160000 --- a/executors/v0.3.x +++ b/executors/v0.3.x @@ -1 +1 @@ -Subproject commit 2e094c6fdd7fb3b620706a2d20845e004fb6d298 +Subproject commit 536c58ffd517e1548db33807e81f5047b7c7e79e diff --git a/flake.nix b/flake.nix index d8a7bef0..a7f80b3c 100644 --- a/flake.nix +++ b/flake.nix @@ -172,6 +172,8 @@ python312Packages.pytest-cov python312Packages.pytest-xdist ]; + # The scanner `genvm-tool audit` drives + packages-audit = [ pkgs.osv-scanner ]; shell-hook-base = '' export CARGO_LD_LIBRARY_PATH="${toString pkgs.xz.out}/lib:${toString pkgs.zlib.out}/lib:${pkgs.stdenv.cc.cc.lib}/lib:${toString pkgs.glibc}/lib" export LLVM_PROFILE_FILE=/dev/null @@ -776,6 +778,7 @@ ++ packages-py-test ++ packages-rust ++ packages-gen-docs + ++ packages-audit ++ [ pkgs.nodejs ] # pre-commit + the hook tools, so `pre-commit run` works in-shell. # (No rustfmt/cargo here — cargo-fmt is a custom hook — so the @@ -787,6 +790,10 @@ # by pre-commit's supported `.legacy` hook mechanism. shellHook = shell-hook-base + pre-commit-check.shellHook + install-manager-push-refs; }; + devShells.audit = pkgs.mkShell { + packages = packages-0 ++ packages-audit; + shellHook = shell-hook-base; + }; devShells.check-qemu = pkgs.mkShell { packages = packages-0 ++ [ pkgs.qemu ]; shellHook = shell-hook-base; diff --git a/install/lib/python/post-install/__main__.py b/install/lib/python/post-install/__main__.py index 4962ae63..dd110771 100644 --- a/install/lib/python/post-install/__main__.py +++ b/install/lib/python/post-install/__main__.py @@ -382,13 +382,14 @@ def _load_registry(file: str | Path) -> dict[str, list[str]]: ret: dict[str, list[str]] = {} + # Frozen legacy lines ship `{id: [hash]}`, the others `{id: {versions: {hash: ...}}}`. for k, v in sorted(contents.items()): - if isinstance(v, str): - ret[k] = [v] - elif isinstance(v, list): - if not all([isinstance(x, str) for x in v]): - raise RuntimeError(f'registry value must be str | list[str] for {k}') + if isinstance(v, dict) and isinstance(v.get('versions'), dict): + ret[k] = list(v['versions']) + elif isinstance(v, list) and all(isinstance(x, str) for x in v): ret[k] = v + else: + raise RuntimeError(f'unexpected registry value for {k}') for v in ret.values(): v.sort() @@ -551,7 +552,7 @@ def process_executor_version(executor_version: str): for executor_version in all_executor_versions: process_executor_version(executor_version) -# Verify installed runners (present, correct hashes, latest ⊆ all) and, when +# Verify installed runners (well-formed registry, present, correct hashes) and, when # requested, precompile them. This is delegated to the manager's `check-install` # subcommand, which fans out to each active executor's own `check` command # instead of the executor being precompiled inline above. diff --git a/libs/deps/dependency-urls.json b/libs/deps/dependency-urls.json index d55ffaf9..1edb823f 100644 --- a/libs/deps/dependency-urls.json +++ b/libs/deps/dependency-urls.json @@ -210,7 +210,53 @@ "manager-arm64-macos", "executor-arm64-macos" ], - "fetcher": "fetchzip" + "fetcher": "fetchzip", + "cpes": [ + "cpe:2.3:a:gnu:libiconv:1.18:*:*:*:*:*:*:*" + ] + }, + { + "name": "lua-5.3.6", + "nix_der_name": "source", + "original_url": "https://github.com/lua/lua/archive/75ea9ccbea7c4886f30da147fb67b693b2624c26.tar.gz", + "alternative_urls": [], + "hash": "sha256-5pIGFXxDh7/kYZjz4PNP6icAULit6ufqig+IvaLxYCU=", + "must_preload": false, + "needed-for": [ + "x86_64-linux", + "aarch64-linux", + "aarch64-macos", + "manager-amd64-linux", + "executor-amd64-linux", + "manager-arm64-linux", + "executor-arm64-linux", + "manager-arm64-macos", + "executor-arm64-macos" + ], + "fetcher": "fetchzip", + "cpes": [ + "cpe:2.3:a:lua:lua:5.3.6:*:*:*:*:*:*:*" + ] + }, + { + "name": "musl-1.2.5", + "nix_der_name": "source", + "original_url": "https://github.com/kraj/musl/archive/0784374d561435f7c787a555aeab8ede699ed298.tar.gz", + "alternative_urls": [], + "hash": "sha256-xNKVsRfEK7TkSiYrxPho9ap8MWGix7nVHLecGW0D7kE=", + "must_preload": false, + "needed-for": [ + "x86_64-linux", + "aarch64-linux", + "manager-amd64-linux", + "executor-amd64-linux", + "manager-arm64-linux", + "executor-arm64-linux" + ], + "fetcher": "fetchzip", + "cpes": [ + "cpe:2.3:a:musl-libc:musl:1.2.5:*:*:*:*:*:*:*" + ] }, { "name": "rust-channel-stable-2025-03-18", @@ -289,6 +335,9 @@ "fetcher": "fetchzip", "fetcher_args": { "extension": ".zip" - } + }, + "cpes": [ + "cpe:2.3:a:sqlite:sqlite:3.24.0:*:*:*:*:*:*:*" + ] } ] diff --git a/libs/libc.nix b/libs/libc.nix index 40953087..78af4dac 100644 --- a/libs/libc.nix +++ b/libs/libc.nix @@ -6,11 +6,7 @@ ... }: let - musl-src = builtins.fetchGit { - url = "https://github.com/kraj/musl.git"; - rev = "0784374d561435f7c787a555aeab8ede699ed298"; - shallow = true; - }; + musl-src = deps."musl-1.2.5"; aarch64-compiler-rt = deps."compiler-rt-aarch64-21.1.5-1"; in pkgs.stdenvNoCC.mkDerivation { diff --git a/runners/all.nix b/runners/all.nix index 074ad19f..99b51dd7 100644 --- a/runners/all.nix +++ b/runners/all.nix @@ -1,6 +1,6 @@ # The accumulated list of every runner across every active executor line, each # tagged with the `version` of the line it came from: -# [ { id; hash; uid; derivation; version; } ] +# [ { id; hash; uid; derivation; available-after; supersedes; version; } ] { host-system ? "x86_64-linux", deps ? null, diff --git a/runners/manifest.nix b/runners/manifest.nix index c92853b9..83d1a568 100644 --- a/runners/manifest.nix +++ b/runners/manifest.nix @@ -1,11 +1,14 @@ -# The runner manifests shipped in one executor's `data/` directory, as Nix -# values (the release view / ninja eval turn these into JSON files): +# The runner manifest shipped in one executor's `data/` directory, as a Nix +# value (the release view / ninja eval turn it into `all.json`): # -# latest : { = ; } the executor's own current runners -# all : { = [ ]; } every runner compatible up to its version +# { = { +# latest = ; the executor's own current runner +# versions = { = { available_after; supersedes; }; }; +# }; } # -# `all` is what the executor consults to accept a requested runner hash; `latest` -# resolves an id to the executor's own newest runner (used in debug mode). +# `versions` holds every runner compatible up to the executor's version and is +# what the executor consults to accept a requested runner hash; `latest` +# resolves an id in debug mode. { executorVersion, ... @@ -15,30 +18,65 @@ let hash32 = x: builtins.head (builtins.tail (builtins.match "([^:]+):(.*)" x.uid)); - own = builtins.filter (runner: runner.version == executorVersion) api.list; + own = builtins.filter ( + runner: runner.version == executorVersion && !(runner.archived or false) + ) api.list; compatible = api.forExecutor executorVersion; - single = - runners: - builtins.listToAttrs ( - builtins.map (x: { - name = x.id; - value = hash32 x; - }) runners - ); + latest = builtins.foldl' ( + acc: x: + if acc ? ${x.id} then + throw "runner ${x.id} ships 2 current runners for ${executorVersion}: ${acc.${x.id}} and ${hash32 x}" + else + acc // { ${x.id} = hash32 x; } + ) { } own; + + timestampRe = "[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])T([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\\.[0-9]+)?(Z|[+-]([01][0-9]|2[0-3]):[0-5][0-9])"; + + meta = + x: + if x.available-after != null && builtins.match timestampRe x.available-after == null then + throw "runner ${x.uid}: available-after `${x.available-after}` is not an RFC 3339 timestamp" + else + { + available_after = x.available-after; + inherit (x) supersedes; + }; - multi = - runners: - builtins.foldl' ( - acc: x: - let - existing = acc.${x.id} or [ ]; - h = hash32 x; - in - acc // { ${x.id} = if builtins.elem h existing then existing else existing ++ [ h ]; } - ) { } runners; + versions = builtins.foldl' ( + acc: x: + let + existing = acc.${x.id} or { }; + h = hash32 x; + in + if existing ? ${h} && existing.${h} != meta x then + throw "runner ${x.uid} is pinned with different metadata across executor lines" + else + acc + // { + ${x.id} = existing // { + ${h} = meta x; + }; + } + ) { } compatible; in -{ - latest = single own; - all = multi compatible; -} +builtins.mapAttrs ( + id: vs: + let + unlisted = builtins.filter (m: m.supersedes != null && !(vs ? ${m.supersedes})) ( + builtins.attrValues vs + ); + selfSuperseding = builtins.filter (h: vs.${h}.supersedes == h) (builtins.attrNames vs); + in + if !(latest ? ${id}) then + throw "runner ${id} ships no current runner for ${executorVersion}" + else if selfSuperseding != [ ] then + throw "runner ${id}:${builtins.head selfSuperseding} supersedes itself" + else if unlisted != [ ] then + throw "runner ${id} supersedes unlisted ${(builtins.head unlisted).supersedes}" + else + { + latest = latest.${id}; + versions = vs; + } +) versions diff --git a/runners/views/release.nix b/runners/views/release.nix index 3008816b..364faeb6 100644 --- a/runners/views/release.nix +++ b/runners/views/release.nix @@ -1,13 +1,9 @@ -# Writes the per-executor runner manifests (see ../manifest.nix) to JSON files +# Writes the per-executor runner manifest (see ../manifest.nix) to a JSON file # for the executor build to drop into its `data/` directory. { executorVersion, ... }@args: -let - manifest = import ../manifest.nix args; -in { - latest = builtins.toFile "latest.json" (builtins.toJSON manifest.latest); - all = builtins.toFile "all.json" (builtins.toJSON manifest.all); + all = builtins.toFile "all.json" (builtins.toJSON (import ../manifest.nix args)); } diff --git a/support/ci/__main__.py b/support/ci/__main__.py index e76636fa..c14ec788 100644 --- a/support/ci/__main__.py +++ b/support/ci/__main__.py @@ -17,7 +17,6 @@ rebase_watch, sync_executor_branches, versions, - wasmtime_watch, ) PIPELINES: list[ci_lib.Pipeline] = [ @@ -39,7 +38,6 @@ *open_executor_prs.COMMANDS, *rebase_watch.COMMANDS, *sync_executor_branches.COMMANDS, - *wasmtime_watch.COMMANDS, ] diff --git a/support/ci/gh_common.py b/support/ci/gh_common.py index 7209c752..ae7cafc3 100644 --- a/support/ci/gh_common.py +++ b/support/ci/gh_common.py @@ -37,7 +37,6 @@ import shlex import subprocess import sys -import tempfile import time import urllib.parse @@ -356,41 +355,3 @@ def has_write_access(login: str) -> bool: if r.returncode != 0: return False return json.loads(r.stdout).get('permission') in WRITE_ROLES - - -def api_json(method: str, path: str, payload: dict, *, retry: bool = False) -> str: - """ - `gh api` with a nested JSON body, as the manager. - - Through a temporary file rather than `-f` fields: a payload that nests (a - check-run's `output.title`, an issue's `assignees` list) cannot be expressed - as `-f` pairs, and `--input -` would need stdin that `gh` above does not - plumb. Defaults to `retry=False` because every caller so far is a write, and - `gh`'s transient-failure markers can misread a request that in fact - succeeded. - """ - with tempfile.NamedTemporaryFile('w', suffix='.json') as body: - json.dump(payload, body) - body.flush() - return gh_manager( - 'api', '--method', method, path, '--input', body.name, retry=retry - ) - - -def ensure_label(name: str, color: str, description: str) -> None: - """ - Create a repo label if it is missing; already-exists (422) is the normal case. - """ - gh_manager( - 'api', - '--method', - 'POST', - f'repos/{repo()}/labels', - '-f', - f'name={name}', - '-f', - f'color={color}', - '-f', - f'description={description}', - check=False, - ) diff --git a/support/ci/tools/wasmtime_watch.py b/support/ci/tools/wasmtime_watch.py deleted file mode 100644 index ed77ac42..00000000 --- a/support/ci/tools/wasmtime_watch.py +++ /dev/null @@ -1,256 +0,0 @@ -#!/usr/bin/env python3 -""" -Report published advisories against the upstream sources the manager vendors. - -A pin does not have to move to become vulnerable, so no PR-triggered check can -catch this — the day an advisory is published, every pin that stood still -becomes a finding and no PR is involved. This sweeps on a schedule instead and -keeps a single tracking issue in sync with what OSV currently says. It gates -nothing and writes no branch; the measurement is `wasmtime_pins`. - -The automation never closes and never reopens an issue. Closing is the reader's -ruling on the findings that issue names — GenVM disables features and carries a -local patch stack, so whether an upstream advisory is reachable here is a -judgement OSV cannot make. So the advisory ids a closed issue covered are -recorded in its own marker, and a later sweep stays quiet while it finds nothing -outside that set. A new id files a fresh issue that supersedes it. - -The issue is found by label (the query GitHub can filter server-side) and -identified by a marker in its body: a label is not a namespace, so anything else -that adopts this label — a person filing by hand, another tool — must not be -mistaken for this tool's issue and rewritten. The marker is also where the -issue's own state lives, since GitHub gives an issue no field this tool owns. -""" - -import argparse -import dataclasses -import json -import os -import re - -import ci_lib -import gh_common -import wasmtime_pins -from gh_common import gh_manager as gh -from gh_common import repo -from tools.versions import active_lines -from wasmtime_pins import Advisory, Subject - -LABEL = 'wasmtime-maintenance' -LABEL_COLOR = 'b60205' -LABEL_DESCRIPTION = 'published advisories against vendored upstream sources' - -MARKER_NAME = 'genvm-ci/wasmtime-watch' -MARKER_RE = re.compile( - rf'' -) - -OWNER_ENV = 'WASMTIME_REBASE_OWNER' - -# A backslash cannot appear inside an f-string expression before python 3.12, -# and the scheduled workflow runs whatever `python3` the runner ships -TABLE_PIPE = r'\|' - - -@dataclasses.dataclass(frozen=True) -class Marker: - """ - The state this tool keeps in an issue body. - """ - - ids: frozenset[str] - supersedes: int | None = None - - def render(self) -> str: - trailer = f' supersedes={self.supersedes}' if self.supersedes else '' - return f'' - - -def parse_marker(body: str | None) -> Marker | None: - """ - The marker an issue body carries, or None if it carries none. - """ - match = MARKER_RE.search(body or '') - if match is None: - return None - return Marker( - ids=frozenset(id for id in match.group(1).split(',') if id), - supersedes=int(match.group(2)) if match.group(2) else None, - ) - - -def body_for( - findings: dict[Advisory, list[Subject]], owner: str | None, marker: Marker -) -> str: - if findings: - rows = '\n'.join( - f'| [{advisory.id}]({advisory.url}) ' - f'| {advisory.summary.replace("|", TABLE_PIPE)} ' - f'| {", ".join(str(subject) for subject in subjects)} |' - for advisory, subjects in findings.items() - ) - table = f'| Advisory | Summary | Affected |\n|---|---|---|\n{rows}' - else: - table = 'No vendored source currently matches a published advisory.' - - owner_line = f'@{owner}' if owner else f'**unset** (`{OWNER_ENV}`)' - supersedes = f'\nSupersedes #{marker.supersedes}.\n' if marker.supersedes else '' - - return f"""{marker.render()} -## Published advisories against vendored upstream sources - -{table} -{supersedes} -**Owner:** {owner_line} - -Each vendored repo is matched by its pinned upstream commit, and every crate -that comes out of one by its locked version. OSV knows nothing about the -features GenVM disables or the patches it carries, so a row is a finding to rule -on rather than a proven exposure. - -Fixing one goes through the usual flow — a manager feature branch, an executor -mirror at `pr//` targeting `-dev`, then the standing -release gate. Nothing here writes to a branch. - -This issue is edited in place while it is open. **Close it once you have ruled -on the findings above**; it is never closed or reopened automatically, and a new -issue is filed only when an advisory outside this list appears. -""" - - -def title_for(findings: dict[Advisory, list[Subject]]) -> str: - return f'[wasmtime] {len(findings)} published advisory(ies) affect vendored sources' - - -def marker_issues(state: str) -> list[dict]: - """ - This tool's own issues in `state`, newest first. - - `sort=created`, because the newest closed issue is the one whose ruling is - current. - """ - out = gh( - 'api', - gh_common.api_path( - f'repos/{repo()}/issues', - state=state, - labels=LABEL, - per_page=100, - sort='created', - direction='desc', - ), - '--jq', - # The issues endpoint returns PRs too; they carry a `pull_request` key. - '.[] | select(has("pull_request") | not) | {number, title, body}', - ) - issues = [json.loads(line) for line in out.splitlines() if line.strip()] - return [issue for issue in issues if parse_marker(issue['body']) is not None] - - -def create_issue(title: str, body: str, owner: str | None) -> None: - gh_common.ensure_label(LABEL, LABEL_COLOR, LABEL_DESCRIPTION) - payload: dict = {'title': title, 'body': body, 'labels': [LABEL]} - if owner: - payload['assignees'] = [owner] - gh_common.api_json('POST', f'repos/{repo()}/issues', payload) - - -def update_issue(issue: dict, title: str, body: str) -> None: - """ - Edit the open issue in place, and only when something actually differs. - - A no-op PATCH still notifies everyone subscribed to the issue, and this runs - daily. Bodies are compared with line endings normalized, so a round-trip - through GitHub cannot make an unchanged body look different every morning. - - Assignees are deliberately left out: they are set once at creation, and a - sweep that re-asserted them would undo every hand-off. - """ - - def normalized(text: str) -> str: - return text.replace('\r\n', '\n') - - payload = {} - if issue['title'] != title: - payload['title'] = title - if normalized(issue['body']) != normalized(body): - payload['body'] = body - if not payload: - print(f'#{issue["number"]} is already up to date') - return - gh_common.api_json('PATCH', f'repos/{repo()}/issues/{issue["number"]}', payload) - - -def sweep(dry_run: bool) -> int: - owner = os.environ.get(OWNER_ENV) or None - if owner is None: - print(f'::warning::{OWNER_ENV} is unset; the issue will be unassigned') - - findings = wasmtime_pins.scan(active_lines()) - current = frozenset(advisory.id for advisory in findings) - - open_issues = marker_issues('open') - if len(open_issues) > 1: - # A past race, or someone filing by hand. Report it rather than picking - # one silently: two issues means two places a ruling can be recorded. - numbers = ', '.join(f'#{issue["number"]}' for issue in open_issues) - ci_lib.github_error(f'several open {LABEL} issues ({numbers}); resolve by hand') - return 1 - - if open_issues: - issue = open_issues[0] - # The supersede link is carried forward from the issue's own marker: it is - # a fact about which issue this one replaced, not about today's findings. - previous = parse_marker(issue['body']) - assert previous is not None, 'marker_issues returns only issues with a marker' - marker = Marker(ids=current, supersedes=previous.supersedes) - print(f'updating #{issue["number"]} ({len(findings)} finding(s))') - if not dry_run: - update_issue(issue, title_for(findings), body_for(findings, owner, marker)) - return 0 - - if not current: - print('no advisories and no open issue: nothing to report') - return 0 - - # Nothing open: the newest closed issue carries the last ruling. Everything it - # already named has been ruled on, so only something outside that set is news. - closed = marker_issues('closed') - ruled_on = parse_marker(closed[0]['body']).ids if closed else frozenset() - new = current - ruled_on - if not new: - print( - f'{len(current)} advisory(ies), all already ruled on in ' - f'#{closed[0]["number"]}: staying quiet' - ) - return 0 - - marker = Marker(ids=current, supersedes=closed[0]['number'] if closed else None) - print(f'filing an issue for {len(new)} new advisory(ies): {", ".join(sorted(new))}') - if not dry_run: - create_issue(title_for(findings), body_for(findings, owner, marker), owner) - return 0 - - -class WasmtimeWatch(ci_lib.Tool): - """ - Track published advisories against the vendored upstream sources. - """ - - def name(self) -> str: - return 'wasmtime-watch' - - def add_to(self, parser: argparse.ArgumentParser) -> None: - gh_common.add_args(parser, executor_repo=False, pr=False, head_ref=False) - parser.add_argument( - '--dry-run', - action='store_true', - help='report what would be filed or edited without touching any issue', - ) - - def handler(self, args: argparse.Namespace) -> int: - gh_common.set_ctx(gh_common.Ctx.from_args(args)) - return sweep(args.dry_run) - - -COMMANDS = [WasmtimeWatch()] diff --git a/support/ci/unit_tests/test_wasmtime_watch.py b/support/ci/unit_tests/test_wasmtime_watch.py deleted file mode 100644 index e9aedfdd..00000000 --- a/support/ci/unit_tests/test_wasmtime_watch.py +++ /dev/null @@ -1,311 +0,0 @@ -import json - -import pytest -import wasmtime_pins -from tools import wasmtime_watch as watch -from wasmtime_pins import Advisory, Subject - -MANIFEST = { - 'repos': { - 'executor/third-party/wasmtime': {'commit': 'a' * 40}, - 'executor/third-party/wasm-tools': {'commit': 'b' * 40}, - } -} - -LOCK = """ -[[package]] -name = "wasmtime" -version = "48.0.1" - -[[package]] -name = "cranelift-codegen" -version = "0.123.0" - -[[package]] -name = "genvm" -version = "0.1.0" - -[[package]] -name = "serde" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -""" - - -@pytest.fixture -def tree(tmp_path, monkeypatch): - line = tmp_path / 'executors' / 'v0.3.x' - (line / '.git-third-party').mkdir(parents=True) - (line / 'executor').mkdir() - (line / wasmtime_pins.MANIFEST).write_text(json.dumps(MANIFEST)) - (line / wasmtime_pins.CARGO_LOCK).write_text(LOCK) - # `git ls-files` needs a real repo; the mapping it feeds is tested separately. - monkeypatch.setattr(wasmtime_pins, 'workspace_crates', lambda tree: {'genvm'}) - return tmp_path - - -def test_every_vendored_crate_is_a_subject_not_just_the_repo_basename(tree): - found = wasmtime_pins.subjects('v0.3', root=tree) - - # The repos, by pinned commit... - assert [(s.name, s.commit) for s in found if s.commit] == [ - ('wasm-tools', 'b' * 40), - ('wasmtime', 'a' * 40), - ] - # ...and every crate that comes out of one, by locked version. `wasmtime` is - # both; `cranelift-codegen` shares its name with no repo and is where most - # advisories against this stack are published. - assert [(s.name, s.version) for s in found if s.version] == [ - ('cranelift-codegen', '0.123.0'), - ('wasmtime', '48.0.1'), - ] - - -def test_registry_and_first_party_crates_are_not_subjects(tree): - names = {s.name for s in wasmtime_pins.subjects('v0.3', root=tree) if s.version} - - # `serde` has a source, so Dependabot covers it; `genvm` is ours. - assert 'serde' not in names - assert 'genvm' not in names - - -def test_a_manifest_with_no_vendored_repo_is_an_error_not_a_clean_bill(tree): - path = tree / 'executors' / 'v0.3.x' / wasmtime_pins.MANIFEST - path.write_text(json.dumps({'repos': {}})) - - with pytest.raises(ValueError, match='lists no vendored repos'): - wasmtime_pins.subjects('v0.3', root=tree) - - -def test_a_pin_without_a_full_commit_is_an_error(tree): - path = tree / 'executors' / 'v0.3.x' / wasmtime_pins.MANIFEST - path.write_text(json.dumps({'repos': {'x/wasmtime': {'commit': 'abc'}}})) - - with pytest.raises(ValueError, match='no usable commit pin'): - wasmtime_pins.subjects('v0.3', root=tree) - - -def test_workspace_crates_reads_tracked_manifests_only(tmp_path, monkeypatch): - (tmp_path / 'crate').mkdir() - (tmp_path / 'crate' / 'Cargo.toml').write_text('[package]\nname = "genvm"\n') - (tmp_path / 'workspace.toml').write_text('[workspace]\nmembers = []\n') - monkeypatch.setattr( - wasmtime_pins.subprocess, - 'run', - lambda *a, **kw: type('R', (), {'stdout': 'crate/Cargo.toml\nworkspace.toml\n'}), - ) - - # A virtual manifest declares no package and must not contribute a name. - assert wasmtime_pins.workspace_crates(tmp_path) == {'genvm'} - - -def test_a_subject_queries_osv_by_commit_or_by_crate_version(): - assert Subject(line='v0.3', name='wasmtime', commit='a' * 40).query() == { - 'commit': 'a' * 40 - } - assert Subject(line='v0.3', name='wasmtime', version='48.0.1').query() == { - 'package': {'name': 'wasmtime', 'ecosystem': 'crates.io'}, - 'version': '48.0.1', - } - - -def test_a_query_shared_between_lines_is_only_sent_once(monkeypatch): - sent = [] - monkeypatch.setattr(wasmtime_pins, '_osv', lambda query: sent.append(query) or []) - subject = Subject(line='v0.2', name='wasmparser', version='0.1') - cache: dict = {} - - wasmtime_pins.advisories_for(subject, cache) - wasmtime_pins.advisories_for( - Subject(line='v0.3', name='wasmparser', version='0.1'), cache - ) - - assert len(sent) == 1 - - -def test_canonical_id_is_stable_across_the_alias_that_matched(): - entry = {'id': 'RUSTSEC-2024-0001', 'aliases': ['GHSA-zzzz', 'CVE-2024-1']} - other = {'id': 'GHSA-zzzz', 'aliases': ['RUSTSEC-2024-0001', 'CVE-2024-1']} - - assert wasmtime_pins.canonical_id(entry) == wasmtime_pins.canonical_id(other) - assert wasmtime_pins.canonical_id({'id': 'OSV-1'}) == 'OSV-1' - - -def test_osv_pagination_is_bounded(monkeypatch): - monkeypatch.setattr( - wasmtime_pins, '_post', lambda page: {'vulns': [], 'next_page_token': 'more'} - ) - - with pytest.raises(RuntimeError, match='did not stop paginating'): - wasmtime_pins._osv({'commit': 'a' * 40}) - - -def findings(*ids: str) -> dict: - subject = Subject(line='v0.3', name='wasmtime', version='48.0.1') - return {Advisory(id=id, summary='boom | bang'): [subject] for id in ids} - - -def marker(*ids: str, supersedes: int | None = None) -> watch.Marker: - return watch.Marker(ids=frozenset(ids), supersedes=supersedes) - - -def test_the_marker_round_trips_the_state_the_issue_carries(): - body = watch.body_for( - findings('GHSA-a', 'GHSA-b'), 'someone', marker('GHSA-a', 'GHSA-b', supersedes=7) - ) - - assert watch.parse_marker(body) == marker('GHSA-a', 'GHSA-b', supersedes=7) - assert watch.parse_marker('no marker here') is None - assert watch.parse_marker(watch.body_for({}, None, marker())) == marker() - - -def test_the_body_names_the_owner_and_the_superseded_issue(): - assert '@someone' in watch.body_for(findings('GHSA-a'), 'someone', marker('GHSA-a')) - assert 'WASMTIME_REBASE_OWNER' in watch.body_for( - findings('GHSA-a'), None, marker('GHSA-a') - ) - assert 'Supersedes #7.' in watch.body_for( - findings('GHSA-a'), None, marker('GHSA-a', supersedes=7) - ) - - -def test_a_pipe_in_a_summary_does_not_break_the_table(): - body = watch.body_for(findings('GHSA-a'), None, marker('GHSA-a')) - row = next(line for line in body.splitlines() if line.startswith('| [GHSA-a]')) - - assert r'boom \| bang' in row - assert row.count('|') - row.count(r'\|') == 4 - - -def issue(number: int, ids: list[str], supersedes: int | None = None) -> dict: - return { - 'number': number, - 'title': watch.title_for(dict.fromkeys(ids, [])), - 'body': marker(*ids, supersedes=supersedes).render(), - } - - -def test_marker_issues_drops_prs_and_issues_that_are_not_ours(monkeypatch): - rows = [ - {'number': 1, 'title': 't', 'body': marker('GHSA-a').render()}, - {'number': 2, 'title': 't', 'body': 'filed by hand, same label'}, - ] - monkeypatch.setattr( - watch, 'gh', lambda *a: '\n'.join(json.dumps(row) for row in rows) + '\n' - ) - - assert [issue['number'] for issue in watch.marker_issues('open')] == [1] - - -@pytest.fixture -def sweep(monkeypatch): - """ - Drive `sweep` over canned findings and issues, capturing what it would write. - """ - calls = [] - monkeypatch.setattr(watch, 'active_lines', lambda: ['v0.3']) - monkeypatch.setattr( - watch, 'create_issue', lambda title, body, owner: calls.append(('create', body)) - ) - monkeypatch.setattr( - watch, - 'update_issue', - lambda issue, title, body: calls.append(('update', issue['number'], body)), - ) - monkeypatch.delenv(watch.OWNER_ENV, raising=False) - - def run(found: dict, *, open_issues: list, closed: list, dry_run: bool = False): - monkeypatch.setattr(watch.wasmtime_pins, 'scan', lambda lines: found) - monkeypatch.setattr( - watch, - 'marker_issues', - lambda state: open_issues if state == 'open' else closed, - ) - return watch.sweep(dry_run), calls - - return run - - -def test_an_open_issue_is_edited_rather_than_duplicated(sweep): - rc, calls = sweep( - findings('GHSA-a', 'GHSA-b'), open_issues=[issue(1, ['GHSA-a'])], closed=[] - ) - - assert rc == 0 - assert [call[:2] for call in calls] == [('update', 1)] - assert watch.parse_marker(calls[0][2]).ids == {'GHSA-a', 'GHSA-b'} - - -def test_an_update_keeps_the_supersede_link_of_the_issue_it_edits(sweep): - rc, calls = sweep( - findings('GHSA-a'), open_issues=[issue(1, ['GHSA-a'], supersedes=9)], closed=[] - ) - - assert rc == 0 - assert watch.parse_marker(calls[0][2]).supersedes == 9 - assert 'Supersedes #9.' in calls[0][2] - - -def test_an_open_issue_is_edited_even_once_the_findings_clear(sweep): - rc, calls = sweep({}, open_issues=[issue(1, ['GHSA-a'])], closed=[]) - - assert rc == 0 - assert watch.parse_marker(calls[0][2]).ids == set() - - -def test_several_open_issues_are_reported_instead_of_picked_between(sweep): - rc, calls = sweep( - findings('GHSA-a'), - open_issues=[issue(1, ['GHSA-a']), issue(2, ['GHSA-a'])], - closed=[], - ) - - assert (rc, calls) == (1, []) - - -def test_findings_already_ruled_on_do_not_reopen_the_conversation(sweep): - rc, calls = sweep( - findings('GHSA-a'), open_issues=[], closed=[issue(9, ['GHSA-a', 'GHSA-b'])] - ) - - assert (rc, calls) == (0, []) - - -def test_a_new_advisory_files_a_fresh_issue_superseding_the_closed_one(sweep): - rc, calls = sweep( - findings('GHSA-a', 'GHSA-new'), open_issues=[], closed=[issue(9, ['GHSA-a'])] - ) - - assert rc == 0 - assert [call[0] for call in calls] == ['create'] - assert watch.parse_marker(calls[0][1]) == marker('GHSA-a', 'GHSA-new', supersedes=9) - - -def test_nothing_is_filed_when_there_is_nothing_to_report(sweep): - assert sweep({}, open_issues=[], closed=[]) == (0, []) - - -def test_a_dry_run_writes_nothing(sweep): - rc, calls = sweep(findings('GHSA-a'), open_issues=[], closed=[], dry_run=True) - assert (rc, calls) == (0, []) - - rc, calls = sweep( - findings('GHSA-a'), open_issues=[issue(1, [])], closed=[], dry_run=True - ) - assert (rc, calls) == (0, []) - - -def test_an_unchanged_issue_is_not_patched(monkeypatch): - patched = [] - monkeypatch.setattr(watch.gh_common, 'api_json', lambda *a, **kw: patched.append(a)) - body = watch.body_for(findings('GHSA-a'), None, marker('GHSA-a')) - title = watch.title_for(findings('GHSA-a')) - - # The same content, and the same content after a CRLF round-trip through the - # API: a daily no-op PATCH notifies every subscriber. - watch.update_issue({'number': 1, 'title': title, 'body': body}, title, body) - watch.update_issue( - {'number': 1, 'title': title, 'body': body.replace('\n', '\r\n')}, title, body - ) - - assert patched == [] diff --git a/support/ci/wasmtime_pins.py b/support/ci/wasmtime_pins.py deleted file mode 100644 index 60e76e54..00000000 --- a/support/ci/wasmtime_pins.py +++ /dev/null @@ -1,243 +0,0 @@ -#!/usr/bin/env python3 -""" -One definition of "which upstream sources does an executor line vendor, and are -any of them subject to a published advisory". - -The subjects live in the manager's own checkout: the gitlink at -`executors/.x` is the snapshot that actually ships, so this reads them out -of the submodule tree rather than resolving the executor's `-dev` / -`.x` refs through the API — those move independently of what the manager -pins. Two files, because upstream is vendored twice over: - -1. `.git-third-party/manifest.json` names each vendored repo and the upstream - commit it is pinned at; -2. `executor/Cargo.lock` names the crates that come out of those repos. A - vendored tree is dozens of crates, not one — `cranelift-codegen`, - `wasmparser` and `wiggle` are where most advisories against this stack are - actually published, and none of them shares a name with the repo it lives in. - -Both are queried against OSV, because neither covers the other: a commit query -misses a crate whose advisory names no commit range, and a version query misses -a vendored tree pinned between releases. - -A crate is vendored if the lock gives it no `source` (so it is a path -dependency) and no tracked `Cargo.toml` in the executor repo declares it. The -residue of that rule is a first-party crate with no committed manifest, which -gets one pointless crates.io query — a false positive a reader can dismiss, -where the opposite mistake is a silent gap. - -Nothing here knows which features GenVM disables or what its patch series -changes, so a hit is a finding to rule on, not a proven exposure. -""" - -import dataclasses -import json -import subprocess -import time -import tomllib -import urllib.error -import urllib.request -from pathlib import Path - -import ci_lib - -MANIFEST = Path('.git-third-party') / 'manifest.json' -CARGO_LOCK = Path('executor') / 'Cargo.lock' -OSV_QUERY_URL = 'https://api.osv.dev/v1/query' - -# OSV is a third party on a daily job's critical path, so a blip must not cost a -# day of monitoring. Mirrors gh_common.gh's policy for the same reason. -_RETRY_ATTEMPTS = 4 -_RETRY_BACKOFF_S = 2.0 -# A server that keeps handing back a page token would otherwise spin until the -# job's timeout. No real query is anywhere near this deep. -_MAX_PAGES = 50 - - -@dataclasses.dataclass(frozen=True, order=True) -class Subject: - """ - One thing an advisory can be published against, in one executor line. - - Exactly one of `commit` (a vendored repo at its pinned upstream commit) and - `version` (a crate that comes out of one) is set. - """ - - line: str - name: str - version: str = '' - commit: str = '' - - def query(self) -> dict: - if self.commit: - return {'commit': self.commit} - return { - 'package': {'name': self.name, 'ecosystem': 'crates.io'}, - 'version': self.version, - } - - def __str__(self) -> str: - what = f'@{self.commit[:12]}' if self.commit else f' {self.version}' - return f'`{self.line}` {self.name}{what}' - - -@dataclasses.dataclass(frozen=True, order=True) -class Advisory: - id: str - summary: str - - @property - def url(self) -> str: - return f'https://osv.dev/vulnerability/{self.id}' - - -def canonical_id(entry: dict) -> str: - """ - The single id an OSV entry is tracked under here. - - OSV returns the same advisory under several aliases and reports whichever the - matching source used, so the raw `id` is not stable across queries — and this - id is what `wasmtime_watch` compares to decide whether a finding is new. GHSA - first because every advisory reaching a Rust crate has one; RUSTSEC next; the - OSV id only when neither exists. - """ - ids = sorted({entry['id'], *entry.get('aliases', [])}) - for prefix in ('GHSA-', 'RUSTSEC-'): - for candidate in ids: - if candidate.startswith(prefix): - return candidate - return entry['id'] - - -def workspace_crates(tree: Path) -> set[str]: - """ - Crate names declared by a tracked `Cargo.toml` in the executor repo. - - Tracked, not on disk: the vendored trees are git-ignored, so they are absent - from this list whether or not `git third-party update` has materialized them. - Globbing the filesystem instead would classify every vendored crate as - first-party the moment someone runs the tool on a materialized checkout, - which is exactly the failure that stays silent. - """ - paths = subprocess.run( - ['git', '-C', str(tree), 'ls-files', '*Cargo.toml'], - check=True, - text=True, - capture_output=True, - ).stdout.split() - - names = set() - for path in paths: - name = tomllib.loads((tree / path).read_text()).get('package', {}).get('name') - if isinstance(name, str): - names.add(name) - return names - - -def subjects(line: str, *, root: Path | None = None) -> list[Subject]: - """ - Everything of one executor line that an advisory could be published against. - - Needs the submodule checked out; `get-all-git.py --third-party=none` is - enough, since both files it reads are tracked. - """ - tree = (root or ci_lib.ROOT_DIR) / 'executors' / f'{line}.x' - repos = json.loads((tree / MANIFEST).read_bytes()).get('repos') - if not repos: - # A monitor that finds nothing to monitor is indistinguishable from a - # clean bill of health, so refuse to report one. - raise ValueError(f'{line}: {MANIFEST} lists no vendored repos') - - result = [] - for key, repo in sorted(repos.items()): - commit = repo.get('commit') - if not isinstance(commit, str) or len(commit) != 40: - raise ValueError(f'{line}: {key} has no usable commit pin: {commit!r}') - result.append(Subject(line=line, name=key.rsplit('/', 1)[-1], commit=commit)) - - first_party = workspace_crates(tree) - lock = tomllib.loads((tree / CARGO_LOCK).read_text()) - for package in sorted(lock.get('package', []), key=lambda p: str(p.get('name'))): - name, version = package.get('name'), package.get('version') - if 'source' in package or name in first_party: - continue - if not isinstance(name, str) or not isinstance(version, str): - raise ValueError(f'{line}: {CARGO_LOCK} has a package with no name/version') - result.append(Subject(line=line, name=name, version=version)) - return result - - -def _post(query: dict) -> dict: - request = urllib.request.Request( - OSV_QUERY_URL, - data=json.dumps(query).encode(), - headers={'Content-Type': 'application/json', 'User-Agent': 'genvm-ci'}, - method='POST', - ) - for attempt in range(_RETRY_ATTEMPTS): - try: - with urllib.request.urlopen(request, timeout=30) as response: - return json.load(response) - except (urllib.error.URLError, TimeoutError, json.JSONDecodeError) as error: - # A 4xx is the caller's fault and will fail identically on a replay. - terminal = isinstance(error, urllib.error.HTTPError) and error.code < 500 - if terminal or attempt + 1 == _RETRY_ATTEMPTS: - raise RuntimeError(f'OSV query {query} failed: {error}') from error - time.sleep(_RETRY_BACKOFF_S * (attempt + 1)) - raise AssertionError('unreachable') - - -def _osv(query: dict) -> list[dict]: - entries: list[dict] = [] - page = dict(query) - for _ in range(_MAX_PAGES): - response = _post(page) - entries.extend(response.get('vulns', [])) - token = response.get('next_page_token') - if not token: - return entries - page = {**query, 'page_token': token} - raise RuntimeError(f'OSV query {query} did not stop paginating') - - -def advisories_for(subject: Subject, cache: dict | None = None) -> list[Advisory]: - """ - Published advisories matching one subject. - - `cache` is keyed by the query rather than by the subject, so the two lines' - shared crates cost one request between them. - """ - query = subject.query() - key = json.dumps(query, sort_keys=True) - if cache is not None and key in cache: - return cache[key] - - found: dict[str, Advisory] = {} - for entry in _osv(query): - canonical = canonical_id(entry) - found[canonical] = Advisory( - id=canonical, - summary=(entry.get('summary') or 'no summary').replace('\n', ' '), - ) - result = sorted(found.values()) - if cache is not None: - cache[key] = result - return result - - -def scan( - lines: list[str], *, root: Path | None = None -) -> dict[Advisory, list[Subject]]: - """ - Every advisory affecting `lines`, with the subjects it was matched against. - """ - cache: dict[str, list[Advisory]] = {} - result: dict[Advisory, list[Subject]] = {} - for line in lines: - checked = subjects(line, root=root) - print(f'{line}: {len(checked)} subject(s)') - for subject in checked: - for advisory in advisories_for(subject, cache): - print(f' {subject}: {advisory.id}') - result.setdefault(advisory, []).append(subject) - return dict(sorted(result.items())) diff --git a/support/default.nix b/support/default.nix index 891675da..a97bbe32 100644 --- a/support/default.nix +++ b/support/default.nix @@ -87,11 +87,7 @@ let dontFixup = true; }; - lua-src = builtins.fetchGit { - url = "https://github.com/lua/lua.git"; - rev = "75ea9ccbea7c4886f30da147fb67b693b2624c26"; - shallow = true; - }; + lua-src = deps."lua-5.3.6"; in rec { inherit pkgs deps lua-src; diff --git a/support/runner-script.py b/support/runner-script.py index 20628778..c74e229c 100755 --- a/support/runner-script.py +++ b/support/runner-script.py @@ -78,13 +78,16 @@ def _load_registry(file: str) -> dict[str, list[str]]: ret: dict[str, list[str]] = {} + # Frozen legacy lines ship `{id: [hash]}`, the others `{id: {versions: {hash: ...}}}`. for k, v in contents.items(): if isinstance(v, str): ret[k] = [v] - elif isinstance(v, list): - if not all([isinstance(x, str) for x in v]): - raise RuntimeError(f'registry value must be str | list[str] for {k}') + elif isinstance(v, dict) and isinstance(v.get('versions'), dict): + ret[k] = list(v['versions']) + elif isinstance(v, list) and all(isinstance(x, str) for x in v): ret[k] = v + else: + raise RuntimeError(f'unexpected registry value for {k}') for v in ret.values(): v.sort() diff --git a/support/tools/genvm-tool/genvm_tool/__main__.py b/support/tools/genvm-tool/genvm_tool/__main__.py index b0e68bc0..8a04e705 100644 --- a/support/tools/genvm-tool/genvm_tool/__main__.py +++ b/support/tools/genvm-tool/genvm_tool/__main__.py @@ -21,6 +21,7 @@ import shtab from . import ( + cmd_audit, cmd_build_manifest, cmd_codegen, cmd_configure, @@ -38,6 +39,7 @@ cmd_build_manifest, cmd_codegen, cmd_docs, + cmd_audit, cmd_fuzz_corpus, ] GROUPS = [git] diff --git a/support/tools/genvm-tool/genvm_tool/cmd_audit.py b/support/tools/genvm-tool/genvm_tool/cmd_audit.py new file mode 100644 index 00000000..65b3da8f --- /dev/null +++ b/support/tools/genvm-tool/genvm_tool/cmd_audit.py @@ -0,0 +1,594 @@ +""" +`genvm-tool audit` — list published advisories against third-party dependencies. + +3 passes, each skippable: + +1. lockfiles: every lockfile tracked in the manager and its executor submodules, + through `osv-scanner` (in the dev shell). Crates of vendored upstream trees + (wasmtime, wasm-tools) are included, first-party path crates are not +2. vendored: each repo in an executor's `.git-third-party/manifest.json`, by its + pinned commit, against OSV. An advisory the lockfile pass already reported + is not repeated +3. native: every `cpes` entry in `libs/deps/dependency-urls.json`, against NVD. + Only native code the manager or an executor links opts in, by listing the CPE + of each product it ships at the version it ships. Set `NVD_API_KEY` to lift + NVD's rate limit + +None of the sources knows which features GenVM disables or what its patches +change, so a finding is a candidate to rule on, not a proven exposure. + +`--markdown` writes a report; `--sarif` writes the findings for GitHub code +scanning, which is where the nightly run publishes them. Exits 0 whether or not +anything is found; a scanner failure, or a pass with nothing to scan, exits +non-zero. +""" + +import dataclasses +import hashlib +import json +import os +import subprocess +import time +import tomllib +import urllib.error +import urllib.parse +import urllib.request +from pathlib import Path + +from . import common + +NAME = 'audit' +HELP = 'list published advisories against third-party dependencies' + +LOCKFILES = frozenset( + { + 'Cargo.lock', + 'package-lock.json', + 'poetry.lock', + 'requirements.txt', + 'uv.lock', + } +) +VENDORED_MANIFEST = '.git-third-party/manifest.json' +DEPS_FILE = Path('libs/deps/dependency-urls.json') +OSV_QUERY_URL = 'https://api.osv.dev/v1/query' +NVD_CVES_URL = 'https://services.nvd.nist.gov/rest/json/cves/2.0' +NVD_API_KEY_ENV = 'NVD_API_KEY' +USER_AGENT = 'genvm-tool-audit' + +# osv-scanner exits 1 when it found something; 128 means no package was found, +# which for these lockfiles is a broken scan, not a clean one +_OSV_RESULT_CODES = (0, 1) +_OSV_ATTEMPTS = 4 +_OSV_BACKOFF_S = 2.0 +_OSV_MAX_PAGES = 50 +_GHSA_SEVERITY_SCORES = {'LOW': 3.0, 'MODERATE': 5.5, 'HIGH': 8.0, 'CRITICAL': 9.5} +# NVD allows 5 requests per 30s without a key and 50 with one +_NVD_PAUSE_S = 6.5 +_NVD_PAUSE_WITH_KEY_S = 0.7 +_NVD_ATTEMPTS = 4 +_NVD_BACKOFF_S = 10.0 + + +def configure(parser): + parser.add_argument( + '--markdown', metavar='FILE', help='also write the findings as a Markdown report' + ) + parser.add_argument( + '--sarif', + metavar='FILE', + help='also write the findings as SARIF, for GitHub code scanning', + ) + parser.add_argument( + '--no-lockfiles', action='store_true', help='skip the osv-scanner pass' + ) + parser.add_argument( + '--no-vendored', action='store_true', help='skip the pinned-commit pass' + ) + parser.add_argument( + '--no-native', action='store_true', help=f'skip the NVD pass over {DEPS_FILE}' + ) + + +@dataclasses.dataclass(frozen=True) +class Finding: + """ + 1 advisory against 1 pinned package version in 1 file. + """ + + # Stable across runs whichever alias a source reports, see `canonical_id` + id: str + aliases: tuple[str, ...] + summary: str + # CVSS base score; None when the advisory carries none (unsound, unmaintained) + score: float | None + package: str + version: str + fixed: tuple[str, ...] + path: str + line: int + + @property + def url(self) -> str: + if self.id.startswith('CVE-'): + return f'https://nvd.nist.gov/vuln/detail/{self.id}' + return f'https://osv.dev/vulnerability/{self.id}' + + +def canonical_id(ids: set[str]) -> str: + """ + The 1 id an advisory is reported under, whichever alias the source used. + + Code scanning keys alerts, and so their dismissals, on it. + """ + for prefix in ('GHSA-', 'RUSTSEC-', 'CVE-'): + matching = sorted(id for id in ids if id.startswith(prefix)) + if matching: + return matching[0] + return min(ids) + + +def _finding(ids: set[str], **fields) -> Finding: + id = canonical_id(ids) + return Finding(id=id, aliases=tuple(sorted(ids - {id})), **fields) + + +def _tracked(root: Path) -> list[tuple[common.Repo, list[str]]]: + """ + Each repo with its tracked files, manager-root-relative; vendored trees are not tracked. + """ + repos = common.discover_repos(root) + missing = [repo.name for repo in repos if not repo.is_checked_out()] + if missing: + # Its pins would silently drop out of the scan + raise common.ToolError(f'not checked out: {", ".join(missing)}') + gitlinks = common.executor_rels(root) + return [ + (repo, [repo.prefix(f) for f in common.ls_files(repo, gitlinks)]) for repo in repos + ] + + +def first_party_crates(root: Path, files: list[str]) -> frozenset[str]: + """ + Crates declared by a tracked `Cargo.toml` among `files`. + + osv-scanner looks every locked crate up on crates.io by name, so a path + dependency is only meaningful when it comes out of a vendored tree. + """ + names = set() + for path in files: + if Path(path).name == 'Cargo.toml': + name = tomllib.loads((root / path).read_text()).get('package', {}).get('name') + if isinstance(name, str): + names.add(name) + return frozenset(names) + + +def _cargo_first_party(lockfile: Path, first_party: frozenset[str]) -> frozenset[str]: + packages = tomllib.loads(lockfile.read_text()).get('package', []) + return frozenset( + p['name'] for p in packages if 'source' not in p and p['name'] in first_party + ) + + +def _line_of(lines: list[str], package: str, version: str) -> int: + """ + 1-based line declaring `package` at `version`, or 1 if none can be told apart. + """ + declarations = (f'name = "{package}"', f'"node_modules/{package}"', f'{package}==') + for i, line in enumerate(lines): + if not line.strip().startswith(declarations): + continue + if any(version in near for near in lines[i : i + 3]): + return i + 1 + return 1 + + +def _fixed_versions(vuln: dict, package: str) -> tuple[str, ...]: + return tuple( + sorted( + { + event['fixed'] + for affected in vuln.get('affected', []) + if affected.get('package', {}).get('name') == package + for range_ in affected.get('ranges', []) + for event in range_.get('events', []) + if 'fixed' in event + } + ) + ) + + +def _scan_lockfiles( + ctx: common.Context, tracked: list[tuple[common.Repo, list[str]]] +) -> list[Finding]: + first_party: dict[str, frozenset[str]] = {} + for _, files in tracked: + crates = first_party_crates(ctx.root, files) + first_party |= {p: crates for p in files if Path(p).name in LOCKFILES} + if not first_party: + raise common.ToolError('no tracked lockfile to scan') + ctx.logger.info('scanning lockfiles', count=len(first_party)) + osv = common.command_to_executable('osv-scanner') + proc = subprocess.run( + [str(osv), 'scan', 'source', '--format', 'json'] + + [f'--lockfile={p}' for p in sorted(first_party)], + cwd=ctx.root, + capture_output=True, + text=True, + ) + if proc.returncode not in _OSV_RESULT_CODES: + raise common.ToolError(f'osv-scanner exited {proc.returncode}:\n{proc.stderr}') + return findings_from_osv(ctx.root, json.loads(proc.stdout), first_party) + + +def findings_from_osv( + root: Path, report: dict, first_party: dict[str, frozenset[str]] +) -> list[Finding]: + """ + :param first_party: per manager-relative lockfile, the crates of its own repo + """ + findings = [] + for result in report.get('results') or []: + lockfile = Path(result['source']['path']) + path = lockfile.relative_to(root).as_posix() + skip = ( + _cargo_first_party(lockfile, first_party.get(path, frozenset())) + if lockfile.name == 'Cargo.lock' + else frozenset() + ) + lines = lockfile.read_text().splitlines() + for entry in result.get('packages') or []: + package = entry['package']['name'] + version = entry['package']['version'] + if package in skip: + continue + vulns = {v['id']: v for v in entry.get('vulnerabilities') or []} + # A group is 1 advisory published under several ids + for group in entry.get('groups') or []: + ids = set(group['ids']) | set(group.get('aliases') or []) + primary = next((vulns[id] for id in group['ids'] if id in vulns), {}) + score = group.get('max_severity') or None + findings.append( + _finding( + ids, + summary=primary.get('summary') or primary.get('details', '').split('\n')[0], + score=float(score) if score else None, + package=package, + version=version, + fixed=_fixed_versions(primary, package), + path=path, + line=_line_of(lines, package, version), + ) + ) + return findings + + +def _fetch_json(request: urllib.request.Request, timeout: float) -> dict: + request.add_header('User-Agent', USER_AGENT) + with urllib.request.urlopen(request, timeout=timeout) as response: + return json.load(response) + + +def _osv_query(query: dict) -> list[dict]: + vulns: list[dict] = [] + page = dict(query) + for _ in range(_OSV_MAX_PAGES): + request = urllib.request.Request( + OSV_QUERY_URL, + data=json.dumps(page).encode(), + headers={'Content-Type': 'application/json'}, + method='POST', + ) + for attempt in range(_OSV_ATTEMPTS): + try: + body = _fetch_json(request, 30) + break + except (urllib.error.URLError, TimeoutError, json.JSONDecodeError) as e: + # A 4xx fails identically on a replay + terminal = isinstance(e, urllib.error.HTTPError) and e.code < 500 + if terminal or attempt == _OSV_ATTEMPTS - 1: + raise common.ToolError(f'OSV query {query} failed: {e}') from e + time.sleep(_OSV_BACKOFF_S * (attempt + 1)) + vulns += body.get('vulns', []) + token = body.get('next_page_token') + if not token: + return vulns + page = {**query, 'page_token': token} + raise common.ToolError(f'OSV query {query} did not stop paginating') + + +def _osv_score(vuln: dict) -> float | None: + """ + The GitHub severity rating as a CVSS-range score, which is what code scanning ranks by. + """ + return _GHSA_SEVERITY_SCORES.get( + str(vuln.get('database_specific', {}).get('severity', '')).upper() + ) + + +def findings_from_commit( + repo: str, commit: str, path: str, line: int, vulns: list[dict] +) -> list[Finding]: + return [ + _finding( + {vuln['id'], *(vuln.get('aliases') or [])}, + summary=vuln.get('summary') or vuln.get('details', '').split('\n')[0], + score=_osv_score(vuln), + package=repo, + version=commit[:12], + fixed=(), + path=path, + line=line, + ) + for vuln in vulns + ] + + +def _scan_vendored( + ctx: common.Context, tracked: list[tuple[common.Repo, list[str]]] +) -> list[Finding]: + """ + Vendored upstream trees by their pinned commit. + + Their crates are matched by version in the lockfile pass too, but an advisory + that names only a commit range is found only here. + """ + manifests = [ + p for _, files in tracked for p in files if p.endswith(VENDORED_MANIFEST) + ] + if not manifests: + raise common.ToolError(f'no tracked {VENDORED_MANIFEST} to scan') + findings = [] + for path in manifests: + text = (ctx.root / path).read_text() + repos = json.loads(text).get('repos') + if not repos: + raise common.ToolError(f'{path} lists no vendored repos') + lines = text.splitlines() + for key, repo in sorted(repos.items()): + commit = repo.get('commit') + if not isinstance(commit, str) or len(commit) != 40: + raise common.ToolError(f'{path}: {key} has no usable commit pin: {commit!r}') + ctx.logger.info('querying OSV', repo=key, commit=commit) + line = next((n + 1 for n, t in enumerate(lines) if commit in t), 1) + findings += findings_from_commit( + key.rsplit('/', 1)[-1], commit, path, line, _osv_query({'commit': commit}) + ) + return findings + + +def _nvd_get(url: str, api_key: str | None) -> dict: + headers = {'apiKey': api_key} if api_key else {} + for attempt in range(_NVD_ATTEMPTS): + try: + return _fetch_json(urllib.request.Request(url, headers=headers), 60) + except urllib.error.HTTPError as e: + # NVD answers its rate limit with 403 or 429, and overload with 503 + if e.code not in (403, 429, 503) or attempt == _NVD_ATTEMPTS - 1: + raise common.ToolError(f'NVD answered {e.code} for {url}') from e + except (urllib.error.URLError, TimeoutError, json.JSONDecodeError) as e: + if attempt == _NVD_ATTEMPTS - 1: + raise common.ToolError(f'NVD query {url} failed: {e}') from e + time.sleep(_NVD_BACKOFF_S * 2**attempt) + raise AssertionError('unreachable') + + +def _nvd_pause(api_key: str | None) -> None: + time.sleep(_NVD_PAUSE_WITH_KEY_S if api_key else _NVD_PAUSE_S) + + +def _nvd_cves(cpe: str, api_key: str | None) -> list[dict]: + """ + Every non-rejected CVE whose configurations match `cpe`, version ranges included. + """ + cves: list[dict] = [] + while True: + query = urllib.parse.urlencode( + {'virtualMatchString': cpe, 'startIndex': len(cves), 'noRejected': ''} + ) + page = _nvd_get(f'{NVD_CVES_URL}?{query}', api_key) + cves += [v['cve'] for v in page.get('vulnerabilities', [])] + if not page.get('vulnerabilities') or len(cves) >= page.get('totalResults', 0): + return cves + _nvd_pause(api_key) + + +def _cvss_score(cve: dict) -> float | None: + metrics = cve.get('metrics', {}) + for key in ('cvssMetricV40', 'cvssMetricV31', 'cvssMetricV30', 'cvssMetricV2'): + if metrics.get(key): + return metrics[key][0]['cvssData']['baseScore'] + return None + + +def findings_from_nvd( + cpe: str, path: str, line: int, cves: list[dict] +) -> list[Finding]: + _, _, _, _, product, version, *_ = cpe.split(':') + return [ + _finding( + {cve['id']}, + summary=next( + (d['value'] for d in cve.get('descriptions', []) if d['lang'] == 'en'), '' + ).split('\n')[0], + score=_cvss_score(cve), + package=product, + version=version, + fixed=(), + path=path, + line=line, + ) + for cve in cves + ] + + +def _scan_native(ctx: common.Context) -> list[Finding]: + deps_file = ctx.root / DEPS_FILE + lines = deps_file.read_text().splitlines() + cpes = [ + cpe for dep in json.loads(deps_file.read_text()) for cpe in dep.get('cpes', []) + ] + if not cpes: + raise common.ToolError(f'no dependency in {DEPS_FILE} lists `cpes`') + ctx.logger.info('querying NVD', count=len(cpes)) + api_key = os.environ.get(NVD_API_KEY_ENV) or None + findings = [] + for i, cpe in enumerate(cpes): + if i: + _nvd_pause(api_key) + line = next((n + 1 for n, text in enumerate(lines) if f'"{cpe}"' in text), 1) + findings += findings_from_nvd( + cpe, DEPS_FILE.as_posix(), line, _nvd_cves(cpe, api_key) + ) + return findings + + +def _severity(score: float | None) -> str: + if score is None: + return '-' + for floor, name in ((9.0, 'critical'), (7.0, 'high'), (4.0, 'medium'), (0.1, 'low')): + if score >= floor: + return name + return 'none' + + +def _by_severity(findings: list[Finding]) -> list[Finding]: + return sorted(findings, key=lambda f: (-(f.score or 0), f.path, f.package, f.id)) + + +def _markdown(findings: list[Finding]) -> str: + """ + 1 table per file holding pins, worst first, after a count per severity. + """ + ordered = _by_severity(findings) + counts: dict[str, int] = {} + for f in ordered: + counts[_severity(f.score)] = counts.get(_severity(f.score), 0) + 1 + out = [ + '# Dependency Audit', + '', + f'{len(findings)} finding(s): ' + + (', '.join(f'{n} {s}' for s, n in counts.items()) if counts else 'none'), + ] + by_path: dict[str, list[Finding]] = {} + for f in ordered: + by_path.setdefault(f.path, []).append(f) + for path in sorted(by_path): + out += [ + '', + f'## `{path}`', + '', + '| Severity | Package | Advisory | Summary | Fixed in |', + '|---|---|---|---|---|', + ] + for f in by_path[path]: + summary = f.summary.replace('|', r'\|') + out.append( + f'| {_severity(f.score)} | `{f.package}@{f.version}` | [{f.id}]({f.url}) ' + f'| {summary} | {", ".join(f.fixed) or "-"} |' + ) + return '\n'.join(out) + '\n' + + +def _print(findings: list[Finding]) -> None: + for f in _by_severity(findings): + fixed = f' (fixed in {", ".join(f.fixed)})' if f.fixed else '' + print( + f'{_severity(f.score):8} {f.path}:{f.line}: {f.package}@{f.version} ' + f'{f.id}: {f.summary}{fixed}' + ) + print(f'{len(findings)} finding(s)') + + +def _sarif(findings: list[Finding]) -> dict: + rules = {} + results = [] + for f in findings: + rules.setdefault( + f.id, + { + 'id': f.id, + 'shortDescription': {'text': f.summary or f.id}, + 'helpUri': f.url, + 'help': { + 'text': f'{f.id}: {f.summary}\n{f.url}', + 'markdown': f'[{f.id}]({f.url}): {f.summary}' + + (f'\n\nAliases: {", ".join(f.aliases)}' if f.aliases else ''), + }, + 'properties': { + 'tags': ['security', 'dependency'], + **({'security-severity': f'{f.score:.1f}'} if f.score is not None else {}), + }, + }, + ) + fixed = f' Fixed in {", ".join(f.fixed)}.' if f.fixed else '' + results.append( + { + 'ruleId': f.id, + 'level': 'note' if f.score is None else 'warning' if f.score < 7.0 else 'error', + 'message': {'text': f'{f.package}@{f.version} is affected by {f.id}.{fixed}'}, + 'locations': [ + { + 'physicalLocation': { + 'artifactLocation': {'uri': f.path}, + 'region': {'startLine': f.line}, + } + } + ], + # No version: a dismissal survives a bump that is still affected, and + # a bump that fixes it stops the report, which closes the alert + 'partialFingerprints': { + 'genvmAudit/v1': hashlib.sha256( + f'{f.path}\0{f.package}\0{f.id}'.encode() + ).hexdigest() + }, + } + ) + return { + '$schema': 'https://json.schemastore.org/sarif-2.1.0.json', + 'version': '2.1.0', + 'runs': [ + { + 'tool': { + 'driver': { + 'name': 'genvm-tool audit', + 'informationUri': 'https://github.com/genlayerlabs/genvm-manager', + 'rules': list(rules.values()), + } + }, + 'results': results, + } + ], + } + + +def not_yet_reported(earlier: list[Finding], later: list[Finding]) -> list[Finding]: + """ + `later` without the advisories `earlier` already reports, under any of their ids. + """ + seen = {id for f in earlier for id in (f.id, *f.aliases)} + return [f for f in later if not seen & {f.id, *f.aliases}] + + +def main(ctx: common.Context, args) -> int: + tracked = _tracked(ctx.root) + findings: list[Finding] = [] + if not args.no_lockfiles: + findings += _scan_lockfiles(ctx, tracked) + if not args.no_vendored: + findings += not_yet_reported(findings, _scan_vendored(ctx, tracked)) + # A native pin is bumped on its own, even when a crate bundles the same code + if not args.no_native: + findings += _scan_native(ctx) + _print(findings) + if args.markdown: + _write(Path(args.markdown), _markdown(findings)) + if args.sarif: + _write(Path(args.sarif), json.dumps(_sarif(findings), indent=1) + '\n') + return 0 + + +def _write(path: Path, text: str) -> None: + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(text) diff --git a/support/tools/genvm-tool/unit_tests/test_audit.py b/support/tools/genvm-tool/unit_tests/test_audit.py new file mode 100644 index 00000000..4ff9e715 --- /dev/null +++ b/support/tools/genvm-tool/unit_tests/test_audit.py @@ -0,0 +1,374 @@ +"""How scanner answers become findings and reports, and when a scan refuses to run.""" + +import dataclasses +import io +import json +import subprocess +import types +import urllib.error + +import pytest +from genvm_tool import cmd_audit, common + +CARGO_LOCK = """\ +version = 4 + +[[package]] +name = "genvm-common" +version = "0.1.0" + +[[package]] +name = "wasmtime" +version = "48.0.3" + +[[package]] +name = "zip" +version = "4.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +""" + + +def _package(name, version, groups, vulns): + return { + 'package': {'name': name, 'version': version, 'ecosystem': 'crates.io'}, + 'groups': groups, + 'vulnerabilities': vulns, + } + + +def _vuln(id, package, fixed): + return { + 'id': id, + 'summary': f'{id} summary', + 'affected': [ + { + 'package': {'name': package}, + 'ranges': [{'events': [{'introduced': '0'}, {'fixed': fixed}]}], + }, + { + 'package': {'name': 'unrelated'}, + 'ranges': [{'events': [{'fixed': '99.0.0'}]}], + }, + ], + } + + +def _findings(tmp_path): + lock = tmp_path / 'executor' / 'Cargo.lock' + lock.parent.mkdir() + lock.write_text(CARGO_LOCK) + report = { + 'results': [ + { + 'source': {'path': str(lock)}, + 'packages': [ + _package( + 'genvm-common', + '0.1.0', + [{'ids': ['RUSTSEC-0'], 'max_severity': '5.0'}], + [_vuln('RUSTSEC-0', 'genvm-common', '0.2.0')], + ), + _package( + 'wasmtime', + '48.0.3', + [{'ids': ['RUSTSEC-1'], 'max_severity': '9.9'}], + [_vuln('RUSTSEC-1', 'wasmtime', '49.0.0')], + ), + _package( + 'zip', + '4.3.0', + [ + { + 'ids': ['RUSTSEC-2', 'GHSA-2'], + 'aliases': ['RUSTSEC-2', 'GHSA-2', 'CVE-2'], + 'max_severity': '9.1', + }, + {'ids': ['RUSTSEC-3'], 'max_severity': ''}, + ], + [ + _vuln('RUSTSEC-2', 'zip', '4.4.0'), + _vuln('GHSA-2', 'zip', '4.4.0'), + _vuln('RUSTSEC-3', 'zip', '5.0.0'), + ], + ), + ], + } + ] + } + first_party = {'executor/Cargo.lock': frozenset({'genvm-common'})} + return cmd_audit.findings_from_osv(tmp_path, report, first_party) + + +def _ctx(root): + return types.SimpleNamespace( + root=root, logger=types.SimpleNamespace(info=lambda *args, **kwargs: None) + ) + + +def test_first_party_path_crates_are_skipped_and_vendored_ones_kept(tmp_path): + assert [f.package for f in _findings(tmp_path)] == ['wasmtime', 'zip', 'zip'] + + +def test_first_party_crates_come_from_tracked_manifests(tmp_path): + (tmp_path / 'a').mkdir() + (tmp_path / 'a' / 'Cargo.toml').write_text('[package]\nname = "genvm-common"\n') + (tmp_path / 'Cargo.toml').write_text('[workspace]\nmembers = ["a"]\n') + files = ['Cargo.toml', 'a/Cargo.toml'] + assert cmd_audit.first_party_crates(tmp_path, files) == {'genvm-common'} + + +def test_one_finding_per_advisory_group(tmp_path): + _, first, second = _findings(tmp_path) + assert first.id == 'GHSA-2' + assert first.aliases == ('CVE-2', 'RUSTSEC-2') + assert first.summary == 'RUSTSEC-2 summary' + assert first.score == 9.1 + assert first.fixed == ('4.4.0',) + assert first.path == 'executor/Cargo.lock' + assert first.line == 12 + assert second.id == 'RUSTSEC-3' + assert second.score is None + + +@pytest.mark.parametrize( + 'ids, expected', + [ + ({'RUSTSEC-1', 'GHSA-b', 'GHSA-a', 'CVE-1'}, 'GHSA-a'), + ({'RUSTSEC-1', 'CVE-1'}, 'RUSTSEC-1'), + ({'OSV-1', 'CVE-1'}, 'CVE-1'), + ({'OSV-2', 'OSV-1'}, 'OSV-1'), + ], +) +def test_canonical_id_does_not_depend_on_which_alias_a_source_reports(ids, expected): + assert cmd_audit.canonical_id(ids) == expected + + +def test_line_of_matches_the_declaration_not_a_substring(): + lines = ['name = "gzip"', 'version = "1.0"', 'name = "zip"', 'version = "1.0"'] + assert cmd_audit._line_of(lines, 'zip', '1.0') == 3 + assert cmd_audit._line_of(lines, 'zip', '2.0') == 1 + + +def test_commit_findings_take_the_github_severity(): + vulns = [ + { + 'id': 'CVE-9', + 'aliases': ['GHSA-9'], + 'summary': 'bad', + 'database_specific': {'severity': 'HIGH'}, + }, + {'id': 'OSV-1', 'details': 'first line\nrest'}, + ] + first, second = cmd_audit.findings_from_commit( + 'wasmtime', 'a' * 40, 'm.json', 3, vulns + ) + assert (first.id, first.aliases, first.package, first.version) == ( + 'GHSA-9', + ('CVE-9',), + 'wasmtime', + 'a' * 12, + ) + assert first.score == 8.0 + assert (second.summary, second.score) == ('first line', None) + + +def test_nvd_findings_name_the_cpe_product_and_prefer_the_newest_cvss(): + cves = [ + { + 'id': 'CVE-1', + 'descriptions': [ + {'lang': 'es', 'value': 'malo'}, + {'lang': 'en', 'value': 'bad\nmore'}, + ], + 'metrics': { + 'cvssMetricV2': [{'cvssData': {'baseScore': 5.0}}], + 'cvssMetricV31': [{'cvssData': {'baseScore': 9.8}}], + }, + }, + {'id': 'CVE-2'}, + ] + first, second = cmd_audit.findings_from_nvd( + 'cpe:2.3:a:sqlite:sqlite:3.24.0:*:*:*:*:*:*:*', 'deps.json', 7, cves + ) + assert (first.package, first.version, first.summary, first.score) == ( + 'sqlite', + '3.24.0', + 'bad', + 9.8, + ) + assert second.score is None + + +def test_a_later_pass_does_not_repeat_an_advisory_under_another_id(tmp_path): + earlier = _findings(tmp_path) + commit = cmd_audit.findings_from_commit( + 'wasmtime', + 'a' * 40, + 'm.json', + 1, + [{'id': 'CVE-2'}, {'id': 'GHSA-new', 'aliases': ['RUSTSEC-1']}, {'id': 'GHSA-8'}], + ) + assert [f.id for f in cmd_audit.not_yet_reported(earlier, commit)] == ['GHSA-8'] + + +def _fingerprint(finding): + (result,) = cmd_audit._sarif([finding])['runs'][0]['results'] + return result['partialFingerprints'] + + +def test_sarif_fingerprint_survives_a_still_affected_bump(tmp_path): + finding = _findings(tmp_path)[1] + assert _fingerprint(finding) == _fingerprint( + dataclasses.replace(finding, version='4.3.1', line=40) + ) + assert _fingerprint(finding) != _fingerprint(dataclasses.replace(finding, path='b')) + + +def test_sarif_levels_and_severity(tmp_path): + sarif = cmd_audit._sarif(_findings(tmp_path)[1:])['runs'][0] + assert [r['level'] for r in sarif['results']] == ['error', 'note'] + rules = {r['id']: r for r in sarif['tool']['driver']['rules']} + assert rules['GHSA-2']['properties']['security-severity'] == '9.1' + assert 'security-severity' not in rules['RUSTSEC-3']['properties'] + + +def test_markdown_groups_by_file_and_escapes_pipes(tmp_path): + wasmtime, zip, _ = _findings(tmp_path) + findings = [ + dataclasses.replace(zip, path='a.json', summary='x | y', score=None), + zip, + wasmtime, + ] + report = cmd_audit._markdown(findings) + assert report.startswith('# Dependency Audit\n\n3 finding(s): 2 critical, 1 -\n') + assert report.index('## `a.json`') < report.index('## `executor/Cargo.lock`') + assert r'x \| y' in report + assert ( + '| critical | `zip@4.3.0` | [GHSA-2](https://osv.dev/vulnerability/GHSA-2) ' + in report + ) + + +def test_an_unchecked_out_submodule_refuses_the_scan(tmp_path, monkeypatch): + (tmp_path / '.git').mkdir() + repos = [ + common.Repo('manager', tmp_path, ''), + common.Repo('executors/v0.3.x', tmp_path / 'executors/v0.3.x', 'executors/v0.3.x'), + ] + monkeypatch.setattr(common, 'discover_repos', lambda root: repos) + with pytest.raises(common.ToolError, match='executors/v0.3.x'): + cmd_audit._tracked(tmp_path) + + +@pytest.mark.parametrize('returncode', [128, 127]) +def test_osv_scanner_exiting_neither_0_nor_1_is_a_failure( + tmp_path, monkeypatch, returncode +): + (tmp_path / 'Cargo.lock').write_text(CARGO_LOCK) + monkeypatch.setattr(common, 'command_to_executable', lambda name: name) + monkeypatch.setattr( + subprocess, + 'run', + lambda *args, **kwargs: subprocess.CompletedProcess(args, returncode, '', 'none'), + ) + repo = common.Repo('manager', tmp_path, '') + with pytest.raises(common.ToolError, match=f'exited {returncode}'): + cmd_audit._scan_lockfiles(_ctx(tmp_path), [(repo, ['Cargo.lock'])]) + + +def test_no_lockfile_is_a_failure(tmp_path): + repo = common.Repo('manager', tmp_path, '') + with pytest.raises(common.ToolError, match='no tracked lockfile'): + cmd_audit._scan_lockfiles(_ctx(tmp_path), [(repo, ['README.md'])]) + + +@pytest.mark.parametrize( + 'manifest, error', + [ + ({'repos': {}}, 'lists no vendored repos'), + ({'repos': {'third-party/wasmtime': {'commit': 'abc'}}}, 'no usable commit pin'), + ], +) +def test_a_vendored_manifest_without_usable_pins_is_a_failure( + tmp_path, manifest, error +): + path = tmp_path / cmd_audit.VENDORED_MANIFEST + path.parent.mkdir() + path.write_text(json.dumps(manifest)) + repo = common.Repo('manager', tmp_path, '') + with pytest.raises(common.ToolError, match=error): + cmd_audit._scan_vendored(_ctx(tmp_path), [(repo, [cmd_audit.VENDORED_MANIFEST])]) + + +def _http_error(code): + return urllib.error.HTTPError('url', code, 'no', {}, io.BytesIO()) + + +def test_osv_query_retries_5xx_follows_pages_and_fails_fast_on_4xx(monkeypatch): + monkeypatch.setattr(cmd_audit.time, 'sleep', lambda s: None) + answers = [ + _http_error(503), + {'vulns': [{'id': 'A'}], 'next_page_token': 't'}, + {'vulns': [{'id': 'B'}]}, + ] + sent = [] + + def fetch(request, timeout): + sent.append(json.loads(request.data)) + answer = answers.pop(0) + if isinstance(answer, Exception): + raise answer + return answer + + monkeypatch.setattr(cmd_audit, '_fetch_json', fetch) + assert [v['id'] for v in cmd_audit._osv_query({'commit': 'c'})] == ['A', 'B'] + assert sent[-1] == {'commit': 'c', 'page_token': 't'} + + answers = [_http_error(400)] + with pytest.raises(common.ToolError, match='400'): + cmd_audit._osv_query({'commit': 'c'}) + + +def test_osv_query_that_never_stops_paginating_is_a_failure(monkeypatch): + monkeypatch.setattr( + cmd_audit, '_fetch_json', lambda request, timeout: {'next_page_token': 't'} + ) + with pytest.raises(common.ToolError, match='did not stop paginating'): + cmd_audit._osv_query({'commit': 'c'}) + + +def test_nvd_follows_pages_and_retries_its_rate_limit(monkeypatch): + monkeypatch.setattr(cmd_audit.time, 'sleep', lambda s: None) + answers = [ + _http_error(403), + { + 'totalResults': 3, + 'vulnerabilities': [{'cve': {'id': 'A'}}, {'cve': {'id': 'B'}}], + }, + {'totalResults': 3, 'vulnerabilities': [{'cve': {'id': 'C'}}]}, + ] + urls = [] + + def fetch(request, timeout): + urls.append(request.full_url) + answer = answers.pop(0) + if isinstance(answer, Exception): + raise answer + return answer + + monkeypatch.setattr(cmd_audit, '_fetch_json', fetch) + assert [c['id'] for c in cmd_audit._nvd_cves('cpe:x', None)] == ['A', 'B', 'C'] + assert 'startIndex=2' in urls[-1] + + answers = [_http_error(404)] + with pytest.raises(common.ToolError, match='404'): + cmd_audit._nvd_cves('cpe:x', None) + + +def test_nvd_stops_on_an_empty_page(monkeypatch): + monkeypatch.setattr( + cmd_audit, + '_fetch_json', + lambda request, timeout: {'totalResults': 5, 'vulnerabilities': []}, + ) + assert cmd_audit._nvd_cves('cpe:x', None) == [] diff --git a/tests/runner/genvm_tool_plugins/ninja.py b/tests/runner/genvm_tool_plugins/ninja.py index a6a47384..74befd6e 100644 --- a/tests/runner/genvm_tool_plugins/ninja.py +++ b/tests/runner/genvm_tool_plugins/ninja.py @@ -110,19 +110,17 @@ def is_subpath(path: Path, base: Path) -> bool: return not relpath(path, base).startswith('..') -# Nix expressions that evaluate a checkout's runner derivations to their -# {id: hash} (latest.json) and {id: [hash]} (all.json) maps. -def runner_manifest_expr(version: str, kind: str) -> str: +def runner_manifest_expr(version: str) -> str: """ - Nix expr for one executor's `latest`/`all` runner manifest. + Nix expr for one executor's ``all.json`` runner manifest. - The umbrella's ./runners owns this: `all` is every runner compatible up to - `version`, `latest` is that executor's own current runners. Evaluated with - cwd at the manager root (the nix_eval rule cds into ``$wd``). + The umbrella's ./runners owns this: every runner compatible up to + `version`, each id naming that executor's own current runner as `latest`. + Evaluated with cwd at the manager root (the nix_eval rule cds into ``$wd``). """ return ( f'(import ./runners/manifest.nix ' - f'{{ executorVersion = "{version}"; host-system = builtins.currentSystem; }}).{kind}' + f'{{ executorVersion = "{version}"; host-system = builtins.currentSystem; }})' ) @@ -500,23 +498,22 @@ def frozen_registry(self) -> None: def nix_manifests(self) -> None: """ - Derive this line's `latest`/`all` runner manifests via the nix machinery. + Derive this line's ``all.json`` runner manifest via the nix machinery. - Manifests are derived by the umbrella machinery (which imports every active - line's runners), so both the line's and the umbrella's nix inputs matter. + It is derived by the umbrella machinery (which imports every active line's + runners), so both the line's and the umbrella's nix inputs matter. Requires the `nix_eval` rule. """ runners_nix_inputs = glob(self.exec_root / 'runners', '**/*.nix') + glob( self.source_dir / 'runners', '**/*.nix' ) - for kind in ('latest', 'all'): - out = f'{self.out_exec}/data/{kind}.json' - edge = self.n.build('nix_eval', out) - edge.var('expr', runner_manifest_expr(self.real, kind)) - edge.var('wd', self.source_dir) - edge.add_implicit_dependency(runners_nix_inputs) - edge.finish() - self.data_phony.add_dependency(out) + out = f'{self.out_exec}/data/all.json' + edge = self.n.build('nix_eval', out) + edge.var('expr', runner_manifest_expr(self.real)) + edge.var('wd', self.source_dir) + edge.add_implicit_dependency(runners_nix_inputs) + edge.finish() + self.data_phony.add_dependency(out) def install_tree(self) -> None: """`cp` this line's install tree into `out_exec` and register its binary.""" diff --git a/tests/runner/genvm_tool_plugins/runners.py b/tests/runner/genvm_tool_plugins/runners.py index e558febd..95337735 100644 --- a/tests/runner/genvm_tool_plugins/runners.py +++ b/tests/runner/genvm_tool_plugins/runners.py @@ -4,7 +4,7 @@ from pathlib import Path # `@RUNNER_LATEST__@`, where `` is `v0.3` or `v0.3.x` and -# `` is a key of that line's `latest.json`, e.g. `py-genlayer`. +# `` is a runner id that line ships, e.g. `py-genlayer`. TOKEN = re.compile(rb'@RUNNER_LATEST_(v\d+\.\d+(?:\.x)?)_([A-Za-z0-9_.-]+)@') @@ -21,16 +21,21 @@ def latest(root_dir: Path, line: str) -> dict[str, str]: if version is None: known = ', '.join(sorted(versions)) or '' raise KeyError(f'unknown executor line {line!r}, built lines are {known}') - path = root_dir / 'build' / 'out' / 'executor' / version / 'data' / 'latest.json' + data_dir = root_dir / 'build' / 'out' / 'executor' / version / 'data' + path = data_dir / 'all.json' if not path.exists(): raise FileNotFoundError( f'{path} is absent: build the runners before running this test' ) - return json.loads(path.read_text()) + registry = json.loads(path.read_text()) + # Frozen legacy lines list bare hashes and keep `latest` in its own file. + if any(isinstance(entry, list) for entry in registry.values()): + return json.loads((data_dir / 'latest.json').read_text()) + return {runner: entry['latest'] for runner, entry in registry.items()} def uid(root_dir: Path, line: str, runner: str) -> str: - """Uid of one runner, as `latest.json` has it after the last build.""" + """Uid of one runner, as the last build recorded it as latest.""" known = latest(root_dir, line) if runner not in known: raise KeyError(