Lesson 13 of 14 · 20m
Dependencies are hostile too: pins, hashes, and dependency confusion
ctx, torchtriton, and Ultralytics shipped real payloads — pin, hash-pin, and audit what you install.
pip install requests resolves to "whatever the latest artifact on PyPI says it is, plus whatever that artifact depends on." Your tool's attack surface isn't the code you reviewed — it's the thirty transitive packages nobody reviewed, fetched from a public registry where a name can change hands. The last few years made this concrete: the ctx package was hijacked in May 2022 when its author's email domain expired and an attacker pushed releases that exfiltrated AWS credentials; torchtriton dependency-confused PyTorch nightly users in December 2022; and in December 2024 the Ultralytics YOLO package shipped versions 8.3.41/8.3.42 containing a cryptominer via a compromised build pipeline. Every one of those was pip install <something you trusted>.
The wrong model: "pip install fetches what I intend"
It fetches the best-matching artifact the index serves right now, resolved fresh each run. Two runs a week apart can install different code for the same install line — different transitive pins, different versions, occasionally different owners. The defense is pinning, and the enforcement is hashes.
# Loose: installs whatever resolves today
pip install requests
# Pinned: installs exactly this version
pip install requests==2.32.3
# Hash-pinned: installs exactly this artifact, byte-for-byte
pip install --require-hashes -r requirements.txtPin, then hash, then audit
A requirements.txt with == pins stops version drift; --require-hashes goes further — every download must match a recorded SHA-256, so a republished or tampered artifact fails the install instead of shipping to your team. Hash-pinning requires every package hashed, including transitive deps, so generate the file mechanically:
# pip-tools: requirements.in (what you ask for) → requirements.txt (exact, hashed)
pip-compile --generate-hashes requirements.in
# or uv, same idea, faster
uv pip compile --generate-hashes requirements.inThen pip install --require-hashes -r requirements.txt everywhere — dev, CI, deploy — and the artifact set is identical on every machine. This is the same discipline as lockfiles in every other ecosystem; Python's version of it is just opt-in.
Typosquats and dependency confusion are two different bugs
Typosquatting is a look-alike public name — python3-dateutil, jeIlyfish (capital-I), colourama — that a fat-fingered install pulls instead of the real package. PyPI removes them when caught; the install-time defense is naming your own packages carefully and pinning hashes, which makes the wrong package fail loudly instead of silently running.
Dependency confusion is subtler and it's the trap for internal tools: your private package acme-scanner is registered as a name on the public PyPI by anyone, your pip install resolves it from the public index (highest version wins), and you run the attacker's code inside your network. Alex Birsan's February 2021 research proved this at Apple, Microsoft, and PayPal — a fake package on a public registry, installed by internal builds, was all it took. The defenses: internal packages only ever installed from your own private index (--index-url, --extra-index-url for public deps), and name-claimed on public PyPI for good measure so the slot can't be squatted.
Audit what you already pinned
Pinning is not verification. pip-audit (from PyPA) scans a resolved environment against the OSV vulnerability database and reports which installed versions carry known CVEs:
pip-audit # audit the current environment
pip-audit -r requirements.txt
uv audit # uv's equivalentRun it in CI on every pinned file. A pinned hash keeps a known-good artifact stable; pip-audit is what tells you the artifact wasn't good to begin with.
Try this
Take a tool with unpinned deps, write requirements.in with just the direct deps, run pip-compile --generate-hashes, inspect the transitive list it produces (probably a third more packages than you expected), then install --require-hashes in a fresh venv. Run pip-audit on the result.
Field notes
requirements.txtvsrequirements.in— one is the ask, the other is the answer; keep both in the repo.- A lockfile doesn't expire. Re-run
pip-auditon a schedule — today's pinned set is next quarter's CVE list. - The bug that ships:
pip install fooinside a Dockerfile or CI with no pin and no hash — reproducibility and tamper-resistance both gone.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
RSA Is Simple. Deploying RSA Correctly Almost Never Is.
The capstone to our RSA series. The math checks out. Shipping it as written gets you breached. This is the nineteen-year-old attack that proves it, and what belongs in production instead.
Implementing RSA, AES-GCM, and a TLS 1.3 Handshake from Scratch in Python
Your HTTPS Is Toast (But Google Has a Plan): Merkle Tree Certificates and the Post-Quantum Web
RSA-2048 can be broken with fewer than 100,000 qubits. What that means for TLS, certificate infrastructure, and the post-quantum transition already underway.
RSA Signatures: The Version in Most Tutorials Is Forgeable
Signing is encryption with the keys swapped, and that symmetry is exactly what makes the naive implementation breakable. Here are two forgeries executed against textbook RSA signatures, and what PSS does that hashing alone cannot.