-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathnode.go
166 lines (143 loc) · 4.2 KB
/
node.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
package node
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
bolt "github.com/etcd-io/bbolt"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy-db/pkg/db"
"github.com/aquasecurity/trivy-db/pkg/types"
"github.com/aquasecurity/trivy-db/pkg/vulnsrc/vulnerability"
)
// https://github.com/nodejs/security-wg.git
const (
nodeDir = "nodejs-security-wg"
)
var (
repoPath string
)
type RawAdvisory struct {
ID int
Title string
ModuleName string `json:"module_name"`
Cves []string
VulnerableVersions string `json:"vulnerable_versions"`
PatchedVersions string `json:"patched_versions"`
Overview string
Recommendation string
References []string
CvssScoreNumber json.Number `json:"cvss_score"`
CvssScore float64
}
type Advisory struct {
VulnerabilityID string `json:",omitempty"`
VulnerableVersions string `json:",omitempty"`
PatchedVersions string `json:",omitempty"`
}
type VulnSrc struct {
dbc db.Operation
}
func NewVulnSrc() VulnSrc {
return VulnSrc{
dbc: db.Config{},
}
}
func (vs VulnSrc) Update(dir string) (err error) {
repoPath = filepath.Join(dir, nodeDir)
if err := vs.update(repoPath); err != nil {
return xerrors.Errorf("failed to update node vulnerabilities: %w", err)
}
return nil
}
func (vs VulnSrc) update(repoPath string) error {
root := filepath.Join(repoPath, "vuln")
err := vs.dbc.BatchUpdate(func(tx *bolt.Tx) error {
if err := vs.walk(tx, root); err != nil {
return xerrors.Errorf("failed to walk node advisories: %w", err)
}
return nil
})
if err != nil {
return xerrors.Errorf("batch update failed: %w", err)
}
return nil
}
func (vs VulnSrc) walk(tx *bolt.Tx, root string) error {
return filepath.Walk(filepath.Join(repoPath, "vuln"), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() || !strings.HasSuffix(info.Name(), ".json") {
return nil
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
advisory := RawAdvisory{}
if err = json.NewDecoder(f).Decode(&advisory); err != nil {
return err
}
// Node.js itself
if advisory.ModuleName == "" {
return nil
}
advisory.ModuleName = strings.ToLower(advisory.ModuleName)
// `cvss_score` returns float or string like "4.8 (MEDIUM)"
s := strings.Split(advisory.CvssScoreNumber.String(), " ")
advisory.CvssScore, err = strconv.ParseFloat(s[0], 64)
if err != nil {
advisory.CvssScore = -1
}
vulnerabilityIDs := advisory.Cves
if len(vulnerabilityIDs) == 0 {
vulnerabilityIDs = []string{fmt.Sprintf("NSWG-ECO-%d", advisory.ID)}
}
a := Advisory{
VulnerableVersions: advisory.VulnerableVersions,
PatchedVersions: advisory.PatchedVersions,
}
for _, vulnID := range vulnerabilityIDs {
// for detecting vulnerabilities
err = vs.dbc.PutAdvisory(tx, vulnerability.NodejsSecurityWg, advisory.ModuleName, vulnID, a)
if err != nil {
return xerrors.Errorf("failed to save node advisory: %w", err)
}
// for displaying vulnerability detail
vuln := types.VulnerabilityDetail{
ID: vulnID,
CvssScore: advisory.CvssScore,
References: advisory.References,
Title: advisory.Title,
Description: advisory.Overview,
}
if err = vs.dbc.PutVulnerabilityDetail(tx, vulnID, vulnerability.NodejsSecurityWg, vuln); err != nil {
return xerrors.Errorf("failed to save node vulnerability detail: %w", err)
}
if err := vs.dbc.PutSeverity(tx, vulnID, types.SeverityUnknown); err != nil {
return xerrors.Errorf("failed to save node vulnerability severity: %w", err)
}
}
return nil
})
}
func (vs VulnSrc) Get(pkgName string) ([]Advisory, error) {
advisories, err := vs.dbc.ForEachAdvisory(vulnerability.NodejsSecurityWg, pkgName)
if err != nil {
return nil, xerrors.Errorf("failed to iterate node vulnerabilities: %w", err)
}
var results []Advisory
for vulnID, a := range advisories {
var advisory Advisory
if err = json.Unmarshal(a, &advisory); err != nil {
return nil, xerrors.Errorf("failed to unmarshal advisory JSON: %w", err)
}
advisory.VulnerabilityID = vulnID
results = append(results, advisory)
}
return results, nil
}