Can someone tell me how the heck `npm audit` is supposed to work?
In my project, npm install warns about potential security issues that we should fix. Sounds great, and I'd happily keep the dependencies up to date, but it's really not working the way I'd expect it to… Here's the scenario:
$ npm audit fix tells me:
updated 1 package in 6.63s
fixed 4 of 5 vulnerabilities in 909376 scanned packages
1 vulnerability required manual review and could not be updated
However, running npm install right after, makes them come back:
updated 1 package and audited 909376 packages in 9.84s
found 5 vulnerabilities (3 moderate, 2 high)
run `npm audit fix` to fix them, or `npm audit` for details
Why is that?
Continuing, if I do npm audit, the first listed item is:
=== npm audit security report ===
# Run npm update js-yaml --depth 5 to resolve 4 vulnerabilities
┌───────────────┬──────────────────────────────────────────────────────────────┐
│ Moderate │ Denial of Service │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package │ js-yaml │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ … │ … │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info │ https://npmjs.com/advisories/788 │
└───────────────┴──────────────────────────────────────────────────────────────┘
Following that helpful link, it tells me:
Remediation
Upgrade to version 3.13.0.
But, if I check which version I have, it says:
$ npm show js-yaml
[email protected] | MIT | deps: 2 | versions: 68
YAML 1.2 parser and serializer
https://github.com/nodeca/js-yaml
keywords: yaml, parser, serializer, pyyaml
bin: js-yaml
…
So, my current version is already higher than the version where the issue was remedied. Why does npm audit keep complaining about it?
Thanks for any helpful pointers! 😇
Posted Jul 23, 2019, 8:09 AM UTC
Audit updates the
package-lock.jsonfile.npm installre-writes it. Audit is meant to be used withnpm ci, which installs from the package-lock. It's actually a faster install and it guarantees the same set of packages to everyone/every time. This is especially good for Continuous Integration (hence npm ci), because you can choose when to update your packages and know that the build server will always install the exact set of packages you did, and not some randomly breaking change at 4:30pm on a Friday.That sounds, euhm, fragile... Why would you want to update the `.lock` file on the fly?
I've always learned that you want to update the lock in DEV, push that to your vcs, and deploy that on PROD, *as is*.. If your CI messes with the lock file, you're not certain that you'll get in PROD what you asked for.
Yeah you dont run audit on your vcs. You run it locally and then continue to use 'npm ci' to install as audit should help cleanup your .lock first
yes you don't want to run
npm installin your CInpm cireplaces it.That's correct. Sorry if I explained it poorly. The idea is that you only periodically run
npm installandnpm auditon a developer workstation when you want to perform package updates. This could be every morning on your workstation or it could be a monthly task so the entire team has a consistent work environment.Outside of those planned update cycles, everyone and everything should use
npm cito ensure they get the same packages specified in the lock file; packages that were intentionally installed, updated and audited.