CCyberwiki

Package install scripts

Package install scripts
TypePackage manager feature
Also known asLifecycle scripts, postinstall scripts, build scripts
Hookspreinstall, install, postinstall, and prepare for git dependencies
OWASP Top 10A03:2025 Software Supply Chain Failures
MITRE ATT&CKT1195.001 Compromise Software Dependencies and Development Tools
RelatedMalicious packages, Lockfile

A package published to npm can name shell commands in the scripts field of its package.json under the keys preinstall, install and postinstall. When a project installs that package, directly or as a dependency of a dependency, the package manager runs those commands on the installing machine.1 These are install scripts. They run at install time, whether or not the project's code ever imports the package.

When they run

npm runs the three install hooks during npm install, npm ci and a global npm install -g. A package installed from a git repository also gets its prepare script run, after its own dependencies are fetched, before it is packed and installed. When a package ships a binding.gyp file and defines no install script of its own, npm fills in node-gyp rebuild as the install command.1

A script does not need to be JavaScript; any executable file works. Since npm 7 these scripts run in the background, and their output shows only when the install runs with --foreground-scripts. A script that exits with a code other than 0 stops the whole install.1 On a CI/CD runner the scripts run inside the build job that installs the dependencies.

Why packages use them

The intended use is compiling native code. A native Node.js add-on has to build, or fetch, a binary for the operating system and processor it lands on, and that work happens on the target machine. npm treats compilation for the target architecture as the only valid use of an install or preinstall script and steers everything else to prepare.1 node-sass, for example, uses postinstall to build a native binary for Sass.2

Abuse

Historically, most compromised packages have used postinstall, which runs their code the moment the package is installed.3 A hostile script reaches an install through a confused or typosquatted name, or through a hijacked maintainer account that publishes a new version of a trusted package (Software supply chain compromise).

On 22 October 2021 the npm account of the ua-parser-js maintainer was taken over and three versions, 0.7.29, 0.8.0 and 1.0.0, were published. The library had millions of downloads a week. Each version carried a preinstall.js that checked the operating system, then fetched an XMRig Monero miner on Linux, and on Windows the miner plus a DLL that stole stored passwords.4 GitHub's advisory told users to treat any computer that had installed one of these versions as fully compromised and to rotate every key and secret on it from a different machine.5

Limiting install scripts

Package managers differ in their defaults. Under an allow list only named packages may run scripts, so a dependency that never needed a build cannot start running one when a compromised version is published.3

Table 1. Dependency install scripts by package manager
ManagerDefaultControl
npmRuns themignore-scripts=true in .npmrc turns them off. An allowScripts field in package.json, filled by npm approve-scripts, allows or denies packages by name; a script it does not cover still runs, with a notice6
pnpmBlocked unless allowedallowBuilds in pnpm-workspace.yaml; strictDepBuilds, on by default, fails the install on an unreviewed script7
BunBuilt-in allow list onlytrustedDependencies in package.json replaces the built-in list2
Yarn (current)Off for third-party packagesenableScripts in .yarnrc.yml, per package through dependenciesMeta8

With ignore-scripts set, npm still runs the script named in npm run, npm test or npm start, but none of the matching pre and post hooks.6 npm's allow list matches the resolved package rather than the name the package claims for itself,6 and pnpm will not approve a git or tarball dependency by name alone, because a name does not identify which code was downloaded.7 Bun applies its built-in list only to packages from the npm registry for the same reason.2

A pnpm project that needs one native build and wants another blocked lists both:

# pnpm-workspace.yaml
allowBuilds:
  esbuild: true
  core-js: false

Running pnpm approve-builds writes these entries. An approved package can still be compromised later, so an update to it deserves a look at what its script now does.3

In SecFlash

SecFlash reports a dependency version that was published to attack the people who install it as SF-SEC-713 on the Security page.

See also

References

  1. a b c d "scripts". npm Docs, CLI v11. GitHub.
  2. a b c Bun. "Lifecycle scripts". Bun documentation.
  3. a b c "Mitigating supply chain attacks". pnpm documentation.
  4. back Abrams, Lawrence (22 October 2021). "Popular NPM library hijacked to install password-stealers, miners". BleepingComputer.
  5. back GitHub (22 October 2021). "Embedded malware in ua-parser-js" (CVE-2021-4229, GHSA-pjwm-rvh2-c87w). GitHub Advisory Database.
  6. a b c "config". npm Docs, CLI v11. GitHub.
  7. a b pnpm. "Build Settings". pnpm documentation.
  8. back Yarn. "Settings (.yarnrc.yml)". Yarn documentation.