Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 41 ยท Releasing and Production

Dependency Security and Supply Chain

Protect a Maven build from vulnerable and malicious dependencies: OWASP dependency-check, Dependabot and Renovate, checksums, signatures and enforcer rules.

Your application is mostly other people's code. A typical Maven project pulls in dozens or hundreds of transitive libraries, and any one of them can contain a known vulnerability, be abandoned, or in the worst case be malicious. Attackers know this: they target popular libraries, publish look-alike packages and hope a typo does the rest. Securing the supply chain means knowing what you depend on, checking it comes from where you think, and updating when problems are found.

This lesson covers four defenses: scanning for known vulnerabilities, automating updates, verifying that downloaded files are intact and genuine, and using the enforcer plugin to make rules part of the build. Scanners need databases and network access, so the real tools are shown read-only. The Java stand-ins reproduce the core logic: matching versions against advisories, and detecting suspicious artifact names and tampered files.

Four defenses

Scan Match dependencies against known CVEs.
Update Bots open pull requests for new versions.
Verify Checksums and signatures prove integrity.
Enforce Build rules stop risky changes.
1 / 2

Scanning for known vulnerabilities

Public flaws receive a CVE identifier and a severity score (CVSS, from 0 to 10). Scanners resolve your full dependency tree, look each artifact up in vulnerability databases and report matches. The OWASP dependency-check plugin is a common choice for Maven:

XML
<plugin>
  <groupId>org.owasp</groupId>
  <artifactId>dependency-check-maven</artifactId>
  <version>12.1.0</version>
  <configuration>
    <failBuildOnCVSS>7</failBuildOnCVSS>
    <suppressionFiles>
      <suppressionFile>config/dependency-suppressions.xml</suppressionFile>
    </suppressionFiles>
  </configuration>
  <executions>
    <execution>
      <goals><goal>check</goal></goals>
    </execution>
  </executions>
</plugin>
Terminal
mvn org.owasp:dependency-check-maven:check

With failBuildOnCVSS set to 7, any finding scored 7 or higher stops the build. The plugin downloads the National Vulnerability Database, which needs an API key for a reasonable download speed, so keep the key in a CI secret and pass it as a property rather than writing it in the POM. Matching is based on names and versions, so false positives happen; a suppression file records a reviewed decision ("this CVE does not apply because we never call that code") with a reason, so it is visible in code review rather than silently ignored.

Other tools fill the same role: Dependency-Track consumes an SBOM (see release workflows), GitHub's dependency graph and Dependabot alerts read your POMs, and commercial scanners add reachability analysis. Use at least one, and scan the final, packaged result as well as the source.

Automating updates

Most vulnerabilities are fixed by moving to a newer version, so the real goal is to make updating routine. Dependabot (built into GitHub) and Renovate (open source, works with many platforms) open a pull request whenever a dependency or plugin has a new version. Each pull request runs your normal CI, so a failing test tells you the update is not safe before anything is merged.

YAML
# .github/dependabot.yml
version: 2
updates:
  - package-ecosystem: maven
    directory: /
    schedule:
      interval: weekly
    groups:
      spring:
        patterns: ["org.springframework*"]
    ignore:
      - dependency-name: "org.apache.logging.log4j:*"
        update-types: ["version-update:semver-major"]

Grouping related libraries into one pull request avoids a flood of tiny changes. Manage the versions in one place with dependencyManagement and BOMs, so an update touches one property and everything stays consistent. For a quick manual check, mvn versions:display-dependency-updates lists what is outdated.

Avoid version ranges like [1.0,2.0) and LATEST: they make a build depend on whatever appeared last night, which is the opposite of controlled updates (see coordinates).

Checksums and signatures

Every file in a repository has checksum files next to it (.sha1, .sha256 and others). After downloading, Maven computes the checksum of the file and compares. Whether a mismatch fails or only warns depends on the policy:

Terminal
mvn verify -C     # strict checksums: fail on mismatch
mvn verify -c     # lax checksums: warn on mismatch

Repositories can set <checksumPolicy>fail</checksumPolicy> in their <releases> block. A checksum catches corruption and a careless mirror, but an attacker who can change the file can change its checksum too, because both come from the same server. Signatures go further. Artifacts on Central carry a detached PGP signature (.asc), made with a key the publisher controls (see publishing). The pgpverify-maven-plugin can check every dependency's signature against a map of trusted keys:

XML
<plugin>
  <groupId>org.simplify4u.plugins</groupId>
  <artifactId>pgpverify-maven-plugin</artifactId>
  <configuration>
    <keysMapLocation>${project.basedir}/config/trusted-keys.list</keysMapLocation>
  </configuration>
  <executions>
    <execution>
      <goals><goal>check</goal></goals>
    </execution>
  </executions>
</plugin>

Not every artifact is signed, so full coverage takes some work. Newer approaches, such as build provenance attestations and reproducible builds, aim to prove that a binary matches its public source.

