From f4cf96e1e8d1b6f7f8d33c325d0cc0d5d108c8b0 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Mon, 23 Nov 2026 15:49:33 +0000 Subject: [PATCH] fix(api): resolve Go module paths to cache keys for vulnerability details The dashboard links Go packages by module path, but Go rows are stored under cache keys (module/@v/version.zip, .mod, .info). The vulnerability details lookup used the module path, so every Go details page returned 404 "package not found". The handler now falls back to the .zip key, then .mod and .info. --- pkg/app/handlers_vulnerabilities.go | 25 ++++++++++- pkg/app/vulnerabilities_details_test.go | 60 +++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 pkg/app/vulnerabilities_details_test.go diff --git a/pkg/app/handlers_vulnerabilities.go b/pkg/app/handlers_vulnerabilities.go index 7f8073d..cfe9b54 100644 --- a/pkg/app/handlers_vulnerabilities.go +++ b/pkg/app/handlers_vulnerabilities.go @@ -1,6 +1,7 @@ package app import ( + "context" "strings" "github.com/gofiber/fiber/v3" @@ -42,14 +43,14 @@ func (a *App) handleVulnerabilities(c fiber.Ctx) error { Str("version", version). Msg("Getting vulnerabilities for package") - pkg, pkgErr := a.metadata.GetPackage(ctx, registry, name, version) + key, pkg, pkgErr := a.resolvePackageKey(ctx, registry, name, version) releaseVersion := "" if pkgErr == nil && pkg != nil { releaseVersion = pkg.ReleaseVersion } // Get scan result from metadata store - scanResult, err := a.metadata.GetScanResult(ctx, registry, name, version) + scanResult, err := a.metadata.GetScanResult(ctx, registry, key, version) if err != nil { if pkgErr != nil { return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "package not found"}) @@ -169,3 +170,23 @@ func (a *App) handleVulnerabilities(c fiber.Ctx) error { return c.Status(fiber.StatusOK).JSON(response) } + +// goCacheKeyExts orders the Go cache files a module version is stored under; the +// .zip carries the scan result. +var goCacheKeyExts = []string{"zip", "mod", "info"} + +// resolvePackageKey maps the name the dashboard shows to the stored row name. +// Go rows are stored under cache keys (module/@v/version.ext), not the module path. +func (a *App) resolvePackageKey(ctx context.Context, registry, name, version string) (string, *metadata.Package, error) { + pkg, err := a.metadata.GetPackage(ctx, registry, name, version) + if err == nil || registry != "go" || strings.Contains(name, "/@v/") { + return name, pkg, err + } + for _, ext := range goCacheKeyExts { + key := name + "/@v/" + version + "." + ext + if p, kerr := a.metadata.GetPackage(ctx, registry, key, version); kerr == nil { + return key, p, nil + } + } + return name, nil, err +} diff --git a/pkg/app/vulnerabilities_details_test.go b/pkg/app/vulnerabilities_details_test.go new file mode 100644 index 0000000..8a5e023 --- /dev/null +++ b/pkg/app/vulnerabilities_details_test.go @@ -0,0 +1,60 @@ +package app + +import ( + "context" + "encoding/json" + "net/http" + "testing" + "time" + + "github.com/lukaszraczylo/gohoarder/pkg/metadata" +) + +// The list shows Go modules by module path, but rows are stored under cache keys. +func TestGoVulnerabilityDetailsResolveCacheKeyNames(t *testing.T) { + a := newTestApp(t, nil) + ctx := context.Background() + const module, version = "github.com/go-git/go-git/v5", "v5.3.0" + for _, ext := range []string{"info", "mod", "zip"} { + name := module + "/@v/" + version + "." + ext + err := a.metadata.SavePackage(ctx, &metadata.Package{ + Registry: "go", Name: name, Version: version, StorageKey: "go/" + name, + Size: 10, CachedAt: time.Now(), LastAccessed: time.Now(), + }) + if err != nil { + t.Fatalf("SavePackage %s: %v", name, err) + } + } + err := a.metadata.SaveScanResult(ctx, &metadata.ScanResult{ + Registry: "go", PackageName: module + "/@v/" + version + ".zip", PackageVersion: version, + Scanner: "osv", Status: metadata.ScanStatusVulnerable, ScannedAt: time.Now(), + Vulnerabilities: []metadata.Vulnerability{{ID: "GHSA-test", Severity: "critical"}}, + VulnerabilityCount: 1, + }) + if err != nil { + t.Fatalf("SaveScanResult: %v", err) + } + + code, _, body := doRequest(t, a, "/api/packages/go/"+module+"/"+version+"/vulnerabilities", nil) + if code != http.StatusOK { + t.Fatalf("GET details = %d %s, want 200", code, body) + } + var out struct { + Scanned bool `json:"scanned"` + Count int `json:"vulnerability_count"` + } + if err := json.Unmarshal([]byte(body), &out); err != nil { + t.Fatalf("decode %q: %v", body, err) + } + if !out.Scanned || out.Count != 1 { + t.Errorf("details = %s, want the .zip scan with 1 vulnerability", body) + } +} + +func TestGoVulnerabilityDetailsMissingModuleIs404(t *testing.T) { + a := newTestApp(t, nil) + code, _, body := doRequest(t, a, "/api/packages/go/example.com/nope/v1.0.0/vulnerabilities", nil) + if code != http.StatusNotFound { + t.Errorf("GET missing = %d %s, want 404", code, body) + } +}