Sub topic 9
Lockfiles, Hash Pinning, and Restricted Install Scripts
This chapter assumes you can run pip and npm and know what requirements.txt and package.json do. You should also know the slopsquatting attack chain from earlier chapters. An assistant suggests a name that doesn't exist, an attacker registers it, and a developer or agent installs it.
The previous chapter covered checking a package before you install it. This chapter covers controls that make the dependency workflow resist mistakes, including mistakes made after you've checked a name.
What these controls do and don't do
Lockfiles, hash pinning, and script restrictions don't tell you whether a package is trustworthy. They give you three narrower guarantees:
- Reproducibility. Every machine installs the same versions that were reviewed.
- Integrity. The bytes downloaded match the bytes recorded when someone approved them.
- Reduced install-time execution. Installing a package does not automatically run its code.
Slopsquatting depends on a developer or agent adding a new name without anyone noticing. These controls turn that addition into a visible, reviewable change.
Lockfiles
A manifest such as package.json or requirements.in states what you want, usually with version ranges. A lockfile records what was actually resolved: every direct and transitive dependency, its exact version, and where it came from.
Without a lockfile, npm install or pip install resolves names against the live registry each time. If an attacker registers a name between two installs, or publishes a new version that matches your range, the next install picks it up.
With a lockfile, installation is a replay of an earlier decision. The commands below use the lockfile as the source of truth:
# npm: install exactly what package-lock.json says, fail if package.json disagrees
npm ci
# Python: compile a pinned file from requirements.in (a human-edited list of top-level names)
pip-compile requirements.in
pip install -r requirements.txtnpm ci deletes node_modules, refuses to modify the lockfile, and exits with an error if package.json and package-lock.json are out of sync. That error is useful. An agent that edited package.json but skipped the lockfile review fails in CI instead of shipping.
The lockfile's blind spot
A lockfile protects against changes after approval. It does not protect against a bad approval. If an assistant suggests fastjson-utils-pro, you run npm install fastjson-utils-pro, and an attacker has already registered that name, npm writes the malicious package into package-lock.json with a valid integrity hash. Every later install faithfully reproduces the malicious package.
This is why the diff of the lockfile matters as much as the lockfile itself. Treat any new entry in a lockfile as a decision that needs a human or a policy check.
Hash pinning in Python
A version pin like requests==2.32.3 names a release, but anyone who controls the registry entry or a mirror can serve different files under that name. A hash pin ties the install to the exact file contents.
pip-compile from pip-tools can write hashes for every package it resolves:
# 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.txtThe generated requirements.txt looks like this:
requests==2.32.3 \
--hash=sha256:70761cfe03c773ceb22aa2f671b4757976145175cdfca038c02654d061d6dcc6 \
--hash=sha256:55365417734eb18255590a9ff9eb97e9e1da868d4ccd6402399eaf68af20a760
# via -r requirements.inThe example hashes are for illustration. Yours come from pip-compile, not from typing them by hand.
In hash-checking mode, pip enforces these rules:
- Every requirement must be pinned with
==and have at least one hash. - Dependencies must be listed too. pip does not resolve extra dependencies in this mode, so a package that pulls in an unlisted name fails the install.
- Each downloaded file must match one of the listed hashes.
Rule 2 is what stops a stray name from slipping in. A suggested package that nobody ran through pip-compile is not in the file, so pip refuses it.
Avoiding source builds
A source distribution (sdist) can run arbitrary build code during install, through setup.py or a build backend. A wheel is a prebuilt archive that pip unpacks without running package code at install time. To refuse sdists in your install step:
pip install --require-hashes --only-binary=:all: -r requirements.txtIf a dependency has no wheel for your platform, the install fails. Then decide deliberately whether to build that one package, preferably in an isolated container.
Watch the compile step
pip-compile itself may need to build an sdist to read its metadata. That means the step that creates the lockfile can execute package code, even before any hash exists. Run pip-compile in a disposable container or CI job with no credentials, not on a laptop that holds SSH keys and cloud tokens.
Integrity and scripts in npm
npm's package-lock.json already stores an integrity field, a sha512 hash, for each resolved package. npm ci verifies it, so lockfile use gives you hash pinning by default. The remaining npm problem is lifecycle scripts.
A package can declare preinstall, install, or postinstall scripts in its package.json. npm runs them during installation, with your user's permissions. A malicious package needs nothing more than that.
# npm: inspect a suggested package, then install from the lockfile without running lifecycle scripts
npm view example-package time
npm ci --ignore-scriptsTo make the safe behavior the default, put it in a project .npmrc that is committed with the code:
ignore-scripts=trueSome legitimate packages need a build step, such as native modules or packages that download binaries. With ignore-scripts=true they may install but not work. Re-enable scripts for specific packages you have reviewed:
npm rebuild esbuild --ignore-scripts=falsepnpm version 10 and later skips dependency lifecycle scripts by default and uses an allowlist (onlyBuiltDependencies in package.json) for packages permitted to run them. If you are choosing a tool, that default is an advantage.
Scripts are only one execution path
Disabling install scripts does not make a package safe. The code still runs when your application imports it, when tests run, or when the build tool loads it. Python wheels can also include .pth files, which the interpreter processes at startup. Script restrictions shrink the window between install and review. They don't replace the review.
Putting the controls into a workflow
The controls work best as stages. A new name must pass a check, appear in a reviewed diff, and then install with the strictest settings.
Catch new names in the diff
Make new dependencies hard to miss. This command, run in CI against the pull request's base branch, lists added pins in a pip-compile output:
git diff --unified=0 origin/main -- requirements.txt | grep -E '^\+[A-Za-z0-9._-]+=='It prints every added or changed pin, so a version bump also appears. Compare the names against the list you expected. A failing CI job that requires a named reviewer for any lockfile change is a stronger version of the same idea. For npm, the equivalent is a code-owner rule on package-lock.json.
Keep agents out of the install step
If a coding agent edits dependencies, let it edit the manifest and propose the lockfile change in a pull request. Give it no permission to run npm install or pip install on a machine with real credentials. The agent can still suggest a hallucinated name, but the name stops at review.
What each control stops
| Control | Stops | Does not stop |
|---|---|---|
| Lockfile with `npm ci` | Silent version drift, unreviewed new names at install time | A malicious package approved when first added |
| `--require-hashes` | Swapped files, unlisted transitive packages | A malicious package whose hashes you generated |
| `--only-binary=:all:` | Build-time code from sdists | Code that runs on import or via `.pth` files |
| `ignore-scripts` | npm lifecycle scripts at install | Code that runs on `require` or in tests |
| Lockfile diff review | Hallucinated names entering the project | Reviewers who approve without checking the name |
Each row leaves a gap, and the next row covers part of it. Using several controls together is what makes the attack hard.
Common mistakes
- Using
npm installin CI. It can rewrite the lockfile. Usenpm ci. - Generating hashes on a machine that already holds a malicious file. If the bad package was in your cache or a mirror, the hash records it. Compile from a clean environment against the real registry.
- Hand-editing
requirements.txt. Editrequirements.inand recompile, so hashes and transitive pins stay consistent. - Approving lockfile changes by line count. A 2,000-line lockfile diff hides one bad name. Use a script or tool to extract only the added package names.
- Treating
--ignore-scriptsas a sandbox. It removes one trigger. Anything you later run still has full user permissions.
Exercise
A teammate asks an assistant for a YAML helper library. The assistant names pyyaml-fast. The teammate runs pip install pyyaml-fast, which succeeds, then runs pip-compile --generate-hashes. A later CI job runs pip install --require-hashes -r requirements.txt and passes. Does the hash check show the package is safe? What two controls would have caught the problem earlier?
Show answer
No. The hashes were generated from whatever files the registry served when the name was first added, so the check only confirms CI installs the same files the teammate got. It says nothing about whether the package is legitimate. The problem should have been caught by (1) verifying the name before installing, for example checking that it exists, has history and an expected publisher, and (2) reviewing the new entry in the requirements diff. Also, the teammate's direct pip install ran the package's code on their machine, so installing only through the locked workflow, in a sandbox, would have avoided that exposure.
Exercise
Your project's .npmrc sets ignore-scripts=true. A dependency that compiles a native addon now installs but crashes at runtime. What is the safe way to fix it?
Show answer
Review that specific package, then re-enable scripts for it alone, for example with npm rebuild <package> --ignore-scripts=false. Do not turn ignore-scripts off globally. With pnpm, add the package to onlyBuiltDependencies. Keep the default restrictive so every other package, including any new one an assistant suggests, still installs without running scripts.