Skip to content

Commit bde844c

Browse files
authored
Extract and add CWE IDs from CVE problem-types (google#4167)
Closes google#4159 Introduces the attachCWEs function to extract CWE IDs from both CNA and ADP problem-types in CVE5 records and store them in the Vulnerability's DatabaseSpecific field. Also updates the ProblemTypes struct to include a CWEID field for more accurate parsing. - Updated ProblemTypes struct to include `CweID` field to match CVE5 JSON schema. - Updated attachCWEs() function to store both CWE ID and human-readable description.
1 parent b9438ba commit bde844c

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

vulnfeeds/cvelist2osv/converter.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,36 @@ func extractConversionMetrics(cve cves.CVE5, refs []osvschema.Reference, metrics
7676
// TODO(jesslowe): Add more analysis based on ADP containers, CVSS, KEV, CWE, etc.
7777
}
7878

79+
// attachCWEs extracts and adds CWE IDs from the CVE5 problem-types
80+
func attachCWEs(v *vulns.Vulnerability, cna cves.CNA, metrics *ConversionMetrics) {
81+
var cwes []string
82+
83+
for _, pt := range cna.ProblemTypes {
84+
for _, desc := range pt.Descriptions {
85+
if desc.CWEID == "" {
86+
continue
87+
}
88+
cwes = append(cwes, desc.CWEID)
89+
}
90+
}
91+
if len(cwes) == 0 {
92+
return
93+
}
94+
95+
// Sort and remove duplicates
96+
slices.Sort(cwes)
97+
cwes = slices.Compact(cwes)
98+
99+
if v.DatabaseSpecific == nil {
100+
v.DatabaseSpecific = make(map[string]any)
101+
}
102+
103+
// Add CWEs to DatabaseSpecific for consistency with GHSA schema.
104+
v.DatabaseSpecific["cwe_ids"] = cwes
105+
106+
metrics.AddNote("Extracted CWEIDs: %v", cwes)
107+
}
108+
79109
// FromCVE5 creates a `vulns.Vulnerability` object from a `cves.CVE5` object.
80110
// It populates the main fields of the OSV record, including ID, summary, details,
81111
// references, timestamps, severity, and version information.
@@ -136,7 +166,8 @@ func FromCVE5(cve cves.CVE5, refs []cves.Reference, metrics *ConversionMetrics)
136166
}
137167
}
138168

139-
// TODO(jesslowe@): Add CWEs.
169+
// attachCWEs extract and adds the cwes from the CVE5 Problem-types
170+
attachCWEs(&v, cve.Containers.CNA, metrics)
140171

141172
return &v
142173
}

vulnfeeds/cvelist2osv/converter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func TestFromCVE5(t *testing.T) {
213213
Details: "An issue has been discovered in GitLab CE/EE affecting all versions from 18.0 before 18.0.1. In certain circumstances, a user with limited permissions could access Job Data via a crafted GraphQL query.",
214214
Aliases: nil,
215215
Related: nil,
216-
DatabaseSpecific: nil,
216+
DatabaseSpecific: map[string]any{"cwe_ids": []string{"CWE-1220"}},
217217
References: []osvschema.Reference{
218218
{Type: "ARTICLE", URL: "https://hackerone.com/reports/2972576"},
219219
{Type: "EVIDENCE", URL: "https://hackerone.com/reports/2972576"},
@@ -254,7 +254,7 @@ func TestFromCVE5(t *testing.T) {
254254
Score: "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H",
255255
},
256256
},
257-
DatabaseSpecific: nil,
257+
DatabaseSpecific: map[string]any{"cwe_ids": []string{"CWE-770"}},
258258
},
259259
},
260260
},

vulnfeeds/cves/cve.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const (
2626

2727
type ProblemTypes []struct {
2828
Descriptions []struct {
29+
CWEID string `json:"cweId,omitempty"`
2930
Type string `json:"type,omitempty"`
3031
Lang string `json:"lang,omitempty"`
3132
Description string `json:"description,omitempty"`

0 commit comments

Comments
 (0)