Sub topic 2
Why Installing a Package Can Run Attacker Code
This chapter assumes you have used pip or npm to install packages, know what PyPI and the npm registry are, and can run commands in a terminal. It explains why an install can run code you never read. That is the reason a hallucinated package name is dangerous.
Install is not just download
Many developers think of pip install or npm install as copying files into a folder. In practice, the package manager often runs code from the package while it installs. That code runs with your user's permissions, before you have imported or called anything.
If the package is malicious, the attack starts at install time. Your own program does not have to run.
Where install-time code runs
npm lifecycle scripts
An npm package can declare scripts in its package.json. The scripts named preinstall, install and postinstall run automatically during npm install.
{
"name": "example-package",
"version": "1.0.0",
"scripts": {
"postinstall": "node setup.js"
}
}When you install this package, npm runs node setup.js on your machine. The file could build a native module, which is the legitimate use. It could also read environment variables and send them to a remote server.
Python build and setup code
Python packages come in two main forms:
- A wheel (
.whl) is a prebuilt archive. Installing it mostly copies files. It does not runsetup.py. - A source distribution (sdist) must be built first. Building runs
setup.pyor the build backend named inpyproject.toml. That is arbitrary Python code.
If no wheel matches your platform or Python version, pip falls back to the sdist. An attacker can publish only an sdist, which forces the build step to run.
# setup.py in a malicious sdist (illustration only; this prints instead of stealing)
import os
from setuptools import setup
print("Running during install as user:", os.getenv("USER"))
# A real attack could read ~/.ssh, ~/.aws, or environment variables here.
setup(name="example-package", version="1.0.0")Wheels are not automatically safe. Code in the package still runs when you import it. Some packages also ship .pth files, which Python runs at interpreter startup.
What the code can reach
Install-time code runs as you. It can usually do what you can do:
| Target | Examples |
|---|---|
| Files | `~/.ssh`, `~/.aws/credentials`, `.env` files, browser data |
| Environment | API keys and tokens in environment variables |
| Network | Outbound requests to send data out or download more code |
| Persistence | Shell profile edits, cron jobs, startup entries |
| Project | Edits to other source files or to the lockfile |
In CI, the reach can be wider. Build servers often hold deploy keys and cloud credentials.
Why this matters for hallucinated names
The registry does not check whether a package is safe or whether it matches what you meant. It matches the name you typed. If an attacker registers a name that a model tends to invent, an install of that name pulls the attacker's package and runs its install code. The later chapters cover how the attack is built and how to check a name first.
Reducing install-time exposure
You can limit what runs during install:
- With npm, use
npm ci --ignore-scriptsto skip lifecycle scripts. Some legitimate packages need their scripts, so test your project afterward. - With pip, use
pip install --only-binary=:all: package-nameto refuse sdists, so no build code runs. The install fails if no wheel exists, which is a useful signal. - Install in a container or virtual machine without your real credentials, when you are trying an unfamiliar package.
# npm: install without running lifecycle scripts
npm ci --ignore-scripts
# pip: accept only prebuilt wheels, so no setup.py runs
pip install --only-binary=:all: requestsThese steps shrink the exposure. They do not remove it, because the package's code can still run when you import it. They are one layer, and the later chapters on verification and lockfiles add others.
Exercise
A developer runs pip install --only-binary=:all: some-new-package, and the install succeeds. Does this guarantee the package is safe? Explain what the flag does and does not prevent.
Show answer
No. The flag blocks source distributions, so no setup.py or build backend code runs during install. A wheel was installed instead. The package's code can still run when it is imported, and .pth files in the wheel run at Python startup. The flag reduces install-time risk but does not prove the package is trustworthy.