forked from golang/vulndb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghsa.go
242 lines (222 loc) · 6.74 KB
/
ghsa.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package ghsa supports GitHub security advisories.
package ghsa
import (
"context"
"fmt"
"time"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
// A SecurityAdvisory represents a GitHub security advisory.
type SecurityAdvisory struct {
// The GitHub Security Advisory identifier.
ID string
// A complete list of identifiers, e.g. CVE numbers.
Identifiers []Identifier
// A short description of the advisory.
Summary string
// A full description of the advisory.
Description string
// Where the advisory came from.
Origin string
// A link to a page for the advisory.
Permalink string
// When the advisory was first published.
PublishedAt time.Time
// When the advisory was last updated; should always be >= PublishedAt.
UpdatedAt time.Time
// The vulnerabilities associated with this advisory.
Vulns []*Vuln
}
// An Identifier identifies an advisory according to some scheme or
// organization, given by the Type field. Example types are GHSA and CVE.
type Identifier struct {
Type string
Value string
}
// A Vuln represents a vulnerability.
type Vuln struct {
// The vulnerable Go package or module.
Package string
// The severity of the vulnerability.
Severity githubv4.SecurityAdvisorySeverity
// The earliest fixed version.
EarliestFixedVersion string
// A string representing the range of vulnerable versions.
// E.g. ">= 1.0.3"
VulnerableVersionRange string
// When the vulnerability was last updated.
UpdatedAt time.Time
}
// A gqlSecurityAdvisory represents a GitHub security advisory structured for
// GitHub's GraphQL schema. The fields must be exported to be populated by
// Github's Client.Query function.
type gqlSecurityAdvisory struct {
GhsaID string
Identifiers []Identifier
Summary string
Description string
Origin string
Permalink githubv4.URI
PublishedAt time.Time
UpdatedAt time.Time
Vulnerabilities struct {
Nodes []struct {
Package struct {
Name string
Ecosystem string
}
FirstPatchedVersion struct{ Identifier string }
Severity githubv4.SecurityAdvisorySeverity
UpdatedAt time.Time
VulnerableVersionRange string
}
PageInfo struct {
HasNextPage bool
}
} `graphql:"vulnerabilities(first: 100, ecosystem: $go)"` // include only Go vulns
}
// securityAdvisory converts a gqlSecurityAdvisory into a SecurityAdvisory.
// Errors if the security advisory was updated before it was published, or if
// there are more than 100 vulnerabilities associated with the advisory.
func (sa *gqlSecurityAdvisory) securityAdvisory() (*SecurityAdvisory, error) {
if sa.PublishedAt.After(sa.UpdatedAt) {
return nil, fmt.Errorf("%s: published at %s, after updated at %s", sa.GhsaID, sa.PublishedAt, sa.UpdatedAt)
}
if sa.Vulnerabilities.PageInfo.HasNextPage {
return nil, fmt.Errorf("%s has more than 100 vulns", sa.GhsaID)
}
s := &SecurityAdvisory{
ID: sa.GhsaID,
Identifiers: sa.Identifiers,
Summary: sa.Summary,
Description: sa.Description,
Origin: sa.Origin,
Permalink: sa.Permalink.URL.String(),
PublishedAt: sa.PublishedAt,
UpdatedAt: sa.UpdatedAt,
}
for _, v := range sa.Vulnerabilities.Nodes {
s.Vulns = append(s.Vulns, &Vuln{
Package: v.Package.Name,
Severity: v.Severity,
EarliestFixedVersion: v.FirstPatchedVersion.Identifier,
VulnerableVersionRange: v.VulnerableVersionRange,
UpdatedAt: v.UpdatedAt,
})
}
return s, nil
}
func newGitHubClient(ctx context.Context, accessToken string) *githubv4.Client {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken})
tc := oauth2.NewClient(ctx, ts)
return githubv4.NewClient(tc)
}
// List returns all SecurityAdvisories that affect Go,
// published or updated since the given time.
func List(ctx context.Context, accessToken string, since time.Time) ([]*SecurityAdvisory, error) {
client := newGitHubClient(ctx, accessToken)
var query struct { // the GraphQL query
SAs struct {
Nodes []gqlSecurityAdvisory
PageInfo struct {
EndCursor githubv4.String
HasNextPage bool
}
} `graphql:"securityAdvisories(updatedSince: $since, first: 100, after: $cursor)"`
}
vars := map[string]any{
"cursor": (*githubv4.String)(nil),
"go": githubv4.SecurityAdvisoryEcosystemGo,
"since": githubv4.DateTime{Time: since},
}
var sas []*SecurityAdvisory
// We need a loop to page through the list. The GitHub API limits us to 100
// values per call.
for {
if err := client.Query(ctx, &query, vars); err != nil {
return nil, err
}
for _, sa := range query.SAs.Nodes {
if len(sa.Vulnerabilities.Nodes) == 0 {
continue
}
s, err := sa.securityAdvisory()
if err != nil {
return nil, err
}
sas = append(sas, s)
}
if !query.SAs.PageInfo.HasNextPage {
break
}
vars["cursor"] = githubv4.NewString(query.SAs.PageInfo.EndCursor)
}
return sas, nil
}
func ListForCVE(ctx context.Context, accessToken string, cve string) ([]*SecurityAdvisory, error) {
client := newGitHubClient(ctx, accessToken)
var query struct { // The GraphQL query
SAs struct {
Nodes []gqlSecurityAdvisory
PageInfo struct {
EndCursor githubv4.String
HasNextPage bool
}
} `graphql:"securityAdvisories(identifier: $id, first: 100)"`
}
vars := map[string]any{
"id": githubv4.SecurityAdvisoryIdentifierFilter{
Type: githubv4.SecurityAdvisoryIdentifierTypeCve,
Value: githubv4.String(cve),
},
"go": githubv4.SecurityAdvisoryEcosystemGo,
}
if err := client.Query(ctx, &query, vars); err != nil {
return nil, err
}
if query.SAs.PageInfo.HasNextPage {
return nil, fmt.Errorf("CVE %s has more than 100 GHSAs", cve)
}
var sas []*SecurityAdvisory
for _, sa := range query.SAs.Nodes {
if len(sa.Vulnerabilities.Nodes) == 0 {
continue
}
exactMatch := false
for _, id := range sa.Identifiers {
if id.Type == "CVE" && id.Value == cve {
exactMatch = true
continue
}
}
if !exactMatch {
continue
}
s, err := sa.securityAdvisory()
if err != nil {
return nil, err
}
sas = append(sas, s)
}
return sas, nil
}
// FetchGHSA returns the SecurityAdvisory for the given Github Security
// Advisory ID.
func FetchGHSA(ctx context.Context, accessToken, ghsaID string) (_ *SecurityAdvisory, err error) {
client := newGitHubClient(ctx, accessToken)
var query struct {
SA gqlSecurityAdvisory `graphql:"securityAdvisory(ghsaId: $id)"`
}
vars := map[string]any{
"id": githubv4.String(ghsaID),
"go": githubv4.SecurityAdvisoryEcosystemGo,
}
if err := client.Query(ctx, &query, vars); err != nil {
return nil, err
}
return query.SA.securityAdvisory()
}