r/node•
bopp

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

439
oculus42•

Audit updates the package-lock.json file. npm install re-writes it. Audit is meant to be used with npm 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.

13
boppOP•

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.

5
marvinfuture•

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

3
abumalick•

yes you don't want to run npm install in your CI npm ci replaces it.

2
oculus42•

That's correct. Sorry if I explained it poorly. The idea is that you only periodically run npm install and npm audit on 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 ci to ensure they get the same packages specified in the lock file; packages that were intentionally installed, updated and audited.

2
boppOP•

Additional note: running the suggested `npm update js-yaml --depth 5` makes no difference.

2
laibo•

If you run 'npm ls js-yaml' you should see which packages are using it and which version. Potentially the last remaining 1 out of 5 issue is a hard dependency and you have to up the version of some of the parent(s) by hand.

2
boppOP•

Thanks for the reply! If I run that, it says:

[email protected] /Users/bob/Sites/bolt-four
├─┬ @vue/[email protected]
│ └─┬ @intervolga/[email protected]
│   └─┬ [email protected]
│     └─┬ [email protected]
│       └─┬ [email protected]
│         └── [email protected]
├─┬ UNMET PEER DEPENDENCY [email protected]
│ └── [email protected]
├─┬ [email protected]
│ └─┬ [email protected]
│   └─┬ [email protected]
│     └── [email protected]
└─┬ [email protected]
  └─┬ [email protected]
    └── [email protected]  deduped

npm ERR! peer dep missing: eslint@^5.0.0, required by [email protected]

Also, it seems that it does use 3.13.1 already, so that doesn't explain why it keeps complaining. Well, not to this layman at least.

That last vulnerability in the list is this: https://www.npmjs.com/advisories/882 So, that doesn't have a patch yet, but imho that shouldn't prevent the other fixes from 'sticking', right?

1
laibo•

Under eslint's [email protected] it chains down to [email protected], maybe that's the issue? I believe you can skip Dev depencies with a switch when running the audit, as eslint does not make it to the final product.

1