Dependencies and the supply chain

Fifteen declared packages become 515, and four starters become 61 jars — each running with your privileges. Reachability, lockfiles, and the question Log4Shell was really about.

13 min read🛡️ Application Security

Most of the code in your application is not yours, and you did not read any of it. That is not a criticism — it is the only way anything ships — but it means your security depends on a supply chain, and the shape of that chain is worth knowing exactly.

Count what you did not choose

This project's package.json declares 15 dependencies, thirteen of them runtime. What is actually installed:

plaintext
###   total declared:  15
###   total packages in the tree: 515

Thirty-four times what was chosen. On the Java side, a service declaring four things — the web, data-jpa and validation starters, plus a database driver — gets:

plaintext
### jars on the classpath: 61
### a sample of what you did not choose:
###   antlr:antlr
###   ch.qos.logback:logback-classic
###   com.fasterxml.jackson.core:jackson-databind
###   com.fasterxml:classmate

A parser generator, a logging framework, six Jackson artifacts. Every one of them runs inside your process, with your privileges, with access to your environment variables and your database connection. A vulnerability in any of them is a vulnerability in your service, and a malicious one is game over.

That is the whole problem in one sentence: you audited four decisions and accepted sixty-one.

A CVE is not automatically a vulnerability

The OWASP lesson made this point with this project's own four high-severity advisories, accepted rather than fixed because mysql2 arrives through the Prisma CLI and this project uses PostgreSQL. Reachability decides, and the same lesson's other half applies here: you have to establish that, not assume it.

What makes an advisory actually matter, roughly in order:

  1. Is the vulnerable code on a path your application executes? A CVE in a CLI's sub-dependency that never loads at run time is different from one in your JSON parser.
  2. Can an attacker reach it with input they control? A deserialisation flaw in a library you only feed your own config is not the same as one behind a public endpoint.
  3. Is it a dev dependency or shipped? Build-time code still matters — see the pipeline section below — but the exposure is different.
  4. What would exploitation actually cost you?

Write the answer down when you accept one. An unexamined advisory and a deliberately accepted one look identical in a scanner, and the difference is everything.

The lockfile is the integrity control

A version range says anything compatible. A lockfile says exactly this, and here is its hash:

plaintext
### is the lockfile committed?
###   tracked: package-lock.json
###   entries in it: 573
### does it pin integrity hashes?
###   572 of 572 entries carry an integrity hash

Every entry carries a sha512 of the package contents. If a published version is ever altered, the install fails rather than succeeding quietly — which is the defence against a registry being compromised or a maintainer replacing a release in place.

Two rules follow, and both are commonly broken:

  • Commit the lockfile. Without it, two machines resolve different trees and "works on my machine" becomes a security statement as well as a reliability one.
  • npm ci, not npm install, in CI and in deployment. install may update the lockfile to satisfy a range; ci installs exactly what is locked and fails if the lockfile and manifest disagree. This project's deploy runs npm ci --omit=dev for that reason.

Maven's equivalent is discipline rather than a file: never a version range, never a SNAPSHOT in a release, and a BOM to pin what you did not declare. The JPA course's build lesson covers dependencyManagement, which is how you pin a transitive without depending on it directly. npm's version of that is overrides.

The attacks that are not accidents

A vulnerable dependency is a mistake somebody made. These are deliberate:

  • Typosquatting. A package named one character away from a popular one. Read the name in the diff when a dependency is added; that is the only moment anyone looks.
  • Dependency confusion. Your internal package acme-billing is not on the public registry — so somebody publishes one there with a higher version number, and a misconfigured resolver prefers it. The fix is configuration: scope internal packages and pin the registry that may serve them.
  • A compromised maintainer account. A real package, a real name, a malicious new version. This has happened repeatedly to widely-used packages, and the defence is not vigilance — it is the lockfile plus not upgrading blindly.
  • Install scripts. postinstall runs arbitrary code on every developer machine and every CI runner, before any of your tests. npm ci --ignore-scripts is worth knowing about, with the caveat that some legitimate packages need them.

Maven has less of this — Central requires signed artifacts and does not allow republishing a version — but a plugin in your build runs with the same freedom as any install script.

The pipeline is part of the surface

The place teams under-defend, because it does not feel like production:

  • CI has your secrets. A deploy key, a registry token, a cloud role. A build that can be influenced by a pull request from a fork is a build an attacker can influence.
  • Pin your actions and images. actions/checkout@v4 is a moving tag; a digest is not. The same goes for FROM node:22 in a Dockerfile.
  • Least privilege for the build. A CI job that only runs tests does not need a publish token.
  • Reproducible builds. If the same commit produces a different artifact on different days, you cannot say what you deployed — which is the wrapper-and-pinning discipline from the Maven lesson, in a security frame.

An SBOM says what you shipped

A software bill of materials — CycloneDX or SPDX, generated at build time — is the list of everything in an artifact, with versions.

Its value is not compliance, although that is often what funds it. Its value is the morning a severe vulnerability is announced and the only question that matters is "are we affected, and where?" Without an SBOM that is an archaeology project across every service. With one it is a query.

That is the real lesson of Log4Shell, and it is not the one usually drawn. The vulnerability was severe, but what made it a weeks-long incident for most organisations was that nobody could quickly answer where log4j was — it arrived transitively, in services nobody associated with logging.

