Skip to content

Commit

Permalink
internal/report: support related field in YAML
Browse files Browse the repository at this point in the history
Change-Id: If82eb77622e48d3b404d61020337fba0c112bc57
Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/541117
Reviewed-by: Damien Neil <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
tatianab committed Nov 9, 2023
1 parent e5850f6 commit f6eff24
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
29 changes: 29 additions & 0 deletions internal/report/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,34 @@ func (r *Report) lintCVEs(addIssue func(string)) {
}
}

func (r *Report) lintRelated(addIssue func(string)) {
if len(r.Related) == 0 {
return
}

aliases := r.Aliases()
for _, related := range r.Related {
// In most cases, the related list is very short, so there's no
// need create a map of aliases.
if slices.Contains(aliases, related) {
addIssue(fmt.Sprintf("related: identifier %s is also listed among aliases", related))
}
if !isIdentifier(related) {
addIssue(fmt.Sprintf("related: %s is not a recognized identifier (CVE, GHSA or Go ID)", related))
}
}
}

func isIdentifier(id string) bool {
return cveschema5.IsCVE(id) || ghsa.IsGHSA(id) || IsGoID(id)
}

var goIDregexp = regexp.MustCompile(`^GO-\d{4}-\d{4,}$`)

func IsGoID(s string) bool {
return goIDregexp.MatchString(s)
}

const maxLineLength = 80

func (r *Report) lintLineLength(field, content string, addIssue func(string)) {
Expand Down Expand Up @@ -410,6 +438,7 @@ func (r *Report) lint(pc *proxy.Client) []string {
r.lintLineLength("cve_metadata.description", r.CVEMetadata.Description, addIssue)
}
r.lintCVEs(addIssue)
r.lintRelated(addIssue)

if isFirstParty && !r.IsExcluded() {
r.lintStdLibLinks(addIssue)
Expand Down
17 changes: 17 additions & 0 deletions internal/report/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,23 @@ func TestLintOffline(t *testing.T) {
"excluded report must have at least one associated CVE or GHSA",
},
},
{
desc: "related field",
report: validReport(func(r *Report) {
r.CVEs = []string{"CVE-0000-1111"}
r.Related = []string{
"not-an-id", // bad
"CVE-0000-1111", // bad (duplicate)
"CVE-0000-1112", // ok
"GHSA-0000-0000-0000", // ok
"GO-1990-0001", // ok
}
}),
want: []string{
"not-an-id is not a recognized identifier",
"CVE-0000-1111 is also listed among aliases",
},
},
{
desc: "invalid module-version pair ignored",
report: validReport(func(r *Report) {
Expand Down
4 changes: 4 additions & 0 deletions internal/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ type Report struct {
// the above CVEs.
GHSAs []string `yaml:",omitempty"`

// Related is a list of identifiers (e.g. CVEs or GHSAs)
// that are related to, but are not direct aliases of, this report.
Related []string `yaml:",omitempty"`

Credits []string `yaml:",omitempty"`
References []*Reference `yaml:",omitempty"`

Expand Down

0 comments on commit f6eff24

Please sign in to comment.