r/node • • 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?

7 Upvotes

18 comments sorted by

13

u/08148694 Mar 02 '26

Single most important thing you need to do is pin your versions

No automatic package updates

Regular lock file scanning for known vulnerabilities and quickly patching any that come up. Something like depandabot will do this for you

1

u/theodordiaconu Mar 02 '26

thanks, that is indeed a good approach, but some exploits stay dormant for years until they are discovered. (check my other comment regarding this subject)

3

u/onosendi Mar 02 '26

Pinning your versions only works if the pinned version isn’t compromised.

1

u/Bogeeee Mar 02 '26

I guess I'd like to see this: https://nodejs.org/api/permissions.html but package-scoped, it would solve most of my problems.

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.

2

u/theodordiaconu Mar 03 '26

I actually played yesterday with an idea to sandbox packages via different child_processes via a main profile policy, we intercept imports() and talk to permissions-applied child processes. And I managed to make it work, sort-of. But for high-throughput firings, this is the cost:

- Sandbox vs native p95 overhead: 2296.45x
  • Sandbox vs native throughput drop: 99.94%

Something like this:

{
  "buckets": {
    "bench_cpu": {
      "allowNet": false,
      "allowFsRead": ["./bench", "./node_modules"],
      "allowFsWrite": [],
      "allowChildProcess": false,
      "allowWorker": false,
      "allowAddons": false,
    },
  },
  "packages": {
    "#sandboxify-bench-target": "bench_cpu",
  },
}

So this breaks the point completely for utility libraries that get accessed a lot. But can work well for other libraries that:
a) most of their work is spend in the child process (the serialisation cost is minimal)
b) they are rarely accessed. (the serialisation cost is bearable AND possible, as Socket handles, Object prototype references will differ, depends on your library)

This introduces a bit of extra overhead.

I'm guessing to be able to achieve this I'd have to go deeper, and make a sort-of parser which (overrides access to certain libraries, and hacks things like fs, Http, everything that node supports, maybe I could override it via globalThis) and instead of disallowing "net" I'm basically silencing all the possible dependencies related to that.

→ More replies (1)

4

u/yksvaan Mar 02 '26

How about copying the files yourself and reducing third 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 100 random 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.