Skip to content
This repository was archived by the owner on Jan 30, 2018. It is now read-only.

Commit 3618468

Browse files
committed
feat: audit package for vulnerable deps
1 parent 8a89786 commit 3618468

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
}
1414
},
1515
"dependencies": {
16-
"@semantic-release/error": "^1.0.0"
16+
"@semantic-release/error": "^1.0.0",
17+
"nsp": "1.1.0"
1718
},
1819
"devDependencies": {
1920
"babel": "^5.5.8",

src/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
const SRError = require('@semantic-release/error')
2+
var auditPackage = require('nsp/lib/auditPackage.js')
23

3-
module.exports = function (pluginConfig, config, cb) {
4-
cb(null)
4+
module.exports = function (pluginConfig, packagePath, cb) {
5+
if (!packagePath) {
6+
packagePath = process.cwd() + '/package.json'
7+
}
8+
9+
auditPackage(packagePath, (err, results) => {
10+
if (err) return cb(new SRError('nsp returned unexpected error code', 'ENSPFAIL'))
11+
12+
if (results.length > 0) return cb(new SRError('Vulnerable Dependencies', 'EVULNERABLEDEPS'))
13+
14+
return cb(null)
15+
})
516
}

test/data/dep-package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "git-deps",
3+
"version": "0.0.1",
4+
"dependencies": {
5+
"file-dep": "file:../node",
6+
"some-dep": "https://github.com/joyent/node.git",
7+
"other-dep": "git+ssh://git@github.com:nodesecurity/nsp.git",
8+
"short-url-dep": "nodesecurity/nsp.git"
9+
}
10+
}
11+

test/data/vulnerable-package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "test",
3+
"version": "0.0.1",
4+
"author": "Node Security Project",
5+
"dependencies": {
6+
"node-print": "0.0.4",
7+
"request": "^2.40.0",
8+
"qs": "^0.5"
9+
}
10+
}

test/specs/index.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,40 @@ const { test } = require('tap')
33
const SRError = require('@semantic-release/error')
44

55
const condition = proxyquire('../../', {
6-
// ...
6+
'auditPackage': (cb) => cb(null)
77
})
88

9-
test('run-script', (t) => {
10-
t.ok(condition)
11-
t.end()
9+
test('find vulnerable packages', (tt) => {
10+
tt.plan(2)
11+
12+
condition({}, 'test/data/vulnerable-package.json', (err, results) => {
13+
tt.ok(err instanceof SRError)
14+
tt.is(err.code, 'EVULNERABLEDEPS')
15+
})
16+
})
17+
18+
test('does not raise error on safe packages', (tt) => {
19+
tt.plan(1)
20+
21+
condition({}, 'test/data/dep-package.json', (err) => {
22+
tt.is(err, null)
23+
})
1224
})
25+
26+
test('requires proper path to package', (tt) => {
27+
tt.plan(2)
28+
29+
condition({}, 'weird', (err) => {
30+
tt.ok(err instanceof SRError)
31+
tt.is(err.code, 'ENSPFAIL')
32+
})
33+
})
34+
35+
test('if no path given, path defaults to cwd + package.json', (tt) => {
36+
tt.plan(1)
37+
38+
condition({}, '', (err) => {
39+
tt.is(err, null)
40+
})
41+
})
42+

0 commit comments

Comments
 (0)