From 419455c61d0877263d274ff9e9d976782825ae9d Mon Sep 17 00:00:00 2001 From: Simon Chrzanowski Date: Sun, 22 Nov 2026 14:03:50 +0100 Subject: [PATCH] Refetch a detached signature cached before its release file Release.gpg and repomd.xml.asc had a cache entry and TTL of their own, apart from the release file they sign. When the release file was fetched again, after its TTL or because an index showed upstream had moved on, the cached signature kept being served with it and failed to verify. A cached signature fetched before its release file is now fetched again. --- docs/configuration.md | 2 +- internal/handler/debian.go | 9 +- internal/handler/release_index.go | 32 ++++++ internal/handler/release_signature_test.go | 120 +++++++++++++++++++++ internal/handler/rpm.go | 10 +- 5 files changed, 168 insertions(+), 5 deletions(-) create mode 100644 internal/handler/release_signature_test.go diff --git a/docs/configuration.md b/docs/configuration.md index 991fb61d..b439fb10 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1715,7 +1715,7 @@ Set to `"0"` to always revalidate with upstream (ETag-based conditional requests When upstream is unreachable and the cached entry is past its TTL (with a `metadata_ttl` of `"0"`, on every such request), the proxy serves the stale cached copy with a `Warning: 110 - "Response is Stale"` header so clients can tell the data may be outdated. The `ETag` and `Last-Modified` sent with a document are always those of the bytes served, so a document fetched from upstream but not stored, for example because storage failed, carries the upstream's validators rather than the older cached copy's. -APT and RPM clients fetch a release file first (`InRelease` or `Release`, `repomd.xml`) and reject an index file whose checksum doesn't match it. So an index file the cached release file lists (`Packages`, `Sources`, a plainly named `primary.xml.gz`, …) does not follow its own `metadata_ttl`: its cached copy is served while it matches the cached release file's sha256, however old, and otherwise it is fetched from upstream. If upstream's copy doesn't match either, the repository has changed since the release file was cached; that copy is served and the release file is expired, so the next `apt update` or `dnf makecache` gets a matching pair. Where a suite has both `InRelease` and `Release` cached, the one fetched last decides. The release file itself follows `metadata_ttl`. Index files it doesn't list, such as `by-hash` paths, follow `metadata_ttl` too. +APT and RPM clients fetch a release file first (`InRelease` or `Release`, `repomd.xml`) and reject an index file whose checksum doesn't match it. So an index file the cached release file lists (`Packages`, `Sources`, a plainly named `primary.xml.gz`, …) does not follow its own `metadata_ttl`: its cached copy is served while it matches the cached release file's sha256, however old, and otherwise it is fetched from upstream. If upstream's copy doesn't match either, the repository has changed since the release file was cached; that copy is served and the release file is expired, so the next `apt update` or `dnf makecache` gets a matching pair. Where a suite has both `InRelease` and `Release` cached, the one fetched last decides. The release file itself follows `metadata_ttl`. Its detached signature (`Release.gpg`, `repomd.xml.asc`) follows it: a cached signature fetched before the release file was last fetched is fetched again, so it isn't served with a release file it doesn't sign. Index files it doesn't list, such as `by-hash` paths, follow `metadata_ttl` too. ### Change feeds diff --git a/internal/handler/debian.go b/internal/handler/debian.go index 28b9e50a..fd7f447e 100644 --- a/internal/handler/debian.go +++ b/internal/handler/debian.go @@ -191,7 +191,8 @@ func (h *DebianHandler) handlePackageDownload( // handleMetadata serves repository metadata files through the metadata cache, // so they keep resolving while the upstream archive is unreachable. // Index files the cached InRelease or Release lists follow that listing (see -// proxyListedIndex). +// proxyListedIndex), and Release.gpg follows Release (see +// proxyReleaseSignature). func (h *DebianHandler) handleMetadata( w http.ResponseWriter, r *http.Request, @@ -200,9 +201,13 @@ func (h *DebianHandler) handleMetadata( cacheKey := h.metadataCacheKeyFor(repository, upstreamURL, path) fileURL := fmt.Sprintf("%s/%s", upstreamURL, path) switch pathpkg.Base(path) { - case "InRelease", "Release", "Release.gpg": + case "InRelease", "Release": h.proxy.ProxyCached(w, r, fileURL, "debian", cacheKey, "*/*") return + case "Release.gpg": + releaseKey := h.metadataCacheKeyFor(repository, upstreamURL, strings.TrimSuffix(path, ".gpg")) + h.proxy.proxyReleaseSignature(w, r, fileURL, "debian", cacheKey, releaseKey) + return } release, want := h.releaseListing(r, repository, upstreamURL, path) h.proxy.proxyListedIndex(w, r, fileURL, "debian", cacheKey, release, want) diff --git a/internal/handler/release_index.go b/internal/handler/release_index.go index 626792a2..18f3cd21 100644 --- a/internal/handler/release_index.go +++ b/internal/handler/release_index.go @@ -131,3 +131,35 @@ func repomdSHA256(repomd []byte, href string) string { } return "" } + +// proxyReleaseSignature serves the detached signature of the release file +// cached under releaseKey (Release.gpg, repomd.xml.asc). Each has its own +// cache entry, so a cached signature fetched before the release file was +// fetched again, after its TTL or because an index showed upstream had +// moved on, signs an older release file. It is fetched again rather than +// served with one it doesn't match. +func (p *Proxy) proxyReleaseSignature(w http.ResponseWriter, r *http.Request, upstreamURL, ecosystem, cacheKey, releaseKey string) { + if !p.signaturePredatesRelease(ecosystem, cacheKey, releaseKey) { + p.ProxyCached(w, r, upstreamURL, ecosystem, cacheKey, "*/*") + return + } + p.recordMetadataCacheMiss(ecosystem) + res := p.refreshMetadata(r.Context(), ecosystem, cacheKey, upstreamURL, "*/*", "identity", nil) + if res.err != nil { + p.writeMetadataFetchError(w, res.err) + return + } + writeMetadataResult(w, r, res) +} + +// signaturePredatesRelease reports whether the signature cached under +// cacheKey was fetched before the release file cached under releaseKey. +func (p *Proxy) signaturePredatesRelease(ecosystem, cacheKey, releaseKey string) bool { + if !p.CacheMetadata || p.DB == nil { + return false + } + signature, _ := p.DB.GetMetadataCache(ecosystem, cacheKey) + release, _ := p.DB.GetMetadataCache(ecosystem, releaseKey) + return signature != nil && release != nil && signature.FetchedAt.Valid && + release.FetchedAt.Time.After(signature.FetchedAt.Time) +} diff --git a/internal/handler/release_signature_test.go b/internal/handler/release_signature_test.go new file mode 100644 index 00000000..95b8519d --- /dev/null +++ b/internal/handler/release_signature_test.go @@ -0,0 +1,120 @@ +package handler + +import ( + "fmt" + "net/http" + "net/http/httptest" + "sync/atomic" + "testing" + "time" +) + +func signatureBody(gen int32) string { return fmt.Sprintf("signature of release generation %d\n", gen) } + +// signatureCase is a repository whose release file has a detached +// signature: Debian Release and Release.gpg, RPM repomd.xml and +// repomd.xml.asc. +type signatureCase struct { + releaseIndexCase + signaturePath string +} + +func signatureCases() []signatureCase { + cases := releaseIndexCases() + debian := cases[0] + debian.name = "debian Release.gpg" + debian.archive.releasePath = "dists/stable/Release" + return []signatureCase{ + {releaseIndexCase: debian, signaturePath: "dists/stable/Release.gpg"}, + {releaseIndexCase: cases[1], signaturePath: "f/39/os/repodata/repomd.xml.asc"}, + } +} + +// signedArchive serves tc's archive and a signature for each generation of +// its release file, counting the signature requests. +func signedArchive(t *testing.T, tc signatureCase) (http.Handler, func(path string), *atomic.Int32) { + t.Helper() + a := tc.archive + var signatures atomic.Int32 + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/"+tc.signaturePath { + signatures.Add(1) + _, _ = w.Write([]byte(signatureBody(a.gen.Load()))) + return + } + a.serve(w, r) + })) + t.Cleanup(upstream.Close) + proxy, db, _, _ := setupTestProxy(t) + proxy.CacheMetadata = true + proxy.MetadataTTL = time.Hour + proxy.HTTPClient = upstream.Client() + h, key := tc.handler(proxy, upstream.URL) + expire := func(path string) { + t.Helper() + if _, err := db.ExpireMetadataCache(tc.ecosystem, []string{key(path)}, time.Now().Add(time.Second)); err != nil { + t.Fatal(err) + } + } + return h, expire, &signatures +} + +// TestSignatureFollowsRefetchedRelease checks that a cached detached +// signature isn't served with a release file fetched after it, which it +// can't sign. +func TestSignatureFollowsRefetchedRelease(t *testing.T) { + for _, tc := range signatureCases() { + t.Run(tc.name, func(t *testing.T) { + a := tc.archive + h, expire, signatures := signedArchive(t, tc) + + getBody(t, h, a.releasePath) + getBody(t, h, tc.signaturePath) + + // An index the cached release file doesn't list shows upstream + // has moved on, and the release file is expired. + a.gen.Store(1) + getBody(t, h, a.indexPath) + getBody(t, h, a.releasePath) + if got := getBody(t, h, tc.signaturePath); got != signatureBody(1) { + t.Errorf("signature after the release file was expired = %q, want %q", got, signatureBody(1)) + } + + // The release file's entry runs out before the signature's. + a.gen.Store(2) + expire(a.releasePath) + getBody(t, h, a.releasePath) + if got := getBody(t, h, tc.signaturePath); got != signatureBody(2) { + t.Errorf("signature after the release file's TTL = %q, want %q", got, signatureBody(2)) + } + + // A signature fetched after the release file is served from the + // cache. + before := signatures.Load() + if got := getBody(t, h, tc.signaturePath); got != signatureBody(2) { + t.Errorf("cached signature = %q", got) + } + if got := signatures.Load(); got != before { + t.Errorf("signature fetched %d more times, want it served from the cache", got-before) + } + }) + } +} + +// TestSignatureWithoutCachedRelease checks that a signature asked for +// whose release file isn't cached is served as other metadata. +func TestSignatureWithoutCachedRelease(t *testing.T) { + for _, tc := range signatureCases() { + t.Run(tc.name, func(t *testing.T) { + h, _, signatures := signedArchive(t, tc) + for range 2 { + if got := getBody(t, h, tc.signaturePath); got != signatureBody(0) { + t.Errorf("signature = %q, want %q", got, signatureBody(0)) + } + } + if got := signatures.Load(); got != 1 { + t.Errorf("signature fetched %d times, want it cached", got) + } + }) + } +} diff --git a/internal/handler/rpm.go b/internal/handler/rpm.go index 3a177408..6e07c021 100644 --- a/internal/handler/rpm.go +++ b/internal/handler/rpm.go @@ -139,7 +139,8 @@ func (h *RPMHandler) handlePackageDownload(w http.ResponseWriter, r *http.Reques // primary.xml.gz, etc.). repomd.xml changes with every repository update and // follows the metadata TTL. The files it points at are usually named after // their own checksum and so never change; those are kept for good. Plainly -// named ones follow the cached repomd.xml (see proxyListedIndex). +// named ones follow the cached repomd.xml (see proxyListedIndex), and +// repomd.xml.asc follows repomd.xml (see proxyReleaseSignature). func (h *RPMHandler) handleMetadata(w http.ResponseWriter, r *http.Request, path string) { upstreamURL := fmt.Sprintf("%s/%s", h.upstreamURL, path) if rpmContentAddressed(path) { @@ -147,13 +148,18 @@ func (h *RPMHandler) handleMetadata(w http.ResponseWriter, r *http.Request, path return } i := strings.LastIndex(path, "/repodata/") + repomdPath := path[:i] + "/repodata/repomd.xml" + if path == repomdPath+".asc" { + h.proxy.proxyReleaseSignature(w, r, upstreamURL, "rpm", h.metadataCacheKey(path), h.metadataCacheKey(repomdPath)) + return + } if strings.HasPrefix(path[i:], "/repodata/repomd.xml") { h.proxy.ProxyCached(w, r, upstreamURL, "rpm", h.metadataCacheKey(path), "*/*") return } // Plainly named files (createrepo --simple-md-filenames) follow the // cached repomd.xml that lists them. - repomd := h.proxy.readCachedRelease(r.Context(), "rpm", h.metadataCacheKey(path[:i]+"/repodata/repomd.xml")) + repomd := h.proxy.readCachedRelease(r.Context(), "rpm", h.metadataCacheKey(repomdPath)) var want string if repomd != nil { want = repomdSHA256(repomd.body, path[i+1:])