|
| 1 | +package cvrf |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/xml" |
| 5 | + "log" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "golang.org/x/xerrors" |
| 9 | + |
| 10 | + "github.com/aquasecurity/vuln-list-update/utils" |
| 11 | +) |
| 12 | + |
| 13 | +// cveCvrfDoc is a minimal parse of SUSE cvrf-cve/* XML for CVSS score extraction. |
| 14 | +type cveCvrfDoc struct { |
| 15 | + XMLName xml.Name `xml:"cvrfdoc"` |
| 16 | + Vuln cveCvrfVuln `xml:"http://docs.oasis-open.org/csaf/ns/csaf-cvrf/v1.2/vuln Vulnerability"` |
| 17 | +} |
| 18 | + |
| 19 | +type cveCvrfVuln struct { |
| 20 | + CVSSScoreSets cveCvrfCVSSScoreSets `xml:"http://docs.oasis-open.org/csaf/ns/csaf-cvrf/v1.2/vuln CVSSScoreSets"` |
| 21 | +} |
| 22 | + |
| 23 | +type cveCvrfCVSSScoreSets struct { |
| 24 | + ScoreSetV2 []cveScoreSetV2 `xml:"http://docs.oasis-open.org/csaf/ns/csaf-cvrf/v1.2/vuln ScoreSetV2"` |
| 25 | + ScoreSetV3 []cveScoreSetV3 `xml:"http://docs.oasis-open.org/csaf/ns/csaf-cvrf/v1.2/vuln ScoreSetV3"` |
| 26 | +} |
| 27 | + |
| 28 | +type cveScoreSetV2 struct { |
| 29 | + BaseScoreV2 string `xml:"http://docs.oasis-open.org/csaf/ns/csaf-cvrf/v1.2/vuln BaseScoreV2"` |
| 30 | + VectorV2 string `xml:"http://docs.oasis-open.org/csaf/ns/csaf-cvrf/v1.2/vuln VectorV2"` |
| 31 | +} |
| 32 | + |
| 33 | +type cveScoreSetV3 struct { |
| 34 | + BaseScoreV3 string `xml:"http://docs.oasis-open.org/csaf/ns/csaf-cvrf/v1.2/vuln BaseScoreV3"` |
| 35 | + VectorV3 string `xml:"http://docs.oasis-open.org/csaf/ns/csaf-cvrf/v1.2/vuln VectorV3"` |
| 36 | +} |
| 37 | + |
| 38 | +func parseCVECvrfScoreSets(b []byte) ([]ScoreSet, error) { |
| 39 | + var doc cveCvrfDoc |
| 40 | + if err := xml.Unmarshal(b, &doc); err != nil { |
| 41 | + return nil, xerrors.Errorf("decode CVE CVRF: %w", err) |
| 42 | + } |
| 43 | + return scoreSetsFromCVE12(doc.Vuln.CVSSScoreSets), nil |
| 44 | +} |
| 45 | + |
| 46 | +func (c Config) mergeCVEDetailsFromCVEFeed(cv *Cvrf, cache map[string][]ScoreSet) { |
| 47 | + if c.CvrfCVEURL == "" { |
| 48 | + return |
| 49 | + } |
| 50 | + base := strings.TrimSuffix(c.CvrfCVEURL, "/") |
| 51 | + for i := range cv.Vulnerabilities { |
| 52 | + cveID := strings.TrimSpace(cv.Vulnerabilities[i].CVE) |
| 53 | + if cveID == "" { |
| 54 | + continue |
| 55 | + } |
| 56 | + if sets, ok := cache[cveID]; ok { |
| 57 | + if len(sets) > 0 { |
| 58 | + cv.Vulnerabilities[i].CVSSScoreSets = sets |
| 59 | + } |
| 60 | + continue |
| 61 | + } |
| 62 | + u := base + "/cvrf-" + cveID + ".xml" |
| 63 | + b, err := utils.FetchURL(u, "", c.Retry) |
| 64 | + if err != nil { |
| 65 | + log.Printf("CVE CVRF fetch skipped for %s: %v", cveID, err) |
| 66 | + cache[cveID] = nil |
| 67 | + continue |
| 68 | + } |
| 69 | + sets, err := parseCVECvrfScoreSets(b) |
| 70 | + if err != nil { |
| 71 | + log.Printf("CVE CVRF parse failed for %s: %v", cveID, err) |
| 72 | + cache[cveID] = nil |
| 73 | + continue |
| 74 | + } |
| 75 | + cache[cveID] = sets |
| 76 | + if len(sets) > 0 { |
| 77 | + cv.Vulnerabilities[i].CVSSScoreSets = sets |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func scoreSetsFromCVE12(cvss cveCvrfCVSSScoreSets) []ScoreSet { |
| 83 | + var out []ScoreSet |
| 84 | + for _, s := range cvss.ScoreSetV2 { |
| 85 | + if strings.TrimSpace(s.BaseScoreV2) == "" && strings.TrimSpace(s.VectorV2) == "" { |
| 86 | + continue |
| 87 | + } |
| 88 | + out = append(out, ScoreSet{BaseScore: s.BaseScoreV2, Vector: s.VectorV2}) |
| 89 | + } |
| 90 | + for _, s := range cvss.ScoreSetV3 { |
| 91 | + if strings.TrimSpace(s.BaseScoreV3) == "" && strings.TrimSpace(s.VectorV3) == "" { |
| 92 | + continue |
| 93 | + } |
| 94 | + out = append(out, ScoreSet{BaseScore: s.BaseScoreV3, Vector: s.VectorV3}) |
| 95 | + } |
| 96 | + return out |
| 97 | +} |
0 commit comments