-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
audit: configurable audit level for non-zero exit (#31)
`npm audit` currently exits with exit code 1 if any vulnerabilities are found of any level. Add a flag of `--audit-level` to `npm audit` to allow it to pass if only vulnerabilities below a certain level are found. Example: `npm audit --audit-level=high` will exit with 0 if only low or moderate level vulns are detected. Fixes: https://npm.community/t/245 PR-URL: #31 Credit: @lennym Reviewed-By: @zkat
- Loading branch information
Showing
4 changed files
with
283 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,268 @@ | ||
'use strict' | ||
|
||
const BB = require('bluebird') | ||
|
||
const common = BB.promisifyAll(require('../common-tap.js')) | ||
const mr = BB.promisify(require('npm-registry-mock')) | ||
const path = require('path') | ||
const rimraf = BB.promisify(require('rimraf')) | ||
const Tacks = require('tacks') | ||
const tap = require('tap') | ||
const test = tap.test | ||
|
||
const Dir = Tacks.Dir | ||
const File = Tacks.File | ||
const testDir = path.join(__dirname, path.basename(__filename, '.js')) | ||
|
||
const EXEC_OPTS = { cwd: testDir } | ||
|
||
tap.tearDown(function () { | ||
process.chdir(__dirname) | ||
try { | ||
rimraf.sync(testDir) | ||
} catch (e) { | ||
if (process.platform !== 'win32') { | ||
throw e | ||
} | ||
} | ||
}) | ||
|
||
function tmock (t) { | ||
return mr({port: common.port}).then(s => { | ||
t.tearDown(function () { | ||
s.done() | ||
s.close() | ||
rimraf.sync(testDir) | ||
}) | ||
return s | ||
}) | ||
} | ||
|
||
test('exits with zero exit code for vulnerabilities below the `audit-level` flag', t => { | ||
const fixture = new Tacks(new Dir({ | ||
'package.json': new File({ | ||
name: 'foo', | ||
version: '1.0.0', | ||
dependencies: { | ||
baddep: '1.0.0' | ||
} | ||
}) | ||
})) | ||
fixture.create(testDir) | ||
return tmock(t).then(srv => { | ||
srv.filteringRequestBody(req => 'ok') | ||
srv.post('/-/npm/v1/security/audits/quick', 'ok').reply(200, 'yeah') | ||
srv.get('/baddep').twice().reply(200, { | ||
name: 'baddep', | ||
'dist-tags': { | ||
'latest': '1.2.3' | ||
}, | ||
versions: { | ||
'1.0.0': { | ||
name: 'baddep', | ||
version: '1.0.0', | ||
_hasShrinkwrap: false, | ||
dist: { | ||
shasum: 'deadbeef', | ||
tarball: common.registry + '/idk/-/idk-1.0.0.tgz' | ||
} | ||
}, | ||
'1.2.3': { | ||
name: 'baddep', | ||
version: '1.2.3', | ||
_hasShrinkwrap: false, | ||
dist: { | ||
shasum: 'deadbeef', | ||
tarball: common.registry + '/idk/-/idk-1.2.3.tgz' | ||
} | ||
} | ||
} | ||
}) | ||
return common.npm([ | ||
'install', | ||
'--audit', | ||
'--json', | ||
'--package-lock-only', | ||
'--registry', common.registry, | ||
'--cache', path.join(testDir, 'npm-cache') | ||
], EXEC_OPTS).then(([code, stdout, stderr]) => { | ||
srv.filteringRequestBody(req => 'ok') | ||
srv.post('/-/npm/v1/security/audits', 'ok').reply(200, { | ||
actions: [{ | ||
action: 'update', | ||
module: 'baddep', | ||
target: '1.2.3', | ||
resolves: [{path: 'baddep'}] | ||
}], | ||
metadata: { | ||
vulnerabilities: { | ||
low: 1 | ||
} | ||
} | ||
}) | ||
return common.npm([ | ||
'audit', | ||
'--audit-level', 'high', | ||
'--json', | ||
'--registry', common.registry, | ||
'--cache', path.join(testDir, 'npm-cache') | ||
], EXEC_OPTS).then(([code, stdout, stderr]) => { | ||
t.equal(code, 0, 'exited OK') | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
test('exits with non-zero exit code for vulnerabilities at the `audit-level` flag', t => { | ||
const fixture = new Tacks(new Dir({ | ||
'package.json': new File({ | ||
name: 'foo', | ||
version: '1.0.0', | ||
dependencies: { | ||
baddep: '1.0.0' | ||
} | ||
}) | ||
})) | ||
fixture.create(testDir) | ||
return tmock(t).then(srv => { | ||
srv.filteringRequestBody(req => 'ok') | ||
srv.post('/-/npm/v1/security/audits/quick', 'ok').reply(200, 'yeah') | ||
srv.get('/baddep').twice().reply(200, { | ||
name: 'baddep', | ||
'dist-tags': { | ||
'latest': '1.2.3' | ||
}, | ||
versions: { | ||
'1.0.0': { | ||
name: 'baddep', | ||
version: '1.0.0', | ||
_hasShrinkwrap: false, | ||
dist: { | ||
shasum: 'deadbeef', | ||
tarball: common.registry + '/idk/-/idk-1.0.0.tgz' | ||
} | ||
}, | ||
'1.2.3': { | ||
name: 'baddep', | ||
version: '1.2.3', | ||
_hasShrinkwrap: false, | ||
dist: { | ||
shasum: 'deadbeef', | ||
tarball: common.registry + '/idk/-/idk-1.2.3.tgz' | ||
} | ||
} | ||
} | ||
}) | ||
return common.npm([ | ||
'install', | ||
'--audit', | ||
'--json', | ||
'--package-lock-only', | ||
'--registry', common.registry, | ||
'--cache', path.join(testDir, 'npm-cache') | ||
], EXEC_OPTS).then(([code, stdout, stderr]) => { | ||
srv.filteringRequestBody(req => 'ok') | ||
srv.post('/-/npm/v1/security/audits', 'ok').reply(200, { | ||
actions: [{ | ||
action: 'update', | ||
module: 'baddep', | ||
target: '1.2.3', | ||
resolves: [{path: 'baddep'}] | ||
}], | ||
metadata: { | ||
vulnerabilities: { | ||
high: 1 | ||
} | ||
} | ||
}) | ||
return common.npm([ | ||
'audit', | ||
'--audit-level', 'high', | ||
'--json', | ||
'--registry', common.registry, | ||
'--cache', path.join(testDir, 'npm-cache') | ||
], EXEC_OPTS).then(([code, stdout, stderr]) => { | ||
t.equal(code, 1, 'exited OK') | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
test('exits with non-zero exit code for vulnerabilities at the `audit-level` flag', t => { | ||
const fixture = new Tacks(new Dir({ | ||
'package.json': new File({ | ||
name: 'foo', | ||
version: '1.0.0', | ||
dependencies: { | ||
baddep: '1.0.0' | ||
} | ||
}) | ||
})) | ||
fixture.create(testDir) | ||
return tmock(t).then(srv => { | ||
srv.filteringRequestBody(req => 'ok') | ||
srv.post('/-/npm/v1/security/audits/quick', 'ok').reply(200, 'yeah') | ||
srv.get('/baddep').twice().reply(200, { | ||
name: 'baddep', | ||
'dist-tags': { | ||
'latest': '1.2.3' | ||
}, | ||
versions: { | ||
'1.0.0': { | ||
name: 'baddep', | ||
version: '1.0.0', | ||
_hasShrinkwrap: false, | ||
dist: { | ||
shasum: 'deadbeef', | ||
tarball: common.registry + '/idk/-/idk-1.0.0.tgz' | ||
} | ||
}, | ||
'1.2.3': { | ||
name: 'baddep', | ||
version: '1.2.3', | ||
_hasShrinkwrap: false, | ||
dist: { | ||
shasum: 'deadbeef', | ||
tarball: common.registry + '/idk/-/idk-1.2.3.tgz' | ||
} | ||
} | ||
} | ||
}) | ||
return common.npm([ | ||
'install', | ||
'--audit', | ||
'--json', | ||
'--package-lock-only', | ||
'--registry', common.registry, | ||
'--cache', path.join(testDir, 'npm-cache') | ||
], EXEC_OPTS).then(([code, stdout, stderr]) => { | ||
srv.filteringRequestBody(req => 'ok') | ||
srv.post('/-/npm/v1/security/audits', 'ok').reply(200, { | ||
actions: [{ | ||
action: 'update', | ||
module: 'baddep', | ||
target: '1.2.3', | ||
resolves: [{path: 'baddep'}] | ||
}], | ||
metadata: { | ||
vulnerabilities: { | ||
high: 1 | ||
} | ||
} | ||
}) | ||
return common.npm([ | ||
'audit', | ||
'--audit-level', 'moderate', | ||
'--json', | ||
'--registry', common.registry, | ||
'--cache', path.join(testDir, 'npm-cache') | ||
], EXEC_OPTS).then(([code, stdout, stderr]) => { | ||
t.equal(code, 1, 'exited OK') | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
test('cleanup', t => { | ||
return rimraf(testDir) | ||
}) |