feat(vulnerable): filter vulnerable packages by severity

The four severity tiles on the vulnerable packages page are now toggle
buttons with aria-pressed. Each tile shows "Filter on" while active.
A tile selects packages with at least one vulnerability of that
severity. Several tiles combine with OR. A second click clears the
tile, and a Clear filters control clears all of them.

The selection lives in the URL (?severity=critical,high), so it
survives a reload and works as a link. An empty result shows the
library empty state with a clear action.

GET /api/packages accepts a severity list. The page uses it to load
only vulnerable packages instead of the whole cache. The filter is a
subquery on the latest scan result, so the statement count does not
grow with the number of rows. An unknown severity returns 400.
This commit is contained in:
2026-11-23 14:54:44 +00:00
parent 4d31e535ff
commit bd9d686383
12 changed files with 652 additions and 24 deletions
+49
View File
@@ -0,0 +1,49 @@
package app
import (
"net/http"
"testing"
)
func TestSeverityFilterOnPackagesList(t *testing.T) {
a, _ := newScanningApp(t)
seedScanned(t, a, 0, 60) // every fifth package carries one critical finding
cases := []struct {
query string
want int
}{
{"", 60},
{"?severity=", 60},
{"?severity=critical", 12},
{"?severity=Critical,high", 12},
{"?severity=high", 0},
{"?severity=high,moderate,low", 0},
{"?severity=critical&q=pkg-005", 1},
{"?severity=critical&q=pkg-006", 0},
}
for _, c := range cases {
if got := getList(t, a, "/api/packages"+c.query); len(got.Packages) != c.want {
t.Errorf("%q returned %d packages, want %d", c.query, len(got.Packages), c.want)
}
}
paged := getList(t, a, "/api/packages?severity=critical&page=1&page_size=5")
if paged.TotalGroups != 12 || len(paged.Packages) != 5 {
t.Errorf("paged severity list = %d groups, %d rows; want 12 groups, 5 rows", paged.TotalGroups, len(paged.Packages))
}
for _, p := range getList(t, a, "/api/packages?severity=critical").Packages {
if p.Vulnerabilities.Counts.Critical == 0 {
t.Errorf("%s has no critical finding but passed the critical filter", p.Name)
}
}
}
func TestSeverityFilterRejectsUnknownValue(t *testing.T) {
a, _ := newScanningApp(t)
for _, q := range []string{"?severity=urgent", "?severity=critical,nope"} {
if code, _, body := doRequest(t, a, "/api/packages"+q, nil); code != http.StatusBadRequest {
t.Errorf("%q = %d %s, want 400", q, code, body)
}
}
}