Dependency and supply-chain security
Most of the code you ship was written by strangers. Here is how to know what you run, and to trust it.
Updated since you finished it. We changed this lesson after you marked it done. Your progress is kept.
What broke
In March 2024, a developer named Andres Freund noticed something small. SSH logins on his Debian unstable (sid) systems used a lot of CPU. A tool called valgrind also showed odd errors. 1
He measured it. A failed login took about 0.8 seconds instead of 0.3. He traced the delay to liblzma, part of the xz compression tools. Then he found a backdoor. 1
The bad code was in xz versions 5.6.0 and 5.6.1. The part that started it was not in the Git repository that people read. It was only in the release tarballs, the files that Linux distributions download and build. 1 There, a line in one build file ran a hidden script at the end of configure. That script pulled more code out of two "test" files in the repository and put it into the library. 1
OpenSSH does not use liblzma directly. But Debian and several other distributions patch it to talk to systemd, and libsystemd depends on liblzma. That chain loaded the backdoor into the SSH server. 1
Freund pointed to weeks of activity by the person who committed the changes. He wrote that they were "either directly involved" or their system was badly compromised. 1 Luckily, few distributions had shipped 5.6.0 or 5.6.1 yet, and mostly in pre-release versions. 1
Look at what the attacker needed. Not a bug in your code. Just one trusted committer, one build step nobody read, and a dependency of a dependency.
It happens for real
xz-utils (open source) · 29 Mar 2024xz/liblzma: a backdoor hidden in release tarballs reached the SSH serverxz versions 5.6.0 and 5.6.1 carried a backdoor in liblzma. On some Linux distributions it was loaded into the SSH server and made logins slow, which is how a developer found it. All incidents tagged “security”The idea in simple words
Your app is mostly code that you did not write. Every library, every build tool and every CI action runs with your app's power. OWASP now ranks "Software Supply Chain Failures" third in its Top 10. 4 You met it in the OWASP lesson.
You face two separate problems:
- Known flaws. A library you use has a published bug, a CVE. You need to know you use it, and then upgrade.
- Malicious code. Someone put harmful code into a package, a build or a tool on purpose. No scanner knows about it yet.
The defenses answer three questions. What exactly do we run? A lockfile and an SBOM answer that. Is any of it known to be bad? A scanner answers that. Did it really come from where we think? Signatures and provenance answer that.
The key idea. Treat every dependency, build tool and CI action as code that runs with your permissions. Fix the exact versions, know the full list, check where each piece came from, and give your pipeline the smallest power it needs.
How it works
Where attacks get in
Step through the path from source code to your app. Use Next and Back, or the ← and → keys.
- Code you depend on passes through several hands. You review the source, but you install a built package from a registry. Each arrow is a hand-off an attacker can use.
- Source to build: the xz release tarballs held a line that was not in git. It ran a hidden script while the package was built.
- Registry: PyTorch nightly used a package called torchtriton from its own index. Someone put a package with the same name on PyPI, and pip installed that one instead.
- Build: a CI action is code too. In March 2025, attackers moved tags of tj-actions/changed-files to point to a malicious commit. Workflows that used the tag ran it.
- Defenses, one per hand-off. A lockfile fixes the exact version and its hash. Provenance says which source and build made a package. A commit SHA cannot be moved like a tag.
Here are the common ways in:
- Transitive dependencies. You add 5 packages. They can bring in hundreds more. OWASP says to track "not just your direct dependencies, but their (transitive) dependencies". 4 xz reached SSH through two hops. 1
- Typosquatting. An attacker publishes
reqeustsand waits for someone to mistyperequests. - Dependency confusion. Your private package name also exists on a public index. From 25 to 30 December 2022, PyTorch nightly installs on Linux used a package called
torchtritonfrom PyTorch's own index. Someone uploadedtorchtritonto PyPI. Because the PyPI index took precedence, pip installed the fake one. 2 When imported, it read files such as$HOME/.ssh/*and sent them out through DNS queries. 2 - Maintainer takeover. An attacker gets a maintainer's account or token, or becomes a maintainer, as in xz. The Shai-Hulud worm of 2025 stole npm tokens and used them to publish bad versions of other packages. It reached over 500 package versions. 4
- Compromised build systems and CI. In March 2025, attackers moved several version tags of the GitHub Action
tj-actions/changed-filesto a malicious commit. The code read secrets from the runner's memory and printed them, encoded, in workflow logs. The bad code was live on 14 and 15 March 2025. GitHub's advisory says over 23,000 repositories used the action. 3
Lockfiles: know exactly what you install
A version range like ^4.17.0 means "any 4.x from 4.17 up". Each install can pick a different version. A lockfile fixes the whole tree. npm's package-lock.json lets later installs "generate identical trees, regardless of intermediate dependency updates". 5 For each package it stores where it came from and an integrity hash. 5 So a swapped file fails the install.
In CI, use the strict install. npm ci needs a lockfile. If the lockfile and package.json disagree, it "will exit with an error, instead of updating the package lock". It never writes to either file. 6 Python, Go and Rust tools have the same idea under other names.
Commit the lockfile. Review changes to it like code: a new package in the lockfile is new code in production.
Scanning: when to upgrade
A scanner such as Dependabot compares your dependency graph with an advisory database. It alerts when a new advisory affects a version you use. 7 GitHub is clear about the limits: "Alerts can't catch every security issue", and new flaws can take time to reach the database. 7 A scanner finds known flaws. It does not find a fresh backdoor.
So when should you upgrade? OWASP says to choose versions on purpose and "upgrade only when there is need". It also says to watch for libraries that are no longer maintained. 4 OWASP gives no time frames. Here is our own suggestion:
- Critical flaw in code you actually call: upgrade within days, like any production bug.
- Other alerts: batch them into a regular update, for example every two weeks.
- A brand-new release with no security reason: wait, for example a few days. Others then have time to spot a bad release before you install it.
OWASP also suggests staged rollouts for updates, so one bad vendor release does not reach every system at once. 4
SBOM: the full list
A software bill of materials lists every component and version in what you ship. OWASP says to generate and manage it centrally for all your software. 4 When the next Log4Shell appears, you search your SBOMs instead of asking every team. Generate one per build artifact, in CI, and store it next to the artifact.
Signed builds and provenance
A lockfile says which version. It cannot say that the package was built from the source you think. The xz tarballs were not the same as git. 1
Provenance is a record of how an artifact was built: the source, the build platform and the inputs. 8 SLSA is a framework that grades this in build levels. 8
- Build L0: nothing. "L0 represents the lack of SLSA." 8
- Build L1: provenance exists. It helps prevent mistakes but "is trivial to bypass or forge". 8
- Build L2: a hosted build platform makes and signs the provenance. This stops tampering after the build. 8
- Build L3: a hardened build platform. Runs cannot affect each other, and build steps cannot reach the signing secret. This stops tampering during the build. 8
Signing used to mean guarding a long-lived private key. Sigstore removes that. The signer proves its identity with an OIDC token. Sigstore's certificate authority, Fulcio, issues a short-lived certificate, and the private key is thrown away after one signing. 9 Each signature goes into Rekor, a transparency log. It is append-only, and anyone can audit it. 9
You can use this today. On npm, npm publish --provenance from GitHub Actions or GitLab CI/CD signs provenance through Sigstore. Users check it with npm audit signatures. 10 npm is careful here: provenance "does not guarantee the package has no malicious code". 10 It proves where a package came from, not that the source is safe.
Hardening CI
Your CI runs third-party code with your secrets. The CI/CD lesson showed pinning briefly. Here is the full set.
permissions:
contents: read # default for every job: read-only
jobs:
test:
runs-on: ubuntu-latest
steps:
# pin to a full commit SHA, with the version as a comment
- uses: actions/checkout@<full-commit-sha> # v4
- run: npm ci- Pin actions to a full commit SHA. GitHub warns that "a tag can be moved or deleted" by someone who gets access to the action's repo. 11 That is what happened to tj-actions. 3 Check that the SHA comes from the action's own repo, not a fork. 11
- Give the token least privilege. Start with read-only contents. Raise permissions only for the job that needs them. 11 Use short-lived OIDC credentials for deploys, as in Secrets and key management.
- Be careful with
pull_request_target. It runs with privileges. Combined with checking out untrusted pull request code, GitHub says it can lead to repository takeover. 11 - Keep pinned actions up to date. Dependabot can update action references. Its alerts do not cover actions pinned to a SHA, so plan regular updates. 11 7
- Build once and promote. OWASP lists separation of duties, signed builds, environment-scoped secrets and promoting artifacts rather than rebuilding. 4
If a CI dependency is compromised, treat every secret it could read as leaked. Rotate them. 3
Trade-offs
| Defense | Stops | Does not stop | Cost |
|---|---|---|---|
Lockfile plus npm ci | Yes Silent version changes and swapped files. 5 | A bad version you chose to lock. | Low. Review lockfile diffs. |
| Vulnerability scanning | Yes Known CVEs in versions you use. 7 | No New backdoors and flaws not yet in the database. 7 | Low to start. Ongoing upgrade work. |
| SBOM per build | Slow answers to "are we affected?" | No It only lists. It blocks nothing. | Low. One CI step. |
| Provenance and signatures | Yes Packages not built from the claimed source and workflow. 8 | No Malicious code in the real source. 10 | Medium. Build setup, and verify on install. |
| Actions pinned to SHA | Yes Moved tags. 11 | A bad commit you pinned yourself. | Medium. You must update pins. |
| Least-privilege CI token | Damage after a CI compromise. 11 | The compromise itself. | Low. |
Simple rule: lock and scan everything from day one. Pin CI actions and cut token permissions next. Add provenance for anything you publish.
At work: how a senior uses this
- In review, treat a lockfile diff as a code diff. Ask: "Why do we need this new package, and who maintains it?"
- Before adding a dependency, check its downloads, maintainers, last release and its own dependency count. Prefer the standard library for small tasks.
- Ask: "If this package turned bad tonight, which secrets could it read?" Install scripts and CI steps run with real credentials.
- Use scoped or namespaced names for internal packages, and own the same names on public indexes. Point installs at one index you control.
- Keep an SBOM per release, so "are we affected?" takes minutes.
- Set a written patch deadline for critical CVEs, and track it like a bug.
- Pin every third-party action to a SHA. Set
permissions: contents: readat the top of every workflow.
With AI
Coding agents add packages to fix problems. They can also suggest package names that do not exist. One study of 576,000 code samples found that, on average, at least 5.2% of the packages from commercial models did not exist. For open-source models it was 21.7%. 12 Attackers can register those names. This is called slopsquatting.
What to ask the agent
"List every direct dependency in this repo with its version, last release date and number of maintainers. Flag any package that is unmaintained or has fewer than 1,000 weekly downloads."
"Find every GitHub Action in .github/workflows that is not pinned to a full commit SHA. Propose the pinned line with the version as a comment. Do not change behaviour."
"Add a top-level permissions: contents: read to each workflow. Then list which jobs need more, and why."
What to check yourself
- Every package the agent added really exists, is the one you meant, and is in the lockfile.
- The agent did not change the lockfile in ways the task did not need.
- Each pinned SHA comes from the action's own repo, not a fork.
- The agent did not add
pull_request_targetor wider token permissions to make a workflow pass. - No new install script runs during the build without a reason.
Check yourself
Your progress is saved only in this browser. Export it to keep it safe.
You have finished 5 lessons. Your progress lives only in this browser. Export a copy so you don’t lose it.
Go deeper
Show 12 sources and related lessons
- Andres Freund, “backdoor in upstream xz/liblzma leading to ssh server compromise”, oss-security mailing list (29 March 2024) (opens in a new tab)
The first public report. How slow SSH logins led to a backdoor hidden in the xz release tarballs. - PyTorch blog, “Compromised PyTorch-nightly dependency chain between December 25th and December 30th, 2022.” (opens in a new tab)
A real dependency confusion attack. Why pip picked the fake package, what it stole, and how PyTorch fixed it. - GitHub Advisory Database, “tj-actions changed-files through 45.0.7 allows remote attackers to discover secrets by reading actions logs.” (GHSA-mrrh-fwg8-r2c3) (opens in a new tab)
How moved tags made thousands of CI workflows print their secrets into logs. - OWASP Top 10:2025, A03:2025 Software Supply Chain Failures (opens in a new tab)
Real attacks such as Shai-Hulud, and a prevention list from SBOMs to signed builds. - npm Docs, “package-lock.json” (opens in a new tab)
What the lockfile records, including where each package came from and its integrity hash. - npm Docs, “npm-ci” (opens in a new tab)
The install command for CI, which fails instead of changing the lockfile. - GitHub Docs, “Dependabot alerts” (opens in a new tab)
How a scanner matches your dependency graph against an advisory database, and its limits. - SLSA v1.2, “Build: Track Basics”, slsa.dev (opens in a new tab)
The four build levels, L0 to L3, and the threats each one stops. - Sigstore documentation, “Overview” (opens in a new tab)
Keyless signing with short-lived certificates, Cosign, Fulcio and the Rekor log. - npm Docs, “Generating provenance statements” (opens in a new tab)
How to publish an npm package with provenance from CI, and how users check it. - GitHub Docs, “Secure use reference” (opens in a new tab)
Pinning actions to a commit SHA, token permissions, and risky triggers. - Spracklen et al., “We Have a Package for You! A Comprehensive Analysis of Package Hallucinations by Code Generating LLMs”, USENIX Security 2025 (arXiv 2406.10279) (opens in a new tab)
How often code models invent package names that do not exist, across 576,000 code samples.
Real incident: xz-utils (open source), 29 Mar 2024: xz/liblzma: a backdoor hidden in release tarballs reached the SSH server
Related: OWASP Top 10 and secure coding · Secrets and key management · CI/CD and trunk-based development · Reviewing code written by humans and AI · Working with coding agents · Threat modelling and zero trust
- OWASP Top 10 and secure coding: where supply-chain failures sit among the common flaws.
- Secrets and key management: what to rotate after a CI compromise.
- CI/CD and trunk-based development: build once, then promote.