Skip to content

Commit 0d29855

Browse files
committedSep 11, 2023
feat: add no-package-lock mode to npm audit
1 parent 0355153 commit 0d29855

File tree

7 files changed

+51
-3
lines changed

7 files changed

+51
-3
lines changed
 

‎docs/lib/content/commands/npm-audit.md

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ vulnerability is found. It may be useful in CI environments to include the
3030
will cause the command to fail. This option does not filter the report
3131
output, it simply changes the command's failure threshold.
3232

33+
### Package lock
34+
35+
By default npm requires a package-lock or shrinkwrap in order to run the
36+
audit. You can bypass the package lock with `--no-package-lock` but be
37+
aware the results may be different with every run, since npm will
38+
re-build the dependency tree each time.
39+
3340
### Audit Signatures
3441

3542
To ensure the integrity of packages you download from the public npm registry, or any registry that supports signatures, you can verify the registry signatures of downloaded packages using the npm CLI.

‎docs/lib/content/commands/npm-query.md

+7
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ npm query ":type(git)" | jq 'map(.name)' | xargs -I {} npm why {}
134134
...
135135
```
136136

137+
### Package lock only mode
138+
139+
If package-lock-only is enabled, only the information in the package
140+
lock (or shrinkwrap) is loaded. This means that information from the
141+
package.json files of your dependencies will not be included in the
142+
result set (e.g. description, homepage, engines).
143+
137144
### Configuration
138145

139146
<!-- AUTOGENERATED CONFIG DESCRIPTIONS -->

‎lib/commands/audit.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ class Audit extends ArboristWorkspaceCmd {
404404
'force',
405405
'json',
406406
'package-lock-only',
407+
'package-lock',
407408
'omit',
408409
'foreground-scripts',
409410
'ignore-scripts',
@@ -439,6 +440,10 @@ class Audit extends ArboristWorkspaceCmd {
439440
}
440441

441442
async auditAdvisories (args) {
443+
const fix = args[0] === 'fix'
444+
if (this.npm.config.get('package-lock') === false && fix) {
445+
throw this.usageError('fix can not be used without a package-lock')
446+
}
442447
const reporter = this.npm.config.get('json') ? 'json' : 'detail'
443448
const Arborist = require('@npmcli/arborist')
444449
const opts = {
@@ -450,7 +455,6 @@ class Audit extends ArboristWorkspaceCmd {
450455
}
451456

452457
const arb = new Arborist(opts)
453-
const fix = args[0] === 'fix'
454458
await arb.audit({ fix })
455459
if (fix) {
456460
await reifyFinish(this.npm, arb)

‎tap-snapshots/test/lib/docs.js.test.cjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2552,7 +2552,7 @@ npm audit [fix|signatures]
25522552
25532553
Options:
25542554
[--audit-level <info|low|moderate|high|critical|none>] [--dry-run] [-f|--force]
2555-
[--json] [--package-lock-only]
2555+
[--json] [--package-lock-only] [--no-package-lock]
25562556
[--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
25572557
[--foreground-scripts] [--ignore-scripts]
25582558
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
@@ -2569,6 +2569,7 @@ npm audit [fix|signatures]
25692569
#### \`force\`
25702570
#### \`json\`
25712571
#### \`package-lock-only\`
2572+
#### \`package-lock\`
25722573
#### \`omit\`
25732574
#### \`foreground-scripts\`
25742575
#### \`ignore-scripts\`

‎test/lib/commands/audit.js

+12
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ t.test('audit fix - bulk endpoint', async t => {
210210
)
211211
})
212212

213+
t.test('audit fix no package lock', async t => {
214+
const { npm } = await loadMockNpm(t, {
215+
config: {
216+
'package-lock': false,
217+
},
218+
})
219+
await t.rejects(
220+
npm.exec('audit', ['fix']),
221+
{ code: 'EUSAGE' }
222+
)
223+
})
224+
213225
t.test('completion', async t => {
214226
const { audit } = await loadMockNpm(t, { command: 'audit' })
215227
t.test('fix', async t => {

‎workspaces/arborist/lib/arborist/audit.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ module.exports = cls => class Auditor extends cls {
2222
options = { ...this.options, ...options }
2323

2424
process.emit('time', 'audit')
25-
const tree = await this.loadVirtual()
25+
let tree
26+
if (options.packageLock === false) {
27+
// build ideal tree
28+
await this.loadActual(options)
29+
await this.buildIdealTree()
30+
tree = this.idealTree
31+
} else {
32+
tree = await this.loadVirtual()
33+
}
2634
if (this[_workspaces] && this[_workspaces].length) {
2735
options.filterSet = this.workspaceDependencySet(
2836
tree,

‎workspaces/arborist/test/arborist/audit.js

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ t.test('audit finds the bad deps', async t => {
2727
t.equal(report.size, 2)
2828
})
2929

30+
t.test('no package lock finds no bad deps', async t => {
31+
const path = resolve(fixtures, 'deprecated-dep')
32+
t.teardown(auditResponse(resolve(fixtures, 'audit-nyc-mkdirp/audit.json')))
33+
const arb = newArb(path, { packageLock: false })
34+
const report = await arb.audit()
35+
t.equal(report.topVulns.size, 0)
36+
t.equal(report.size, 0)
37+
})
38+
3039
t.test('audit fix reifies out the bad deps', async t => {
3140
const path = fixture(t, 'deprecated-dep')
3241
t.teardown(auditResponse(resolve(fixtures, 'audit-nyc-mkdirp/audit.json')))

0 commit comments

Comments
 (0)
Please sign in to comment.