Skip to content

Commit c5eaaec

Browse files
tatianabTatiana Bradley
authored and
Tatiana Bradley
committed
internal/ghsa: change "ID" in a SecurityAdvisory to mean the GHSA ID
This is a safe change because it will cause future FireStore entries to be keyed by GHSA, but the worker does not depend on the keys being any particular format. Also makes some improvements to the GHSA tests. Change-Id: I6f12993ff2289e38584e46e2f4382e76f5ad639f Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/433135 Reviewed-by: Tatiana Bradley <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]>
1 parent 1a0e793 commit c5eaaec

File tree

2 files changed

+30
-38
lines changed

2 files changed

+30
-38
lines changed

internal/ghsa/ghsa.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
// A SecurityAdvisory represents a GitHub security advisory.
1818
type SecurityAdvisory struct {
19-
// The GitHub Security Advisory identifier
19+
// The GitHub Security Advisory identifier.
2020
ID string
2121
// A complete list of identifiers, e.g. CVE numbers.
2222
Identifiers []Identifier
@@ -84,7 +84,7 @@ func (s *SecurityAdvisory) PrettyID() string {
8484
// GitHub's GraphQL schema. The fields must be exported to be populated by
8585
// Github's Client.Query function.
8686
type gqlSecurityAdvisory struct {
87-
ID string
87+
GhsaID string
8888
Identifiers []Identifier
8989
Summary string
9090
Description string
@@ -114,13 +114,13 @@ type gqlSecurityAdvisory struct {
114114
// there are more than 100 vulnerabilities associated with the advisory.
115115
func (sa *gqlSecurityAdvisory) securityAdvisory() (*SecurityAdvisory, error) {
116116
if sa.PublishedAt.After(sa.UpdatedAt) {
117-
return nil, fmt.Errorf("%s: published at %s, after updated at %s", sa.ID, sa.PublishedAt, sa.UpdatedAt)
117+
return nil, fmt.Errorf("%s: published at %s, after updated at %s", sa.GhsaID, sa.PublishedAt, sa.UpdatedAt)
118118
}
119119
if sa.Vulnerabilities.PageInfo.HasNextPage {
120-
return nil, fmt.Errorf("%s has more than 100 vulns", sa.ID)
120+
return nil, fmt.Errorf("%s has more than 100 vulns", sa.GhsaID)
121121
}
122122
s := &SecurityAdvisory{
123-
ID: sa.ID,
123+
ID: sa.GhsaID,
124124
Identifiers: sa.Identifiers,
125125
Summary: sa.Summary,
126126
Description: sa.Description,

internal/ghsa/ghsa_test.go

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,29 @@ import (
1515

1616
var githubTokenFile = flag.String("ghtokenfile", "",
1717
"path to file containing GitHub access token")
18+
var githubToken = flag.String("ghtoken", os.Getenv("VULN_GITHUB_ACCESS_TOKEN"), "GitHub access token")
1819

1920
func mustGetAccessToken(t *testing.T) string {
20-
if *githubTokenFile == "" {
21-
t.Skip("-ghtokenfile not provided")
22-
}
23-
bytes, err := os.ReadFile(*githubTokenFile)
24-
if err != nil {
25-
t.Fatal(err)
21+
var token string
22+
switch {
23+
case *githubToken != "":
24+
token = *githubToken
25+
case *githubTokenFile != "":
26+
bytes, err := os.ReadFile(*githubTokenFile)
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
token = string(bytes)
31+
default:
32+
t.Skip("neither -ghtokenfile nor -ghtoken provided")
2633
}
27-
return strings.TrimSpace(string(bytes))
34+
return strings.TrimSpace(string(token))
2835
}
2936

3037
func TestList(t *testing.T) {
3138
accessToken := mustGetAccessToken(t)
3239
// There were at least three relevant SAs since this date.
33-
since := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
40+
since := time.Date(2022, 9, 1, 0, 0, 0, 0, time.UTC)
3441
got, err := List(context.Background(), accessToken, since)
3542
if err != nil {
3643
t.Fatal(err)
@@ -39,11 +46,6 @@ func TestList(t *testing.T) {
3946
if len(got) < want {
4047
t.Errorf("got %d, want at least %d", len(got), want)
4148
}
42-
for _, g := range got {
43-
if isCVE(g.Identifiers) {
44-
t.Errorf("isCVE true, want false for %+v", g)
45-
}
46-
}
4749
}
4850

4951
func TestFetchGHSA(t *testing.T) {
@@ -54,15 +56,7 @@ func TestFetchGHSA(t *testing.T) {
5456
if err != nil {
5557
t.Fatal(err)
5658
}
57-
want := ghsaID
58-
var gotID string
59-
for _, id := range got.Identifiers {
60-
if id.Type == "GHSA" {
61-
gotID = id.Value
62-
break
63-
}
64-
}
65-
if gotID != want {
59+
if gotID, want := got.ID, ghsaID; gotID != want {
6660
t.Errorf("got GHSA with id %q, want %q", got.ID, want)
6761
}
6862
}
@@ -78,17 +72,15 @@ func TestListForCVE(t *testing.T) {
7872
if err != nil {
7973
t.Fatal(err)
8074
}
81-
var ids []string
82-
for _, sa := range got {
83-
for _, id := range sa.Identifiers {
84-
if id.Type != "GHSA" {
85-
continue
86-
}
87-
ids = append(ids, id.Value)
88-
if id.Value == ghsaID {
89-
return
90-
}
75+
76+
want := ghsaID
77+
if len(got) != 1 {
78+
var gotIDs []string
79+
for _, sa := range got {
80+
gotIDs = append(gotIDs, sa.ID)
9181
}
82+
t.Errorf("got %v GHSAs %v, want %v", len(got), gotIDs, want)
83+
} else if gotID := got[0].ID; gotID != want {
84+
t.Errorf("got GHSA %v, want %v", gotID, want)
9285
}
93-
t.Errorf("got %v GHSAs %v, want %v", len(got), ids, ghsaID)
9486
}

0 commit comments

Comments
 (0)