feat(scanner): match bypasses and merge findings across advisory aliases

Scanners report one advisory under different ids, such as a GHSA id from
OSV and the CVE id from Trivy. Findings now carry an aliases list.
The merge folds findings that share any id into one finding. A bypass
for any id of a finding now covers it. Stored findings without aliases
still load and match by their own id.
This commit is contained in:
2026-11-24 01:39:51 +00:00
parent 64793f9c7e
commit 43e075baf3
20 changed files with 617 additions and 66 deletions
+96
View File
@@ -0,0 +1,96 @@
package metadata
import (
"regexp"
"strings"
)
// advisoryIDPattern matches the advisory ids that scanners report for one issue.
var advisoryIDPattern = regexp.MustCompile(`(?i)\b(CVE-\d{4}-\d{4,}|GHSA(?:-[a-z0-9]{4}){3}|GO-\d{4}-\d{4,}|PYSEC-\d{4}-\d+)`)
// advisoryURLMarkers keep ExtractAdvisoryIDs to links that name the advisory itself,
// not links that merely mention another one.
var advisoryURLMarkers = []string{"/advisories/", "/vuln/detail/", "/vulnerability/", "/vuln/", "/cve/"}
// CanonicalAdvisoryID writes an advisory id in its usual case: CVE and GO ids in
// upper case, GHSA ids as "GHSA-" plus lower-case groups.
func CanonicalAdvisoryID(id string) string {
id = strings.TrimSpace(id)
if len(id) >= 5 && strings.EqualFold(id[:5], "GHSA-") {
return "GHSA-" + strings.ToLower(id[5:])
}
return strings.ToUpper(id)
}
// ExtractAdvisoryIDs returns the advisory ids named by advisory links, in order, once each.
func ExtractAdvisoryIDs(urls ...string) []string {
var out []string
seen := map[string]bool{}
for _, u := range urls {
lower := strings.ToLower(u)
named := false
for _, marker := range advisoryURLMarkers {
if strings.Contains(lower, marker) {
named = true
break
}
}
if !named {
continue
}
for _, id := range advisoryIDPattern.FindAllString(u, -1) {
id = CanonicalAdvisoryID(id)
if !seen[id] {
seen[id] = true
out = append(out, id)
}
}
}
return out
}
// NormalizeAliases returns the candidate ids that differ from id, once each (case-insensitive).
func NormalizeAliases(id string, candidates ...string) []string {
seen := map[string]bool{strings.ToUpper(strings.TrimSpace(id)): true}
var out []string
for _, c := range candidates {
c = CanonicalAdvisoryID(c)
if c == "" || seen[strings.ToUpper(c)] {
continue
}
seen[strings.ToUpper(c)] = true
out = append(out, c)
}
return out
}
// IDs returns the finding's id and its aliases, upper-cased, once each.
func (v Vulnerability) IDs() []string {
out := make([]string, 0, 1+len(v.Aliases))
seen := map[string]bool{}
for _, id := range append([]string{v.ID}, v.Aliases...) {
key := strings.ToUpper(strings.TrimSpace(id))
if key == "" || seen[key] {
continue
}
seen[key] = true
out = append(out, key)
}
return out
}
// IsCVE reports whether id is a CVE id.
func IsCVE(id string) bool {
return len(id) >= 4 && strings.EqualFold(id[:4], "CVE-")
}
// BypassFor returns the bypass that covers the finding under its id or any alias.
// The map is keyed by upper-cased target.
func BypassFor(bypassed map[string]*CVEBypass, v Vulnerability) (*CVEBypass, bool) {
for _, id := range v.IDs() {
if b, ok := bypassed[id]; ok {
return b, true
}
}
return nil, false
}
+52
View File
@@ -0,0 +1,52 @@
package metadata
import (
"slices"
"testing"
)
func TestCanonicalAdvisoryID(t *testing.T) {
for in, want := range map[string]string{
"cve-2021-44906": "CVE-2021-44906",
"GHSA-XXXX-Yyyy-ZZZZ": "GHSA-xxxx-yyyy-zzzz",
" go-2022-0001 ": "GO-2022-0001",
"": "",
"ghsa-phwq-j96m-2c2q": "GHSA-phwq-j96m-2c2q",
"PYSEC-2021-1": "PYSEC-2021-1",
"npm:1097": "NPM:1097",
} {
if got := CanonicalAdvisoryID(in); got != want {
t.Errorf("CanonicalAdvisoryID(%q) = %q, want %q", in, got, want)
}
}
}
func TestExtractAdvisoryIDsKeepsOnlyAdvisoryLinks(t *testing.T) {
got := ExtractAdvisoryIDs(
"https://github.com/advisories/GHSA-xvch-5gv4-984h",
"https://nvd.nist.gov/vuln/detail/CVE-2021-44906",
"https://github.com/minimistjs/minimist/issues/CVE-2020-9999-discussion",
"https://avd.aquasec.com/nvd/cve-2021-44906",
"https://github.com/advisories/GHSA-xvch-5gv4-984h",
)
want := []string{"GHSA-xvch-5gv4-984h", "CVE-2021-44906"}
if !slices.Equal(got, want) {
t.Errorf("ExtractAdvisoryIDs = %v, want %v", got, want)
}
}
func TestNormalizeAliasesDropsTheIDAndDuplicates(t *testing.T) {
got := NormalizeAliases("CVE-2021-44906", "cve-2021-44906", "GHSA-XVCH-5GV4-984H", "ghsa-xvch-5gv4-984h", "")
want := []string{"GHSA-xvch-5gv4-984h"}
if !slices.Equal(got, want) {
t.Errorf("NormalizeAliases = %v, want %v", got, want)
}
}
func TestVulnerabilityIDsAreUpperCasedOnce(t *testing.T) {
v := Vulnerability{ID: "ghsa-xvch-5gv4-984h", Aliases: []string{"CVE-2021-44906", "GHSA-XVCH-5GV4-984H"}}
want := []string{"GHSA-XVCH-5GV4-984H", "CVE-2021-44906"}
if got := v.IDs(); !slices.Equal(got, want) {
t.Errorf("IDs = %v, want %v", got, want)
}
}
+7
View File
@@ -399,6 +399,13 @@ func vulnerabilitiesFromDetails(details JSONBField) []metadata.Vulnerability {
}
}
}
if aliases, ok := vulnMap["aliases"].([]interface{}); ok {
for _, a := range aliases {
if aStr, ok := a.(string); ok {
vuln.Aliases = append(vuln.Aliases, aStr)
}
}
}
vulnerabilities = append(vulnerabilities, vuln)
}
return vulnerabilities
+1
View File
@@ -222,6 +222,7 @@ type Vulnerability struct {
References []string `json:"references"`
FixedIn string `json:"fixed_in"` // Version where fixed
DetectedBy []string `json:"detected_by,omitempty"` // List of scanners that detected this vulnerability
Aliases []string `json:"aliases,omitempty"` // Other ids of the same advisory; a CVE bypass matches any of them
}
// NormalizeSeverity normalizes severity names to standard values