Sub topic 11
Registry Defenses and Organizational Dependency Policy
This chapter assumes you can use pip or npm, know how PyPI and the npm registry work, and have read the earlier chapters on slopsquatting, verification, and lockfiles. Individual habits help, but they don't scale to a team. A team needs rules that make the safe path the default one.
What registries can and cannot do
Public registries such as PyPI and npm are open by design. Anyone can register a name, and the registry cannot know whether a name was invented by a model. Registries do offer some protections:
- Malware reporting and takedown. Users and researchers report packages, and maintainers remove them. Removal happens after the package is already live, so there is always a window of exposure.
- Name-similarity checks. Some registries block names that are too close to popular ones. This helps against typosquatting, but a hallucinated name is usually a plausible, distinct name, so it often passes.
- Account protections. Two-factor authentication and trusted publishing (publishing from a verified CI workflow instead of a stored token) reduce account takeover. They don't stop a new attacker from registering a new name.
- Provenance metadata. Both ecosystems support attestations that link a release to its source repository and build. A package with provenance is easier to trust. A package without it is not necessarily bad.
Treat these as a safety net under your own controls, not a replacement for them.
Put a controlled registry in front of the public one
The strongest organizational control is to stop developers and build systems from talking to the public registry directly. Instead, point them at an internal proxy or mirror, such as Artifactory, Nexus, or a cloud provider's artifact repository. The proxy fetches from PyPI or npm only when policy allows.
Useful policies on the proxy:
- Allowlist. Only approved packages can be fetched. A hallucinated name fails with "not found" because nobody approved it.
- Minimum age. Block versions or packages published in the last few days. Most malicious packages are caught quickly, so a short delay removes much of the risk.
- Namespace pinning. Route your internal package names to the internal registry only. This also stops dependency confusion.
- Audit log. Record who requested which package and when. This is essential during incident response.
Point tools at the proxy
Both package managers read the registry address from configuration. Commit these files so everyone gets the same setting.
# pip.conf (or ~/.config/pip/pip.conf)
[global]
index-url = https://artifacts.example.com/api/pypi/pypi-approved/simple# .npmrc
registry=https://artifacts.example.com/api/npm/npm-approved/
ignore-scripts=trueThe hostname above is a placeholder. Use your own proxy's address. Setting ignore-scripts=true in .npmrc makes the install-script restriction a project default instead of a flag people must remember.
Avoid --extra-index-url in pip. It lets pip pick the highest version across several indexes, which is the mechanism behind dependency confusion.
Write a dependency policy
A policy turns the controls into rules people can follow and reviewers can enforce. Keep it short. A workable policy covers:
| Area | Example rule |
|---|---|
| Adding a dependency | A pull request must name the package, explain why it is needed, and link its source repository. |
| AI-suggested packages | A human must verify any package that an assistant or agent proposes. Agents may not run installs outside a sandbox. |
| Pinning | All dependencies are locked, with hashes for Python. CI installs from the lock file only. |
| Install scripts | Disabled by default. Exceptions are listed and reviewed. |
| Updates | Automated update tools open pull requests. Nobody installs "latest" by hand. |
| Ownership | Each approved package has a team that answers for it. |
Enforce the rules in CI so they don't depend on memory. For example, fail the build if the lock file changed but the pull request has no dependency review label, or if a new package appears that is not on the allowlist.
Handling exceptions
Sometimes a developer needs a package that is not approved. Give them a fast, documented path: a request form, a named reviewer, and a target response time. If the path is slow, people will bypass the proxy with a personal laptop or a direct install, and you lose visibility.
Exercise
A coding agent in your CI pipeline suggests installing a package called fastjson-utils, which no one on your team has used. Your organization runs an internal proxy with an allowlist and a minimum package age of seven days. What happens when the agent tries to install it, and what should the developer do next?
Show answer
The proxy rejects the request because the package is not on the allowlist, and it records the attempt in the audit log. The developer should verify that the package exists, check its age, maintainers, and source repository, and decide whether it is really needed. If it is, they submit an approval request through the exception process. They should not bypass the proxy by installing it directly.