Skip to content

Lesson 6 of 20 · 18m

Dependencies: Pin What You Actually Run

Ranges resolve at install time — lock files, --require-hashes, and the PyPI supply-chain incidents that made pinning a security control.

pip install "requests>=2.31" does not install requests. It installs whatever the resolver selected that day — today 2.32.3, next month something newer — plus every transitive dependency it drags along. Two builds of the same commit, a week apart, run different code. The wrong mental model is "the dependencies in pyproject.toml are what my app runs." They are constraints on a search. The lock file is the answer.

Libraries publish ranges; applications lock

A library's job is to coexist: dependencies = ["requests>=2.31,<3"] declares what it tolerates. An application's job is to be reproducible: every transitive package pinned to an exact version, ideally to an exact artifact. pip-tools (pip-compile requirements.in -o requirements.txt --generate-hashes) produces a fully pinned tree with a sha256 per wheel; uv lock writes uv.lock; poetry writes poetry.lock. Commit the lock file.

uv sync and pip install -r requirements.txt --require-hashes install exactly the recorded tree — --require-hashes refuses any artifact whose file doesn't match, closing the gap where a version stayed the same but the bytes behind it changed.

Why anyone bothers: the registry is adversarial

December 2022: PyTorch's nightly builds referenced torchtriton, a dependency published only to PyTorch's own index. Someone registered the name on public PyPI and shipped a package that exfiltrated /etc/passwd, /etc/hosts, SSH keys, and environment variables off every machine that installed nightly. That is dependency confusion — Alex Birsan demonstrated the class in 2021 by beating internal package names at Apple, Microsoft, and PayPal with public uploads — and it works because resolvers compare names and versions, not provenance. A CI step passing --index-url <private> --extra-index-url https://pypi.org/simple lets the public index win whenever it publishes a higher version of your internal name.

May 2022: the ctx package's maintainer email domain expired. An attacker registered the domain, received the PyPI password-reset email, and shipped malicious versions that stole AWS credentials — the same story hit phpass. PyPI made 2FA mandatory for maintainers in 2023 largely because of that takeover class. Typosquatting — reqeusts, homoglyph lookalikes — never stopped; PyPI removes hundreds of malicious packages a year and still misses a steady stream.

The practice

  • Applications: lock file committed; --require-hashes (pip) or uv sync (which always verifies) for deploy installs. Nobody installs loose ranges in production.
  • Private packages: one index. Never mix --extra-index-url https://pypi.org/simple with a private index. uv's [[tool.uv.index]] with explicit = true binds a package name to one index.
  • Audit in CI: pip-audit checks the installed tree against the OSV advisory database and reads uv.lock too. A new CVE in a pinned dep should fail the build, not wait for a human to notice.
  • Updates are deliberate: Dependabot or Renovate PRs, and a version bump gets reviewed like any code change — read the package's diff, not just yours. The torchtriton attacker shipped a "newer" version; nothing about a bump is safe by default.
  • Pin requires-python, and review the lock file under the same rigor as the code.

Expect the transitive CVE: the advisory that fires in pip-audit is almost always two levels below what you declared — the dep of your dep. That is the other reason the lock file earns its place: it is the only artifact listing what you actually run.

The tree is bigger than your list

Your dependencies are the visible tip. uv tree or pipdeptree prints the full resolved tree — a project declaring six direct deps routinely resolves forty or more transitives. Each of those is code running in your process that you never picked. ResolutionImpossible — the resolver's error when ranges conflict — is the constraint solver telling you the intersection is empty, not that it failed.

Ranges describe tolerance; the lock describes reality. The lock file is the artifact that makes two builds identical.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.