-
Notifications
You must be signed in to change notification settings - Fork 534
/
test.js
190 lines (166 loc) · 5.62 KB
/
test.js
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
module.exports = test;
var debug = require('debug')('snyk');
var Promise = require('es6-promise').Promise; // jshint ignore:line
var request = require('./request');
var path = require('path');
var fs = require('then-fs');
var snyk = require('..');
var spinner = require('./spinner');
var moduleToOjbect = require('snyk-module');
var isCI = require('./is-ci');
var _ = require('lodash');
var analytics = require('./analytics');
// important: this is different from ./config (which is the *user's* config)
var config = require('./config');
function test(root, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
if (!options) {
options = {};
}
var hasDevDependencies = false;
var promise = fs.exists(root).then(function (exists) {
// if the file exists, let's read the package file and post
// that up to the server.
// if it doesn't, then we're assuming this is an existing
// module on npm, so send the bare argument
var payload = {
url: config.API + '/vuln/npm',
json: true,
headers: {
'x-is-ci': isCI,
authorization: 'token ' + snyk.api,
},
};
var p = Promise.resolve();
var policyLocations = [root];
analytics.add('local', exists);
var modules = null;
if (exists) {
p = snyk.modules(root, options).then(function (pkg) {
modules = pkg;
// if there's no package name, let's get it from the root dir
if (!pkg.name) {
pkg.name = path.basename(path.resolve(root));
}
policyLocations = policyLocations.concat(pluckPolicies(pkg));
debug('policies found', policyLocations);
analytics.add('policies', policyLocations.length);
hasDevDependencies = pkg.hasDevDependencies;
payload.method = 'POST';
payload.body = pkg;
return {
package: pkg,
payload: payload,
};
});
} else {
var module = moduleToOjbect(root);
debug('testing remote: %s', module.name + '@' + module.version);
p = p.then(function () {
payload.method = 'GET';
payload.url += '/' +
encodeURIComponent(module.name + '@' + module.version);
return {
package: module,
payload: payload,
};
});
}
var lbl = 'Querying vulnerabilities database...';
return p.then(spinner(lbl)).then(function (data) {
analytics.add('packageName', data.package.name);
analytics.add('packageVersion', data.package.version);
analytics.add('package', data.package.name + '@' + data.package.version);
return new Promise(function (resolve, reject) {
request(data.payload, function (error, res, body) {
if (error) {
return reject(error);
}
if (res.statusCode !== 200) {
var err = new Error(body && body.error ?
body.error :
res.statusCode);
// this is the case where a local module has been tested, but
// doesn't have any production deps, but we've noted that they
// have dep deps, so we'll error with a more useful message
if (res.statusCode === 404 && hasDevDependencies) {
err.code = 'NOT_FOUND_HAS_DEV_DEPS';
} else {
err.code = res.statusCode;
}
if (res.statusCode === 500) {
debug('Server error', body.stack);
}
return reject(err);
}
resolve(body);
});
});
}).then(function (res) {
if (modules) {
res.dependencyCount = modules.numDependencies;
if (res.vulnerabilities) {
res.vulnerabilities.forEach(function (vuln) {
var plucked = modules.pluck(vuln.from, vuln.name, vuln.version);
vuln.__filename = plucked.__filename;
vuln.shrinkwrap = plucked.shrinkwrap;
vuln.bundled = plucked.bundled;
vuln.depType = plucked.depType;
var parentPkg = moduleToOjbect(vuln.from[1]);
var parent = modules.pluck(vuln.from.slice(0, 2), parentPkg.name, parentPkg.version);
vuln.parentDep = parent.depType;
});
}
}
return res;
}).then(function (res) {
analytics.add('vulns-pre-policy', res.vulnerabilities.length);
options.loose = true; // allows merge without root policy
return snyk.policy.load(policyLocations, options).then(function (policy) {
return policy.filter(res, root);
}, function (error) { // note: inline catch, to handle error from .load
// the .snyk file wasn't found, which is fine, so we'll return the vulns
if (error.code === 'ENOENT') {
return res;
}
throw error;
}).then(function (res) {
analytics.add('vulns', res.vulnerabilities.length);
// add the unique count of vulnerabilities found
res.uniqueCount = 0;
var seen = {};
res.uniqueCount = res.vulnerabilities.reduce(function (acc, curr) {
if (!seen[curr.id]) {
seen[curr.id] = true;
acc++;
}
return acc;
}, 0);
return res;
});
}).then(spinner.clear(lbl));
});
if (callback) {
promise.then(function (res) {
callback(null, res);
}).catch(callback);
}
return promise;
}
function pluckPolicies(pkg) {
if (!pkg) {
return null;
}
if (pkg.snyk) {
return pkg.snyk;
}
if (!pkg.dependencies) {
return null;
}
return _.flatten(Object.keys(pkg.dependencies).map(function (name) {
return pluckPolicies(pkg.dependencies[name]);
}).filter(Boolean));
}