|
45 | 45 | issueRepo = flag.String("issue-repo", "github.com/golang/vulndb", "repo to create issues in")
|
46 | 46 | githubToken = flag.String("ghtoken", "", "GitHub access token (default: value of VULN_GITHUB_ACCESS_TOKEN)")
|
47 | 47 | 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") |
49 | 49 | updateIssue = flag.Bool("up", false, "for commit, create a CL that updates (doesn't fix) the tracking bug")
|
50 | 50 | indent = flag.Bool("indent", false, "for newcve, indent JSON output")
|
51 | 51 | 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
|
303 | 303 | parsed.ghsas = append(parsed.ghsas, sa.ID)
|
304 | 304 | }
|
305 | 305 | }
|
306 |
| - slices.Sort(parsed.ghsas) |
307 |
| - parsed.ghsas = slices.Compact(parsed.ghsas) |
| 306 | + parsed.ghsas = dedupeAndSort(parsed.ghsas) |
308 | 307 | }
|
309 | 308 |
|
310 | 309 | r, err = newReport(ctx, cfg, parsed)
|
@@ -452,13 +451,8 @@ func newReport(ctx context.Context, cfg *createCfg, parsed *parsedIssue) (*repor
|
452 | 451 |
|
453 | 452 | // Fill an any CVEs and GHSAs we found that may have been missed
|
454 | 453 | // 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...)) |
462 | 456 |
|
463 | 457 | return r, nil
|
464 | 458 | }
|
@@ -626,9 +620,12 @@ func fix(ctx context.Context, filename string, ghsaClient *ghsa.Client) (err err
|
626 | 620 | return err
|
627 | 621 | }
|
628 | 622 | }
|
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 | + } |
631 | 627 | }
|
| 628 | + |
632 | 629 | // Write unconditionally in order to format.
|
633 | 630 | if err := r.Write(filename); err != nil {
|
634 | 631 | return err
|
@@ -1067,27 +1064,24 @@ func setDates(filename string, dates map[string]gitrepo.Dates) (err error) {
|
1067 | 1064 | return r.Write(filename)
|
1068 | 1065 | }
|
1069 | 1066 |
|
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) |
1079 | 1078 | if err != nil {
|
1080 | 1079 | return err
|
1081 | 1080 | }
|
1082 | 1081 | for _, sa := range sas {
|
1083 |
| - m[sa.ID] = struct{}{} |
| 1082 | + ghsas = append(ghsas, sa.ID) |
1084 | 1083 | }
|
1085 | 1084 | }
|
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) |
1092 | 1086 | return nil
|
1093 | 1087 | }
|
0 commit comments