Why you need it
You add one library to your app. That library brings others, and those bring more. Most of the code that ships in your app was never chosen by anyone on your team. If one of them puts out a bad new version, your next build can pick it up without anyone noticing.
Think of it like this
You order a cake from a bakery you trust. The bakery buys its eggs from a farm you have never heard of. If the farm sends bad eggs one week, your cake is bad too, even though you only chose the bakery.
Where you’ll find it
- npm: Writes every exact version into
package-lock.json, andnpm ciinstalls only what it says. - Rust and Go: Rust keeps
Cargo.lock; Go keepsgo.sumwith a fingerprint of every module. - GitHub: Reads lockfiles to draw your dependency graph, and Dependabot suggests a safe update when one is found to be bad.
What to know first
Artifact. One build, sealed and named, that is tested and then shipped as it is. See Build once, deploy many.
Library. Code someone else wrote that your app uses, like charts for drawing graphs.
Version range. Many apps ask for "1.4 or anything newer in 1.x" instead of one exact version. Written ^1.4 in some tools.
How it works
- You chose one, you got two. You chose
charts. Inside it,chartsusescolors, which you never chose. Real apps often have 10 hidden libraries for every one they picked. This is the box opening in the animation. - The newest one walks in.
chartsasks forcolors1.4 or newer. A badcolors1.5 comes out. The next build fetches the newest that fits, so 1.5 gets in. This is the red tile coming down. - Write down the exact versions. A lockfile lists every library, hidden ones too, at the exact version that was used, often with a fingerprint of its files. It is saved in main next to the code.
- Build only from the lockfile. Every build installs exactly what the lockfile says, and fails if a file does not match its fingerprint. A new 1.5 changes nothing. This is 1.5 stopping above the box.
- Update on purpose. A lockfile does not mean never updating. You update it as a normal change, so the new versions go through the tests in Continuous integration like any other change.
An example
Your app lists 20 libraries. Through them, it installs 600.
On Monday a bad colors 1.5 is published. Without a lockfile, every build from then on ships it.
With a lockfile, builds keep colors 1.4. On Thursday 1.5 is found to be bad, and you were never on it.
Further reading
- Cargo.toml vs Cargo.lock, The Cargo Book. A short, clear page on what you ask for versus what you get. Start with this one.
- Details about the event-stream incident, The npm blog. A real case: a hidden library was given bad code that targeted one app.
- SLSA, OpenSSF. The wider picture: levels of trust for everything that goes into a build.