-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathjavascript.go
249 lines (204 loc) · 6.18 KB
/
javascript.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
243
244
245
246
247
248
249
package insider
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/insidersec/insider/report"
)
type JavaScriptAnalyzer struct {
logger *log.Logger
npm NPM
}
func NewJavaScriptAnalyzer(npm NPM, logger *log.Logger) JavaScriptAnalyzer {
return JavaScriptAnalyzer{
logger: logger,
npm: npm,
}
}
func (js JavaScriptAnalyzer) Analyze(ctx context.Context, dir string) (report.Reporter, error) {
var r report.Report
js.logger.Println("Analysing JavaScript dependencies")
if err := js.analyzeDependencies(ctx, &r, dir); err != nil {
return report.Report{}, err
}
return r, nil
}
func (js JavaScriptAnalyzer) analyzeDependencies(ctx context.Context, r *report.Report, dir string) error {
pkgJSON, err := js.findPackageJSON(dir)
if err != nil {
if !os.IsNotExist(err) {
return err
}
js.logger.Printf("Not found package.json at %s\n", dir)
return nil
}
libraries := make([]report.Library, 0, len(pkgJSON.Dependencies))
for dependency, version := range pkgJSON.Dependencies {
libraryFound := report.Library{
Version: version,
Name: dependency,
}
libraries = append(libraries, libraryFound)
}
r.Libraries = libraries
r.Info.Name = pkgJSON.Name
r.Info.Version = pkgJSON.Version
auditResult, err := js.npm.AuditLibraries(pkgJSON)
if err != nil {
return err
}
for _, libraryAdvisory := range auditResult.Advisories {
libraryIssue := convertAdvisoryToReport(libraryAdvisory)
r.LibraryIssues = append(r.LibraryIssues, libraryIssue)
}
return nil
}
func (js JavaScriptAnalyzer) findPackageJSON(dir string) (PackageJSON, error) {
file := filepath.Join(dir, "package.json")
b, err := ioutil.ReadFile(file)
if err != nil {
return PackageJSON{}, err
}
var pkg PackageJSON
if err := json.Unmarshal(b, &pkg); err != nil {
return PackageJSON{}, err
}
return pkg, nil
}
type PackageJSON struct {
// General information
Name string `json:"name"`
Version string `json:"version"`
IsPrivate bool `json:"private"`
License string `json:"license"`
// Metadata about the root package
SupportedOSs []string `json:"os"`
SupportedCPUArchitectures []string `json:"cpu"`
SupportedEngines map[string]string `json:"engines"`
Keywords []string `json:"keywords"`
// Main information that we are looking for :D
Dependencies map[string]string `json:"dependencies"`
}
type NPM interface {
AuditLibraries(PackageJSON) (AuditResult, error)
}
// NPMDependency is a DTO for dependencies sent over to NPM's API
type NPMDependency struct {
Version string `json:"version"`
}
// NPMAdvisoryPayload holds a DTO for sending Library information to the
// NPM Advisory API
type NPMAdvisoryPayload struct {
Name string `json:"name"`
Version string `json:"version"`
RequiredLibraries map[string]string `json:"requires"`
Dependencies map[string]NPMDependency `json:"dependencies"`
}
// AdvisoryMetadata self-explained
type AdvisoryMetadata struct {
ModuleType string `json:"module_type"`
Exploitability int `json:"exploitability"`
}
// Advisory holds data about the advisories section
type Advisory struct {
ID int `json:"id"`
Metadata AdvisoryMetadata `json:"metadata"`
// Module info
ModuleName string `json:"module_name"`
PatchedVersions string `json:"patched_versions"`
VulnerableVersions string `json:"vulnerable_versions"`
// Vulnerability info
ReferenceURL string `json:"url"`
CVEs []string `json:"cves"`
CWE string `json:"cwe"`
Title string `json:"title"`
Severity string `json:"severity"`
Overview string `json:"overview"`
References string `json:"references"`
Recomendation string `json:"recommendation"`
}
type AuditResult struct {
Advisories map[string]Advisory `json:"advisories"`
}
type NpmAdvisory struct {
url string
userAgent string
client *http.Client
}
func NewNPMAdvisory(url, userAgent string, timeout time.Duration) NpmAdvisory {
return NpmAdvisory{
url: url,
userAgent: userAgent,
client: &http.Client{
Timeout: timeout,
},
}
}
// AuditLibraries gets the information from NPM Advisory API for the given pkgJSON
func (npm NpmAdvisory) AuditLibraries(pkgJSON PackageJSON) (AuditResult, error) {
body := transformLibrariesForAuditing(pkgJSON.Name, pkgJSON.Version, pkgJSON.Dependencies)
bodyData, err := json.Marshal(body)
if err != nil {
return AuditResult{}, err
}
bodyReader := bytes.NewReader(bodyData)
req, err := http.NewRequest(http.MethodPost, npm.url, bodyReader)
if err != nil {
return AuditResult{}, err
}
req.Header.Add("User-Agent", npm.userAgent)
res, err := npm.client.Do(req)
if err != nil {
return AuditResult{}, err
}
defer res.Body.Close()
responseData, err := ioutil.ReadAll(res.Body)
if err != nil {
return AuditResult{}, err
}
var result AuditResult
if err := json.Unmarshal(responseData, &result); err != nil {
return AuditResult{}, err
}
return result, nil
}
// convertAdvisoryToReport do the trick to add the Advisory response from the NPM API
// to the standard reports.LibraryVulnerability struct
func convertAdvisoryToReport(advisory Advisory) report.LibraryVulnerability {
var title string
if advisory.Title == "" {
title = fmt.Sprintf("Vulnerability - %s", advisory.ModuleName)
} else {
title = fmt.Sprintf("%s - %s", advisory.Title, advisory.ModuleName)
}
return report.LibraryVulnerability{
Title: title,
ID: advisory.ID,
CWE: advisory.CWE,
CVEs: strings.Join(advisory.CVEs, " "),
Severity: advisory.Severity,
Description: advisory.Overview,
Recomendation: advisory.Recomendation,
}
}
func transformLibrariesForAuditing(name, version string, libraries map[string]string) (payload NPMAdvisoryPayload) {
payload.Name = name
payload.Version = version
payload.RequiredLibraries = libraries
payload.Dependencies = make(map[string]NPMDependency)
for module, moduleVersion := range libraries {
dependency := NPMDependency{
Version: moduleVersion,
}
payload.Dependencies[module] = dependency
}
return payload
}