Sub topic 9
Lockfiles, Hash Pinning, and Restricted Install Scripts
This chapter assumes you can use pip and npm, know what a requirements file or package.json is, and have read the earlier chapters on the slopsquatting attack chain and on checking a package before you install it. Checking a name is a manual step. Lockfiles, hashes, and script restrictions make the package manager enforce the result of that check.
What these controls do
A hallucinated name only becomes a compromise if it gets installed. Three controls cut the path between "the assistant suggested it" and "its code ran on my machine."
- A lockfile records the exact versions that were reviewed, so a new name cannot appear without a visible change in version control.
- Hash pinning records a cryptographic digest of each package file. The installer rejects any file that does not match.
- Disabling install scripts stops a package from running code at install time, which is the main way a malicious package gets control.
None of these proves a package is safe. They make sure that only packages someone approved get installed, and that approval is recorded in a diff a reviewer can read.
Python: hash-pinned requirements
The pip-tools package compiles a short input file of top-level dependencies into a fully pinned file. With --generate-hashes, each entry also gets the digests of every published file for that version.
# Python: compile a hash-pinned requirements file, then refuse anything without a matching hash
pip-compile --generate-hashes requirements.in
pip install --require-hashes -r requirements.txtPut the dependencies you chose in requirements.in. The compiled requirements.txt then lists each package, including transitive ones, with lines like --hash=sha256:.... In --require-hashes mode pip applies three rules:
- Every requirement must be pinned to an exact version with
==. - Every requirement must have at least one hash.
- The downloaded file must match one of those hashes.
If an assistant adds import fastjsonx and someone runs pip install fastjsonx, the name is not in the hashed file. A command that installs from the compiled file never picks it up. Adding the package means editing requirements.in and recompiling, and that shows up as a diff.
npm: lockfile and no lifecycle scripts
npm records resolved versions and integrity hashes in package-lock.json. Two commands matter here.
# npm: inspect a suggested package, then install from the lockfile without running lifecycle scripts
npm view example-package time
npm ci --ignore-scriptsnpm view example-package time prints the creation and publish timestamps. A package created last week with one release deserves suspicion, whatever the assistant said about it.
npm ci installs exactly what the lockfile specifies. It fails if package.json and the lockfile disagree, and it never rewrites the lockfile. npm install some-name, by contrast, resolves the name from the registry and adds it to both files. Use npm ci in CI and for fresh checkouts.
--ignore-scripts skips preinstall, install, and postinstall scripts. You can make this the default for a project:
npm config set ignore-scripts trueSome legitimate packages need a build step, such as native modules. Run those scripts for named packages after review, not for everything.
Common errors
| Mistake | Result | Fix |
|---|---|---|
| Running `pip install name` after the assistant's suggestion | Bypasses the hashed file and installs whatever the registry serves | Add the name to `requirements.in`, review, recompile |
| Compiling hashes, then installing without `--require-hashes` | Hashes are ignored in some setups | Use the flag in CI and deployment |
| Using `npm install` in CI | Can change the lockfile and pull new versions | Use `npm ci` |
| Committing a lockfile change without reading it | A new dependency enters unreviewed | Require review of lockfile diffs |
| Letting an agent regenerate the lockfile freely | The attacker's name gets locked in | Restrict agent write access to lock files, or require approval |
A lockfile locks in whatever was resolved at the time. If a hallucinated name was installed and then locked, the lock now protects the malicious version. Verification must happen before the lock step.
Exercise
A teammate's coding agent adds a new dependency to package.json and runs npm install in CI. Which command should CI use instead, and which flag keeps the new package's postinstall script from running?
Show answer
CI should run npm ci, which installs only what the lockfile specifies and fails if package.json and the lockfile disagree. Adding --ignore-scripts skips preinstall, install, and postinstall scripts. The unreviewed lockfile change should also be caught in code review before it merges.
Exercise
You run pip install --require-hashes -r requirements.txt and pip fails on one line that has no hash. Is the right fix to remove --require-hashes? Explain.
Show answer
No. The missing hash means that entry was not compiled and reviewed. Removing the flag would turn off the protection. Instead, add the package to requirements.in after verifying it, then rerun pip-compile --generate-hashes so the file contains pinned versions and hashes.