Sub topic 1
How a package name becomes installed code
Prerequisites
This chapter assumes you have:
- Used
pip installornpm installin a real project. - Seen a public registry such as PyPI or the npm registry.
- Basic command-line skills.
You do not need to know anything about slopsquatting yet. This chapter covers the mechanism that the attack depends on.
From a name to files on disk
A package manager turns a short string into code that runs on your machine. When you type pip install requests, you give it only a name. The tool then does the following:
- It reads its configured index. By default this is
https://pypi.org/simple/for pip andhttps://registry.npmjs.org/for npm. - It looks up the name and gets a list of available versions and files.
- It picks a version that fits your constraints and your platform.
- It downloads the archive, and for each dependency of that package it repeats steps 2 to 4.
- It unpacks the files and runs any install steps the package defines.
The registry does not check that a name matches what you meant. It also does not check who published it. A name belongs to whoever registered it first. If the name exists, you get its code. If it does not exist, the install fails with an error such as No matching distribution found.
The diagram shows the key point for this tutorial. The only gate between a name and code on your machine is whether the name exists. Nothing in the flow asks whether a person or a model made up the name.
Names are a flat, shared namespace
A namespace is the set of all names a registry hands out. PyPI and npm use one global namespace each. Anyone with an account can publish a new name, and no review happens before it goes live. This openness is why the ecosystems grew so quickly. It also means a name has no built-in trust.
Two details matter later:
- Normalization. PyPI treats
My_Package,my-package, andmy.packageas the same project. Two names that look different can point to one package. - Transitive dependencies. A package you install can list other packages it needs. You never typed those names, but the tool resolves and installs them as well. Every name in that tree is a name someone must have registered.
Install steps can run code
Downloading files is harmless by itself. Risk starts when the install runs code.
- npm supports lifecycle scripts such as
preinstall,install, andpostinstall. They are shell commands listed inpackage.json, and npm runs them duringnpm install. - pip installs wheels (prebuilt archives) without running package code. Installing a source distribution (sdist) runs its build backend, and for older packages that means executing
setup.py. Either way, code from the package can run at install time.
Even without install scripts, the package runs later when you import it. So a bad package gets a chance to run at install time, at first import, or both.
Seeing it for yourself
You can look at what a name resolves to without installing it. These commands only read metadata.
# Python: ask the index which versions exist, without installing
pip index versions requests
# npm: show metadata, including when the package was created
npm view express time --jsonpip index versions is marked experimental, but it works in current pip releases. If the name does not exist, both commands return an error. A later chapter builds a full checking script on the same idea.
For npm, you can also see which scripts a package would run:
npm view esbuild scriptsAn empty result means the published package.json declares no lifecycle scripts. A non-empty result lists commands that would run on install.
Why this matters for AI suggestions
A coding assistant that suggests pip install some-helper-lib hands you a name. Your package manager handles that name exactly as it handles requests. It does not know the name came from a model. If someone registered that name, the manager installs their code.
The chain, then, is short: a name, a registry lookup, a download, possible install-time code. The next chapters look at why models produce names that no one registered, and what happens when an attacker registers them.
Exercise
A teammate says, "pip only downloads files, so installing an unknown package cannot hurt me until I import it." Give two reasons this is wrong.
Show answer
First, installing a source distribution runs the package's build backend (and often setup.py), so code can run during install. Second, npm runs lifecycle scripts such as postinstall during install. Also, a package you install can pull in transitive dependencies that you never named, and any of those can carry install-time code too.
Exercise
What are the only two outcomes when a package manager looks up a name on a public registry, and which of them can be dangerous?
Show answer
Either the name does not exist and the install fails, or the name exists and its code is installed. The second can be dangerous, because the registry does not check who registered the name or whether it is what you meant.