fix!: remove old audit fallback request · npm/cli@080a0f2 · GitHub
Skip to content

Commit 080a0f2

Browse files
committed
fix!: remove old audit fallback request
BREAKING CHANGE: npm will no longer fall back to the old audit endpoint if the bulk advisory request fails. This legacy code has a long tail in npm. Getting rid of it was difficult because of how load-bearing some of those requests were in tests. This PR removes the old "mock server" that arborist tests spun up, and moved that logic into the existing mock registry that the cli uses. This will allow us to consolidate our logic in tests, and also outline more granularly which tests actually make registry requests. A few tests that were testing just the fallback behavior were also removed.
1 parent 75a3f12 commit 080a0f2

14 files changed

Lines changed: 1333 additions & 1434 deletions

File tree

mock-registry/lib/index.js

Lines changed: 72 additions & 3 deletions

package-lock.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18456,6 +18456,7 @@
1845618456
},
1845718457
"devDependencies": {
1845818458
"@npmcli/eslint-config": "^5.0.1",
18459+
"@npmcli/mock-registry": "^1.0.0",
1845918460
"@npmcli/template-oss": "4.23.3",
1846018461
"benchmark": "^2.1.4",
1846118462
"minify-registry-metadata": "^4.0.0",

workspaces/arborist/lib/audit-report.js

Lines changed: 16 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -274,33 +274,6 @@ class AuditReport extends Map {
274274
throw new Error('do not call AuditReport.set() directly')
275275
}
276276

277-
// convert a quick-audit into a bulk advisory listing
278-
static auditToBulk (report) {
279-
if (!report.advisories) {
280-
// tack on the report json where the response body would go
281-
throw Object.assign(new Error('Invalid advisory report'), {
282-
body: JSON.stringify(report),
283-
})
284-
}
285-
286-
const bulk = {}
287-
const { advisories } = report
288-
for (const advisory of Object.values(advisories)) {
289-
const {
290-
id,
291-
url,
292-
title,
293-
severity = 'high',
294-
vulnerable_versions = '*',
295-
module_name: name,
296-
} = advisory
297-
bulk[name] = bulk[name] || []
298-
bulk[name].push({ id, url, title, severity, vulnerable_versions })
299-
}
300-
301-
return bulk
302-
}
303-
304277
async [_getReport] () {
305278
// if we're not auditing, just return false
306279
if (this.options.audit === false || this.options.offline === true || this.tree.inventory.size === 1) {
@@ -309,39 +282,24 @@ class AuditReport extends Map {
309282

310283
const timeEnd = time.start('auditReport:getReport')
311284
try {
312-
try {
313-
// first try the super fast bulk advisory listing
314-
const body = prepareBulkData(this.tree, this[_omit], this.filterSet)
315-
log.silly('audit', 'bulk request', body)
316-
317-
// no sense asking if we don't have anything to audit,
318-
// we know it'll be empty
319-
if (!Object.keys(body).length) {
320-
return null
321-
}
285+
const body = prepareBulkData(this.tree, this[_omit], this.filterSet)
286+
log.silly('audit', 'bulk request', body)
322287

323-
const res = await fetch('/-/npm/v1/security/advisories/bulk', {
324-
...this.options,
325-
registry: this.options.auditRegistry || this.options.registry,
326-
method: 'POST',
327-
gzip: true,
328-
body,
329-
})
330-
331-
return await res.json()
332-
} catch (er) {
333-
log.silly('audit', 'bulk request failed', String(er.body))
334-
// that failed, try the quick audit endpoint
335-
const body = prepareData(this.tree, this.options)
336-
const res = await fetch('/-/npm/v1/security/audits/quick', {
337-
...this.options,
338-
registry: this.options.auditRegistry || this.options.registry,
339-
method: 'POST',
340-
gzip: true,
341-
body,
342-
})
343-
return AuditReport.auditToBulk(await res.json())
288+
// no sense asking if we don't have anything to audit,
289+
// we know it'll be empty
290+
if (!Object.keys(body).length) {
291+
return null
344292
}
293+
294+
const res = await fetch('/-/npm/v1/security/advisories/bulk', {
295+
...this.options,
296+
registry: this.options.auditRegistry || this.options.registry,
297+
method: 'POST',
298+
gzip: true,
299+
body,
300+
})
301+
302+
return await res.json()
345303
} catch (er) {
346304
log.verbose('audit error', er)
347305
log.silly('audit error', String(er.body))
@@ -384,32 +342,4 @@ const prepareBulkData = (tree, omit, filterSet) => {
384342
return payload
385343
}
386344

387-
const prepareData = (tree, opts) => {
388-
const { npmVersion: npm_version } = opts
389-
const node_version = process.version
390-
const { platform, arch } = process
391-
const { NODE_ENV: node_env } = process.env
392-
const data = tree.meta.commit()
393-
// the legacy audit endpoint doesn't support any kind of pre-filtering
394-
// we just have to get the advisories and skip over them in the report
395-
return {
396-
name: data.name,
397-
version: data.version,
398-
requires: {
399-
...(tree.package.devDependencies || {}),
400-
...(tree.package.peerDependencies || {}),
401-
...(tree.package.optionalDependencies || {}),
402-
...(tree.package.dependencies || {}),
403-
},
404-
dependencies: data.dependencies,
405-
metadata: {
406-
node_version,
407-
npm_version,
408-
platform,
409-
arch,
410-
node_env,
411-
},
412-
}
413-
}
414-
415345
module.exports = AuditReport

workspaces/arborist/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
},
4141
"devDependencies": {
4242
"@npmcli/eslint-config": "^5.0.1",
43+
"@npmcli/mock-registry": "^1.0.0",
4344
"@npmcli/template-oss": "4.23.3",
4445
"benchmark": "^2.1.4",
4546
"minify-registry-metadata": "^4.0.0",
@@ -81,7 +82,7 @@
8182
"test-env": [
8283
"LC_ALL=sk"
8384
],
84-
"timeout": "360",
85+
"timeout": "720",
8586
"nyc-arg": [
8687
"--exclude",
8788
"tap-snapshots/**"

workspaces/arborist/tap-snapshots/test/arborist/build-ideal-tree.js.test.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159887,6 +159887,7 @@ ArboristNode {
159887159887
"location": "node_modules/foo",
159888159888
"name": "foo",
159889159889
"path": "{CWD}/test/arborist/tap-testdir-build-ideal-tree-workspaces-should-allow-cyclic-peer-dependencies-between-workspaces-and-packages-from-a-repository/node_modules/foo",
159890+
"resolved": "https://registry.npmjs.org/foo/-/foo-1.0.0.tgz",
159890159891
"version": "1.0.0",
159891159892
},
159892159893
"workspace-a" => ArboristLink {

workspaces/arborist/tap-snapshots/test/arborist/reify.js.test.cjs

Lines changed: 8 additions & 5 deletions

0 commit comments

Comments
 (0)