mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
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:
@@ -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)
|
||||
|
||||
@@ -63,6 +63,10 @@ func (s *GORMStoreV2) groupFilter(opts metadata.GroupListOptions) (clause string
|
||||
clause += " AND registry_id = ?"
|
||||
args = append(args, id)
|
||||
}
|
||||
if sev := severityFilter(opts.Severities); sev != "" {
|
||||
clause += " AND security_scanned = ? AND id IN (" + latestScanWith(sev) + ")"
|
||||
args = append(args, true)
|
||||
}
|
||||
if q := strings.TrimSpace(opts.Query); q != "" {
|
||||
like := escapeLike(q)
|
||||
clause += " AND (LOWER(name) LIKE ? ESCAPE '" + likeEscape + "'" +
|
||||
@@ -73,6 +77,38 @@ func (s *GORMStoreV2) groupFilter(opts metadata.GroupListOptions) (clause string
|
||||
return clause, args, false, nil
|
||||
}
|
||||
|
||||
// severityColumns maps a severity filter value to its scan_results count column.
|
||||
var severityColumns = map[string]string{
|
||||
metadata.SeverityCritical: "critical_count",
|
||||
metadata.SeverityHigh: "high_count",
|
||||
metadata.SeverityModerate: "medium_count",
|
||||
metadata.SeverityLow: "low_count",
|
||||
}
|
||||
|
||||
// severityFilter ORs the count columns of the known severities; unknown values are ignored.
|
||||
func severityFilter(severities []string) string {
|
||||
var conds []string
|
||||
seen := map[string]bool{}
|
||||
for _, sev := range severities {
|
||||
col, ok := severityColumns[sev]
|
||||
if !ok || seen[sev] {
|
||||
continue
|
||||
}
|
||||
seen[sev] = true
|
||||
conds = append(conds, col+" > 0")
|
||||
}
|
||||
return strings.Join(conds, " OR ")
|
||||
}
|
||||
|
||||
// latestScanWith selects the packages whose newest scan satisfies cond. A tie on
|
||||
// scanned_at goes to the higher id, as GetScanSummaries decides it.
|
||||
func latestScanWith(cond string) string {
|
||||
return `SELECT package_id FROM scan_results WHERE deleted_at IS NULL AND (` + cond + `)
|
||||
AND scanned_at = (SELECT MAX(x.scanned_at) FROM scan_results x WHERE x.package_id = scan_results.package_id AND x.deleted_at IS NULL)
|
||||
AND NOT EXISTS (SELECT 1 FROM scan_results y WHERE y.package_id = scan_results.package_id AND y.deleted_at IS NULL
|
||||
AND y.scanned_at = scan_results.scanned_at AND y.id > scan_results.id)`
|
||||
}
|
||||
|
||||
// groupOrder is the ORDER BY of the group page query.
|
||||
// It reads the aliases of the grouped subquery, which MySQL's only_full_group_by requires.
|
||||
func groupOrder(opts metadata.GroupListOptions) string {
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
//go:build pgintegration
|
||||
|
||||
package gormstore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPostgresSeverityFilter(t *testing.T) {
|
||||
store, _ := newPostgresStore(t)
|
||||
|
||||
for name, check := range map[string]func(*testing.T, *GORMStoreV2){
|
||||
"severity filter": checkSeverityFilter,
|
||||
"severity statements constant": checkSeverityFilterStatementsAreConstant,
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
require.NoError(t, store.db.Exec("TRUNCATE packages RESTART IDENTITY CASCADE").Error)
|
||||
check(t, store)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package gormstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func checkSeverityFilter(t *testing.T, store *GORMStoreV2) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
for _, n := range []string{"crit", "high", "mod", "low", "clean", "fixed", "multi", "unscanned"} {
|
||||
seedAt(t, store, n, "1.0.0", 1, daysAgo(3))
|
||||
}
|
||||
scanAt(t, store, "crit", "1.0.0", daysAgo(2), "critical")
|
||||
scanAt(t, store, "high", "1.0.0", daysAgo(2), "high")
|
||||
scanAt(t, store, "mod", "1.0.0", daysAgo(2), "moderate")
|
||||
scanAt(t, store, "low", "1.0.0", daysAgo(2), "low")
|
||||
scanAt(t, store, "clean", "1.0.0", daysAgo(2))
|
||||
scanAt(t, store, "fixed", "1.0.0", daysAgo(2), "critical")
|
||||
scanAt(t, store, "fixed", "1.0.0", daysAgo(1)) // the newest scan decides
|
||||
scanAt(t, store, "multi", "1.0.0", daysAgo(2), "high", "low")
|
||||
|
||||
pick := func(opts metadata.GroupListOptions) []string {
|
||||
page, err := store.ListPackageGroups(ctx, opts)
|
||||
require.NoError(t, err)
|
||||
out := names(page.Packages)
|
||||
for i := range out {
|
||||
for j := i + 1; j < len(out); j++ {
|
||||
if out[j] < out[i] {
|
||||
out[i], out[j] = out[j], out[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
require.Equal(t, []string{"crit"}, pick(metadata.GroupListOptions{Severities: []string{"critical"}}))
|
||||
require.Equal(t, []string{"high", "multi"}, pick(metadata.GroupListOptions{Severities: []string{"high"}}))
|
||||
require.Equal(t, []string{"crit", "mod"}, pick(metadata.GroupListOptions{Severities: []string{"critical", "moderate"}}), "OR across severities")
|
||||
require.Equal(t, []string{"low", "multi"}, pick(metadata.GroupListOptions{Severities: []string{"low"}}))
|
||||
require.Equal(t, []string{"crit", "high", "low", "mod", "multi"},
|
||||
pick(metadata.GroupListOptions{Severities: metadata.Severities}), "every severity = every vulnerable package")
|
||||
require.Len(t, pick(metadata.GroupListOptions{}), 8, "no filter keeps everything")
|
||||
|
||||
paged, err := store.ListPackageGroups(ctx, metadata.GroupListOptions{Severities: []string{"high", "low"}, Page: 1, PageSize: 2})
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, 3, paged.TotalGroups, "paging counts the filtered groups")
|
||||
require.Len(t, paged.Packages, 2)
|
||||
|
||||
require.Equal(t, []string{"crit"}, pick(metadata.GroupListOptions{Severities: []string{"critical"}, Query: "cr"}), "composes with search")
|
||||
require.Empty(t, pick(metadata.GroupListOptions{Severities: []string{"critical"}, Query: "high"}))
|
||||
}
|
||||
|
||||
func checkSeverityFilterStatementsAreConstant(t *testing.T, store *GORMStoreV2) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
counter := attachCounter(store)
|
||||
measure := func() [2]int {
|
||||
var out [2]int
|
||||
counter.Reset()
|
||||
_, err := store.ListPackageGroups(ctx, metadata.GroupListOptions{Severities: []string{"critical", "high"}})
|
||||
require.NoError(t, err)
|
||||
out[0] = counter.Count()
|
||||
counter.Reset()
|
||||
_, err = store.ListPackageGroups(ctx, metadata.GroupListOptions{Severities: []string{"low"}, Page: 1, PageSize: 25})
|
||||
require.NoError(t, err)
|
||||
out[1] = counter.Count()
|
||||
return out
|
||||
}
|
||||
seedScanned(t, store, 0, 10)
|
||||
small := measure()
|
||||
seedScanned(t, store, 10, 90)
|
||||
large := measure()
|
||||
t.Logf("statements for 10 rows %v, for 100 rows %v", small, large)
|
||||
require.Equal(t, small, large)
|
||||
require.LessOrEqual(t, large[0], 1, "the filter is a subquery of the one list read")
|
||||
require.LessOrEqual(t, large[1], 3)
|
||||
}
|
||||
|
||||
func TestSeverityFilter(t *testing.T) { checkSeverityFilter(t, newTimeSeriesStore(t)) }
|
||||
func TestSeverityFilterStatementsAreConstant(t *testing.T) {
|
||||
checkSeverityFilterStatementsAreConstant(t, newTimeSeriesStore(t))
|
||||
}
|
||||
@@ -332,11 +332,25 @@ type GroupListOptions struct {
|
||||
Registry string // empty = every registry
|
||||
Query string // case-insensitive substring of name, version or release version
|
||||
SortBy string // one of the GroupSort* keys; empty = name
|
||||
SortDesc bool
|
||||
Page int // 1-based
|
||||
PageSize int // 0 = every group, unpaginated
|
||||
// Severities keeps rows whose latest scan holds a vulnerability of any listed
|
||||
// severity (SeverityCritical and the like); empty = no severity filter.
|
||||
Severities []string
|
||||
Page int // 1-based
|
||||
PageSize int // 0 = every group, unpaginated
|
||||
SortDesc bool
|
||||
}
|
||||
|
||||
// Severity filter values of GroupListOptions.
|
||||
const (
|
||||
SeverityCritical = "critical"
|
||||
SeverityHigh = "high"
|
||||
SeverityModerate = "moderate"
|
||||
SeverityLow = "low"
|
||||
)
|
||||
|
||||
// Severities lists the severity filter values, most severe first.
|
||||
var Severities = []string{SeverityCritical, SeverityHigh, SeverityModerate, SeverityLow}
|
||||
|
||||
// GroupPage is one page of groups, flattened to rows.
|
||||
type GroupPage struct {
|
||||
// Packages holds every version row of each group on the page, in group
|
||||
|
||||
Reference in New Issue
Block a user