berry: [Bug?]: `yarn npm audit` fails with 400 error
Self-service
- I’d be willing to implement a fix
Describe the bug
Running yarn npm audit -AR
fails in the Jest repo with a 400 error from the npm registry.
To reproduce
$ git clone git@github.com:facebook/jest.git
$ cd jest
$ yarn
$ yarn npm audit -AR
Environment
System:
OS: macOS 12.1
CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
Binaries:
Node: 16.14.0 - /private/var/folders/gj/0mygpdfn6598xh34njlyrqzc0000gn/T/xfs-9e7a75e5/node
Yarn: 3.2.0-rc.15.git.20220211.hash-32c522a7c - /private/var/folders/gj/0mygpdfn6598xh34njlyrqzc0000gn/T/xfs-9e7a75e5/yarn
npm: 8.4.1 - ~/.nvm/versions/node/v16.14.0/bin/npm
npmPackages:
jest: workspace:* => 28.0.0-alpha.0
Additional context
I’ve added a log statement after the network call, and the reported error from npm is
{
statusCode: 400,
error: 'Bad Request',
message: 'Invalid package tree, run npm install to rebuild your package-lock.json'
}
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 20 (7 by maintainers)
Commits related to this issue
- Updates the audit implementation to the bulk endpoint (#5501) **What's the problem this PR addresses?** The current audit implementation uses the older `/audit/quick` endpoint, which has various ... — committed to yarnpkg/berry by arcanis a year ago
- devDeps: @lavamoat/allow-scripts@2.0.3->2.3.1 (#20018) * devDeps: @lavamoat/allow-scripts@2.0.3->2.3.1 Note: As of right now, this causes depcheck script to fail under yarn due to a server error i... — committed to MetaMask/metamask-extension by legobeat a year ago
now
npm audit
use/-/npm/v1/security/advisories/bulk
and only fall back to/-/npm/v1/security/audits/quick
if it fails. (code)npm-audit.md
I cloned the npm repository and tested the same scenario, and if npm doesn’t use
/-/npm/v1/security/advisories/bulk
, they also receive 400 errorsIn my opinion, the solution to this issue is:
yarn npm audit
lacks maintenance, it still always uses/-/npm/v1/security/audits/quick
, but it should have the same behavior as npm./-/npm/v1/security/audits/quick
🤔? if it can be fixed, then there will be no error here.I did a bit of digging with an HTTPS proxy.
Yarn v3
Here’s a sample request body for the /quick endpoint that Yarn uses, generated from my above linked repro project.
My best guess is that the audit endpoint rejects it because the dependency tree doesn’t look complete; there appears to be a dependency
@sargunv/fake-testlib-a
that’s not present. Looking at this format, it’s unclear how multiple versions of the same package in one tree would be represented, and I can’t find an NPM API spec anywhere.The error from NPM’s /quick endpoint for the above request is as follows:
Additionally, I can confirm that this issue affects any released version of Yarn Berry since
yarn npm audit
was added (tested as far back as v2.4.0 and as far forward as v4.0.0-rc.34).NPM
Now, here’s what the request looks like when using the /bulk endpoint (with the npm cli, on the same project):
The “fake” dependency name is no longer relevant; the request simply includes a hash of package names, each with a list of versions included in the tree. This request succeeds.
Yarn v1
Finally, this is the request Yarn v1 sends. It uses neither the /quick nor the /bulk endpoint, but a different /audits endpoint.
In this case, Yarn v1 forms the request using the “fake” name of the dependency key, not the “real” name of the dependency version/resolution. This request also succeeds. Sure enough, if I manually tweak the original request to do the same, NPM no longer says “bad request”!:
I’ve looked in the payload by logging the request in the source code, but yes a native way would be great @Glandos.
The dependency I have issues with is
"@vue/cli-service": "~5.0.1"
with yarn@3.1.0 and yarn@4.0.0-rc.26.dev, just like @revyh in this commentDoes anybody have any clue by now?
It should be great if yarn was able to output the payload sent to the registry server. Right now, the only way to inspect it seems to be changing
npmRegistryServer
to an HTTP one, and listen with tcpdump/wireshark for the incoming payload.Hm - I can reproduce it on this very repository as well. I’m sure it used to work, so perhaps a backend change broke something 🙁