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.
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.
How the attack unfolds
What it looks like in practice
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.
How Guard catches this
How to stop it
Use Guard to pause package installs that include postinstall scripts. Review the script contents before allowing execution. Pin dependencies to known-good versions and use lockfiles to prevent unexpected version changes.
Common questions
Postinstall scripts run automatically with the developer’s full permissions during npm install. They can read files, make network requests, and modify configuration — all before anyone reviews the code. In AI coding environments, they can also install backdoor MCP servers or modify agent trust settings.
No. Guard pauses and shows you the script contents so you can decide. Many legitimate packages use postinstall scripts for compilation or setup. Guard gives you the information to make an informed choice, rather than blocking all scripts.
Typosquatting is when an attacker publishes a package with a name similar to a popular package — for example, "chark-utils" instead of "chart-utils". Developers who mistype the package name install the malicious version, which often includes a postinstall script.
More threats to know about
Prompt injection via issue comments and pull requests
Attackers embed hidden instructions in GitHub issues, PR comments, and commit messages. When an AI agent reads these to help triage or review, it follows the embedded instructions — potentially approving malicious code or leaking repository secrets.
Read advisoryShadow MCP server discovery and persistent access
MCP servers added to a project during development can persist in configuration files and maintain access to the agent’s context window long after they are forgotten. These "shadow" servers continue receiving tool calls and may be modified by attackers who compromise the original server.
Read advisoryStop this threat before it reaches your agent
Install HOL Guard to get real-time protection against this attack and others like it.