-
Notifications
You must be signed in to change notification settings - Fork 534
/
index.js
136 lines (122 loc) · 4.18 KB
/
index.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
module.exports = test;
var debug = require('debug')('snyk');
var Promise = require('es6-promise').Promise; // jshint ignore:line
var request = require('../../request');
var fs = require('then-fs');
var snyk = require('../..');
var spinner = require('../../spinner');
var moduleToOjbect = require('snyk-module');
var isCI = require('../../is-ci');
var analytics = require('../../analytics');
var config = require('../../config');
var getModuleInfo = require('../../module-info');
function test(root, options) {
var promise = Promise.resolve().then(function () {
var policyLocations = [root];
var hasDevDependencies = false;
var lbl = 'Querying vulnerabilities database...';
return assemblePayload(root, options, policyLocations)
.then(spinner(lbl))
.then(function (payload) {
return new Promise(function (resolve, reject) {
request(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) {
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));
});
return promise;
}
function assemblePayload(root, options, policyLocations) {
var local = fs.existsSync(root);
analytics.add('local', local);
return local ? assembleLocalPayload(root, options, policyLocations)
: assembleRemotePayload(root);
}
function assembleLocalPayload(root, options, policyLocations) {
return getModuleInfo('maven', root, options)
.then(function (module) {
analytics.add('policies', policyLocations.length);
analytics.add('packageName', module.name);
analytics.add('packageVersion', module.version);
analytics.add('package', module.name + '@' + module.version);
var payload = {
method: 'POST',
url: vulnUrl(),
json: true,
headers: {
'x-is-ci': isCI,
authorization: 'token ' + snyk.api,
},
body: module,
};
return payload;
});
}
function assembleRemotePayload(root) {
var module = moduleToOjbect(root);
debug('testing remote: %s', module.name + '@' + module.version);
var encodedName = encodeURIComponent(module.name + '@' + module.version);
analytics.add('packageName', module.name);
analytics.add('packageVersion', module.version);
analytics.add('package', module.name + '@' + module.version);
var payload = {
method: 'GET',
url: vulnUrl() + '/' + encodedName,
json: true,
headers: {
'x-is-ci': isCI,
authorization: 'token ' + snyk.api,
},
};
return Promise.resolve(payload);
}
function vulnUrl() {
return config.API + '/vuln/maven';
}