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.
This commit is contained in:
2026-11-23 15:49:33 +00:00
parent aa8487ad87
commit f4cf96e1e8
2 changed files with 83 additions and 2 deletions
+60
View File
@@ -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)
}
}