critical severityCurated advisory

npm postinstall script abuse in AI coding environments

Malicious npm packages use postinstall scripts to execute arbitrary code during installation. In AI coding environments, these scripts can modify agent configuration, install backdoor MCP servers, or exfiltrate project secrets — all before the developer reviews the package.

Key facts

npm postinstall script abuse in AI coding environments is a critical-severity AI agent security threat targeting npm postinstall scripts, package.json dependencies, agent configuration files, project .env files. Use Guard package-script controls to review supported npm, pnpm, yarn, and Bun lifecycle scripts before they execute locally. Keep lockfiles and pinned dependencies, and review any newly introduced install script.

4 affected surfacesLast updated Jun 25, 2026
Affected surfacesnpm postinstall scriptspackage.json dependenciesagent configuration filesproject .env files
The attack

What happens

An attacker publishes a malicious npm package (or compromises a popular one) that includes a postinstall script. When the package is installed — often as a transitive dependency — the script runs automatically with the user’s permissions, before anyone reviews the code.

Step by step

How the attack unfolds

1Attacker publishes a package with a helpful name similar to a popular package (typosquatting) or contributes a malicious postinstall to an existing package.
2Package is installed directly or as a transitive dependency during npm install.
3npm automatically runs the postinstall script defined in package.json.
4The script reads .env files, sends secrets to an external server, or modifies the agent’s configuration to trust a backdoor MCP server.
5The attack persists across reinstalls because the modified config file remains even after the package is removed.
Example

What it looks like in practice

Scenario

A developer runs npm install to add a charting library. A transitive dependency called "chart-utils" includes a postinstall script that reads the project’s .env file and sends its contents to an attacker server. The developer never sees the script because it runs in the background. The next day, the team’s API keys are used from an unfamiliar IP address.

Detection

Detection and Guard coverage

Guard recognizes supported npm, pnpm, yarn, and Bun package-script surfaces, including postinstall and other lifecycle scripts, and evaluates them before local execution.
Guard can warn, block, or require approval for supported package-script actions according to the active local policy.
Guard Cloud Firewall provides review visibility for supported package changes. Do not infer a universal malicious-package reputation lookup from this advisory.
Mitigation

How to stop it

Recommended action

Use Guard package-script controls to review supported npm, pnpm, yarn, and Bun lifecycle scripts before they execute locally. Keep lockfiles and pinned dependencies, and review any newly introduced install script.

Guard configuration
Use Protection Center to review which Guard controls are active on this device.
Use `hol-guard command controls status` and `hol-guard command controls list` to inspect the active local control catalog.
Guard coverage is action- and harness-specific. Do not treat an advisory label as a configurable Guard setting unless it appears in the current control catalog.

Related threat guides

Use the protections Guard supports here

Guard can review or block supported actions for this threat. The advisory above calls out what Guard covers and where other controls are still needed.

npm postinstall script abuse in AI coding environments | HOL Guard Advisory