forked from aquasecurity/vuln-list-update
/
ghsa.go
187 lines (164 loc) · 5.22 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
package ghsa
import (
"context"
"fmt"
"log"
"math"
"os"
"path/filepath"
"strings"
"time"
"github.com/huzai9527/vein-vuln-update/utils"
"github.com/cheggaaa/pb"
githubql "github.com/shurcooL/githubv4"
"github.com/shurcooL/graphql"
"github.com/spf13/afero"
"golang.org/x/xerrors"
)
// https://developer.github.com/v4/enum/securityadvisoryecosystem/
type SecurityAdvisoryEcosystem string
var (
Composer SecurityAdvisoryEcosystem = "COMPOSER"
Maven SecurityAdvisoryEcosystem = "MAVEN"
Npm SecurityAdvisoryEcosystem = "NPM"
Nuget SecurityAdvisoryEcosystem = "NUGET"
Pip SecurityAdvisoryEcosystem = "PIP"
Rubygems SecurityAdvisoryEcosystem = "RUBYGEMS"
Go SecurityAdvisoryEcosystem = "GO"
Rust SecurityAdvisoryEcosystem = "RUST"
Erlang SecurityAdvisoryEcosystem = "ERLANG"
Ecosystems = []SecurityAdvisoryEcosystem{Composer, Maven, Npm, Nuget, Pip, Rubygems, Go, Erlang, Rust}
wait = func(i int) time.Duration {
sleep := math.Pow(float64(i), 2) + float64(utils.RandInt()%10)
return time.Duration(sleep) * time.Second
}
)
const (
ghsaDir = "ghsa"
retry = 5
maxResponseSize = 100
)
type Config struct {
vulnListDir string
appFs afero.Fs
retry int
client GithubClient
}
type GithubClient interface {
Query(ctx context.Context, q interface{}, variables map[string]interface{}) error
}
func NewConfig(client GithubClient) Config {
return Config{
vulnListDir: utils.VulnListDir(),
appFs: afero.NewOsFs(),
retry: retry,
client: client,
}
}
func (c Config) Update() error {
log.Print("Fetching GitHub Security Advisory")
for _, ecosystem := range Ecosystems {
err := c.update(ecosystem)
if err != nil {
return xerrors.Errorf("failed to update github security advisory ,%s: %w", ecosystem, err)
}
}
return nil
}
func (c Config) update(ecosystem SecurityAdvisoryEcosystem) error {
log.Printf("Fetching GitHub Security Advisory: %s", ecosystem)
dir := filepath.Join(c.vulnListDir, ghsaDir, strings.ToLower(string(ecosystem)))
if err := os.RemoveAll(dir); err != nil {
return xerrors.Errorf("unable to remove github security advisory directory: %w", err)
}
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return xerrors.Errorf("failed to mkdir: %w", err)
}
ghsas, err := c.fetchGithubSecurityAdvisories(ecosystem)
if err != nil {
return xerrors.Errorf("failed to fetch github security advisory: %w", err)
}
ghsaJsonMap := make(map[string]GithubSecurityAdvisoryJson)
for _, ghsa := range ghsas {
// skip bad ghsa
if ghsa.Package.Name == "" {
continue
}
ghsa.Package.Name = strings.TrimSpace(ghsa.Package.Name)
ghsaJson, ok := ghsaJsonMap[ghsa.Advisory.GhsaId+ghsa.Package.Name]
if ok {
va := Version{
FirstPatchedVersion: ghsa.FirstPatchedVersion,
VulnerableVersionRange: ghsa.VulnerableVersionRange,
}
ghsaJson.Versions = append(ghsaJson.Versions, va)
ghsaJsonMap[ghsa.Advisory.GhsaId+ghsa.Package.Name] = ghsaJson
} else {
ghsaJsonMap[ghsa.Advisory.GhsaId+ghsa.Package.Name] = GithubSecurityAdvisoryJson{
Severity: ghsa.Severity,
UpdatedAt: ghsa.UpdatedAt,
Package: ghsa.Package,
Advisory: ghsa.Advisory,
Versions: []Version{
{
FirstPatchedVersion: ghsa.FirstPatchedVersion,
VulnerableVersionRange: ghsa.VulnerableVersionRange,
},
},
}
}
}
bar := pb.StartNew(len(ghsaJsonMap))
for _, ghsaJson := range ghsaJsonMap {
dir := filepath.Join(c.vulnListDir, ghsaDir, strings.ToLower(string(ecosystem)), strings.Replace(ghsaJson.Package.Name, ":", "/", -1))
err := c.saveGSHA(dir, ghsaJson.Advisory.GhsaId, ghsaJson)
if err != nil {
return xerrors.Errorf("failed to save github security advisory: %w", err)
}
bar.Increment()
}
bar.Finish()
return nil
}
func (c Config) fetchGithubSecurityAdvisories(ecosystem SecurityAdvisoryEcosystem) ([]GithubSecurityAdvisory, error) {
var getVulnerabilitiesQuery GetVulnerabilitiesQuery
var ghsas []GithubSecurityAdvisory
variables := map[string]interface{}{
"ecosystem": ecosystem,
"total": graphql.Int(maxResponseSize),
"cursor": (*githubql.String)(nil),
}
for {
var err error
for i := 0; i <= c.retry; i++ {
if i > 0 {
sleep := wait(i)
log.Printf("retry after %s", sleep)
time.Sleep(sleep)
}
err = c.client.Query(context.Background(), &getVulnerabilitiesQuery, variables)
if err == nil || len(getVulnerabilitiesQuery.Nodes) > 0 {
break
}
}
// GitHub GraphQL API may return error and one of nodes == nil
// We must write other nodes.
// Bad node will be skipped in 'update' function
if err != nil && len(getVulnerabilitiesQuery.Nodes) == 0 {
return nil, xerrors.Errorf("graphql api error: %w", err)
}
ghsas = append(ghsas, getVulnerabilitiesQuery.Nodes...)
if !getVulnerabilitiesQuery.PageInfo.HasNextPage {
break
}
variables["cursor"] = githubql.NewString(getVulnerabilitiesQuery.PageInfo.EndCursor)
}
return ghsas, nil
}
func (c Config) saveGSHA(dirName string, ghsaID string, data interface{}) error {
fileName := fmt.Sprintf("%s.json", ghsaID)
if err := utils.WriteJSON(c.appFs, dirName, fileName, data); err != nil {
return xerrors.Errorf("failed to write file: %w", err)
}
return nil
}