mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-25 06:35:21 +00:00
fix(scanner): honour CVE bypasses under block_on_severity LOW
The LOW policy counted every vulnerability, including bypassed ones, so an active bypass never unblocked a package. It now counts only the remaining vulnerabilities.
This commit is contained in:
@@ -0,0 +1,71 @@
|
|||||||
|
package scanner
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/lukaszraczylo/gohoarder/pkg/config"
|
||||||
|
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A vulnerability covered by an active bypass must not block under any block_on_severity.
|
||||||
|
func TestCheckVulnerabilities_BypassedCVENeverBlocks(t *testing.T) {
|
||||||
|
policies := []string{"CRITICAL", "HIGH", "MODERATE", "MEDIUM", "LOW", "none", ""}
|
||||||
|
severities := []string{"CRITICAL", "HIGH", "MODERATE", "MEDIUM", "LOW", "UNKNOWN"}
|
||||||
|
|
||||||
|
for _, policy := range policies {
|
||||||
|
for _, severity := range severities {
|
||||||
|
t.Run(policy+"/"+severity, func(t *testing.T) {
|
||||||
|
store := &stubBypassStore{
|
||||||
|
stubMetadataStore: &stubMetadataStore{},
|
||||||
|
bypasses: []*metadata.CVEBypass{
|
||||||
|
{ID: "b1", Type: metadata.BypassTypeCVE, Target: "CVE-1", Active: true, ExpiresAt: time.Now().Add(time.Hour)},
|
||||||
|
},
|
||||||
|
scan: &metadata.ScanResult{
|
||||||
|
ID: "r", Scanner: "stub", ScannedAt: time.Now(), Status: metadata.ScanStatusVulnerable,
|
||||||
|
Vulnerabilities: []metadata.Vulnerability{{ID: "CVE-1", Severity: severity}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
mgr, err := New(config.SecurityConfig{
|
||||||
|
Enabled: true,
|
||||||
|
BlockOnSeverity: policy,
|
||||||
|
BlockThresholds: config.VulnerabilityThresholds{Critical: -1, High: -1, Medium: -1, Low: -1},
|
||||||
|
}, store)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
blocked, reason, err := mgr.CheckVulnerabilities(context.Background(), "npm", "left-pad", "1.0.0")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.False(t, blocked, "bypassed CVE blocked: %s", reason)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// An unbypassed vulnerability at or above the policy severity still blocks.
|
||||||
|
func TestCheckVulnerabilities_UnbypassedCVEBlocksAtPolicy(t *testing.T) {
|
||||||
|
store := &stubBypassStore{
|
||||||
|
stubMetadataStore: &stubMetadataStore{},
|
||||||
|
bypasses: []*metadata.CVEBypass{
|
||||||
|
{ID: "b1", Type: metadata.BypassTypeCVE, Target: "CVE-1", Active: true, ExpiresAt: time.Now().Add(time.Hour)},
|
||||||
|
},
|
||||||
|
scan: &metadata.ScanResult{
|
||||||
|
ID: "r", Scanner: "stub", ScannedAt: time.Now(), Status: metadata.ScanStatusVulnerable,
|
||||||
|
Vulnerabilities: []metadata.Vulnerability{
|
||||||
|
{ID: "CVE-1", Severity: "LOW"},
|
||||||
|
{ID: "CVE-2", Severity: "LOW"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
mgr, err := New(config.SecurityConfig{
|
||||||
|
Enabled: true,
|
||||||
|
BlockOnSeverity: "LOW",
|
||||||
|
BlockThresholds: config.VulnerabilityThresholds{Critical: -1, High: -1, Medium: -1, Low: -1},
|
||||||
|
}, store)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
blocked, _, err := mgr.CheckVulnerabilities(context.Background(), "npm", "left-pad", "1.0.0")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, blocked)
|
||||||
|
}
|
||||||
@@ -624,6 +624,7 @@ func (m *Manager) CheckVulnerabilities(ctx context.Context, registry, packageNam
|
|||||||
|
|
||||||
// Count vulnerabilities by severity, excluding bypassed CVEs
|
// Count vulnerabilities by severity, excluding bypassed CVEs
|
||||||
severityCounts := make(map[string]int)
|
severityCounts := make(map[string]int)
|
||||||
|
remaining := 0
|
||||||
for _, vuln := range result.Vulnerabilities {
|
for _, vuln := range result.Vulnerabilities {
|
||||||
// Check if this CVE is bypassed
|
// Check if this CVE is bypassed
|
||||||
if bypass, ok := bypassedCVEs[strings.ToUpper(vuln.ID)]; ok {
|
if bypass, ok := bypassedCVEs[strings.ToUpper(vuln.ID)]; ok {
|
||||||
@@ -637,6 +638,7 @@ func (m *Manager) CheckVulnerabilities(ctx context.Context, registry, packageNam
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
severityCounts[metadata.NormalizeSeverity(vuln.Severity)]++
|
severityCounts[metadata.NormalizeSeverity(vuln.Severity)]++
|
||||||
|
remaining++
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check against thresholds
|
// Check against thresholds
|
||||||
@@ -687,7 +689,7 @@ func (m *Manager) CheckVulnerabilities(ctx context.Context, registry, packageNam
|
|||||||
return true, "Package has MODERATE, HIGH, or CRITICAL vulnerabilities", nil
|
return true, "Package has MODERATE, HIGH, or CRITICAL vulnerabilities", nil
|
||||||
}
|
}
|
||||||
case "LOW":
|
case "LOW":
|
||||||
if len(result.Vulnerabilities) > 0 {
|
if remaining > 0 {
|
||||||
return true, "Package has vulnerabilities", nil
|
return true, "Package has vulnerabilities", nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user