Trusted repositories

Where artifacts come from matters as much as how they are checked. Good habits:

  • Resolve everything through one repository manager (Nexus or Artifactory) as a mirror, so you control what is reachable and can block bad artifacts centrally (see settings, mirrors and credentials).
  • Use https only. Maven 3.8.1 and later already blocks plain http repositories by default.
  • Be careful with extra <repository> entries in a POM. A library that adds a custom repository can pull dependencies from a place you never reviewed; a mirror with mirrorOf set to * catches them all.
  • Keep CI credentials least-privilege: a build that only needs to read should not hold deploy rights.

Typosquatting and dependency confusion

Typosquatting means publishing a package whose name is almost the name of a popular one, hoping for a mistyped copy-paste. Maven coordinates have a defence: Central ties a groupId to a verified namespace (a domain you own), so org.apache.commons cannot be claimed by a stranger. Look-alike groupIds (org.apche.commons) are still possible, so read coordinates carefully when you add a dependency, and prefer libraries with a long history and an active repository.

Dependency confusion targets internal artifacts: if your company's private library com.acme:billing is also uploaded to a public repository with a higher version, a misconfigured build might fetch the public one. Mirrors that route your own groupIds to the private repository only, and namespaces you own, prevent this.

Enforcer rules as guardrails

The maven-enforcer-plugin turns policy into build failures. Useful built-in rules:

XML
<plugin>
  <artifactId>maven-enforcer-plugin</artifactId>
  <executions>
    <execution>
      <id>enforce</id>
      <goals><goal>enforce</goal></goals>
      <configuration>
        <rules>
          <dependencyConvergence/>
          <requireUpperBoundDeps/>
          <banDuplicatePomDependencyVersions/>
          <requireReleaseDeps>
            <message>No SNAPSHOT dependencies in a release.</message>
          </requireReleaseDeps>
          <bannedDependencies>
            <excludes>
              <exclude>commons-collections:commons-collections:(,3.2.2)</exclude>
              <exclude>log4j:log4j</exclude>
            </excludes>
          </bannedDependencies>
        </rules>
      </configuration>
    </execution>
  </executions>
</plugin>

dependencyConvergence fails when two paths in the tree want different versions of the same library; requireUpperBoundDeps fails when mediation picks a version lower than one requested elsewhere (see diagnosing conflicts); bannedDependencies blocks libraries or version ranges that are known to be bad. Together they make "we never ship a known-bad library" a checked property rather than a hope.

A vulnerability matcher

The first stand-in does what a scanner does at its core: compare a dependency list with an advisory list that gives, for each library, the range of affected versions and a severity. It uses a simple numeric version comparison and fails the build above a chosen score.

Java Matching dependencies against 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 IO.readln() or a Scanner), it gets the next line from this box.

The advisory identifiers and libraries are invented. Note the two ways the matcher stays quiet: web-framework-core has a different name than the advisory's artifact, and json-lib 2.4.1 is already at or beyond the fixed version, so only the first two entries match. A real scanner works with package URLs and CPE names and far more data, but the range check is the same.

A name and file integrity checker

The second stand-in covers two more defenses. First it flags a requested artifact whose name is one edit away from a trusted one (a typosquat suspect), using edit distance. Then it verifies downloaded bytes against a published SHA-256 value, the way Maven checks a checksum file.

Java Typosquat suspects and checksum verification

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 IO.readln() or a Scanner), it gets the next line from this box.

Edit distance catches simple typos but not every trick, so treat it as a hint. And remember the limit shown earlier: a checksum proves the file matches what the server published, a signature proves who published it.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does failBuildOnCVSS 7 do in the OWASP dependency-check plugin?

    The value is a CVSS severity threshold. Findings at or above it stop the build; lower ones are only reported.
  2. 2. Why is a checksum alone not enough to trust an artifact?

    Checksum and file come from the same place. A PGP signature made with the publisher's key adds proof of origin.
  3. 3. What do Dependabot and Renovate do?

    They watch your POMs and propose updates as pull requests, which then run your normal CI.
  4. 4. Which enforcer rule fails the build when two dependency paths ask for different versions of one library?

    dependencyConvergence reports every library reached with differing versions, which exposes silent mediation choices.
  5. 5. What is dependency confusion?

    If a private groupId is not routed only to your private repository, a build may fetch a malicious public artifact with the same coordinates.

Summary

  • Scan the resolved dependency tree for known vulnerabilities and fail the build above a severity you choose; record reviewed exceptions in suppression files.
  • Automate updates with Dependabot or Renovate, and avoid version ranges and LATEST.
  • Checksums detect corruption, signatures prove origin; use strict checksum policies and verify PGP signatures where possible.
  • Resolve through one trusted https repository manager, and protect internal groupIds against confusion and look-alike names.
  • Use enforcer rules such as dependencyConvergence and bannedDependencies to make policy part of every build.

Next, step back and compare Maven with its main alternative in Maven vs Gradle.