There's nothing preventing those scripts from performing any actions as current user. You need to avoid running install scripts.
Most malicious packages are using those, but in rare cases (like lofygang recently) the packages may carry malicious code in the functionality too.
How to protect a project from malicious packages
- make sure you don't run lifecycle (postinstall) scripts unless they're known and necessary (see my talk on this topic)
- put 3rdparty code in a compartment, lock down the environment, decide on which powerful APIs to pass to each package.
The second step requires the use of Compartment, which is a work-in-progress in TC39 https://github.com/tc39/proposal-compartments/
But a shim exists. And Some tooling was built on top of that shim.
You could use the SES-shim directly and implement your own controls, or use the convenience of LavaMoat
LavaMoat lets you generate and tweak a per-package policy where you can decide which globals and builtins it should have access to.
LavaMoat also offers a tool to manage install scripts.
Here's my talk on SES and LavaMoat with a demo at the end.
How to set up LavaMoat
See LavaMoat docs for more details
- disable/allow dependency lifecycle scripts (eg. "postinstall") via @lavamoat/allow-scripts
npm i --ignore-scripts -D @lavamoat/allow-scripts
npx --no-install allow-scripts setup
npx --no-install allow-scripts auto
- then, edit the allow-list in package.json
- after every insstall/reinstall run
allow-scripts
- run your server or build process in lavamoat-node
npm i -D lavamoat
in your package.json add something like:
"scripts": {
"lavamoat-policy": "lavamoat app.js --autopolicy",
"start": "lavamoat app.js"
- run lavamoat-policy every time you make changes to your dependency tree and review the policy (see also: policy override)
- run
npm start
to start your app
Disclaimer: I contribute to LavaMoat and Endo. They are Open Source projects on permissive licenses.