From b1de7ffec2453c1f0ca15478a093640fba66a33d Mon Sep 17 00:00:00 2001 From: Quick104 <31828688+Quick104@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:50:39 -0500 Subject: [PATCH 1/2] feat(catalog): check release inputs and verify signed community releases update-catalog now validates the repository and tag before building any request URL, looks the repository up in approved-plugins.json before fetching, refuses drafts, prereleases, mismatched tags, older versions, and asset URLs outside the release, and reads manifest.json at one commit. Releases that carry plugin.sigstore.json are verified with gh attestation verify against either Silo's shared release workflow or a workflow in the plugin's own repository run from the release tag, and the entry records bundle_url. Once an entry is signed, unsigned releases of that plugin are refused. A manual run can roll back with allow_rollback. The update workflow drops its concurrency group, gives the workflow token no permissions, pins actions by SHA, and rebuilds on the current main for each push attempt. Ports Silo-Server/silo-plugins#21. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/update-catalog.yml | 87 ++++--- CONTRIBUTING.md | 51 ++++ README.md | 87 ++++++- catalog/approval.go | 13 + catalog/catalog.go | 32 ++- catalog/catalog_test.go | 377 ++++++++++++++++++++++++++- catalog/release.go | 131 ++++++++++ catalog/signature.go | 123 +++++++++ cmd/update-catalog/main.go | 236 ++++++++++++++--- go.mod | 1 + go.sum | 2 + 11 files changed, 1044 insertions(+), 96 deletions(-) create mode 100644 CONTRIBUTING.md create mode 100644 catalog/release.go create mode 100644 catalog/signature.go diff --git a/.github/workflows/update-catalog.yml b/.github/workflows/update-catalog.yml index 0d5d38c..4f74246 100644 --- a/.github/workflows/update-catalog.yml +++ b/.github/workflows/update-catalog.yml @@ -14,54 +14,83 @@ on: description: Release tag to publish required: true type: string + allow_rollback: + description: Accept a version that is not newer than the listed one (deliberate rollback only) + required: false + type: boolean + default: false -permissions: - contents: write +# CATALOG_PUSH_TOKEN pushes the catalog, and every approved repository is +# public, so the workflow token needs no permissions. +permissions: {} -concurrency: - group: plugin-catalog-update - cancel-in-progress: false +# No concurrency group. A group keyed on the dispatch payload lets anyone with +# the dispatch token cancel a plugin's pending run by sending bogus dispatches, +# since GitHub keeps one pending run per group. Runs instead race on the push: +# each attempt below rebuilds on the current main, and update-catalog re-checks +# the version against it, so a duplicate run becomes a no-op and an older +# release that loses to a newer one is refused. jobs: update: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v6 + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 with: token: ${{ secrets.CATALOG_PUSH_TOKEN }} - - uses: actions/setup-go@v6 + - uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0 with: go-version: "1.26" - - name: Update catalog manifest + - name: Test catalog tooling env: + GOWORK: "off" + run: go test ./... + + - name: Update, validate, and push the catalog + env: + GOWORK: "off" GITHUB_TOKEN: ${{ github.token }} PLUGIN_REPO: ${{ inputs.repo || github.event.client_payload.repo }} PLUGIN_TAG: ${{ inputs.tag || github.event.client_payload.tag }} + # Only a manual run can roll back; a repository_dispatch payload + # cannot set this. + ALLOW_ROLLBACK: ${{ github.event_name == 'workflow_dispatch' && inputs.allow_rollback }} run: | test -n "$PLUGIN_REPO" test -n "$PLUGIN_TAG" - go run ./cmd/update-catalog -repo "$PLUGIN_REPO" -tag "$PLUGIN_TAG" - - - name: Validate approved catalog - env: - GOWORK: off - run: | - go test ./... - go run ./cmd/check-catalog - - - name: Commit catalog changes - env: - PLUGIN_REPO: ${{ inputs.repo || github.event.client_payload.repo }} - PLUGIN_TAG: ${{ inputs.tag || github.event.client_payload.tag }} - run: | - if git diff --quiet -- manifest.json; then - exit 0 - fi git config user.name "github-actions[bot]" git config user.email "41898282+github-actions[bot]@users.noreply.github.com" - git add manifest.json - git commit -m "chore: update catalog for ${PLUGIN_REPO}@${PLUGIN_TAG}" - git pull --rebase origin main - git push origin HEAD:main + args=(-repo "$PLUGIN_REPO" -tag "$PLUGIN_TAG") + if [ "$ALLOW_ROLLBACK" = "true" ]; then + args+=(-allow-rollback) + fi + # update-catalog validates the repository and tag, refuses a + # repository outside approved-plugins.json, a version not newer than + # the listed one, drafts and prereleases, asset URLs outside the + # release, and an unsigned release of a plugin whose entry is signed. + # It verifies a signed release's binaries with gh attestation verify + # and reads manifest.json at the release's commit. + # A repository_dispatch run checks out main as it was when the release + # fired, and other releases may have updated the catalog since. Each + # attempt rebuilds the entry on the current main, so losing a push race + # retries instead of conflicting on manifest.json. + for attempt in 1 2 3 4 5; do + git fetch --quiet origin main + git reset --quiet --hard FETCH_HEAD + go run ./cmd/update-catalog "${args[@]}" + if git diff --quiet -- manifest.json; then + echo "The catalog already lists ${PLUGIN_REPO}@${PLUGIN_TAG}." + exit 0 + fi + go run ./cmd/check-catalog + git add manifest.json + git commit --quiet -m "chore: update catalog for ${PLUGIN_REPO}@${PLUGIN_TAG}" + if git push origin HEAD:main; then + exit 0 + fi + sleep $((attempt * 5)) + done + echo "Could not push the catalog update after 5 attempts." >&2 + exit 1 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..466fe45 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,51 @@ +# Contributing to the Approved Community Plugin Catalog + +This repository holds the catalog of approved community plugins and the tooling +that builds it. Plugin implementation changes belong in the plugin's own +repository. Approval requests and the review rules are covered by +[`APPROVAL_POLICY.md`](APPROVAL_POLICY.md). + +## Before you start + +Open an [issue](https://github.com/Silo-Community/silo-plugins/issues) before +changing catalog schema, the approval registry format, release ingestion, +signature policy, or automation. + +Catalog entries are generated from a plugin's published GitHub release by the +update workflow. Do not hand-edit `manifest.json` to work around a missing or +incorrect upstream artifact. Changes to `approved-plugins.json` follow the +approval policy and are reviewed by its code owner. + +## Development setup + +Use the Go version declared in `go.mod`. The update command reads release assets +from GitHub, so use a real tagged release when testing ingestion, and run it +against a copy of `manifest.json` as the README shows. Never place tokens in +command arguments, committed files, or logs. + +## Validate your change + +```sh +GOWORK=off go test ./... +GOWORK=off go vet ./... +GOWORK=off go build ./... +GOWORK=off go run ./cmd/check-catalog +gofmt -l . +``` + +`gofmt -l .` should print nothing. For catalog output changes, inspect the +complete `manifest.json` diff and verify repository URLs, versions, platforms, +the checksum URL, the bundle URL when the release is signed, capabilities, +presentation metadata, and approval metadata against the source release and +the registry. + +Changes to `update-catalog`'s refusal rules (input validation, the approval +registry lookup, version order, asset URLs, the commit binding, or signature +policy) need a unit test for each refusal in `catalog/catalog_test.go`. + +## Open the pull request + +Use a Conventional Commit title, explain the catalog or automation impact, and +paste the actual validation results. Disclose AI assistance as the +[AI-assisted contribution policy](https://github.com/Silo-Server/silo-server/blob/main/docs/ai-contributions.md) +describes. diff --git a/README.md b/README.md index 487382d..8148c12 100644 --- a/README.md +++ b/README.md @@ -22,9 +22,88 @@ can publish entries. See [`APPROVAL_POLICY.md`](APPROVAL_POLICY.md) for the review requirements. After an approved plugin publishes a checksum-bearing GitHub release, its -release workflow dispatches `plugin_release_published` to this repository. The -catalog updater verifies the repository and plugin ID against the approval -registry before updating `manifest.json`. +release workflow dispatches `plugin_release_published` to this repository. + +## Catalog updates + +`update-catalog` refuses a release unless all of these hold, and writes +`manifest.json` only after the last check: + +- The repository is `owner/name` and the tag is `v..`, + optionally with a `-suffix`. Both are checked before either goes into a URL. +- The repository is listed in `approved-plugins.json`, spelled exactly as it is + there, and the release's `manifest.json` names the plugin ID approved for it. + A dispatch cannot add a repository or change which plugin it publishes. +- The version is newer than the plugin's listed version. Re-running for the + listed version does nothing and succeeds. A deliberate rollback needs the + `allow_rollback` input of a manual `workflow_dispatch` run; a + `repository_dispatch` cannot set it. +- The release is published for exactly that tag and is neither a draft nor a + prerelease. There is no override for drafts or prereleases. +- Every catalogued asset URL (the `plugin--` binaries, + `checksums.txt`, and `plugin.sigstore.json`) is + `https://github.com//releases/download//`. Other + release assets, such as `.zip` archives, are ignored. + +`manifest.json` is read at a commit SHA, never at the tag name: the commit the +signature names for a signed release, or the commit the tag points at +(following an annotated tag) for an unsigned one. + +Entries whose plugin is no longer in `approved-plugins.json` are removed the +next time the catalog is written. + +### Signed releases + +A signed release carries `plugin.sigstore.json`: a Sigstore bundle with one SLSA +provenance attestation covering every `plugin--` binary. The catalog +entry then gains a package-level `bundle_url` beside `checksums_url`. Before +writing the entry, `update-catalog` checks each binary with +`gh attestation verify`. The certificate must name one of two signers: + +- Silo's shared release workflow, + `https://github.com/Silo-Server/.github/.github/workflows/plugin-release.yml@refs/heads/main`. +- A workflow in the plugin's own repository run from the release tag, + `https://github.com//.github/workflows/@refs/tags/`, where + `` is a `.yml` or `.yaml` file directly in that directory. + +Either way, the tool also requires the issuer +`https://token.actions.githubusercontent.com`, the approved repository as the +source repository, the source ref `refs/tags/`, a GitHub-hosted runner, +SLSA provenance v1, a Sigstore public-good transparency-log entry, and the same +source commit for every binary. Any failure refuses the release. + +Unsigned releases are still accepted for a plugin until its catalog entry has a +`bundle_url`; after that, an unsigned release of that plugin is refused. + +### Workflow + +The update workflow has no concurrency group. Runs race on the push, and each +attempt rebuilds the entry on the current `main` and runs `check-catalog`, so a +duplicate run is a no-op and an older release that loses the race to a newer +one is refused. `CATALOG_PUSH_TOKEN` pushes the update; the workflow's own +token has no permissions, because every approved repository is public. + +### Running it locally + +Pass the repository in `owner/name` form and the exact release tag. Verifying a +signed release needs the `gh` CLI on `PATH`. To try the tool against a release +the catalog already lists, rebuild that entry in a copy of the catalog: + +```sh +catalog_copy="$(mktemp)" +cp manifest.json "$catalog_copy" +GOWORK=off go run ./cmd/update-catalog \ + -repo Silo-Community/silo-plugins-requests-arr \ + -tag v0.1.11 \ + -allow-rollback \ + -manifest "$catalog_copy" +diff manifest.json "$catalog_copy" +``` + +Without `-allow-rollback`, the listed version does nothing and an older one is +refused. + +## Development Validate the registry and generated catalog locally with: @@ -33,6 +112,8 @@ GOWORK=off go test ./... GOWORK=off go run ./cmd/check-catalog ``` +See [CONTRIBUTING.md](CONTRIBUTING.md) before opening a pull request. + ## License The catalog tooling is licensed under `Apache-2.0`. See [LICENSE](LICENSE). diff --git a/catalog/approval.go b/catalog/approval.go index 44ff4d0..13e9d67 100644 --- a/catalog/approval.go +++ b/catalog/approval.go @@ -71,6 +71,19 @@ func (r ApprovalRegistry) Resolve(repository, pluginID string) (Approval, error) return Approval{}, fmt.Errorf("plugin %s from %s is not approved", pluginID, repository) } +// ApprovedPluginID returns the plugin ID the registry binds to repo, or an +// error when repo is not approved. update-catalog calls it before any fetch. +// The comparison is exact, so every request and asset URL built from repo uses +// the spelling the registry records. +func (r ApprovalRegistry) ApprovedPluginID(repo string) (string, error) { + for _, approved := range r.Plugins { + if approved.Repository == repo { + return approved.PluginID, nil + } + } + return "", fmt.Errorf("repository %s is not in the approval registry", repo) +} + func ValidateApprovedIndex(index RepositoryIndex, registry ApprovalRegistry) error { seenPluginIDs := make(map[string]struct{}, len(index.Plugins)) for _, pkg := range index.Plugins { diff --git a/catalog/catalog.go b/catalog/catalog.go index 7ca51a0..7ce4abc 100644 --- a/catalog/catalog.go +++ b/catalog/catalog.go @@ -19,8 +19,10 @@ type Asset struct { } type Release struct { - TagName string `json:"tag_name"` - Assets []Asset `json:"assets"` + TagName string `json:"tag_name"` + Draft bool `json:"draft"` + Prerelease bool `json:"prerelease"` + Assets []Asset `json:"assets"` } type PlatformBinary struct { @@ -31,6 +33,7 @@ type CatalogPackage struct { Manifest *pluginv1.PluginManifest `json:"manifest"` RepoURL string `json:"repo_url,omitempty"` ChecksumsURL string `json:"checksums_url,omitempty"` + BundleURL string `json:"bundle_url,omitempty"` Binaries map[string]PlatformBinary `json:"binaries,omitempty"` Approval *Approval `json:"approval,omitempty"` } @@ -70,22 +73,16 @@ func BuildPackageFromRelease(repo string, source *SourceManifest, release Releas return CatalogPackage{}, fmt.Errorf("release tag_name is required") } - checksumsURL := "" - binaries := map[string]PlatformBinary{} - for _, asset := range release.Assets { - switch { - case asset.Name == "checksums.txt": - checksumsURL = asset.BrowserDownloadURL - case strings.HasPrefix(asset.Name, "plugin-"): - platform, ok := platformKeyFromAssetName(asset.Name) - if !ok { - continue - } - binaries[platform] = PlatformBinary{URL: asset.BrowserDownloadURL} - } + assets, err := ParseReleaseAssets(repo, release) + if err != nil { + return CatalogPackage{}, err + } + binaries := make(map[string]PlatformBinary, len(assets.Binaries)) + for platform, url := range assets.Binaries { + binaries[platform] = PlatformBinary{URL: url} } - if checksumsURL == "" { + if assets.ChecksumsURL == "" { return CatalogPackage{}, fmt.Errorf("release %q is missing checksums.txt", release.TagName) } if len(binaries) == 0 { @@ -107,7 +104,8 @@ func BuildPackageFromRelease(repo string, source *SourceManifest, release Releas return CatalogPackage{ Manifest: manifest, RepoURL: repositoryURL, - ChecksumsURL: checksumsURL, + ChecksumsURL: assets.ChecksumsURL, + BundleURL: assets.BundleURL, Binaries: binaries, }, nil } diff --git a/catalog/catalog_test.go b/catalog/catalog_test.go index fe933e3..d7ec9cf 100644 --- a/catalog/catalog_test.go +++ b/catalog/catalog_test.go @@ -2,6 +2,8 @@ package catalog import ( "encoding/json" + "regexp" + "slices" "testing" pluginv1 "github.com/Silo-Server/silo-plugin-sdk/pkg/pluginproto/silo/plugin/v1" @@ -26,9 +28,9 @@ func TestBuildPackageFromRelease_MinimalManifestAndAssets(t *testing.T) { release := Release{ TagName: "v1.2.3", Assets: []Asset{ - {Name: "plugin-linux-amd64", BrowserDownloadURL: "https://example.invalid/tmdb/plugin-linux-amd64"}, - {Name: "plugin-linux-arm64", BrowserDownloadURL: "https://example.invalid/tmdb/plugin-linux-arm64"}, - {Name: "checksums.txt", BrowserDownloadURL: "https://example.invalid/tmdb/checksums.txt"}, + {Name: "plugin-linux-amd64", BrowserDownloadURL: "https://github.com/Silo-Server/silo-plugin-tmdb/releases/download/v1.2.3/plugin-linux-amd64"}, + {Name: "plugin-linux-arm64", BrowserDownloadURL: "https://github.com/Silo-Server/silo-plugin-tmdb/releases/download/v1.2.3/plugin-linux-arm64"}, + {Name: "checksums.txt", BrowserDownloadURL: "https://github.com/Silo-Server/silo-plugin-tmdb/releases/download/v1.2.3/checksums.txt"}, {Name: "notes.txt", BrowserDownloadURL: "https://example.invalid/tmdb/notes.txt"}, }, } @@ -71,8 +73,8 @@ func TestBuildPackageFromRelease_TagWinsOverManifestVersion(t *testing.T) { release := Release{ TagName: "v1.2.3", Assets: []Asset{ - {Name: "plugin-linux-amd64", BrowserDownloadURL: "https://example.invalid/tmdb/plugin-linux-amd64"}, - {Name: "checksums.txt", BrowserDownloadURL: "https://example.invalid/tmdb/checksums.txt"}, + {Name: "plugin-linux-amd64", BrowserDownloadURL: "https://github.com/Silo-Server/silo-plugin-tmdb/releases/download/v1.2.3/plugin-linux-amd64"}, + {Name: "checksums.txt", BrowserDownloadURL: "https://github.com/Silo-Server/silo-plugin-tmdb/releases/download/v1.2.3/checksums.txt"}, }, } @@ -97,8 +99,8 @@ func TestBuildPackageFromRelease_RequiresCompletePresentation(t *testing.T) { release := Release{ TagName: "v0.1.1", Assets: []Asset{ - {Name: "plugin-linux-amd64", BrowserDownloadURL: "https://example.invalid/arr/plugin-linux-amd64"}, - {Name: "checksums.txt", BrowserDownloadURL: "https://example.invalid/arr/checksums.txt"}, + {Name: "plugin-linux-amd64", BrowserDownloadURL: "https://github.com/Silo-Community/silo-plugins-requests-arr/releases/download/v0.1.1/plugin-linux-amd64"}, + {Name: "checksums.txt", BrowserDownloadURL: "https://github.com/Silo-Community/silo-plugins-requests-arr/releases/download/v0.1.1/checksums.txt"}, }, } @@ -118,10 +120,10 @@ func TestBuildPackageFromRelease_PreservesManifestMetadataAndConfigSchema(t *tes "summary": "Routes requests to Sonarr and Radarr.", "description_markdown": "Routes approved requests.", "setup_markdown": "Add a connection.", - "homepage_url": "https://github.com/Silo-Server/silo-plugins-requests-arr", - "source_url": "https://github.com/Silo-Server/silo-plugins-requests-arr", - "support_url": "https://github.com/Silo-Server/silo-plugins-requests-arr/issues", - "changelog_url": "https://github.com/Silo-Server/silo-plugins-requests-arr/releases", + "homepage_url": "https://github.com/Silo-Community/silo-plugins-requests-arr", + "source_url": "https://github.com/Silo-Community/silo-plugins-requests-arr", + "support_url": "https://github.com/Silo-Community/silo-plugins-requests-arr/issues", + "changelog_url": "https://github.com/Silo-Community/silo-plugins-requests-arr/releases", "publisher_name": "Silo", "publisher_url": "https://github.com/Silo-Server", "license_spdx": "AGPL-3.0-only" @@ -165,12 +167,12 @@ func TestBuildPackageFromRelease_PreservesManifestMetadataAndConfigSchema(t *tes release := Release{ TagName: "v0.1.1", Assets: []Asset{ - {Name: "plugin-linux-amd64", BrowserDownloadURL: "https://example.invalid/arr/plugin-linux-amd64"}, - {Name: "checksums.txt", BrowserDownloadURL: "https://example.invalid/arr/checksums.txt"}, + {Name: "plugin-linux-amd64", BrowserDownloadURL: "https://github.com/Silo-Community/silo-plugins-requests-arr/releases/download/v0.1.1/plugin-linux-amd64"}, + {Name: "checksums.txt", BrowserDownloadURL: "https://github.com/Silo-Community/silo-plugins-requests-arr/releases/download/v0.1.1/checksums.txt"}, }, } - pkg, err := BuildPackageFromRelease("Silo-Server/silo-plugins-requests-arr", source, release) + pkg, err := BuildPackageFromRelease("Silo-Community/silo-plugins-requests-arr", source, release) if err != nil { t.Fatalf("BuildPackageFromRelease() error = %v", err) } @@ -274,3 +276,350 @@ func TestUpsertPackage_ReplacesExistingPluginAndSorts(t *testing.T) { t.Fatalf("Plugins[1].PluginID = %q", index.Plugins[1].Manifest.GetPluginId()) } } + +const ( + testRepo = "Silo-Community/silo-plugins-requests-arr" + testBase = "https://github.com/" + testRepo + "/releases/download/v0.1.11/" +) + +func TestBuildPackageFromRelease_BundleURL(t *testing.T) { + source := &SourceManifest{ + PluginId: "silo.requests.arr", + Version: "0.1.11", + SiloApiVersion: "v1", + Presentation: catalogTestPresentation("https://github.com/" + testRepo), + Capabilities: []*pluginv1.CapabilityDescriptor{{Type: "request_router.v1", Id: "arr"}}, + } + assets := []Asset{ + {Name: "plugin-linux-amd64", BrowserDownloadURL: testBase + "plugin-linux-amd64"}, + {Name: "checksums.txt", BrowserDownloadURL: testBase + "checksums.txt"}, + // Other release assets, such as zip archives, are not catalogued. + {Name: "silo-plugin-0.1.11-linux-amd64.zip", BrowserDownloadURL: testBase + "silo-plugin-0.1.11-linux-amd64.zip"}, + } + + unsigned, err := BuildPackageFromRelease(testRepo, source, Release{TagName: "v0.1.11", Assets: assets}) + if err != nil { + t.Fatalf("BuildPackageFromRelease(unsigned) error = %v", err) + } + if unsigned.BundleURL != "" { + t.Fatalf("unsigned BundleURL = %q, want empty", unsigned.BundleURL) + } + data, err := json.Marshal(unsigned) + if err != nil { + t.Fatal(err) + } + var raw map[string]json.RawMessage + if err := json.Unmarshal(data, &raw); err != nil { + t.Fatal(err) + } + if _, ok := raw["bundle_url"]; ok { + t.Fatalf("unsigned entry emits bundle_url: %s", data) + } + + bundle := testBase + BundleAssetName + signedAssets := append(slices.Clone(assets), Asset{Name: BundleAssetName, BrowserDownloadURL: bundle}) + signed, err := BuildPackageFromRelease(testRepo, source, Release{TagName: "v0.1.11", Assets: signedAssets}) + if err != nil { + t.Fatalf("BuildPackageFromRelease(signed) error = %v", err) + } + if signed.BundleURL != bundle { + t.Fatalf("BundleURL = %q, want %q", signed.BundleURL, bundle) + } + if len(signed.Binaries) != 1 { + t.Fatalf("Binaries = %v, want only the plugin binary", signed.Binaries) + } + data, err = json.Marshal(signed) + if err != nil { + t.Fatal(err) + } + var decoded struct { + BundleURL string `json:"bundle_url"` + ChecksumsURL string `json:"checksums_url"` + } + if err := json.Unmarshal(data, &decoded); err != nil { + t.Fatal(err) + } + if decoded.BundleURL != bundle || decoded.ChecksumsURL == "" { + t.Fatalf("encoded entry = %s, want bundle_url beside checksums_url", data) + } +} + +func TestApprovedPluginID(t *testing.T) { + registry, err := DecodeApprovalRegistry([]byte(`{ + "plugins": [ + {"plugin_id": "silo.requests.arr", "repository": "Silo-Community/silo-plugins-requests-arr", "approved_at": "2026-07-09", "review_url": "https://github.com/Silo-Community/silo-plugins/issues/2"}, + {"plugin_id": "silo.requests.seerr", "repository": "Silo-Community/silo-plugins-requests-seerr", "approved_at": "2026-07-09", "review_url": "https://github.com/Silo-Community/silo-plugins/issues/1"} + ] +}`)) + if err != nil { + t.Fatalf("DecodeApprovalRegistry() error = %v", err) + } + if got, err := registry.ApprovedPluginID("Silo-Community/silo-plugins-requests-seerr"); err != nil || got != "silo.requests.seerr" { + t.Fatalf("ApprovedPluginID(seerr) = %q, %v; want silo.requests.seerr", got, err) + } + for _, repo := range []string{ + "Silo-Community/silo-plugins-requests-other", + "silo-community/silo-plugins-requests-arr", + "Silo-Server/silo-plugins-requests-arr", + "", + } { + if got, err := registry.ApprovedPluginID(repo); err == nil { + t.Fatalf("ApprovedPluginID(%q) = %q, want an error", repo, got) + } + } +} + +func TestCheckSignatureRatchet(t *testing.T) { + const bundle = testBase + BundleAssetName + signed := &CatalogPackage{Manifest: &pluginv1.PluginManifest{PluginId: "silo.requests.arr"}, BundleURL: bundle} + unsigned := &CatalogPackage{Manifest: &pluginv1.PluginManifest{PluginId: "silo.requests.seerr"}} + + tests := []struct { + name string + current *CatalogPackage + bundleURL string + wantErr bool + }{ + {"unsigned after signed", signed, "", true}, + {"signed after signed", signed, bundle, false}, + {"unsigned after unsigned", unsigned, "", false}, + {"signed after unsigned", unsigned, bundle, false}, + {"unsigned first release", nil, "", false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := CheckSignatureRatchet(tt.current, tt.bundleURL) + if (err != nil) != tt.wantErr { + t.Fatalf("CheckSignatureRatchet() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestVerifyArgs(t *testing.T) { + got := VerifyArgs("/tmp/v/binary-0", "/tmp/v/plugin.sigstore.json", testRepo, "v0.1.11") + want := []string{ + "attestation", "verify", "/tmp/v/binary-0", + "--bundle=/tmp/v/plugin.sigstore.json", + "--repo=Silo-Community/silo-plugins-requests-arr", + `--cert-identity-regex=^(?:https://github\.com/Silo-Server/\.github/\.github/workflows/plugin-release\.yml@refs/heads/main|https://github\.com/Silo-Community/silo-plugins-requests-arr/\.github/workflows/[A-Za-z0-9._-]+\.ya?ml@refs/tags/v0\.1\.11)$`, + "--cert-oidc-issuer=https://token.actions.githubusercontent.com", + "--source-ref=refs/tags/v0.1.11", + "--predicate-type=https://slsa.dev/provenance/v1", + "--deny-self-hosted-runners", + "--format=json", + } + if !slices.Equal(got, want) { + t.Fatalf("VerifyArgs() =\n%q\nwant\n%q", got, want) + } +} + +func TestCertIdentityRegex(t *testing.T) { + pattern := regexp.MustCompile(CertIdentityRegex(testRepo, "v0.1.11")) + const own = "https://github.com/" + testRepo + "/.github/workflows/" + tests := []struct { + name string + san string + want bool + }{ + {"shared Silo workflow at main", SharedSignerIdentity, true}, + {"own release workflow at the tag", own + "release.yml@refs/tags/v0.1.11", true}, + {"own workflow with yaml extension", own + "publish.yaml@refs/tags/v0.1.11", true}, + {"shared workflow at a tag", "https://github.com/Silo-Server/.github/.github/workflows/plugin-release.yml@refs/tags/v1.0.0", false}, + {"shared workflow at another branch", "https://github.com/Silo-Server/.github/.github/workflows/plugin-release.yml@refs/heads/dev", false}, + {"shared workflow in a fork", "https://github.com/someone/.github/.github/workflows/plugin-release.yml@refs/heads/main", false}, + {"another repository's workflow", "https://github.com/Silo-Community/silo-plugins-requests-seerr/.github/workflows/release.yml@refs/tags/v0.1.11", false}, + {"repository name as a prefix", "https://github.com/Silo-Community/silo-plugins-requests-arrx/.github/workflows/release.yml@refs/tags/v0.1.11", false}, + {"another tag", own + "release.yml@refs/tags/v0.1.10", false}, + {"tag as a prefix", own + "release.yml@refs/tags/v0.1.110", false}, + {"suffixed tag", own + "release.yml@refs/tags/v0.1.11-rc.1", false}, + {"branch ref", own + "release.yml@refs/heads/main", false}, + {"branch named like the tag", own + "release.yml@refs/heads/v0.1.11", false}, + {"workflow in a subdirectory", own + "nested/release.yml@refs/tags/v0.1.11", false}, + {"workflow outside .github/workflows", "https://github.com/" + testRepo + "/release.yml@refs/tags/v0.1.11", false}, + {"unescaped dot in the tag", own + "release.yml@refs/tags/v0x1x11", false}, + {"unescaped dot in the host", "https://githubxcom/" + testRepo + "/.github/workflows/release.yml@refs/tags/v0.1.11", false}, + {"trailing text", own + "release.yml@refs/tags/v0.1.11/extra", false}, + {"leading text", "x" + SharedSignerIdentity, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := pattern.MatchString(tt.san); got != tt.want { + t.Fatalf("CertIdentityRegex matches %q = %v, want %v", tt.san, got, tt.want) + } + }) + } +} + +func TestCheckVerifyOutput(t *testing.T) { + const commitA = "29fd28771a006d9d68a311865a2b858c5afe301a" + const commitB = "63837fb778d6a39f02be0ce76ff89f2bc8d74bdc" + result := func(issuer, tsType, digest string) string { + return `{"verificationResult":{"signature":{"certificate":{"certificateIssuer":"` + issuer + + `","sourceRepositoryDigest":"` + digest + `"}},"verifiedTimestamps":[{"type":"` + tsType + `","uri":"https://rekor.sigstore.dev"}]}}` + } + const publicGood = "CN=sigstore-intermediate,O=sigstore.dev" + tests := []struct { + name string + out string + wantCommit string + wantErr bool + }{ + {"public-good with transparency log", "[" + result(publicGood, "Tlog", commitA) + "]", commitA, false}, + {"two attestations, one commit", "[" + result(publicGood, "Tlog", commitA) + "," + result(publicGood, "Tlog", commitA) + "]", commitA, false}, + {"two attestations, two commits", "[" + result(publicGood, "Tlog", commitA) + "," + result(publicGood, "Tlog", commitB) + "]", "", true}, + {"no source commit", "[" + result(publicGood, "Tlog", "") + "]", "", true}, + {"short source commit", "[" + result(publicGood, "Tlog", "29fd287") + "]", "", true}, + {"GitHub private instance", "[" + result("CN=Fulcio Intermediate l2,O=GitHub\\, Inc.", "TimestampAuthority", commitA) + "]", "", true}, + {"no transparency-log entry", "[" + result(publicGood, "TimestampAuthority", commitA) + "]", "", true}, + {"no attestations", `[]`, "", true}, + {"not JSON", `verified`, "", true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + commit, err := CheckVerifyOutput([]byte(tt.out)) + if (err != nil) != tt.wantErr { + t.Fatalf("CheckVerifyOutput() error = %v, wantErr %v", err, tt.wantErr) + } + if commit != tt.wantCommit { + t.Fatalf("CheckVerifyOutput() commit = %q, want %q", commit, tt.wantCommit) + } + }) + } +} + +func TestValidateInputs(t *testing.T) { + tests := []struct { + name string + repo string + tag string + wantErr bool + }{ + {"release tag", testRepo, "v1.2.3", false}, + {"suffixed tag", testRepo, "v1.2.3-hotfix.1", false}, + {"traversal in tag", testRepo, "../../../../Silo-Community/silo-plugins-requests-seerr/releases/tags/v0.2.1", true}, + {"traversal after version", testRepo, "v1.2.3/../../../attacker/evil", true}, + {"slash in suffix", testRepo, "v1.2.3-a/b", true}, + {"query in tag", testRepo, "v1.2.3?x=1", true}, + {"encoded slash", testRepo, "v1.2.3-%2F", true}, + {"no v prefix", testRepo, "1.2.3", true}, + {"leading zero", testRepo, "v01.2.3", true}, + {"leading zero in suffix", testRepo, "v1.2.3-01", true}, + {"empty suffix identifier", testRepo, "v1.2.3-a..b", true}, + {"missing patch", testRepo, "v1.2", true}, + {"empty tag", testRepo, "", true}, + {"three path segments", testRepo + "/x", "v1.2.3", true}, + {"traversal in repo", "Silo-Community/../attacker", "v1.2.3", true}, + {"no owner", "/silo-plugins-requests-arr", "v1.2.3", true}, + {"empty repo", "", "v1.2.3", true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := ValidateInputs(tt.repo, tt.tag) + if (err != nil) != tt.wantErr { + t.Fatalf("ValidateInputs(%q, %q) error = %v, wantErr %v", tt.repo, tt.tag, err, tt.wantErr) + } + }) + } +} + +func TestValidateCommit(t *testing.T) { + for _, sha := range []string{"", "29fd287", "29FD28771A006D9D68A311865A2B858C5AFE301A", "../../29fd28771a006d9d68a311865a2b858c5af"} { + if err := ValidateCommit(sha); err == nil { + t.Fatalf("ValidateCommit(%q) accepted an invalid SHA", sha) + } + } + if err := ValidateCommit("29fd28771a006d9d68a311865a2b858c5afe301a"); err != nil { + t.Fatalf("ValidateCommit() error = %v", err) + } +} + +func TestCheckRelease(t *testing.T) { + tests := []struct { + name string + release Release + wantErr bool + }{ + {"published release", Release{TagName: "v1.2.3"}, false}, + {"different tag", Release{TagName: "v9.0.0"}, true}, + {"draft", Release{TagName: "v1.2.3", Draft: true}, true}, + {"prerelease", Release{TagName: "v1.2.3", Prerelease: true}, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := CheckRelease("v1.2.3", tt.release) + if (err != nil) != tt.wantErr { + t.Fatalf("CheckRelease() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestParseReleaseAssets(t *testing.T) { + valid := []Asset{ + {Name: "plugin-linux-amd64", BrowserDownloadURL: testBase + "plugin-linux-amd64"}, + {Name: "checksums.txt", BrowserDownloadURL: testBase + "checksums.txt"}, + {Name: BundleAssetName, BrowserDownloadURL: testBase + BundleAssetName}, + // Not catalogued, so their URLs are not checked. + {Name: "notes.txt", BrowserDownloadURL: "https://example.invalid/notes.txt"}, + {Name: "silo-plugin-0.1.11-linux-amd64.zip", BrowserDownloadURL: "https://example.invalid/plugin.zip"}, + } + assets, err := ParseReleaseAssets(testRepo, Release{TagName: "v0.1.11", Assets: valid}) + if err != nil { + t.Fatalf("ParseReleaseAssets() error = %v", err) + } + if assets.ChecksumsURL != testBase+"checksums.txt" || assets.BundleURL != testBase+BundleAssetName || assets.Binaries["linux/amd64"] != testBase+"plugin-linux-amd64" || len(assets.Binaries) != 1 { + t.Fatalf("ParseReleaseAssets() = %+v", assets) + } + + tests := []struct { + name string + asset Asset + }{ + {"binary in another repository", Asset{Name: "plugin-linux-arm64", BrowserDownloadURL: "https://github.com/Silo-Community/silo-plugins-requests-seerr/releases/download/v0.1.11/plugin-linux-arm64"}}, + {"binary on another host", Asset{Name: "plugin-linux-arm64", BrowserDownloadURL: "https://example.invalid/plugin-linux-arm64"}}, + {"binary in another tag", Asset{Name: "plugin-linux-arm64", BrowserDownloadURL: "https://github.com/" + testRepo + "/releases/download/v0.1.10/plugin-linux-arm64"}}, + {"binary path traversal", Asset{Name: "plugin-linux-arm64", BrowserDownloadURL: testBase + "../../../../../attacker/evil/releases/download/v0.1.11/plugin-linux-arm64"}}, + {"binary under another name", Asset{Name: "plugin-linux-arm64", BrowserDownloadURL: testBase + "plugin-linux-amd64"}}, + {"plain-http checksums", Asset{Name: "checksums.txt", BrowserDownloadURL: "http://github.com/" + testRepo + "/releases/download/v0.1.11/checksums.txt"}}, + {"bundle in another repository", Asset{Name: BundleAssetName, BrowserDownloadURL: "https://github.com/attacker/evil/releases/download/v0.1.11/" + BundleAssetName}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + release := Release{TagName: "v0.1.11", Assets: append(slices.Clone(valid[:1]), tt.asset)} + if _, err := ParseReleaseAssets(testRepo, release); err == nil { + t.Fatalf("ParseReleaseAssets() accepted %+v", tt.asset) + } + }) + } +} + +func TestCheckVersion(t *testing.T) { + current := &CatalogPackage{Manifest: &pluginv1.PluginManifest{PluginId: "silo.requests.arr", Version: "1.2.10"}} + tests := []struct { + name string + current *CatalogPackage + version string + allowRollback bool + wantListed bool + wantErr bool + }{ + {"first release", nil, "0.1.0", false, false, false}, + {"newer patch, compared numerically", current, "1.2.11", false, false, false}, + {"newer minor", current, "1.3.0", false, false, false}, + {"same version is a no-op", current, "1.2.10", false, true, false}, + {"older patch, compared numerically", current, "1.2.9", false, false, true}, + {"older major", current, "0.9.0", false, false, true}, + {"suffixed version sorts before its release", current, "1.2.10-hotfix.1", false, false, true}, + {"rollback with override", current, "1.2.9", true, false, false}, + {"same version with override rewrites", current, "1.2.10", true, false, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + listed, err := CheckVersion(tt.current, tt.version, tt.allowRollback) + if (err != nil) != tt.wantErr || listed != tt.wantListed { + t.Fatalf("CheckVersion() = %v, %v; want listed %v, wantErr %v", listed, err, tt.wantListed, tt.wantErr) + } + }) + } +} diff --git a/catalog/release.go b/catalog/release.go new file mode 100644 index 0000000..db9473f --- /dev/null +++ b/catalog/release.go @@ -0,0 +1,131 @@ +package catalog + +import ( + "fmt" + "net/url" + "regexp" + "strings" + + "golang.org/x/mod/semver" +) + +var ( + repoPattern = regexp.MustCompile(`^[A-Za-z0-9-]+/[A-Za-z0-9._-]+$`) + tagPattern = regexp.MustCompile(`^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[0-9A-Za-z.-]+)?$`) + commitPattern = regexp.MustCompile(`^[0-9a-f]{40}$`) +) + +// ValidateInputs checks the caller-supplied repository and tag before either +// goes into a URL. Both come from a dispatch payload; a tag such as +// "../../other/repo/..." would otherwise make the release and manifest fetches +// resolve to another repository. +func ValidateInputs(repo, tag string) error { + if !repoPattern.MatchString(repo) { + return fmt.Errorf("repository %q is not in owner/name form", repo) + } + // The pattern keeps URL metacharacters out; semver.IsValid also rejects + // suffixes CheckVersion could not order, such as a leading-zero number. + if !tagPattern.MatchString(tag) || !semver.IsValid(tag) { + return fmt.Errorf("tag %q is not a v.. release tag", tag) + } + return nil +} + +// ValidateCommit checks a commit SHA before it goes into a URL. +func ValidateCommit(sha string) error { + if !commitPattern.MatchString(sha) { + return fmt.Errorf("commit %q is not a full lowercase SHA-1", sha) + } + return nil +} + +// VersionFromTag returns the catalog version a validated tag names. +func VersionFromTag(tag string) string { + return strings.TrimPrefix(tag, "v") +} + +// FindPackage returns the catalog entry for pluginID, or nil. +func FindPackage(index RepositoryIndex, pluginID string) *CatalogPackage { + for i := range index.Plugins { + if packagePluginID(index.Plugins[i]) == pluginID { + return &index.Plugins[i] + } + } + return nil +} + +// CheckVersion refuses a release that does not advance the plugin's catalog +// version, so a dispatch cannot replace the current entry with an older +// release. alreadyListed reports a re-run for the listed version, which the +// caller treats as a successful no-op. allowRollback, set only for a +// deliberate manual run, accepts an older version and rewrites an equal one. +func CheckVersion(current *CatalogPackage, version string, allowRollback bool) (alreadyListed bool, err error) { + if current == nil || current.Manifest == nil { + return false, nil + } + listed := current.Manifest.GetVersion() + switch cmp := semver.Compare("v"+version, "v"+listed); { + case cmp > 0, allowRollback: + return false, nil + case cmp == 0: + return true, nil + default: + return false, fmt.Errorf("version %s is older than the listed version %s", version, listed) + } +} + +// CheckRelease refuses a release that is not the published, final release for +// tag. +func CheckRelease(tag string, release Release) error { + if release.TagName != tag { + return fmt.Errorf("release tag %q does not match requested tag %q", release.TagName, tag) + } + if release.Draft { + return fmt.Errorf("release %s is a draft", tag) + } + if release.Prerelease { + return fmt.Errorf("release %s is a prerelease", tag) + } + return nil +} + +// ReleaseAssets are the release assets the catalog records. +type ReleaseAssets struct { + ChecksumsURL string + BundleURL string + // Binaries maps "/" to the binary's download URL. + Binaries map[string]string +} + +// ParseReleaseAssets picks the catalogued assets out of a release of repo. +// Every catalogued URL must be the asset's own download URL in that +// repository and tag, so an entry cannot point at another repository or host. +func ParseReleaseAssets(repo string, release Release) (ReleaseAssets, error) { + assets := ReleaseAssets{Binaries: map[string]string{}} + prefix := "https://github.com/" + repo + "/releases/download/" + url.PathEscape(release.TagName) + "/" + for _, asset := range release.Assets { + platform, isBinary := "", false + switch { + case asset.Name == "checksums.txt", asset.Name == BundleAssetName: + case strings.HasPrefix(asset.Name, "plugin-"): + platform, isBinary = platformKeyFromAssetName(asset.Name) + if !isBinary { + continue + } + default: + continue + } + if want := prefix + url.PathEscape(asset.Name); asset.BrowserDownloadURL != want { + return ReleaseAssets{}, fmt.Errorf("asset %q has download URL %q, want %q", asset.Name, asset.BrowserDownloadURL, want) + } + switch { + case isBinary: + assets.Binaries[platform] = asset.BrowserDownloadURL + case asset.Name == BundleAssetName: + assets.BundleURL = asset.BrowserDownloadURL + default: + assets.ChecksumsURL = asset.BrowserDownloadURL + } + } + return assets, nil +} diff --git a/catalog/signature.go b/catalog/signature.go new file mode 100644 index 0000000..c176113 --- /dev/null +++ b/catalog/signature.go @@ -0,0 +1,123 @@ +package catalog + +import ( + "encoding/json" + "fmt" + "regexp" + "strings" +) + +const ( + // BundleAssetName is the release asset holding the Sigstore bundle. It + // carries one SLSA provenance attestation whose subjects are every + // plugin-- binary in the release. + BundleAssetName = "plugin.sigstore.json" + + // SharedSignerIdentity is the exact certificate SAN of Silo's reusable + // plugin release workflow. Callers reference it at @main, so a tag or SHA + // reference, or a fork commit, does not match. + SharedSignerIdentity = "https://github.com/Silo-Server/.github/.github/workflows/plugin-release.yml@refs/heads/main" + + // OIDCIssuer is GitHub Actions' token issuer. + OIDCIssuer = "https://token.actions.githubusercontent.com" + + slsaProvenancePredicate = "https://slsa.dev/provenance/v1" + + // Fulcio's public-good intermediate. GitHub's own Sigstore instance, used + // for private repositories, has a different issuer and no transparency log. + publicGoodCertificateIssuerOrg = "O=sigstore.dev" +) + +// CertIdentityRegex returns the certificate SAN pattern a community release +// must match. Two signers are accepted, and nothing else: Silo's shared +// release workflow at main, or a workflow file in the plugin's own repository +// run from the release tag. The repository and tag are regex-escaped and the +// pattern is anchored at both ends, so another repository's workflow, another +// ref, or a branch ref does not match. +func CertIdentityRegex(repo, tag string) string { + ownWorkflow := regexp.QuoteMeta("https://github.com/"+repo) + + `/\.github/workflows/[A-Za-z0-9._-]+\.ya?ml@refs/tags/` + regexp.QuoteMeta(tag) + return `^(?:` + regexp.QuoteMeta(SharedSignerIdentity) + `|` + ownWorkflow + `)$` +} + +// VerifyArgs returns the `gh` arguments that verify one release binary against +// the release's bundle. --repo binds the certificate's source repository to the +// approved repository whichever workflow signed; the identity regex limits the +// signer; and the source ref, issuer, GitHub-hosted runner, and SLSA +// provenance predicate are required for both signers. Flags use the +// --flag=value form so no value can be read as another flag. +func VerifyArgs(binaryPath, bundlePath, repo, tag string) []string { + return []string{ + "attestation", "verify", binaryPath, + "--bundle=" + bundlePath, + "--repo=" + repo, + "--cert-identity-regex=" + CertIdentityRegex(repo, tag), + "--cert-oidc-issuer=" + OIDCIssuer, + "--source-ref=refs/tags/" + tag, + "--predicate-type=" + slsaProvenancePredicate, + "--deny-self-hosted-runners", + "--format=json", + } +} + +// CheckVerifyOutput checks the JSON that `gh attestation verify --format=json` +// prints on success and returns the source commit the certificate names. gh +// also accepts GitHub's private Sigstore instance; the Silo server trusts only +// the public-good root with a transparency-log entry, so the catalog refuses +// anything else here rather than publish an entry every server would reject. +// Every verified attestation must name the same commit. +func CheckVerifyOutput(out []byte) (commit string, err error) { + var results []struct { + VerificationResult struct { + VerifiedTimestamps []struct { + Type string `json:"type"` + } `json:"verifiedTimestamps"` + Signature struct { + Certificate struct { + CertificateIssuer string `json:"certificateIssuer"` + SourceRepositoryDigest string `json:"sourceRepositoryDigest"` + } `json:"certificate"` + } `json:"signature"` + } `json:"verificationResult"` + } + if err := json.Unmarshal(out, &results); err != nil { + return "", fmt.Errorf("decode gh attestation verify output: %w", err) + } + if len(results) == 0 { + return "", fmt.Errorf("gh attestation verify reported no verified attestation") + } + for _, result := range results { + cert := result.VerificationResult.Signature.Certificate + if !strings.Contains(cert.CertificateIssuer, publicGoodCertificateIssuerOrg) { + return "", fmt.Errorf("attestation certificate is from %q, not the Sigstore public-good instance", cert.CertificateIssuer) + } + logged := false + for _, ts := range result.VerificationResult.VerifiedTimestamps { + if ts.Type == "Tlog" { + logged = true + break + } + } + if !logged { + return "", fmt.Errorf("attestation has no verified transparency-log entry") + } + if err := ValidateCommit(cert.SourceRepositoryDigest); err != nil { + return "", fmt.Errorf("attestation source commit: %w", err) + } + if commit != "" && cert.SourceRepositoryDigest != commit { + return "", fmt.Errorf("attestations name commits %s and %s", commit, cert.SourceRepositoryDigest) + } + commit = cert.SourceRepositoryDigest + } + return commit, nil +} + +// CheckSignatureRatchet refuses an unsigned release of a plugin whose current +// catalog entry is signed. Unsigned releases stay accepted until a plugin's +// first signed release lands, which lets plugins move to signing one at a time. +func CheckSignatureRatchet(current *CatalogPackage, bundleURL string) error { + if bundleURL == "" && current != nil && current.BundleURL != "" { + return fmt.Errorf("plugin %q already has a signed catalog entry; this release has no %s", packagePluginID(*current), BundleAssetName) + } + return nil +} diff --git a/cmd/update-catalog/main.go b/cmd/update-catalog/main.go index 2d002fc..a9c0424 100644 --- a/cmd/update-catalog/main.go +++ b/cmd/update-catalog/main.go @@ -9,71 +9,213 @@ import ( "io" "net/http" "os" + "os/exec" + "path/filepath" + "slices" "strings" "time" "github.com/Silo-Community/silo-plugins/catalog" ) -const githubAPIVersion = "2022-11-28" +const ( + githubAPIVersion = "2022-11-28" + + maxBundleBytes = 256 << 10 + maxBinaryBytes = 512 << 20 +) func main() { - var repo string - var tag string - var manifestPath string - var approvalsPath string + var repo, tag, manifestPath, approvalsPath string + var allowRollback bool flag.StringVar(&repo, "repo", "", "GitHub repository in owner/name form") - flag.StringVar(&tag, "tag", "", "Git tag to publish from") + flag.StringVar(&tag, "tag", "", "Release tag to publish, v..") flag.StringVar(&manifestPath, "manifest", "manifest.json", "Path to the catalog manifest") flag.StringVar(&approvalsPath, "approvals", "approved-plugins.json", "Path to the approved plugin registry") + flag.BoolVar(&allowRollback, "allow-rollback", false, "Accept a version that is not newer than the listed one (deliberate rollback only)") flag.Parse() - if strings.TrimSpace(repo) == "" { - exitf("repo is required") + if err := run(context.Background(), repo, tag, manifestPath, approvalsPath, allowRollback); err != nil { + exitf("%v", err) + } +} + +// run applies every check that needs no network first, then fetches the +// release, ties it to one commit (the signed one, or the tag's commit for an +// unsigned release), reads manifest.json at that commit, and only then writes +// the catalog. +func run(ctx context.Context, repo, tag, manifestPath, approvalsPath string, allowRollback bool) error { + if err := catalog.ValidateInputs(repo, tag); err != nil { + return fmt.Errorf("refuse release: %w", err) } - if strings.TrimSpace(tag) == "" { - exitf("tag is required") + registry, err := loadApprovals(approvalsPath) + if err != nil { + return fmt.Errorf("load approvals: %w", err) + } + pluginID, err := registry.ApprovedPluginID(repo) + if err != nil { + return fmt.Errorf("refuse release: %w", err) + } + + index, err := loadIndex(manifestPath) + if err != nil { + return fmt.Errorf("load catalog: %w", err) + } + current := catalog.FindPackage(index, pluginID) + version := catalog.VersionFromTag(tag) + alreadyListed, err := catalog.CheckVersion(current, version, allowRollback) + if err != nil { + return fmt.Errorf("refuse release: %w", err) + } + if alreadyListed { + fmt.Fprintf(os.Stderr, "The catalog already lists %s v%s; nothing to do.\n", pluginID, version) + return nil } token := os.Getenv("GITHUB_TOKEN") client := &http.Client{Timeout: 30 * time.Second} - ctx := context.Background() release, err := fetchRelease(ctx, client, token, repo, tag) if err != nil { - exitf("fetch release: %v", err) + return fmt.Errorf("fetch release: %w", err) } - - sourceManifest, err := fetchSourceManifest(ctx, client, token, repo, tag) + if err := catalog.CheckRelease(tag, release); err != nil { + return fmt.Errorf("refuse release: %w", err) + } + assets, err := catalog.ParseReleaseAssets(repo, release) if err != nil { - exitf("fetch source manifest: %v", err) + return fmt.Errorf("refuse release: %w", err) + } + if err := catalog.CheckSignatureRatchet(current, assets.BundleURL); err != nil { + return fmt.Errorf("refuse release: %w", err) } - registry, err := loadApprovals(approvalsPath) + var commit string + if assets.BundleURL != "" { + // Binaries are tens of megabytes, so downloads get more time than the + // API calls. + downloadClient := &http.Client{Timeout: 5 * time.Minute} + commit, err = verifyRelease(ctx, downloadClient, repo, tag, assets) + if err != nil { + return fmt.Errorf("refuse release: verify signature: %w", err) + } + } else { + commit, err = resolveTagCommit(ctx, client, token, repo, tag) + if err != nil { + return fmt.Errorf("resolve tag %s: %w", tag, err) + } + } + + sourceManifest, err := fetchSourceManifest(ctx, client, token, repo, commit) if err != nil { - exitf("load approvals: %v", err) + return fmt.Errorf("fetch source manifest: %w", err) } - approval, err := registry.Resolve(repo, sourceManifest.GetPluginId()) + if got := sourceManifest.GetPluginId(); got != pluginID { + return fmt.Errorf("refuse release: manifest.json names plugin %q, but %s is approved for %q", got, repo, pluginID) + } + approval, err := registry.Resolve(repo, pluginID) if err != nil { - exitf("approve plugin: %v", err) + return fmt.Errorf("refuse release: %w", err) } - pkg, err := catalog.BuildPackageFromRelease(repo, sourceManifest, release) if err != nil { - exitf("build catalog package: %v", err) + return fmt.Errorf("build catalog package: %w", err) } pkg.Approval = &approval - index, err := loadIndex(manifestPath) - if err != nil { - exitf("load catalog: %v", err) - } index = catalog.UpsertPackage(index, pkg) index = catalog.PruneUnapproved(index, registry) if err := writeIndex(manifestPath, index); err != nil { - exitf("write catalog: %v", err) + return fmt.Errorf("write catalog: %w", err) + } + return nil +} + +// verifyRelease downloads the bundle and every catalogued binary, checks each +// binary against the bundle with gh, and returns the one commit all of them +// were built from. Any failure refuses the release. +func verifyRelease(ctx context.Context, client *http.Client, repo, tag string, assets catalog.ReleaseAssets) (string, error) { + if len(assets.Binaries) == 0 { + return "", fmt.Errorf("release has no plugin binaries") + } + dir, err := os.MkdirTemp("", "silo-plugin-verify-") + if err != nil { + return "", err + } + defer os.RemoveAll(dir) + + bundlePath := filepath.Join(dir, catalog.BundleAssetName) + if err := download(ctx, client, assets.BundleURL, bundlePath, maxBundleBytes); err != nil { + return "", fmt.Errorf("download %s: %w", catalog.BundleAssetName, err) + } + + platforms := make([]string, 0, len(assets.Binaries)) + for platform := range assets.Binaries { + platforms = append(platforms, platform) + } + slices.Sort(platforms) + var commit string + for i, platform := range platforms { + binaryPath := filepath.Join(dir, fmt.Sprintf("binary-%d", i)) + if err := download(ctx, client, assets.Binaries[platform], binaryPath, maxBinaryBytes); err != nil { + return "", fmt.Errorf("download %s binary: %w", platform, err) + } + cmd := exec.CommandContext(ctx, "gh", catalog.VerifyArgs(binaryPath, bundlePath, repo, tag)...) + cmd.Stderr = os.Stderr + out, err := cmd.Output() + if err != nil { + return "", fmt.Errorf("%s binary: gh attestation verify: %w", platform, err) + } + binaryCommit, err := catalog.CheckVerifyOutput(out) + if err != nil { + return "", fmt.Errorf("%s binary: %w", platform, err) + } + if commit != "" && binaryCommit != commit { + return "", fmt.Errorf("%s binary was built from %s, other binaries from %s", platform, binaryCommit, commit) + } + commit = binaryCommit + fmt.Fprintf(os.Stderr, "Verified the %s binary of %s %s, built from %s.\n", platform, repo, tag, commit) + } + return commit, nil +} + +// resolveTagCommit returns the commit a tag points at, following annotated +// tags. An unsigned release has no certificate naming its commit, so this +// pins manifest.json to one commit rather than a ref a branch could shadow. +func resolveTagCommit(ctx context.Context, client *http.Client, token, repo, tag string) (string, error) { + var ref struct { + Object gitObject `json:"object"` + } + if err := githubJSON(ctx, client, token, "https://api.github.com/repos/"+repo+"/git/ref/tags/"+tag, &ref); err != nil { + return "", err + } + object := ref.Object + for range 5 { + if err := catalog.ValidateCommit(object.SHA); err != nil { + return "", err + } + switch object.Type { + case "commit": + return object.SHA, nil + case "tag": + var annotated struct { + Object gitObject `json:"object"` + } + if err := githubJSON(ctx, client, token, "https://api.github.com/repos/"+repo+"/git/tags/"+object.SHA, &annotated); err != nil { + return "", err + } + object = annotated.Object + default: + return "", fmt.Errorf("tag points at a %s, not a commit", object.Type) + } } + return "", fmt.Errorf("tag is nested more than 5 annotated tags deep") +} + +type gitObject struct { + SHA string `json:"sha"` + Type string `json:"type"` } func loadApprovals(path string) (catalog.ApprovalRegistry, error) { @@ -88,6 +230,37 @@ func loadApprovals(path string) (catalog.ApprovalRegistry, error) { return registry, nil } +func download(ctx context.Context, client *http.Client, url, path string, limit int64) error { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return fmt.Errorf("build request: %w", err) + } + req.Header.Set("User-Agent", "silo-plugins-catalog-updater") + resp, err := client.Do(req) + if err != nil { + return fmt.Errorf("GET %s: %w", url, err) + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("GET %s: status %d", url, resp.StatusCode) + } + file, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600) + if err != nil { + return err + } + written, err := io.Copy(file, io.LimitReader(resp.Body, limit+1)) + if closeErr := file.Close(); err == nil { + err = closeErr + } + if err != nil { + return fmt.Errorf("read %s: %w", url, err) + } + if written > limit { + return fmt.Errorf("GET %s: larger than %d bytes", url, limit) + } + return nil +} + func fetchRelease(ctx context.Context, client *http.Client, token, repo, tag string) (catalog.Release, error) { var release catalog.Release if err := githubJSON(ctx, client, token, "https://api.github.com/repos/"+repo+"/releases/tags/"+tag, &release); err != nil { @@ -96,8 +269,9 @@ func fetchRelease(ctx context.Context, client *http.Client, token, repo, tag str return release, nil } -func fetchSourceManifest(ctx context.Context, client *http.Client, token, repo, tag string) (*catalog.SourceManifest, error) { - url := "https://raw.githubusercontent.com/" + repo + "/" + tag + "/manifest.json" +// fetchSourceManifest reads manifest.json at a validated commit SHA. +func fetchSourceManifest(ctx context.Context, client *http.Client, token, repo, commit string) (*catalog.SourceManifest, error) { + url := "https://raw.githubusercontent.com/" + repo + "/" + commit + "/manifest.json" req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return nil, fmt.Errorf("build request: %w", err) @@ -120,11 +294,7 @@ func fetchSourceManifest(ctx context.Context, client *http.Client, token, repo, if err != nil { return nil, fmt.Errorf("read source manifest: %w", err) } - manifest, err := catalog.DecodeSourceManifest(body) - if err != nil { - return nil, err - } - return manifest, nil + return catalog.DecodeSourceManifest(body) } func githubJSON(ctx context.Context, client *http.Client, token, url string, dest any) error { diff --git a/go.mod b/go.mod index e3caa30..5f7ec41 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.26.0 require ( github.com/Silo-Server/silo-plugin-sdk v0.28.0 + golang.org/x/mod v0.43.0 google.golang.org/protobuf v1.36.11 ) diff --git a/go.sum b/go.sum index a352238..0d659cc 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,8 @@ go.opentelemetry.io/otel/sdk/metric v1.44.0 h1:3LlKgI+VjbVsjNRFZJZAJ30WjXC5VkNRk go.opentelemetry.io/otel/sdk/metric v1.44.0/go.mod h1:5B5pMARnXxKhltooO4xUuCBorl65a4EpnTalObqOigA= go.opentelemetry.io/otel/trace v1.44.0 h1:jxF5CsGYCe74MCRx2X4g7WsY/VBKRqqpNvXlX/6gtIk= go.opentelemetry.io/otel/trace v1.44.0/go.mod h1:oLl1jrMQAVo6v3GAggN+1VH9VIz9iUSvW53sW1Q8PIE= +golang.org/x/mod v0.43.0 h1:MFuq5J6s35W9D3IjkwyWc9vHh0nENOrt5K8Iof4ofmg= +golang.org/x/mod v0.43.0/go.mod h1:4ec1JnuyBz5+FIVhg86UsA39YG8BpnrZdJYSNHmGzHY= golang.org/x/net v0.58.0 h1:ynWG7rqYi4ccpTEuPZ2QGWHktVEM9DMCj9yzDE0Q7To= golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU= golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs= From 2287d23d3e6ac847a7fae68070a397cd7d0c7a76 Mon Sep 17 00:00:00 2001 From: Quick104 <31828688+Quick104@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:08:17 -0500 Subject: [PATCH 2/2] fix(catalog): accept only the shared signer unless approval names one Review follow-up. A signed release now must be signed by Silo's shared release workflow, checked with an exact --cert-identity. A plugin may be signed by a workflow in its own repository only when its approved-plugins.json entry names that file in the new optional signer_workflow field; the catalog then accepts exactly that workflow run from the release tag, and only for a commit GitHub reports as on the repository's default branch. check-catalog validates the field. No entry sets it, so every plugin requires the shared workflow. Signed entries now record each binary's SHA-256 in binaries..checksum, computed from the file gh verified, so the listed bytes stay pinned if release assets change. Unsigned entries are unchanged. Binary asset names must have lowercase alphanumeric os and arch parts, so plugin-linux-amd64.zip is no longer read as a binary. The private Sigstore instance test now fails on the issuer check, and new tests cover the manifest plugin ID, a signed release without binaries, the download size cap, annotated tag resolution, and the signer rules. Co-Authored-By: Claude Opus 5.5 --- APPROVAL_POLICY.md | 18 ++++ README.md | 48 +++++++-- catalog/approval.go | 35 +++++-- catalog/catalog.go | 13 ++- catalog/catalog_test.go | 176 +++++++++++++++++++++++--------- catalog/signature.go | 45 +++++--- cmd/update-catalog/main.go | 124 ++++++++++++++++------ cmd/update-catalog/main_test.go | 159 +++++++++++++++++++++++++++++ 8 files changed, 500 insertions(+), 118 deletions(-) create mode 100644 cmd/update-catalog/main_test.go diff --git a/APPROVAL_POLICY.md b/APPROVAL_POLICY.md index abacf34..5320609 100644 --- a/APPROVAL_POLICY.md +++ b/APPROVAL_POLICY.md @@ -17,6 +17,24 @@ An approved community plugin must satisfy all of the following at review time: Approval records the evidence URL and date in `approved-plugins.json`. A release dispatch cannot approve a repository by itself. +## Release signing + +Signed releases of an approved plugin must be signed by Silo's shared release +workflow (`Silo-Server/.github`, `plugin-release.yml` at `main`) unless its +registry entry sets `signer_workflow`. That optional field names one workflow +file in the plugin's own repository, as a plain file name ending in `.yml` or +`.yaml` (for example `"signer_workflow": "release.yml"`). When it is set, the +catalog accepts only that workflow, run from the release tag, for a commit on +the repository's default branch, and no longer accepts the shared workflow for +that plugin. + +Setting `signer_workflow` is an approval decision. The shared workflow fixes the +build steps; a plugin's own workflow lets its maintainers change them. Before +setting it, a reviewer checks that the named workflow builds and attests the +release binaries, runs only on GitHub-hosted runners, and is triggered by +release tags, and that the repository protects its default branch. Approval +records this review the same way as the original approval. + Approval may be withdrawn if the plugin becomes unsafe, misleading, unmaintained, or incompatible. Removing it from the registry and regenerating the catalog stops new installs and update discovery. Silo does not remotely diff --git a/README.md b/README.md index 8148c12..75fc27a 100644 --- a/README.md +++ b/README.md @@ -55,25 +55,51 @@ next time the catalog is written. ### Signed releases A signed release carries `plugin.sigstore.json`: a Sigstore bundle with one SLSA -provenance attestation covering every `plugin--` binary. The catalog -entry then gains a package-level `bundle_url` beside `checksums_url`. Before -writing the entry, `update-catalog` checks each binary with -`gh attestation verify`. The certificate must name one of two signers: - -- Silo's shared release workflow, +provenance attestation covering every `plugin--` binary. Before +writing the entry, `update-catalog` downloads every binary and checks it with +`gh attestation verify`. The entry then gains a package-level `bundle_url` +beside `checksums_url`, and each binary gains a `checksum`: the SHA-256 of the +file the tool verified. Silo servers prefer that checksum over `checksums_url`, +so the listed bytes stay pinned even if the release assets are replaced later. +Unsigned entries carry no per-binary checksum. + +The certificate must name exactly one signer, chosen by the plugin's entry in +`approved-plugins.json`: + +- By default, Silo's shared release workflow, `https://github.com/Silo-Server/.github/.github/workflows/plugin-release.yml@refs/heads/main`. -- A workflow in the plugin's own repository run from the release tag, - `https://github.com//.github/workflows/@refs/tags/`, where - `` is a `.yml` or `.yaml` file directly in that directory. - -Either way, the tool also requires the issuer + No other workflow is accepted. +- If the entry sets `signer_workflow` (for example `"signer_workflow": + "release.yml"`), that workflow file in the plugin's own repository, run from + the release tag: + `https://github.com//.github/workflows/@refs/tags/`. + The shared workflow is then not accepted for that plugin. The signed commit + must also be on the repository's default branch: GitHub's compare API must + report it as identical to or behind that branch, not ahead or diverged. + +For both signers, the tool also requires the issuer `https://token.actions.githubusercontent.com`, the approved repository as the source repository, the source ref `refs/tags/`, a GitHub-hosted runner, SLSA provenance v1, a Sigstore public-good transparency-log entry, and the same source commit for every binary. Any failure refuses the release. +What a signature proves: + +- Shared workflow: the binaries were built on a GitHub-hosted runner by Silo's + release workflow at `main`, from the approved repository at the commit the + release tag named. The build steps are Silo's, not the plugin's. It does not + prove the plugin's source at that commit was reviewed. +- Own workflow: the binaries were built on a GitHub-hosted runner by the named + workflow file, as it existed at the tagged commit, from a commit already on + the default branch. Anyone who can push to the default branch and create + tags can change that workflow and what it builds, so this proves origin from + the approved repository's default branch, not that the build steps or source + were reviewed. + Unsigned releases are still accepted for a plugin until its catalog entry has a `bundle_url`; after that, an unsigned release of that plugin is refused. +`signer_workflow` is an approval decision; see +[`APPROVAL_POLICY.md`](APPROVAL_POLICY.md). `check-catalog` validates its format. ### Workflow diff --git a/catalog/approval.go b/catalog/approval.go index 13e9d67..9db92a9 100644 --- a/catalog/approval.go +++ b/catalog/approval.go @@ -4,10 +4,15 @@ import ( "encoding/json" "fmt" "net/url" + "regexp" "strings" "time" ) +// signerWorkflowPattern is a plain workflow file name in .github/workflows: +// no path separators, no leading dot, and a .yml or .yaml extension. +var signerWorkflowPattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]*\.ya?ml$`) + type Approval struct { ApprovedAt string `json:"approved_at"` ReviewURL string `json:"review_url"` @@ -18,6 +23,10 @@ type ApprovedPlugin struct { Repository string `json:"repository"` ApprovedAt string `json:"approved_at"` ReviewURL string `json:"review_url"` + // SignerWorkflow, when set, names the one workflow file in the plugin's + // own repository whose signature the catalog accepts instead of Silo's + // shared release workflow. Setting it is an approval decision. + SignerWorkflow string `json:"signer_workflow,omitempty"` } type ApprovalRegistry struct { @@ -56,6 +65,9 @@ func DecodeApprovalRegistry(data []byte) (ApprovalRegistry, error) { if err := validateGitHubURL(plugin.ReviewURL); err != nil { return ApprovalRegistry{}, fmt.Errorf("review_url for %s: %w", pluginID, err) } + if plugin.SignerWorkflow != "" && !signerWorkflowPattern.MatchString(plugin.SignerWorkflow) { + return ApprovalRegistry{}, fmt.Errorf("signer_workflow for %s must be a .yml or .yaml file name with no path, got %q", pluginID, plugin.SignerWorkflow) + } seenPluginIDs[pluginID] = struct{}{} seenRepositories[repositoryKey] = struct{}{} } @@ -71,17 +83,26 @@ func (r ApprovalRegistry) Resolve(repository, pluginID string) (Approval, error) return Approval{}, fmt.Errorf("plugin %s from %s is not approved", pluginID, repository) } -// ApprovedPluginID returns the plugin ID the registry binds to repo, or an -// error when repo is not approved. update-catalog calls it before any fetch. -// The comparison is exact, so every request and asset URL built from repo uses -// the spelling the registry records. -func (r ApprovalRegistry) ApprovedPluginID(repo string) (string, error) { +// ApprovedRepository returns the registry entry for repo, or an error when +// repo is not approved. update-catalog calls it before any fetch. The +// comparison is exact, so every request and asset URL built from repo uses the +// spelling the registry records. +func (r ApprovalRegistry) ApprovedRepository(repo string) (ApprovedPlugin, error) { for _, approved := range r.Plugins { if approved.Repository == repo { - return approved.PluginID, nil + return approved, nil } } - return "", fmt.Errorf("repository %s is not in the approval registry", repo) + return ApprovedPlugin{}, fmt.Errorf("repository %s is not in the approval registry", repo) +} + +// CheckManifestPluginID refuses a release whose manifest.json names a plugin +// other than the one approved for its repository. +func (a ApprovedPlugin) CheckManifestPluginID(got string) error { + if got != a.PluginID { + return fmt.Errorf("manifest.json names plugin %q, but %s is approved for %q", got, a.Repository, a.PluginID) + } + return nil } func ValidateApprovedIndex(index RepositoryIndex, registry ApprovalRegistry) error { diff --git a/catalog/catalog.go b/catalog/catalog.go index 7ce4abc..6ae7b72 100644 --- a/catalog/catalog.go +++ b/catalog/catalog.go @@ -2,6 +2,7 @@ package catalog import ( "fmt" + "regexp" "sort" "strings" @@ -27,6 +28,10 @@ type Release struct { type PlatformBinary struct { URL string `json:"url"` + // Checksum is the lowercase hex SHA-256 of the binary the catalog updater + // verified, recorded for signed releases only. Silo servers prefer it over + // checksums_url, which pins the listed bytes even if release assets change. + Checksum string `json:"checksum,omitempty"` } type CatalogPackage struct { @@ -159,12 +164,16 @@ func packagePluginID(pkg CatalogPackage) string { return pkg.Manifest.GetPluginId() } +// platformPartPattern is one of the os or arch parts of a binary asset name, +// so plugin-linux-amd64 is a binary and plugin-linux-amd64.zip is not. +var platformPartPattern = regexp.MustCompile(`^[a-z0-9]+$`) + func platformKeyFromAssetName(name string) (string, bool) { parts := strings.Split(name, "-") - if len(parts) != 3 { + if len(parts) != 3 || parts[0] != "plugin" { return "", false } - if parts[0] != "plugin" || parts[1] == "" || parts[2] == "" { + if !platformPartPattern.MatchString(parts[1]) || !platformPartPattern.MatchString(parts[2]) { return "", false } return parts[1] + "/" + parts[2], true diff --git a/catalog/catalog_test.go b/catalog/catalog_test.go index d7ec9cf..54fd66e 100644 --- a/catalog/catalog_test.go +++ b/catalog/catalog_test.go @@ -2,7 +2,6 @@ package catalog import ( "encoding/json" - "regexp" "slices" "testing" @@ -344,18 +343,23 @@ func TestBuildPackageFromRelease_BundleURL(t *testing.T) { } } -func TestApprovedPluginID(t *testing.T) { +func TestApprovedRepository(t *testing.T) { registry, err := DecodeApprovalRegistry([]byte(`{ "plugins": [ {"plugin_id": "silo.requests.arr", "repository": "Silo-Community/silo-plugins-requests-arr", "approved_at": "2026-07-09", "review_url": "https://github.com/Silo-Community/silo-plugins/issues/2"}, - {"plugin_id": "silo.requests.seerr", "repository": "Silo-Community/silo-plugins-requests-seerr", "approved_at": "2026-07-09", "review_url": "https://github.com/Silo-Community/silo-plugins/issues/1"} + {"plugin_id": "silo.requests.seerr", "repository": "Silo-Community/silo-plugins-requests-seerr", "approved_at": "2026-07-09", "review_url": "https://github.com/Silo-Community/silo-plugins/issues/1", "signer_workflow": "release.yml"} ] }`)) if err != nil { t.Fatalf("DecodeApprovalRegistry() error = %v", err) } - if got, err := registry.ApprovedPluginID("Silo-Community/silo-plugins-requests-seerr"); err != nil || got != "silo.requests.seerr" { - t.Fatalf("ApprovedPluginID(seerr) = %q, %v; want silo.requests.seerr", got, err) + arr, err := registry.ApprovedRepository("Silo-Community/silo-plugins-requests-arr") + if err != nil || arr.PluginID != "silo.requests.arr" || arr.SignerWorkflow != "" { + t.Fatalf("ApprovedRepository(arr) = %+v, %v; want silo.requests.arr with the shared signer", arr, err) + } + seerr, err := registry.ApprovedRepository("Silo-Community/silo-plugins-requests-seerr") + if err != nil || seerr.PluginID != "silo.requests.seerr" || seerr.SignerWorkflow != "release.yml" { + t.Fatalf("ApprovedRepository(seerr) = %+v, %v; want silo.requests.seerr signed by release.yml", seerr, err) } for _, repo := range []string{ "Silo-Community/silo-plugins-requests-other", @@ -363,8 +367,50 @@ func TestApprovedPluginID(t *testing.T) { "Silo-Server/silo-plugins-requests-arr", "", } { - if got, err := registry.ApprovedPluginID(repo); err == nil { - t.Fatalf("ApprovedPluginID(%q) = %q, want an error", repo, got) + if got, err := registry.ApprovedRepository(repo); err == nil { + t.Fatalf("ApprovedRepository(%q) = %+v, want an error", repo, got) + } + } +} + +func TestCheckManifestPluginID(t *testing.T) { + approved := ApprovedPlugin{PluginID: "silo.requests.arr", Repository: testRepo} + if err := approved.CheckManifestPluginID("silo.requests.arr"); err != nil { + t.Fatalf("CheckManifestPluginID(approved ID) error = %v", err) + } + for _, got := range []string{"silo.requests.seerr", "", "Silo.requests.arr"} { + if err := approved.CheckManifestPluginID(got); err == nil { + t.Fatalf("CheckManifestPluginID(%q) accepted another plugin ID", got) + } + } +} + +func TestDecodeApprovalRegistrySignerWorkflow(t *testing.T) { + entry := func(signerWorkflow string) []byte { + return []byte(`{"plugins": [{"plugin_id": "silo.requests.arr", "repository": "Silo-Community/silo-plugins-requests-arr", "approved_at": "2026-07-09", "review_url": "https://github.com/Silo-Community/silo-plugins/issues/2", "signer_workflow": "` + signerWorkflow + `"}]}`) + } + tests := []struct { + signerWorkflow string + wantErr bool + }{ + {"", false}, + {"release.yml", false}, + {"plugin-release.yaml", false}, + {"release_v2.yml", false}, + {"release.json", true}, + {"release", true}, + {"workflows/release.yml", true}, + {"../release.yml", true}, + {".github/workflows/release.yml", true}, + {"release.yml@refs/heads/main", true}, + {".yml", true}, + {"release.yml\\n", true}, + {"release .yml", true}, + } + for _, tt := range tests { + _, err := DecodeApprovalRegistry(entry(tt.signerWorkflow)) + if (err != nil) != tt.wantErr { + t.Fatalf("DecodeApprovalRegistry(signer_workflow %q) error = %v, wantErr %v", tt.signerWorkflow, err, tt.wantErr) } } } @@ -397,60 +443,93 @@ func TestCheckSignatureRatchet(t *testing.T) { } func TestVerifyArgs(t *testing.T) { - got := VerifyArgs("/tmp/v/binary-0", "/tmp/v/plugin.sigstore.json", testRepo, "v0.1.11") - want := []string{ - "attestation", "verify", "/tmp/v/binary-0", - "--bundle=/tmp/v/plugin.sigstore.json", - "--repo=Silo-Community/silo-plugins-requests-arr", - `--cert-identity-regex=^(?:https://github\.com/Silo-Server/\.github/\.github/workflows/plugin-release\.yml@refs/heads/main|https://github\.com/Silo-Community/silo-plugins-requests-arr/\.github/workflows/[A-Za-z0-9._-]+\.ya?ml@refs/tags/v0\.1\.11)$`, - "--cert-oidc-issuer=https://token.actions.githubusercontent.com", - "--source-ref=refs/tags/v0.1.11", - "--predicate-type=https://slsa.dev/provenance/v1", - "--deny-self-hosted-runners", - "--format=json", - } - if !slices.Equal(got, want) { - t.Fatalf("VerifyArgs() =\n%q\nwant\n%q", got, want) + common := func(identity string) []string { + return []string{ + "attestation", "verify", "/tmp/v/binary-0", + "--bundle=/tmp/v/plugin.sigstore.json", + "--repo=Silo-Community/silo-plugins-requests-arr", + "--cert-identity=" + identity, + "--cert-oidc-issuer=https://token.actions.githubusercontent.com", + "--source-ref=refs/tags/v0.1.11", + "--predicate-type=https://slsa.dev/provenance/v1", + "--deny-self-hosted-runners", + "--format=json", + } + } + shared := VerifyArgs("/tmp/v/binary-0", "/tmp/v/plugin.sigstore.json", testRepo, "v0.1.11", "") + if want := common("https://github.com/Silo-Server/.github/.github/workflows/plugin-release.yml@refs/heads/main"); !slices.Equal(shared, want) { + t.Fatalf("VerifyArgs(shared) =\n%q\nwant\n%q", shared, want) + } + own := VerifyArgs("/tmp/v/binary-0", "/tmp/v/plugin.sigstore.json", testRepo, "v0.1.11", "release.yml") + if want := common("https://github.com/Silo-Community/silo-plugins-requests-arr/.github/workflows/release.yml@refs/tags/v0.1.11"); !slices.Equal(own, want) { + t.Fatalf("VerifyArgs(own workflow) =\n%q\nwant\n%q", own, want) } } -func TestCertIdentityRegex(t *testing.T) { - pattern := regexp.MustCompile(CertIdentityRegex(testRepo, "v0.1.11")) +// TestSignerIdentity checks which certificate SANs each signer policy +// accepts. gh compares --cert-identity to the SAN exactly, so a SAN is +// accepted only when it equals SignerIdentity. +func TestSignerIdentity(t *testing.T) { const own = "https://github.com/" + testRepo + "/.github/workflows/" tests := []struct { - name string - san string - want bool + name string + signerWorkflow string + san string + want bool }{ - {"shared Silo workflow at main", SharedSignerIdentity, true}, - {"own release workflow at the tag", own + "release.yml@refs/tags/v0.1.11", true}, - {"own workflow with yaml extension", own + "publish.yaml@refs/tags/v0.1.11", true}, - {"shared workflow at a tag", "https://github.com/Silo-Server/.github/.github/workflows/plugin-release.yml@refs/tags/v1.0.0", false}, - {"shared workflow at another branch", "https://github.com/Silo-Server/.github/.github/workflows/plugin-release.yml@refs/heads/dev", false}, - {"shared workflow in a fork", "https://github.com/someone/.github/.github/workflows/plugin-release.yml@refs/heads/main", false}, - {"another repository's workflow", "https://github.com/Silo-Community/silo-plugins-requests-seerr/.github/workflows/release.yml@refs/tags/v0.1.11", false}, - {"repository name as a prefix", "https://github.com/Silo-Community/silo-plugins-requests-arrx/.github/workflows/release.yml@refs/tags/v0.1.11", false}, - {"another tag", own + "release.yml@refs/tags/v0.1.10", false}, - {"tag as a prefix", own + "release.yml@refs/tags/v0.1.110", false}, - {"suffixed tag", own + "release.yml@refs/tags/v0.1.11-rc.1", false}, - {"branch ref", own + "release.yml@refs/heads/main", false}, - {"branch named like the tag", own + "release.yml@refs/heads/v0.1.11", false}, - {"workflow in a subdirectory", own + "nested/release.yml@refs/tags/v0.1.11", false}, - {"workflow outside .github/workflows", "https://github.com/" + testRepo + "/release.yml@refs/tags/v0.1.11", false}, - {"unescaped dot in the tag", own + "release.yml@refs/tags/v0x1x11", false}, - {"unescaped dot in the host", "https://githubxcom/" + testRepo + "/.github/workflows/release.yml@refs/tags/v0.1.11", false}, - {"trailing text", own + "release.yml@refs/tags/v0.1.11/extra", false}, - {"leading text", "x" + SharedSignerIdentity, false}, + {"default: shared workflow at main", "", SharedSignerIdentity, true}, + {"default: own workflow at the tag", "", own + "release.yml@refs/tags/v0.1.11", false}, + {"default: shared workflow at a tag", "", "https://github.com/Silo-Server/.github/.github/workflows/plugin-release.yml@refs/tags/v1.0.0", false}, + {"default: shared workflow in a fork", "", "https://github.com/someone/.github/.github/workflows/plugin-release.yml@refs/heads/main", false}, + {"declared: that workflow at the tag", "release.yml", own + "release.yml@refs/tags/v0.1.11", true}, + {"declared: shared workflow", "release.yml", SharedSignerIdentity, false}, + {"declared: another workflow file", "release.yml", own + "ci.yml@refs/tags/v0.1.11", false}, + {"declared: branch ref", "release.yml", own + "release.yml@refs/heads/main", false}, + {"declared: branch named like the tag", "release.yml", own + "release.yml@refs/heads/v0.1.11", false}, + {"declared: another tag", "release.yml", own + "release.yml@refs/tags/v0.1.10", false}, + {"declared: another repository", "release.yml", "https://github.com/Silo-Community/silo-plugins-requests-seerr/.github/workflows/release.yml@refs/tags/v0.1.11", false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := pattern.MatchString(tt.san); got != tt.want { - t.Fatalf("CertIdentityRegex matches %q = %v, want %v", tt.san, got, tt.want) + if got := SignerIdentity(testRepo, "v0.1.11", tt.signerWorkflow) == tt.san; got != tt.want { + t.Fatalf("SignerIdentity(%q) accepts %q = %v, want %v", tt.signerWorkflow, tt.san, got, tt.want) } }) } } +func TestCheckOnDefaultBranch(t *testing.T) { + for status, wantErr := range map[string]bool{"identical": false, "behind": false, "ahead": true, "diverged": true, "": true} { + if err := CheckOnDefaultBranch(status, "main", "29fd28771a006d9d68a311865a2b858c5afe301a"); (err != nil) != wantErr { + t.Fatalf("CheckOnDefaultBranch(%q) error = %v, wantErr %v", status, err, wantErr) + } + } +} + +func TestPlatformKeyFromAssetName(t *testing.T) { + tests := []struct { + name string + want string + wantOK bool + }{ + {"plugin-linux-amd64", "linux/amd64", true}, + {"plugin-darwin-arm64", "darwin/arm64", true}, + {"plugin-linux-amd64.zip", "", false}, + {"plugin-linux.x-amd64", "", false}, + {"plugin-Linux-amd64", "", false}, + {"plugin-linux-", "", false}, + {"plugin--amd64", "", false}, + {"plugin-linux-amd64-v2", "", false}, + {"plugin.sigstore.json", "", false}, + } + for _, tt := range tests { + got, ok := platformKeyFromAssetName(tt.name) + if got != tt.want || ok != tt.wantOK { + t.Fatalf("platformKeyFromAssetName(%q) = %q, %v; want %q, %v", tt.name, got, ok, tt.want, tt.wantOK) + } + } +} + func TestCheckVerifyOutput(t *testing.T) { const commitA = "29fd28771a006d9d68a311865a2b858c5afe301a" const commitB = "63837fb778d6a39f02be0ce76ff89f2bc8d74bdc" @@ -470,7 +549,8 @@ func TestCheckVerifyOutput(t *testing.T) { {"two attestations, two commits", "[" + result(publicGood, "Tlog", commitA) + "," + result(publicGood, "Tlog", commitB) + "]", "", true}, {"no source commit", "[" + result(publicGood, "Tlog", "") + "]", "", true}, {"short source commit", "[" + result(publicGood, "Tlog", "29fd287") + "]", "", true}, - {"GitHub private instance", "[" + result("CN=Fulcio Intermediate l2,O=GitHub\\, Inc.", "TimestampAuthority", commitA) + "]", "", true}, + // A Tlog timestamp, so the issuer check is what refuses it. + {"GitHub private instance", "[" + result("CN=Fulcio Intermediate l2,O=GitHub, Inc.", "Tlog", commitA) + "]", "", true}, {"no transparency-log entry", "[" + result(publicGood, "TimestampAuthority", commitA) + "]", "", true}, {"no attestations", `[]`, "", true}, {"not JSON", `verified`, "", true}, diff --git a/catalog/signature.go b/catalog/signature.go index c176113..b4195ac 100644 --- a/catalog/signature.go +++ b/catalog/signature.go @@ -3,7 +3,6 @@ package catalog import ( "encoding/json" "fmt" - "regexp" "strings" ) @@ -28,30 +27,30 @@ const ( publicGoodCertificateIssuerOrg = "O=sigstore.dev" ) -// CertIdentityRegex returns the certificate SAN pattern a community release -// must match. Two signers are accepted, and nothing else: Silo's shared -// release workflow at main, or a workflow file in the plugin's own repository -// run from the release tag. The repository and tag are regex-escaped and the -// pattern is anchored at both ends, so another repository's workflow, another -// ref, or a branch ref does not match. -func CertIdentityRegex(repo, tag string) string { - ownWorkflow := regexp.QuoteMeta("https://github.com/"+repo) + - `/\.github/workflows/[A-Za-z0-9._-]+\.ya?ml@refs/tags/` + regexp.QuoteMeta(tag) - return `^(?:` + regexp.QuoteMeta(SharedSignerIdentity) + `|` + ownWorkflow + `)$` +// SignerIdentity returns the one certificate SAN a release of repo at tag may +// carry. By default that is Silo's shared release workflow. A plugin whose +// approval names a signerWorkflow is signed instead by that exact workflow +// file in its own repository, run from the release tag; no other workflow, +// ref, or repository matches. +func SignerIdentity(repo, tag, signerWorkflow string) string { + if signerWorkflow == "" { + return SharedSignerIdentity + } + return "https://github.com/" + repo + "/.github/workflows/" + signerWorkflow + "@refs/tags/" + tag } // VerifyArgs returns the `gh` arguments that verify one release binary against // the release's bundle. --repo binds the certificate's source repository to the -// approved repository whichever workflow signed; the identity regex limits the -// signer; and the source ref, issuer, GitHub-hosted runner, and SLSA -// provenance predicate are required for both signers. Flags use the +// approved repository whichever workflow signed; --cert-identity names the one +// accepted signer; and the source ref, issuer, GitHub-hosted runner, and SLSA +// provenance predicate are required for both kinds of signer. Flags use the // --flag=value form so no value can be read as another flag. -func VerifyArgs(binaryPath, bundlePath, repo, tag string) []string { +func VerifyArgs(binaryPath, bundlePath, repo, tag, signerWorkflow string) []string { return []string{ "attestation", "verify", binaryPath, "--bundle=" + bundlePath, "--repo=" + repo, - "--cert-identity-regex=" + CertIdentityRegex(repo, tag), + "--cert-identity=" + SignerIdentity(repo, tag, signerWorkflow), "--cert-oidc-issuer=" + OIDCIssuer, "--source-ref=refs/tags/" + tag, "--predicate-type=" + slsaProvenancePredicate, @@ -60,6 +59,20 @@ func VerifyArgs(binaryPath, bundlePath, repo, tag string) []string { } } +// CheckOnDefaultBranch checks the status GitHub's compare API reports for +// .... A plugin signed by its own workflow must +// release a commit already on its default branch, so a tag on an unmerged +// branch cannot be signed: "identical" and "behind" pass, "ahead" and +// "diverged" do not. +func CheckOnDefaultBranch(status, defaultBranch, commit string) error { + switch status { + case "identical", "behind": + return nil + default: + return fmt.Errorf("signed commit %s is not on the default branch %s (compare status %q)", commit, defaultBranch, status) + } +} + // CheckVerifyOutput checks the JSON that `gh attestation verify --format=json` // prints on success and returns the source commit the certificate names. gh // also accepts GitHub's private Sigstore instance; the Silo server trusts only diff --git a/cmd/update-catalog/main.go b/cmd/update-catalog/main.go index a9c0424..f29fe10 100644 --- a/cmd/update-catalog/main.go +++ b/cmd/update-catalog/main.go @@ -3,11 +3,14 @@ package main import ( "bytes" "context" + "crypto/sha256" + "encoding/hex" "encoding/json" "flag" "fmt" "io" "net/http" + "net/url" "os" "os/exec" "path/filepath" @@ -53,10 +56,11 @@ func run(ctx context.Context, repo, tag, manifestPath, approvalsPath string, all if err != nil { return fmt.Errorf("load approvals: %w", err) } - pluginID, err := registry.ApprovedPluginID(repo) + approved, err := registry.ApprovedRepository(repo) if err != nil { return fmt.Errorf("refuse release: %w", err) } + pluginID := approved.PluginID index, err := loadIndex(manifestPath) if err != nil { @@ -75,8 +79,11 @@ func run(ctx context.Context, repo, tag, manifestPath, approvalsPath string, all token := os.Getenv("GITHUB_TOKEN") client := &http.Client{Timeout: 30 * time.Second} + getJSON := func(ctx context.Context, url string, dest any) error { + return githubJSON(ctx, client, token, url, dest) + } - release, err := fetchRelease(ctx, client, token, repo, tag) + release, err := fetchRelease(ctx, getJSON, repo, tag) if err != nil { return fmt.Errorf("fetch release: %w", err) } @@ -92,16 +99,22 @@ func run(ctx context.Context, repo, tag, manifestPath, approvalsPath string, all } var commit string + var checksums map[string]string if assets.BundleURL != "" { // Binaries are tens of megabytes, so downloads get more time than the // API calls. downloadClient := &http.Client{Timeout: 5 * time.Minute} - commit, err = verifyRelease(ctx, downloadClient, repo, tag, assets) + commit, checksums, err = verifyRelease(ctx, downloadClient, repo, tag, approved.SignerWorkflow, assets) if err != nil { return fmt.Errorf("refuse release: verify signature: %w", err) } + if approved.SignerWorkflow != "" { + if err := checkOnDefaultBranch(ctx, getJSON, repo, commit); err != nil { + return fmt.Errorf("refuse release: %w", err) + } + } } else { - commit, err = resolveTagCommit(ctx, client, token, repo, tag) + commit, err = resolveTagCommit(ctx, getJSON, repo, tag) if err != nil { return fmt.Errorf("resolve tag %s: %w", tag, err) } @@ -111,8 +124,8 @@ func run(ctx context.Context, repo, tag, manifestPath, approvalsPath string, all if err != nil { return fmt.Errorf("fetch source manifest: %w", err) } - if got := sourceManifest.GetPluginId(); got != pluginID { - return fmt.Errorf("refuse release: manifest.json names plugin %q, but %s is approved for %q", got, repo, pluginID) + if err := approved.CheckManifestPluginID(sourceManifest.GetPluginId()); err != nil { + return fmt.Errorf("refuse release: %w", err) } approval, err := registry.Resolve(repo, pluginID) if err != nil { @@ -123,6 +136,13 @@ func run(ctx context.Context, repo, tag, manifestPath, approvalsPath string, all return fmt.Errorf("build catalog package: %w", err) } pkg.Approval = &approval + // Pin each signed binary to the bytes gh verified. verifyRelease checked + // every binary in assets, which are the ones the entry lists. + for platform, checksum := range checksums { + binary := pkg.Binaries[platform] + binary.Checksum = checksum + pkg.Binaries[platform] = binary + } index = catalog.UpsertPackage(index, pkg) index = catalog.PruneUnapproved(index, registry) @@ -134,20 +154,21 @@ func run(ctx context.Context, repo, tag, manifestPath, approvalsPath string, all // verifyRelease downloads the bundle and every catalogued binary, checks each // binary against the bundle with gh, and returns the one commit all of them -// were built from. Any failure refuses the release. -func verifyRelease(ctx context.Context, client *http.Client, repo, tag string, assets catalog.ReleaseAssets) (string, error) { +// were built from and each binary's SHA-256 keyed by platform. Any failure +// refuses the release. +func verifyRelease(ctx context.Context, client *http.Client, repo, tag, signerWorkflow string, assets catalog.ReleaseAssets) (string, map[string]string, error) { if len(assets.Binaries) == 0 { - return "", fmt.Errorf("release has no plugin binaries") + return "", nil, fmt.Errorf("release has no plugin binaries") } dir, err := os.MkdirTemp("", "silo-plugin-verify-") if err != nil { - return "", err + return "", nil, err } defer os.RemoveAll(dir) bundlePath := filepath.Join(dir, catalog.BundleAssetName) - if err := download(ctx, client, assets.BundleURL, bundlePath, maxBundleBytes); err != nil { - return "", fmt.Errorf("download %s: %w", catalog.BundleAssetName, err) + if _, err := download(ctx, client, assets.BundleURL, bundlePath, maxBundleBytes); err != nil { + return "", nil, fmt.Errorf("download %s: %w", catalog.BundleAssetName, err) } platforms := make([]string, 0, len(assets.Binaries)) @@ -156,38 +177,70 @@ func verifyRelease(ctx context.Context, client *http.Client, repo, tag string, a } slices.Sort(platforms) var commit string + checksums := make(map[string]string, len(platforms)) for i, platform := range platforms { binaryPath := filepath.Join(dir, fmt.Sprintf("binary-%d", i)) - if err := download(ctx, client, assets.Binaries[platform], binaryPath, maxBinaryBytes); err != nil { - return "", fmt.Errorf("download %s binary: %w", platform, err) + checksum, err := download(ctx, client, assets.Binaries[platform], binaryPath, maxBinaryBytes) + if err != nil { + return "", nil, fmt.Errorf("download %s binary: %w", platform, err) } - cmd := exec.CommandContext(ctx, "gh", catalog.VerifyArgs(binaryPath, bundlePath, repo, tag)...) + cmd := exec.CommandContext(ctx, "gh", catalog.VerifyArgs(binaryPath, bundlePath, repo, tag, signerWorkflow)...) cmd.Stderr = os.Stderr out, err := cmd.Output() if err != nil { - return "", fmt.Errorf("%s binary: gh attestation verify: %w", platform, err) + return "", nil, fmt.Errorf("%s binary: gh attestation verify: %w", platform, err) } binaryCommit, err := catalog.CheckVerifyOutput(out) if err != nil { - return "", fmt.Errorf("%s binary: %w", platform, err) + return "", nil, fmt.Errorf("%s binary: %w", platform, err) } if commit != "" && binaryCommit != commit { - return "", fmt.Errorf("%s binary was built from %s, other binaries from %s", platform, binaryCommit, commit) + return "", nil, fmt.Errorf("%s binary was built from %s, other binaries from %s", platform, binaryCommit, commit) } commit = binaryCommit - fmt.Fprintf(os.Stderr, "Verified the %s binary of %s %s, built from %s.\n", platform, repo, tag, commit) + checksums[platform] = checksum + fmt.Fprintf(os.Stderr, "Verified the %s binary of %s %s, built from %s, SHA-256 %s.\n", platform, repo, tag, commit, checksum) + } + return commit, checksums, nil +} + +// getJSONFunc fetches a GitHub API URL and decodes the JSON response into +// dest. Tests replace it with a stub. +type getJSONFunc func(ctx context.Context, url string, dest any) error + +// checkOnDefaultBranch refuses a commit that is not on repo's default branch. +// The default branch is looked up, not assumed. +func checkOnDefaultBranch(ctx context.Context, getJSON getJSONFunc, repo, commit string) error { + var repository struct { + DefaultBranch string `json:"default_branch"` } - return commit, nil + if err := getJSON(ctx, "https://api.github.com/repos/"+repo, &repository); err != nil { + return fmt.Errorf("look up default branch: %w", err) + } + if repository.DefaultBranch == "" { + return fmt.Errorf("repository %s reports no default branch", repo) + } + if err := catalog.ValidateCommit(commit); err != nil { + return err + } + var comparison struct { + Status string `json:"status"` + } + compareURL := "https://api.github.com/repos/" + repo + "/compare/" + url.PathEscape(repository.DefaultBranch) + "..." + commit + if err := getJSON(ctx, compareURL, &comparison); err != nil { + return fmt.Errorf("compare %s with %s: %w", commit, repository.DefaultBranch, err) + } + return catalog.CheckOnDefaultBranch(comparison.Status, repository.DefaultBranch, commit) } // resolveTagCommit returns the commit a tag points at, following annotated // tags. An unsigned release has no certificate naming its commit, so this // pins manifest.json to one commit rather than a ref a branch could shadow. -func resolveTagCommit(ctx context.Context, client *http.Client, token, repo, tag string) (string, error) { +func resolveTagCommit(ctx context.Context, getJSON getJSONFunc, repo, tag string) (string, error) { var ref struct { Object gitObject `json:"object"` } - if err := githubJSON(ctx, client, token, "https://api.github.com/repos/"+repo+"/git/ref/tags/"+tag, &ref); err != nil { + if err := getJSON(ctx, "https://api.github.com/repos/"+repo+"/git/ref/tags/"+tag, &ref); err != nil { return "", err } object := ref.Object @@ -202,7 +255,7 @@ func resolveTagCommit(ctx context.Context, client *http.Client, token, repo, tag var annotated struct { Object gitObject `json:"object"` } - if err := githubJSON(ctx, client, token, "https://api.github.com/repos/"+repo+"/git/tags/"+object.SHA, &annotated); err != nil { + if err := getJSON(ctx, "https://api.github.com/repos/"+repo+"/git/tags/"+object.SHA, &annotated); err != nil { return "", err } object = annotated.Object @@ -230,40 +283,43 @@ func loadApprovals(path string) (catalog.ApprovalRegistry, error) { return registry, nil } -func download(ctx context.Context, client *http.Client, url, path string, limit int64) error { +// download saves url to path, refusing a body larger than limit, and returns +// the lowercase hex SHA-256 of the bytes written. +func download(ctx context.Context, client *http.Client, url, path string, limit int64) (string, error) { req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { - return fmt.Errorf("build request: %w", err) + return "", fmt.Errorf("build request: %w", err) } req.Header.Set("User-Agent", "silo-plugins-catalog-updater") resp, err := client.Do(req) if err != nil { - return fmt.Errorf("GET %s: %w", url, err) + return "", fmt.Errorf("GET %s: %w", url, err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - return fmt.Errorf("GET %s: status %d", url, resp.StatusCode) + return "", fmt.Errorf("GET %s: status %d", url, resp.StatusCode) } file, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600) if err != nil { - return err + return "", err } - written, err := io.Copy(file, io.LimitReader(resp.Body, limit+1)) + hash := sha256.New() + written, err := io.Copy(io.MultiWriter(file, hash), io.LimitReader(resp.Body, limit+1)) if closeErr := file.Close(); err == nil { err = closeErr } if err != nil { - return fmt.Errorf("read %s: %w", url, err) + return "", fmt.Errorf("read %s: %w", url, err) } if written > limit { - return fmt.Errorf("GET %s: larger than %d bytes", url, limit) + return "", fmt.Errorf("GET %s: larger than %d bytes", url, limit) } - return nil + return hex.EncodeToString(hash.Sum(nil)), nil } -func fetchRelease(ctx context.Context, client *http.Client, token, repo, tag string) (catalog.Release, error) { +func fetchRelease(ctx context.Context, getJSON getJSONFunc, repo, tag string) (catalog.Release, error) { var release catalog.Release - if err := githubJSON(ctx, client, token, "https://api.github.com/repos/"+repo+"/releases/tags/"+tag, &release); err != nil { + if err := getJSON(ctx, "https://api.github.com/repos/"+repo+"/releases/tags/"+tag, &release); err != nil { return catalog.Release{}, err } return release, nil diff --git a/cmd/update-catalog/main_test.go b/cmd/update-catalog/main_test.go new file mode 100644 index 0000000..b86fa45 --- /dev/null +++ b/cmd/update-catalog/main_test.go @@ -0,0 +1,159 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + + "github.com/Silo-Community/silo-plugins/catalog" +) + +const ( + testRepo = "Silo-Community/silo-plugins-requests-arr" + testCommit = "29fd28771a006d9d68a311865a2b858c5afe301a" + testTagSHA = "63837fb778d6a39f02be0ce76ff89f2bc8d74bdc" + apiPrefix = "https://api.github.com/repos/" + testRepo +) + +// stubGitHub answers getJSON from a map of URL to JSON body. A URL not in +// the map fails the request, as an unexpected call should. +func stubGitHub(t *testing.T, responses map[string]string) getJSONFunc { + t.Helper() + return func(_ context.Context, url string, dest any) error { + body, ok := responses[url] + if !ok { + return fmt.Errorf("unexpected request %s", url) + } + return json.Unmarshal([]byte(body), dest) + } +} + +func TestVerifyReleaseRefusesNoBinaries(t *testing.T) { + assets := catalog.ReleaseAssets{ + BundleURL: "https://github.com/" + testRepo + "/releases/download/v0.1.11/" + catalog.BundleAssetName, + Binaries: map[string]string{}, + } + // The client is never used: the refusal comes before any download. + if _, _, err := verifyRelease(context.Background(), nil, testRepo, "v0.1.11", "", assets); err == nil { + t.Fatal("verifyRelease() accepted a signed release with no binaries") + } +} + +func TestDownloadSizeCapAndChecksum(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + fmt.Fprint(w, "hello") + })) + defer server.Close() + dir := t.TempDir() + + checksum, err := download(context.Background(), server.Client(), server.URL, filepath.Join(dir, "exact"), 5) + if err != nil { + t.Fatalf("download() at the limit error = %v", err) + } + // SHA-256 of "hello", lowercase hex. + if want := "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"; checksum != want { + t.Fatalf("download() checksum = %q, want %q", checksum, want) + } + + if _, err := download(context.Background(), server.Client(), server.URL, filepath.Join(dir, "over"), 4); err == nil || !strings.Contains(err.Error(), "larger than 4 bytes") { + t.Fatalf("download() over the limit error = %v, want the size refusal", err) + } +} + +func TestResolveTagCommit(t *testing.T) { + refURL := apiPrefix + "/git/ref/tags/v0.1.11" + tagURL := func(sha string) string { return apiPrefix + "/git/tags/" + sha } + object := func(typ, sha string) string { + return `{"object":{"type":"` + typ + `","sha":"` + sha + `"}}` + } + + t.Run("lightweight tag", func(t *testing.T) { + got, err := resolveTagCommit(context.Background(), stubGitHub(t, map[string]string{refURL: object("commit", testCommit)}), testRepo, "v0.1.11") + if err != nil || got != testCommit { + t.Fatalf("resolveTagCommit() = %q, %v; want %s", got, err, testCommit) + } + }) + t.Run("annotated tag", func(t *testing.T) { + got, err := resolveTagCommit(context.Background(), stubGitHub(t, map[string]string{ + refURL: object("tag", testTagSHA), + tagURL(testTagSHA): object("commit", testCommit), + }), testRepo, "v0.1.11") + if err != nil || got != testCommit { + t.Fatalf("resolveTagCommit() = %q, %v; want %s", got, err, testCommit) + } + }) + t.Run("annotated tags nested too deep", func(t *testing.T) { + // Every tag object points at itself, so resolution never reaches a + // commit and must stop at the nesting limit. + _, err := resolveTagCommit(context.Background(), stubGitHub(t, map[string]string{ + refURL: object("tag", testTagSHA), + tagURL(testTagSHA): object("tag", testTagSHA), + }), testRepo, "v0.1.11") + if err == nil || !strings.Contains(err.Error(), "nested") { + t.Fatalf("resolveTagCommit() error = %v, want the nesting refusal", err) + } + }) + t.Run("tag of a tree", func(t *testing.T) { + if _, err := resolveTagCommit(context.Background(), stubGitHub(t, map[string]string{refURL: object("tree", testCommit)}), testRepo, "v0.1.11"); err == nil { + t.Fatal("resolveTagCommit() accepted a tag that points at a tree") + } + }) + t.Run("malformed object SHA", func(t *testing.T) { + if _, err := resolveTagCommit(context.Background(), stubGitHub(t, map[string]string{refURL: object("commit", "../../x")}), testRepo, "v0.1.11"); err == nil { + t.Fatal("resolveTagCommit() accepted a malformed SHA") + } + }) +} + +func TestCheckOnDefaultBranch(t *testing.T) { + compareURL := func(branch string) string { return apiPrefix + "/compare/" + branch + "..." + testCommit } + tests := []struct { + name string + responses map[string]string + wantErr bool + }{ + {"commit is the default branch head", map[string]string{ + apiPrefix: `{"default_branch":"main"}`, + compareURL("main"): `{"status":"identical"}`, + }, false}, + {"commit is behind the default branch", map[string]string{ + apiPrefix: `{"default_branch":"main"}`, + compareURL("main"): `{"status":"behind"}`, + }, false}, + {"default branch is looked up, not assumed", map[string]string{ + apiPrefix: `{"default_branch":"trunk"}`, + compareURL("trunk"): `{"status":"behind"}`, + }, false}, + {"branch name with a slash is escaped", map[string]string{ + apiPrefix: `{"default_branch":"release/1.x"}`, + compareURL("release%2F1.x"): `{"status":"identical"}`, + }, false}, + {"commit ahead of the default branch", map[string]string{ + apiPrefix: `{"default_branch":"main"}`, + compareURL("main"): `{"status":"ahead"}`, + }, true}, + {"commit on a diverged branch", map[string]string{ + apiPrefix: `{"default_branch":"main"}`, + compareURL("main"): `{"status":"diverged"}`, + }, true}, + {"no default branch", map[string]string{ + apiPrefix: `{}`, + }, true}, + {"compare request fails", map[string]string{ + apiPrefix: `{"default_branch":"main"}`, + }, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := checkOnDefaultBranch(context.Background(), stubGitHub(t, tt.responses), testRepo, testCommit) + if (err != nil) != tt.wantErr { + t.Fatalf("checkOnDefaultBranch() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}