How npm and PyPI install scripts steal your secrets
By the CapyShield team · · 4 min read
In short
npm runs preinstall, install and postinstall scripts from every dependency on npm install and npm ci, and pip runs setup.py when it builds a source distribution, both with your environment and network. Recent npm worms used exactly these hooks to steal tokens. Block unapproved scripts, keep secrets out of the install step, and enforce an outbound allowlist so stolen data can't leave.
When you run npm install or pip install, you are not just downloading files. You are running code written by the authors of every package in your dependency tree, with your user account, your environment variables and your network. That code runs before your tests, before your linter and before any human looks at it. This post explains exactly where that code runs, what a malicious version does with it, and the controls that actually stop the theft.
How can npm install run code on my machine?
Through lifecycle scripts. Any package can declare preinstall, install and postinstall scripts in its package.json, and both npm install and npm ci run them for every dependency, direct or transitive. The npm docs also note that a package with a binding.gyp file and no install script of its own gets one by default: node-gyp rebuild. So even packages that never wrote a script can run a native build.
The script is an arbitrary shell command. It inherits the environment of the process that ran the install, which in CI typically includes NPM_TOKEN, GITHUB_TOKEN, cloud keys and anything else the job exported. On a laptop it can also read ~/.npmrc, ~/.aws/credentials, SSH keys and git credentials, because it runs as you.
Recent npm worms used exactly this hook. Shai-Hulud 2.0 ran in the preinstall phase, and the ChainDrop packages shipped a preinstall dropper that downloaded the Bun runtime plus a second stage. The Nx packages compromised in the s1ngularity incident used a postinstall script to scan the machine for sensitive data and upload it to a public GitHub repository through the GitHub CLI.
Do Python packages have the same problem?
Yes, in a slightly different shape. When pip installs from a source distribution (an sdist, usually a .tar.gz), it has to build it, and building runs the package's build backend: for older packages that means executing setup.py. Anything in that file runs at install time with your environment.
Wheels are different: a wheel is unpacked, not built, so installing one doesn't run package code. The code still runs the first time you import it, but that happens in your application or tests, not silently during dependency resolution. pip lets you refuse sdists entirely:
# Fail instead of building any package from source
pip install --only-binary :all: -r requirements.txt
# Pin exact artifacts: a tampered file with the same version number fails the hash check
pip install --require-hashes -r requirements.txtThe pip docs warn that packages without a binary distribution will fail to install under --only-binary :all:. That is the point: you find out which dependencies need a build step and decide about each one, instead of running them all by default.
Can I just disable install scripts?
Often, and it is the cheapest control you have. npm has an ignore-scripts setting (default false) that stops it running scripts from package.json files during install:
# per run
npm ci --ignore-scripts
# or for the project, in .npmrc
ignore-scripts=trueTwo caveats. First, explicitly run scripts still work: npm test, npm start and npm run <name> run their script but skip the matching pre- and post-scripts. Second, some packages genuinely need their install script, usually to compile a native addon or download a platform binary. Those break, and you have to rebuild them deliberately.
pnpm takes the safer default. Its build settings don't run dependency build scripts unless you allow them: dangerouslyAllowAllBuilds defaults to false, the allowBuilds map lists the packages you have approved, and strictDepBuilds (default true in current versions) fails the install when a dependency has an unreviewed build script. That turns "a new dependency wants to run code" into a review step rather than a surprise.
What still leaks if scripts are disabled?
Disabling scripts closes the install-time door, not every door. The malicious code is still in node_modules, and it runs the moment your build, tests or dev server import it, with the same environment. Build tools, test runners and bundler plugins are dependencies too. And on a laptop, the developer who runs npm run dev right after the install gives the payload everything a postinstall would have had.
So treat script blocking as one layer, and add two more:
- Keep secrets out of the install step. In CI, the step that runs
npm cirarely needs a publish token or cloud credentials. Scope secrets to the step that uses them, and use a read-only registry token if a private registry is involved. - Control where the process can send data. Stealing a secret is only half the attack; it then has to leave the machine. A package install needs your registry and little else, so an outbound allowlist turns exfiltration into a failed connection.
How do I see and block what an install sends out?
This is the gap CapyShield fills. It wraps a single command, runs it behind a local proxy, and records every outbound connection. Start by watching, which blocks nothing, and let it write an allowlist from what it saw:
capyshield watch --emit-policy capyshield.yaml -- npm ci
capyshield guard --config capyshield.yaml -- npm ciguard enforces that policy and exits with code 2 when it blocks something, so CI fails loudly. It checks three things on the way out:
- Host policy: anything outside
allow_hostsorallow_presetsis blocked, which also stops payloads that encrypt their loot before sending it. - Secrets in the payload: 210+ credential formats plus fingerprints of your sensitive environment variables, matched as plain, Base64, URL-encoded and hex.
- Payload size:
max_outbound_bytesfor hosts that aren't allow-listed.
Inspecting HTTPS payloads needs tls_inspection: mitm; the default sni mode enforces hosts and byte counts only. The proxy is cooperative: npm, pip, uv, curl and git honor proxy variables, but code that opens raw sockets can bypass it, so for a hard boundary run the install in a container or network namespace whose only way out is the proxy. The docs and the source on GitHub cover the details.
Frequently asked questions
- Does npm ci run install scripts?
- Yes. npm ci runs the preinstall, install and postinstall scripts of every dependency, just like npm install. Pass --ignore-scripts or set ignore-scripts=true in .npmrc to stop it.
- Does pip install run code from the package?
- It does when it builds a source distribution, because building runs the package's build backend, such as setup.py. Installing a wheel only unpacks files. pip install --only-binary :all: refuses to build from source.
- Is disabling install scripts enough to stop malicious packages?
- No. It stops install-time execution, but the malicious code still runs when your build, tests or app import the package. Combine it with tightly scoped secrets and an outbound network allowlist.
- How can I see what an npm install connects to?
- Run it under capyshield watch, which records every outbound connection without blocking anything. Add --emit-policy to turn what it saw into an allowlist that capyshield guard can enforce.