Skip to content

Lesson 1 of 14 · 13m

Install Time Is Run Time

npm lifecycle scripts and pip's setup.py execute package code with your credentials before your app ever imports it.

The package is a program before it is a library

Here is what happens when you run npm install ua-parser-js. npm resolves the version, downloads the tarball from registry.npmjs.org, unpacks it into node_modules, and then — before your application code ever touches it — executes the package's lifecycle scripts: preinstall, install, postinstall. Those scripts run as your user, inside your shell's environment. Your ~/.ssh is readable. Your ~/.npmrc auth token is readable. AWS_ACCESS_KEY_ID, GITHUB_TOKEN, DATABASE_URL — whatever your environment exports, the script inherits.

On October 22, 2021, somebody pushed ua-parser-js versions 0.7.29, 0.8.0, and 1.0.0 from the real maintainer's npm account. Each shipped a preinstall script that ran jsextension — a credential stealer (browser passwords, OS credentials) on Windows and Linux plus a cryptominer. ua-parser-js pulled roughly eight million downloads a week. The same day, the same actor shipped poisoned versions of coa (2.0.3, 2.1.3) and rc (1.2.9, 1.3.9, 2.3.9). Anyone who ran a routine npm install or npm update in that window executed the payload. No import required. No require. The install was the run.

Note the ordering detail that makes this worse: scripts fire for every package in the resolved tree, including transitive dependencies you never saw in package.json. preinstall and postinstall bracket the unpack; prepare runs on npm install in a local directory and before pack. The dep you chose is one line of your manifest; the deps that run code are the whole graph below it.

The wrong mental model

Most developers think of a dependency as dead code — a library that only does something when called. So the trust question gets framed as "is this library's parsing logic safe to run?" Wrong unit of analysis. The artifact npm hands you is a tarball with a package.json inside, and package.json has a scripts field, and npm executes that field as part of installation. You are not trusting the parser. You are trusting an arbitrary program.

The model persists because lifecycle scripts have a legitimate use: node-gyp compiles native modules at install, esbuild fetches its platform binary, husky wires git hooks. But npm makes no distinction between "rebuild sqlite3 bindings" and "POST ~/.aws/credentials to a VPS." Both are just postinstall.

Python has the same door

pip is not cleaner. A wheel (.whl) is a zip that unpacks — no code runs at install, which is exactly why pip prefers wheels. An sdist (.tar.gz) is different: setup.py is a Python program, and pip executes it during install to resolve metadata and build. pip install --no-binary :all: somepkg forces the sdist path, and whatever setup.py contains — including a top-level urllib.request.urlopen(...) firing during python setup.py ... — runs.

PyPI's ctx package made this concrete in May 2022: an expired maintainer email domain let an attacker reset the account, and the "updated" versions shipped install-time code that scraped AWS credentials and environment variables to a Heroku endpoint.

What actually protects you

  • npm install --ignore-scripts (or ignore-scripts=true in .npmrc) skips every lifecycle script. Native packages will fail to build — that is the point of an allowlist, not a bug. pnpm already works this way: dependency build scripts don't run unless the package appears in onlyBuiltDependencies.
  • Inspect before you install: npm view <pkg> scripts shows every lifecycle hook without downloading anything.
  • In CI, pair npm ci with --ignore-scripts, then explicitly rebuild the natives you know you need (npm rebuild node-sass). Denying scripts by default and opting in beats the inverse.
  • Remember the credential blast radius: whatever environment the install runs in is what the script can read. Installs inside Docker with no secrets mounted aren't a sandbox — they're just a smaller blast radius.

The fix is not paranoia about every package. It's knowing that npm install is chmod +x on a stranger's code — and acting like it.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.