forked from quay/clair
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodejs.go
153 lines (130 loc) · 4.68 KB
/
nodejs.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
// Copyright 2016 clair authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nodejs
import (
"encoding/json"
"net/http"
"strings"
"github.com/coreos/clair/database"
"github.com/coreos/clair/updater"
cerrors "github.com/coreos/clair/utils/errors"
"github.com/coreos/clair/utils/types"
"github.com/coreos/pkg/capnslog"
)
const (
url = "https://api.nodesecurity.io/advisories"
cveURLPrefix = "http://cve.mitre.org/cgi-bin/cvename.cgi?name="
updaterFlag = "nodejsUpdater"
defaultNodejsVersion = "all"
)
var log = capnslog.NewPackageLogger("github.com/coreos/clair", "updater/fetchers/nodejs")
type nodejsAdvisory struct {
ID int `json:"id"`
UpdatedAt string `json:"updated_at"`
ModuleName string `json:"module_name"`
CVES []string `json:"cves"`
PatchedVersions string `json:"patched_versions"`
Overview string `json:"overview"`
CvssScore float32 `json:"cvss_score"`
}
type nodejsAdvisories struct {
Total int `json:"total"`
Count int `json:"count"`
Offset int `json:"offset"`
Results []nodejsAdvisory `json:"results"`
}
// NodejsFetcher implements updater.Fetcher for the Node Security Project
// (https://nodesecurity.io).
type NodejsFetcher struct{}
func init() {
updater.RegisterFetcher("nodejs", &NodejsFetcher{})
}
// FetchUpdate fetches vulnerability updates from the Node Security Project.
func (fetcher *NodejsFetcher) FetchUpdate(datastore database.Datastore) (resp updater.FetcherResponse, err error) {
log.Info("fetching Nodejs vulnerabilities")
// Download JSON.
r, err := http.Get(url)
if err != nil {
log.Errorf("could not download Nodejs's update: %s", err)
return resp, cerrors.ErrCouldNotDownload
}
defer r.Body.Close()
// Get the latest date of the latest update's JSON data
latestUpdate, err := datastore.GetKeyValue(updaterFlag)
if err != nil {
return resp, err
}
// Unmarshal JSON.
var advisories nodejsAdvisories
if err = json.NewDecoder(r.Body).Decode(&advisories); err != nil {
log.Errorf("could not unmarshal Nodejs's JSON: %s", err)
return resp, cerrors.ErrCouldNotParse
}
resp.Vulnerabilities, resp.FlagValue = parseNodejsAdvisories(advisories.Results, latestUpdate)
resp.FlagName = updaterFlag
return resp, nil
}
func parseNodejsAdvisories(advisories []nodejsAdvisory, latestUpdate string) (vulnerabilities []database.Vulnerability, newUpdated string) {
mvulnerabilities := make(map[string]*database.Vulnerability)
for _, advisory := range advisories {
if latestUpdate >= advisory.UpdatedAt {
break
}
if advisory.UpdatedAt > newUpdated {
newUpdated = advisory.UpdatedAt
}
for _, vulnName := range advisory.CVES {
// Get or create the vulnerability.
vulnerability, vulnerabilityAlreadyExists := mvulnerabilities[vulnName]
if !vulnerabilityAlreadyExists {
vulnerability = &database.Vulnerability{
Name: vulnName,
Link: cveURLPrefix + strings.TrimLeft(vulnName, "CVE-"),
Severity: types.Unknown,
Description: advisory.Overview,
}
}
// Set the priority of the vulnerability.
// A vulnerability has one urgency per advisory it affects.
// The highest urgency should be the one set.
if urgency := types.ScoreToPriority(advisory.CvssScore); urgency.Compare(vulnerability.Severity) > 0 {
vulnerability.Severity = urgency
}
// Create and add the feature version.
pkg := database.FeatureVersion{
Feature: database.Feature{
Name: advisory.ModuleName,
Namespace: database.Namespace{
Name: "nodejs:" + defaultNodejsVersion,
},
},
}
if fivs, err := types.NewFixedInVersions(advisory.PatchedVersions); err == nil {
pkg.FixedInVersions = fivs
} else {
log.Warningf("could not parse nodejs patched version: '%s'.", err)
}
vulnerability.FixedIn = append(vulnerability.FixedIn, pkg)
// Store the vulnerability.
mvulnerabilities[vulnName] = vulnerability
}
}
// Convert the vulnerabilities map to a slice
for _, v := range mvulnerabilities {
vulnerabilities = append(vulnerabilities, *v)
}
return
}
// Clean deletes any allocated resources.
func (fetcher *NodejsFetcher) Clean() {}