|
1
|
|
|
/* Vuls - Vulnerability Scanner |
|
2
|
|
|
Copyright (C) 2016 Future Corporation , Japan. |
|
3
|
|
|
|
|
4
|
|
|
This program is free software: you can redistribute it and/or modify |
|
5
|
|
|
it under the terms of the GNU General Public License as published by |
|
6
|
|
|
the Free Software Foundation, either version 3 of the License, or |
|
7
|
|
|
(at your option) any later version. |
|
8
|
|
|
|
|
9
|
|
|
This program is distributed in the hope that it will be useful, |
|
10
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12
|
|
|
GNU General Public License for more details. |
|
13
|
|
|
|
|
14
|
|
|
You should have received a copy of the GNU General Public License |
|
15
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
package github |
|
19
|
|
|
|
|
20
|
|
|
import ( |
|
21
|
|
|
"bytes" |
|
22
|
|
|
"context" |
|
23
|
|
|
"encoding/json" |
|
24
|
|
|
"fmt" |
|
25
|
|
|
"io/ioutil" |
|
26
|
|
|
"net/http" |
|
27
|
|
|
"time" |
|
28
|
|
|
|
|
29
|
|
|
"github.com/future-architect/vuls/config" |
|
30
|
|
|
"github.com/future-architect/vuls/models" |
|
31
|
|
|
"github.com/future-architect/vuls/util" |
|
32
|
|
|
"github.com/k0kubun/pp" |
|
33
|
|
|
"golang.org/x/oauth2" |
|
34
|
|
|
) |
|
35
|
|
|
|
|
36
|
|
|
// FillGitHubSecurityAlerts access to owner/repo on GitHub and fetch scurity alerts of the repository via GitHub API v4 GraphQL and then set to the given ScanResult. |
|
37
|
|
|
// https://help.github.com/articles/about-security-alerts-for-vulnerable-dependencies/ |
|
38
|
|
|
func FillGitHubSecurityAlerts(r *models.ScanResult, owner, repo, token string) (nCVEs int, err error) { |
|
39
|
|
|
src := oauth2.StaticTokenSource( |
|
40
|
|
|
&oauth2.Token{AccessToken: token}, |
|
41
|
|
|
) |
|
42
|
|
|
httpClient := oauth2.NewClient(context.Background(), src) |
|
43
|
|
|
|
|
44
|
|
|
// TODO Use `https://github.com/shurcooL/githubv4` if the tool supports vulnerabilityAlerts Endpoint |
|
45
|
|
|
const jsonfmt = `{"query": |
|
46
|
|
|
"query { repository(owner:\"%s\", name:\"%s\") { url, vulnerabilityAlerts(first: %d, %s) { pageInfo{ endCursor, hasNextPage, startCursor}, edges { node { id, externalIdentifier, externalReference, fixedIn, packageName, dismissReason, dismissedAt } } } } }"}` |
|
47
|
|
|
after := "" |
|
48
|
|
|
|
|
49
|
|
|
for { |
|
50
|
|
|
jsonStr := fmt.Sprintf(jsonfmt, owner, repo, 100, after) |
|
51
|
|
|
req, err := http.NewRequest("POST", |
|
52
|
|
|
"https://api.github.com/graphql", |
|
53
|
|
|
bytes.NewBuffer([]byte(jsonStr)), |
|
54
|
|
|
) |
|
55
|
|
|
if err != nil { |
|
56
|
|
|
return 0, err |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// https://developer.github.com/v4/previews/#repository-vulnerability-alerts |
|
60
|
|
|
// To toggle this preview and access data, need to provide a custom media type in the Accept header: |
|
61
|
|
|
// MEMO: I tried to get the affected version via GitHub API. Bit it seems difficult to determin the affected version if there are multiple dependency files such as package.json. |
|
62
|
|
|
// TODO remove this header if it is no longer preview status in the future. |
|
63
|
|
|
req.Header.Set("Accept", "application/vnd.github.vixen-preview+json") |
|
64
|
|
|
req.Header.Set("Content-Type", "application/json") |
|
65
|
|
|
|
|
66
|
|
|
resp, err := httpClient.Do(req) |
|
67
|
|
|
if err != nil { |
|
68
|
|
|
return 0, err |
|
69
|
|
|
} |
|
70
|
|
|
defer resp.Body.Close() |
|
71
|
|
|
alerts := SecurityAlerts{} |
|
72
|
|
|
if json.NewDecoder(resp.Body).Decode(&alerts); err != nil { |
|
73
|
|
|
return 0, err |
|
74
|
|
|
} |
|
75
|
|
|
if err != nil { |
|
76
|
|
|
return 0, err |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
alerts := SecurityAlerts{} |
|
80
|
|
|
if err = json.Unmarshal(bodyBytes, &alerts); err != nil { |
|
81
|
|
|
return 0, err |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
util.Log.Debugf("%s", pp.Sprint(alerts)) |
|
85
|
|
|
|
|
86
|
|
|
for _, v := range alerts.Data.Repository.VulnerabilityAlerts.Edges { |
|
87
|
|
|
if config.Conf.IgnoreGitHubDismissed && v.Node.DismissReason != "" { |
|
88
|
|
|
continue |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
pkgName := fmt.Sprintf("%s %s", |
|
92
|
|
|
alerts.Data.Repository.URL, v.Node.PackageName) |
|
93
|
|
|
|
|
94
|
|
|
m := models.GitHubSecurityAlert{ |
|
95
|
|
|
PackageName: pkgName, |
|
96
|
|
|
FixedIn: v.Node.FixedIn, |
|
97
|
|
|
AffectedRange: v.Node.AffectedRange, |
|
98
|
|
|
Dismissed: len(v.Node.DismissReason) != 0, |
|
99
|
|
|
DismissedAt: v.Node.DismissedAt, |
|
100
|
|
|
DismissReason: v.Node.DismissReason, |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
cveID := v.Node.ExternalIdentifier |
|
104
|
|
|
|
|
105
|
|
|
if val, ok := r.ScannedCves[cveID]; ok { |
|
106
|
|
|
val.GitHubSecurityAlerts = val.GitHubSecurityAlerts.Add(m) |
|
107
|
|
|
r.ScannedCves[cveID] = val |
|
108
|
|
|
nCVEs++ |
|
109
|
|
|
} else { |
|
110
|
|
|
v := models.VulnInfo{ |
|
111
|
|
|
CveID: cveID, |
|
112
|
|
|
Confidences: models.Confidences{models.GitHubMatch}, |
|
113
|
|
|
GitHubSecurityAlerts: models.GitHubSecurityAlerts{m}, |
|
114
|
|
|
} |
|
115
|
|
|
r.ScannedCves[cveID] = v |
|
116
|
|
|
nCVEs++ |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
if !alerts.Data.Repository.VulnerabilityAlerts.PageInfo.HasNextPage { |
|
120
|
|
|
break |
|
121
|
|
|
} |
|
122
|
|
|
after = fmt.Sprintf(`after: \"%s\"`, alerts.Data.Repository.VulnerabilityAlerts.PageInfo.EndCursor) |
|
123
|
|
|
} |
|
124
|
|
|
return nCVEs, err |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
//SecurityAlerts has detected CVE-IDs, PackageNames, Refs |
|
128
|
|
|
type SecurityAlerts struct { |
|
129
|
|
|
Data struct { |
|
130
|
|
|
Repository struct { |
|
131
|
|
|
URL string `json:"url,omitempty"` |
|
132
|
|
|
VulnerabilityAlerts struct { |
|
133
|
|
|
PageInfo struct { |
|
134
|
|
|
EndCursor string `json:"endCursor,omitempty"` |
|
135
|
|
|
HasNextPage bool `json:"hasNextPage,omitempty"` |
|
136
|
|
|
StartCursor string `json:"startCursor,omitempty"` |
|
137
|
|
|
} `json:"pageInfo,omitempty"` |
|
138
|
|
|
Edges []struct { |
|
139
|
|
|
Node struct { |
|
140
|
|
|
ID string `json:"id,omitempty"` |
|
141
|
|
|
ExternalIdentifier string `json:"externalIdentifier,omitempty"` |
|
142
|
|
|
ExternalReference string `json:"externalReference,omitempty"` |
|
143
|
|
|
FixedIn string `json:"fixedIn,omitempty"` |
|
144
|
|
|
AffectedRange string `json:"affectedRange,omitempty"` |
|
145
|
|
|
PackageName string `json:"packageName,omitempty"` |
|
146
|
|
|
DismissReason string `json:"dismissReason,omitempty"` |
|
147
|
|
|
DismissedAt time.Time `json:"dismissedAt,omitempty"` |
|
148
|
|
|
} `json:"node,omitempty"` |
|
149
|
|
|
} `json:"edges,omitempty"` |
|
150
|
|
|
} `json:"vulnerabilityAlerts,omitempty"` |
|
151
|
|
|
} `json:"repository,omitempty"` |
|
152
|
|
|
} `json:"data,omitempty"` |
|
153
|
|
|
} |
|
154
|
|
|
|