Skip to content

Commit 2c87650

Browse files
committed
cmd/vulnreport: add GHSAs by default in vulnreport fix
Change the default behavior of vulnreport fix to pull in all GHSAs for existing CVEs (this can be turned off via the flag "skip-ghsa"). Also change the behavior to append to the list of GHSAs instead of overwriting it. Change-Id: I1bd8363b4868121b8630e988eee4ed598f995c6d Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/466575 Run-TryBot: Tatiana Bradley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Tim King <[email protected]>
1 parent 546c8bf commit 2c87650

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

cmd/vulnreport/main.go

+22-28
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var (
4545
issueRepo = flag.String("issue-repo", "github.com/golang/vulndb", "repo to create issues in")
4646
githubToken = flag.String("ghtoken", "", "GitHub access token (default: value of VULN_GITHUB_ACCESS_TOKEN)")
4747
skipSymbols = flag.Bool("skip-symbols", false, "for lint and fix, don't load package for symbols checks")
48-
alwaysFixGHSA = flag.Bool("always-fix-ghsa", false, "for fix, always update GHSAs")
48+
skipGHSA = flag.Bool("skip-ghsa", false, "for fix, skip adding new GHSAs")
4949
updateIssue = flag.Bool("up", false, "for commit, create a CL that updates (doesn't fix) the tracking bug")
5050
indent = flag.Bool("indent", false, "for newcve, indent JSON output")
5151
closedOk = flag.Bool("closed-ok", false, "for create & create-excluded, allow closed issues to be created")
@@ -303,8 +303,7 @@ func createReport(ctx context.Context, cfg *createCfg, iss *issues.Issue) (r *re
303303
parsed.ghsas = append(parsed.ghsas, sa.ID)
304304
}
305305
}
306-
slices.Sort(parsed.ghsas)
307-
parsed.ghsas = slices.Compact(parsed.ghsas)
306+
parsed.ghsas = dedupeAndSort(parsed.ghsas)
308307
}
309308

310309
r, err = newReport(ctx, cfg, parsed)
@@ -452,13 +451,8 @@ func newReport(ctx context.Context, cfg *createCfg, parsed *parsedIssue) (*repor
452451

453452
// Fill an any CVEs and GHSAs we found that may have been missed
454453
// in report creation.
455-
r.CVEs = append(r.CVEs, parsed.cves...)
456-
slices.Sort(r.CVEs)
457-
r.CVEs = slices.Compact(r.CVEs)
458-
459-
r.GHSAs = append(r.GHSAs, parsed.ghsas...)
460-
slices.Sort(r.GHSAs)
461-
r.GHSAs = slices.Compact(r.GHSAs)
454+
r.CVEs = dedupeAndSort(append(r.CVEs, parsed.cves...))
455+
r.GHSAs = dedupeAndSort(append(r.GHSAs, parsed.ghsas...))
462456

463457
return r, nil
464458
}
@@ -626,9 +620,12 @@ func fix(ctx context.Context, filename string, ghsaClient *ghsa.Client) (err err
626620
return err
627621
}
628622
}
629-
if err := fixGHSAs(ctx, r, ghsaClient); err != nil {
630-
return err
623+
if !*skipGHSA {
624+
if err := addGHSAs(ctx, r, ghsaClient); err != nil {
625+
return err
626+
}
631627
}
628+
632629
// Write unconditionally in order to format.
633630
if err := r.Write(filename); err != nil {
634631
return err
@@ -1067,27 +1064,24 @@ func setDates(filename string, dates map[string]gitrepo.Dates) (err error) {
10671064
return r.Write(filename)
10681065
}
10691066

1070-
// fixGHSAs replaces r.GHSAs with a sorted list of GitHub Security
1071-
// Advisory IDs that correspond to the CVEs.
1072-
func fixGHSAs(ctx context.Context, r *report.Report, ghsaClient *ghsa.Client) error {
1073-
if len(r.GHSAs) > 0 && !*alwaysFixGHSA {
1074-
return nil
1075-
}
1076-
m := map[string]struct{}{}
1077-
for _, cid := range r.CVEs {
1078-
sas, err := ghsaClient.ListForCVE(ctx, cid)
1067+
func dedupeAndSort[T constraints.Ordered](s []T) []T {
1068+
s = slices.Clone(s)
1069+
slices.Sort(s)
1070+
return slices.Compact(s)
1071+
}
1072+
1073+
// addGHSAs adds any missing GHSAs that correspond to the CVEs in the report.
1074+
func addGHSAs(ctx context.Context, r *report.Report, ghsaClient *ghsa.Client) error {
1075+
ghsas := r.GHSAs
1076+
for _, cve := range r.GetCVEs() {
1077+
sas, err := ghsaClient.ListForCVE(ctx, cve)
10791078
if err != nil {
10801079
return err
10811080
}
10821081
for _, sa := range sas {
1083-
m[sa.ID] = struct{}{}
1082+
ghsas = append(ghsas, sa.ID)
10841083
}
10851084
}
1086-
var gids []string
1087-
for gid := range m {
1088-
gids = append(gids, gid)
1089-
}
1090-
sort.Strings(gids)
1091-
r.GHSAs = gids
1085+
r.GHSAs = dedupeAndSort(ghsas)
10921086
return nil
10931087
}

0 commit comments

Comments
 (0)