What to actually do

In order of value per hour spent:

  1. Automate the upgrades. Dependabot or Renovate, with grouped minor updates and CI proving them. Being current is the defence that works before an advisory exists — because the emergency upgrade is the one you cannot do quickly.
  2. Scan in CI and fail on what matters. npm audit, mvn dependency-check, Snyk, Trivy for images. Choose the severity that blocks, and record accepted advisories with a reason and a review date.
  3. Commit lockfiles and install with ci.
  4. Generate an SBOM with each build and keep it with the artifact.
  5. Reduce the surface. The cheapest dependency is the one you do not add. A left-pad is not worth 515 packages of ancestry, and a starter you use one class from is worth a second look.

Under the hood: what npm audit actually checks, and how a lockfile catches tampering

npm audit is a lookup, not an analysis. It sends the resolved dependency tree — every package name and exact version your lockfile currently pins — to an advisory database (the GitHub Advisory Database, populated from CVEs and first-party reports) and reports which of those exact name-and-version pairs match a known, published advisory. That is the whole mechanism: it has no idea what your code does with any package, whether the vulnerable function is ever called, or whether the vulnerable code path is even reachable from your entry points. That is the precise, mechanical reason "reachability decides" from earlier in this lesson is not a hedge — the tool that finds the advisory and the judgment that decides whether it matters are two entirely different steps, and only a human does the second one.

The lockfile's integrity field is where the real cryptographic guarantee lives. Each entry carries a sha512-<hash> computed over the exact bytes of that package's published tarball at the moment it was locked. On every install, npm downloads the tarball for that entry and recomputes the same hash over the bytes it actually received, refusing the install outright if they do not match. That is what makes a lockfile more than a version pin: a version number alone says "any bytes claiming to be 4.2.1," while the integrity hash says "these exact bytes, or the install fails" — so even a compromised registry serving different content under an unchanged version number, or a tampered mirror, cannot get altered code onto disk silently. The check runs client-side, after download, before anything from that package is ever required or imported.

Dependency confusion is worth tracing one level deeper than "somebody publishes your internal name publicly," because the mechanism is what the fix actually targets. A resolver asking for a bare package name — no scope, like acme-billing rather than @acme/billing — and configured to consult more than one registry (a private one for internal packages, the public one for everything else) picks, among every version it finds across every registry it is allowed to ask, the highest semver version. An attacker who learns your internal package's name (visible in a client bundle's import graph, a leaked package.json, an error message) publishes that exact name publicly at version 999.0.0. A resolver that is not told "this name only ever comes from the private registry" sees a higher version on a registry it trusts and installs the attacker's package — whose postinstall script then runs with full developer or CI privileges, the moment npm install executes, before a single test runs. Scoping internal packages (@yourorg/billing) and pinning which registry may serve that scope closes the hole by making the comparison never happen at all: scope-to-registry mapping is resolved before any version comparison, so the public registry is never even asked for a @yourorg/* name.

Try it yourself

Work out where dependency confusion actually gets stopped

A company's internal package is named payments-core (no scope) and is published only to a private, authenticated registry. Their CI resolver is configured with the public npm registry as a fallback for any name the private registry does not have.

Before reading on: an attacker publishes a public package also named payments-core at version 99.0.0. Does the attack succeed against this setup, and at which of the following points could it actually be stopped — the CI resolver's configuration, the lockfile, or npm audit?

The lockfile and the audit both arrive too late; only the resolver's own scope-to-registry mapping stops it before the fact

With no scope and a public fallback, the resolver has no way to know payments-core is supposed to come only from the private registry — from its point of view, it is one bare name, resolvable from either source, and it picks the higher version, 99.0.0, from the public registry. npm audit cannot help here: the attacker's package has no advisory filed against it, so there is nothing for audit to match. A lockfile does not help on the first install either — the lockfile records whatever the resolver decided, so if the resolver already chose the malicious package, the lockfile will faithfully lock in that choice and its (genuinely correct) integrity hash for the attacker's own tarball. The only point that actually prevents this is configuring the resolver so the bare name payments-core (or, better, a scoped @company/payments-core) can never be satisfied by the public registry at all — closing the ambiguity before any version comparison happens, rather than trying to catch the result afterward.

Misconceptions

  • "A clean npm audit means the app has no supply-chain risk." It means no currently resolved package-and-version pair matches a known, published advisory — nothing more. A malicious package with no advisory filed yet, a compromised maintainer's account publishing a new bad version under an existing trusted name, or a reachable flaw the advisory database simply has not catalogued are all invisible to it. Audit is a floor, not a guarantee.
  • "Pinning an exact version in package.json (no ^ or ~) gives the same protection as a lockfile." An exact version controls which release gets installed; it says nothing about whether the bytes delivered for that release are the ones you reviewed. Only the lockfile's integrity hash (checked on every install, strictly by npm ci) verifies the content itself — a version pin without a checked lockfile still trusts whatever the registry hands back for that version number.

Sources

  • OWASP Top 10 — A06:2021 Vulnerable and Outdated Components.
  • OWASP Dependency-Check documentation / OWASP Software Component Verification Standard.
  • npm documentation — package-lock.json and the integrity field, and the npm audit / npm ci commands.
Progress is saved on this device and to your account when signed in.