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
+30
View File
@@ -37,9 +37,39 @@ func (s *Store) ListPackageGroups(ctx context.Context, opts metadata.GroupListOp
p.ID = packageKey(p)
}
}
if len(opts.Severities) > 0 {
if all, err = s.withSeverity(ctx, all, opts.Severities); err != nil {
return nil, err
}
}
return metadata.PageGroups(all, opts), nil
}
// withSeverity keeps the packages whose scan holds a vulnerability of one of the severities.
func (s *Store) withSeverity(ctx context.Context, all []*metadata.Package, severities []string) ([]*metadata.Package, error) {
sums, err := s.GetScanSummaries(ctx, nil, false)
if err != nil {
return nil, err
}
hit := make(map[string]struct{}, len(sums))
for _, sum := range sums {
for _, sev := range severities {
if (sev == metadata.SeverityCritical && sum.Critical > 0) || (sev == metadata.SeverityHigh && sum.High > 0) ||
(sev == metadata.SeverityModerate && sum.Moderate > 0) || (sev == metadata.SeverityLow && sum.Low > 0) {
hit[sum.PackageID] = struct{}{}
break
}
}
}
out := all[:0]
for _, p := range all {
if _, ok := hit[p.ID]; ok {
out = append(out, p)
}
}
return out, nil
}
// GetScanSummaries reads each scanned package's latest scan file.
func (s *Store) GetScanSummaries(ctx context.Context, packageIDs []string, withDetails bool) ([]*metadata.ScanSummary, error) {
all, err := s.listRecords(ctx)