Skip to content

Commit c50b373

Browse files
Prevent invalid empty Events from being added by AddPkgInfo() (#2298)
While investigating some schema violations, I noticed that #2280 was adding empty Event structs when there were multiple non-`introduced` events for a repo. Clean up some dead code that the static checker was complaining about.
1 parent adae4f6 commit c50b373

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

vulnfeeds/vulns/vulns.go

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"cmp"
1919
"encoding/json"
2020
"errors"
21-
"fmt"
2221
"io"
2322
"net/url"
2423
"os"
@@ -224,41 +223,43 @@ func (v *Vulnerability) AddPkgInfo(pkgInfo PackageInfo) {
224223
}
225224
}
226225

226+
// Aggregate commits by their repo, and synthesize a zero introduced commit if necessary.
227227
if len(pkgInfo.VersionInfo.AffectedCommits) > 0 {
228228
gitCommitRangesByRepo := map[string]AffectedRange{}
229229

230230
hasAddedZeroIntroduced := make(map[string]bool)
231231

232232
for _, ac := range pkgInfo.VersionInfo.AffectedCommits {
233233
entry, ok := gitCommitRangesByRepo[ac.Repo]
234+
// Create the stub for the repo if necessary.
234235
if !ok {
235236
entry = AffectedRange{
236237
Type: "GIT",
237238
Events: []Event{},
238239
Repo: ac.Repo,
239240
}
240-
}
241241

242-
if !pkgInfo.VersionInfo.HasIntroducedCommits(ac.Repo) && !hasAddedZeroIntroduced[ac.Repo] {
243-
// There was no explicitly defined introduced commit, so create one at 0
244-
entry.Events = append(entry.Events,
245-
Event{
246-
Introduced: "0",
247-
},
248-
)
249-
hasAddedZeroIntroduced[ac.Repo] = true
242+
if !pkgInfo.VersionInfo.HasIntroducedCommits(ac.Repo) && !hasAddedZeroIntroduced[ac.Repo] {
243+
// There was no explicitly defined introduced commit, so create one at 0.
244+
entry.Events = append(entry.Events,
245+
Event{
246+
Introduced: "0",
247+
},
248+
)
249+
hasAddedZeroIntroduced[ac.Repo] = true
250+
}
250251
}
251252

252-
if pkgInfo.VersionInfo.HasIntroducedCommits(ac.Repo) {
253+
if ac.Introduced != "" {
253254
entry.Events = append(entry.Events, Event{Introduced: ac.Introduced})
254255
}
255-
if pkgInfo.VersionInfo.HasFixedCommits(ac.Repo) {
256+
if ac.Fixed != "" {
256257
entry.Events = append(entry.Events, Event{Fixed: ac.Fixed})
257258
}
258-
if pkgInfo.VersionInfo.HasLastAffectedCommits(ac.Repo) {
259+
if ac.LastAffected != "" {
259260
entry.Events = append(entry.Events, Event{LastAffected: ac.LastAffected})
260261
}
261-
if pkgInfo.VersionInfo.HasLimitCommits(ac.Repo) {
262+
if ac.Limit != "" {
262263
entry.Events = append(entry.Events, Event{Limit: ac.Limit})
263264
}
264265
gitCommitRangesByRepo[ac.Repo] = entry
@@ -599,18 +600,9 @@ func FromCVE(id cves.CVEID, cve cves.CVE) (*Vulnerability, []string) {
599600
Details: cves.EnglishDescription(cve),
600601
Aliases: extractAliases(id, cve),
601602
}
602-
var err error
603603
var notes []string
604604
v.Published = cve.Published.Format(time.RFC3339)
605-
if err != nil {
606-
notes = append(notes, fmt.Sprintf("Failed to parse published date: %v\n", err))
607-
}
608-
609605
v.Modified = cve.LastModified.Format(time.RFC3339)
610-
if err != nil {
611-
notes = append(notes, fmt.Sprintf("Failed to parse modified date: %v\n", err))
612-
}
613-
614606
v.References = ClassifyReferences(cve.References)
615607
v.AddSeverity(cve.Metrics)
616608
return &v, notes

vulnfeeds/vulns/vulns_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,14 @@ func TestAddPkgInfo(t *testing.T) {
200200
Fixed: "dsafwefwfe370a9e65d68d62ef37345597e4100b0e87021dfb",
201201
Repo: "github.com/foo/bar",
202202
},
203+
{
204+
Fixed: "658fe213",
205+
Repo: "github.com/foo/bar",
206+
},
207+
{
208+
LastAffected: "0xdeadf00d",
209+
Repo: "github.com/foo/baz",
210+
},
203211
},
204212
},
205213
}
@@ -210,7 +218,7 @@ func TestAddPkgInfo(t *testing.T) {
210218
AffectedVersions: []cves.AffectedVersion{
211219
{
212220
Introduced: "1.0.0-1",
213-
Fixed: "1.2.3-4",
221+
Fixed: "1.2.3-4",
214222
},
215223
},
216224
},
@@ -220,7 +228,7 @@ func TestAddPkgInfo(t *testing.T) {
220228
vuln.AddPkgInfo(testPkgInfoCommits) // This will end up in vuln.Affected[2]
221229
vuln.AddPkgInfo(testPkgInfoHybrid) // This will end up in vuln.Affected[3]
222230
vuln.AddPkgInfo(testPkgInfoCommitsMultiple) // This will end up in vuln.Affected[4]
223-
vuln.AddPkgInfo(testPkgInfoEcoMultiple) // This will end up in vuln.Affected[5]
231+
vuln.AddPkgInfo(testPkgInfoEcoMultiple) // This will end up in vuln.Affected[5]
224232

225233
t.Logf("Resulting vuln: %+v", vuln)
226234

@@ -283,7 +291,7 @@ func TestAddPkgInfo(t *testing.T) {
283291
// testPkgInfoCommits ^^^^^^^^^^^^^^^
284292

285293
// testPkgInfoCommitsMultiple vvvvvvvvvvvvv
286-
if len(vuln.Affected[4].Ranges[0].Events) != 2 {
294+
if len(vuln.Affected[4].Ranges[0].Events) != 3 {
287295
t.Errorf("AddPkgInfo has not correctly added distinct range events from commits: %+v", vuln.Affected[4].Ranges)
288296
}
289297
// testPkgInfoCommitsMultiple ^^^^^^^^^^^^^
@@ -294,7 +302,6 @@ func TestAddPkgInfo(t *testing.T) {
294302
}
295303
// testPkgInfoEcoMultiple ^^^^^^^^^^^^^
296304

297-
298305
for _, a := range vuln.Affected {
299306
perRepoZeroIntroducedCommitHashCount := make(map[string]int)
300307
for _, r := range a.Ranges {
@@ -307,6 +314,9 @@ func TestAddPkgInfo(t *testing.T) {
307314
perRepoZeroIntroducedCommitHashCount[r.Repo]++
308315
}
309316
}
317+
if e == (Event{}) {
318+
t.Errorf("Empty event detected for the repo %s", r.Repo)
319+
}
310320
}
311321
}
312322
for repo, zeroIntroducedCommitHashCount := range perRepoZeroIntroducedCommitHashCount {

0 commit comments

Comments
 (0)