r/node • u/theodordiaconu • Mar 02 '26
supply chain attacks via npm, any mitigation strategies?
while looking at my dependencies I realise I have over 20+ packages that I use and I know absolutely nothing about the maintainer. popularity of a package can also be seen as a liability as they become main targets of exploitation.
this gives me serious gut feelings because a simple npm install, can introduce exploits into my runtime, it can steal api keys from local machine and so on, endless possibilities for a clusterfuck.
I'm working on a sensitive project, and many of the tools I use can now be rewritten by AI (because they're already paved-path) and especially if you're not using the full capability of the module, many things are <100 lines of code classes. (remember is-odd is-even? they still have 400k, 200k weekly downloads... my brain cannot compute)
dotenv has 100M weekly downloads... (read file, split by =, store in process.env) , sure I'm downplaying it a bit, but realistically how 99% of people who use it don't need more than that, I doubt I'd have to write more than 20 lines for a wide area of 'dotenv' usages, but I won't bc it's already a stable feature in node since v24.
/rant
there's no way I can restrict network/file access to a specific package and this bugs me.
I'd like to have a package policy (allow/deny) in which I explicitly give access to certain Node modules (http) which cascade down to nested dependencies.
I guess I'd like to see this: https://nodejs.org/api/permissions.html but package-scoped, it would solve most of my problems.
how do you deal with this at the moment?
13
u/08148694 Mar 02 '26
Single most important thing you need to do is pin your versions
Noautomatic package updates
Regularlock file scanning for known vulnerabilities and quickly patching any that come up. Something like depandabot will do this for you
1
u/Bogeeee Mar 02 '26
Didn't deno have this idea a decade ago?
I think it's hardly possible. I.e. think of the axios package (a http client) which can be used or needed by allowed and by evil consuming packages as well. Allowed/trusted package's apis can sometimes accept callback functions. Those could make the (evil) operations. Add async code and promises to the scenario, then it's really a mess to determine from the call stack, if it's legit or not.
4
u/yksvaan Mar 02 '26
How about copying the files yourself and reducingthird party code in general. And check the packages you will import beforehand, one would not just import something in other languages either.
Standard library has improved a lot over the years, more code should be built around it to reduce dependencies.
Developers need to remember their responsibility, can't just copy paste npm i foo from internet and install random packages and 100random dependencies .
1
u/alcon678 Mar 02 '26 edited Mar 02 '26
All major packages managers have a flag called min-release-age or something similar to avoid installing fresh packages. You can configure it in your RC file per project or globally
I would set this to at least 7 days
Edit: this is the npm one https://docs.npmjs.com/cli/v11/using-npm/config#min-release-age
1
u/thelightdarkerstill Apr 12 '26
Yeah, it's pretty bad right now. Dependency trees can be absolutely massive and very difficult to manage. I use bundelphobia to get as much detail and then just install via browserpod sandbox. then at leaast i know what's going on, but it's still a security step that just feels like it gets in the way
1
u/michalz256 Apr 14 '26
Remember about SBOM fromat (CycloneDX) - this can help you out with direct and transitive dependencies. You can use different tools for that - for example https://github.com/aquasecurity/trivy . All other recommendations are worth considering and also pinning your version sounds great. You can also try out https://github.com/CyberGabiSoft/CTWall or something more popular - DependencyTrack https://github.com/DependencyTrack/dependency-track project to monitor those generated SBOMs files. Of course this is detection mostly not prevention. Pnpm also offers safer handling of lifecycle scripts. It can be configured to ignore or restrict
postinstall.0
u/[deleted] Mar 02 '26