-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·102 lines (80 loc) · 2.72 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
#!/usr/bin/env node
'use strict';
const async = require('async');
const _ = require('lodash');
const os = require('os');
const path = require('path');
const remote = require('npm-remote-ls');
const malicious = require('./malicious.json');
let manifest;
try {
manifest = require(path.resolve(process.cwd(), 'npm-malice-audit.json'));
} catch (x) {
try {
const pkg = require(path.resolve(process.cwd(), 'package.json'));
manifest = _.uniq(
Object.keys(pkg.dependencies || {})
.concat(Object.keys(pkg.devDependencies || {}))
.concat(Object.keys(pkg.peerDependencies || {}))
.concat(Object.keys(pkg.optionalDependencies || {}))
);
} catch (x) {
console.error('No npm-malice-audit.json or package.json found! Please refer to the README for usage.');
process.exit(1);
}
}
if (!Array.isArray(manifest) || !manifest.length) {
console.warn('No packages specified in either npm-malice-audit.json or package.json');
process.exit(0);
}
// Save logs for the end.
const logs = [];
remote.config({
// Silence npm-remote-ls messages until the end.
logger: {
log: function() {
logs.push(_.toArray(arguments).join(' '));
}
},
// We want _all_ dependencies.
development: true,
optional: true,
peer: true
});
const stats = {
count: 0,
detected: 0,
start: Date.now()
};
const tasks = manifest.filter(item => typeof item === 'string').map(item => {
return (callback) => {
const start = Date.now();
const name = `| ${item} |`;
const bar = '-'.repeat(name.length);
console.log([bar, name, bar].join(os.EOL));
remote.ls(item, 'latest', true, (result) => {
result = _.uniq(result.filter(v => typeof v === 'string').map(v => v.split('@')[0]));
const time = Date.now() - start;
const detected = result.filter(v => malicious.indexOf(v) !== -1);
stats.count += result.length;
stats.detected += detected;
console.log(`Audited ${result.length} packages.`);
if (!result.length) {
console.log('This may mean the package is private or otherwise not accessible. Look for error output below!');
}
console.log(`Completed in ${time / 1000} seconds.`);
console.log(`Found ${detected.length} malicious packages in the tree!`);
console.log(detected.map(v => `- ${v}`).join(os.EOL));
callback(null, result);
});
};
});
async.series(tasks, function(err, results) {
if (logs.length) {
console.log('= ERRORS =' + ('='.repeat(30)));
}
console.log('= SUMMARY =' + ('='.repeat(29)));
console.log(`Audited ${stats.count} total packages.`);
console.log(`Completed audit in ${(Date.now() - stats.start) / 1000} seconds.`);
console.log(`Found a total of ${stats.detected} malicious packages!`);
});