Skip to content
Tutorials Center
My learning

Topics

Lesson 45 of 80 ยท Secure Coding

Dependency and Supply Chain Security

Learn how vulnerable packages, typosquatting and compromised builds threaten your software, and use lockfiles, SBOMs, hashes and signed releases to defend.

A modern application is mostly code you did not write. A small web service can pull in hundreds of packages once you count the dependencies of your dependencies, and every one of them runs with the same privileges as your own code. If any of them is vulnerable, malicious or replaced on the way to you, your application is too. This is the software supply chain problem.

In this lesson you'll learn the main ways supply chains fail, the controls that reduce the risk (lockfiles, hash pinning, SBOMs, signed releases, careful updates), and you'll write two small Python tools: a typosquat detector and a pinning checker. Everything runs on invented package names; no network is used. The general idea of shrinking what you trust is from Secure Coding Principles.

How supply chains fail

flowchart LR
    DEV["Developer"] --> SRC["Source repository"]
    SRC --> BUILD["Build system"]
    BUILD --> REG["Package registry"]
    REG --> YOU["Your project<br/>(install)"]
    YOU --> PROD["Production"]
    A1["Stolen maintainer account"] -.-> REG
    A2["Malicious pull request or<br/>compromised build step"] -.-> BUILD
    A3["Typosquatted or<br/>confused package name"] -.-> YOU
    A4["Known vulnerability<br/>in an old version"] -.-> PROD

Each arrow is a place where code can be swapped or poisoned before it reaches production.

The common failure modes:

  • Known vulnerabilities. A legitimate package has a published flaw (a CVE) and you are still running the old version. This is by far the most common problem; the Log4Shell incident hit organizations that did not even know they used the library.
  • Typosquatting. An attacker publishes reqeusts or python-dateutils hoping developers mistype or guess a name. Installing it runs the attacker's code, often during installation itself.
  • Dependency confusion. A company has an internal package called acme-utils. An attacker publishes acme-utils on the public registry with a higher version number; a misconfigured installer prefers the public one.
  • Account takeover or malicious maintainers. A maintainer's credentials are stolen, or a trusted project is handed to a new owner who adds malware (the "event-stream" style incident).
  • Compromised build pipelines. The source is clean but the artifact built from it is not, as in the SolarWinds attack.
  • Abandoned packages. Nobody fixes bugs and nobody notices if the registry name is reclaimed.

Spotting typosquats

A simple defense for your own review process is to compare newly requested package names with a list of popular ones and flag near matches. This demo uses edit distance, the number of single-character changes between two strings, and a made-up popular list:

Python Flag names that look like popular packages

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice flask-login is not flagged: it is a real, different package, and a heuristic cannot know that. Tools like this reduce risk but never replace judgement. Before adding a dependency, ask: Who maintains it? How old and how widely used is it? When was the last release? Do I really need it, or can twenty lines of my own code do the job?

Lockfiles and pinning

A requirements line like requests>=2.0 means "whatever is newest when someone installs". Two builds a day apart can include different code, and a malicious new release is installed automatically. Pinning fixes exact versions, and a lockfile (poetry.lock, uv.lock, package-lock.json, Pipfile.lock) records the full resolved tree including transitive dependencies. Commit the lockfile and install from it in CI and production, so everyone gets the same bytes.

Even stronger is hash pinning: each requirement carries the expected SHA-256 digest of the file, and pip refuses anything that differs (pip install --require-hashes -r requirements.txt). This defends against a registry serving a tampered file. The hash idea comes from Hash Functions and Integrity.

Python A requirements-file pinning checker

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Pinning has a cost: you must update on purpose, or you stay on old, vulnerable versions. The usual pattern is to pin everything, and let a bot (Dependabot, Renovate) open update pull requests that your tests and a reviewer check.

Finding vulnerable versions

Vulnerability databases (the GitHub Advisory Database, OSV, the Python Packaging Advisory Database) map package versions to known flaws. Tools such as pip-audit, npm audit, OSV-Scanner and Trivy read your lockfile and report matches. Run them in CI on every pull request and on a schedule, because new advisories appear for old code.

The matching logic is straightforward: a version is affected if it falls inside a vulnerable range. This sketch shows the idea with simple tuple comparison on invented advisories:

Python Matching versions to advisories

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real tools use more careful version parsing (pre-releases, epochs), but the principle is the same.

SBOMs and signed releases

A software bill of materials (SBOM) is a machine-readable inventory of everything inside a build: components, versions, licenses and where they came from, in formats like CycloneDX or SPDX. When the next Log4Shell appears, an SBOM lets you answer "which of our products contain this library, and which versions?" in minutes instead of days. Generate an SBOM during each build, store it with the release, and keep it searchable.

Verification closes the other half of the gap: how do you know a file really came from its maintainers? Signing attaches a digital signature (see Public-Key Cryptography) that you can check with the publisher's public key or identity. Ecosystems offer this through Sigstore and build provenance attestations (statements, signed by the build system, saying which source and workflow produced an artifact). Frameworks such as SLSA define levels of build integrity, from "has provenance" up to hardened, isolated builds.

A practical checklist

  • Keep dependencies few; remove ones you no longer use.
  • Commit lockfiles; install from them with hash checking where possible.
  • Use a private index or registry scoping so internal names cannot be confused with public ones.
  • Run vulnerability scans in CI and fail the build on high-severity findings in packages you actually use.
  • Automate update pull requests, and read the changelog of major bumps.
  • Turn on two-factor authentication and trusted publishing for packages you maintain.
  • Produce an SBOM and sign your own releases.
  • Install with the least privilege: build containers should not hold production secrets.
  • Review new dependencies as you would new code: popularity, maintainers, install scripts.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is typosquatting in the package world?

    Attackers rely on mistyped or guessed names so the victim installs the look-alike package, which runs the attacker's code.
  2. 2. What does a lockfile give you?

    A lockfile records the exact versions of direct and transitive dependencies, so builds are reproducible. You still have to update deliberately and scan for vulnerabilities.
  3. 3. How does hash pinning add protection beyond version pinning?

    If a registry or mirror serves a tampered file, its SHA-256 will not match the pinned digest and the install fails.
  4. 4. When is an SBOM most useful?

    An SBOM is an inventory of components and versions, which lets you search for affected software when an advisory is published.

Summary

  • Your software is mostly dependencies, so their flaws, look-alike names, hijacked accounts and compromised builds are your risks.
  • Pin exact versions, commit lockfiles and, where possible, verify file hashes.
  • Scan lockfiles for known vulnerabilities on every change and on a schedule; automate update pull requests.
  • Keep SBOMs for fast impact analysis and prefer signed releases with build provenance.
  • Review a new dependency like new code, and remove ones you do not need.

Next: Secrets, Logging and Error Handling, where you'll keep credentials out of source code and out of your logs.