Preventing Dependency Chain Attacks in Maven

July 3, 2013 | Comments
Tags: HowTo

As regular readers will be aware, I do a lot of work within the Bitcoin community contributing code wherever I can. Recently an interesting problem came up with the Bitcoinj library which has highlighted a particular weakness within Maven that I feel more developers should be aware of: the dependency-chain attack.

What is a dependency-chain attack?

Imagine that some malicious developer, Mallory, was able to gain write access to a Maven repository that you used as part of your development. Mallory knows from examining your pom.xml that you depend on several artifacts which are served from this repository and so he sets about creating a new version of one of these artifacts containing some code that is designed to detect and disrupt your application. In the case of the Bitcoinj library this could be to copy the unencrypted private keys back to his server.

Mallory deletes the original artifact and replaces it with his own which has the same version number as the original but a different SHA1 and does not contain a valid signature.

How would you detect that in your Maven build?

The short answer is that you can't. The SHA1 is valid and Maven does not check JAR signatures during the build process. You might get lucky in that you won't download the artifact since you have a local copy of the release, but if you move to another machine then you'll be pulling down the malicious code.

You need a whitelist.

A whitelist?

By obtaining a list of known good artifacts and their SHA1 signatures it is possible to build up a list of "approved" artifacts and detect when any have changed from expectations. As long as you remain in control of your source code repository then you can be sure that your whitelist has not been tampered with. Obviously, if Mallory got control of your source code then all bets are off.

I have released a simple DigestRule for use with the Maven Enforcer Plugin to provide such a whitelist. You can choose how detailed you want it to be depending on your own security requirements, but bear in mind that every unchecked dependency is a way in for an attacker through the back door.

How is it configured?

The configuration below shows how you would use the DigestRule in your own projects. Of course the values used below should be treated only as examples.

In production you would get the list of URNs as a GPG signed configuration from the developers of the project that you were depending on.

      <!-- Use the Enforcer to verify build integrity -->
                <digestRule implementation="">

                  <!-- Create a snapshot to build the list of URNs below -->

                  <!-- List of required hashes -->
                  <!-- Format is URN of groupId:artifactId:version:type:classifier:scope:hash -->
                  <!-- classifier is "null" if not present -->


                    <!-- A check for the rules themselves -->



        <!-- Ensure we download the enforcer rules -->


I'm not typing in all that!

Clearly trying to manually create the list of URNs would be a painful process, particularly in a large project with many layers of transitive dependencies to explore. Fortunately, the buildSnapshot flag will cause the plugin to examine all the resolved dependencies within your project and build a list of URNs that you can copy-paste (with caution) into your build.

How do I use it?

For maximum effect, the rules should be triggered during the verify phase so that all the dependencies that could affect the build will have been pulled in. This has the useful side effect that as a developer you're not continuously checking yourself for every build - only when you're about to perform an install or deploy.

You may want to grep/find on a case-sensitive match for "URN" to find the verification messages.

You can try it out on itself by checking out the source code from GitHub:

mvn clean install

The reactor will first build the Digest Enforcer Rules and then go on to build another artifact that depends on them working (the Rule Tester project). This second project demonstrates how you would include Digest Enforcer Rules in your projects.

Does it work with third-party build systems?

Yes. One of the design goals was to allow Bitcoinj to be deployed into Maven Central with sufficient support that any compromise to either it or its supporting libraries could be detected. Now your projects that include Bitcoinj will be able to build through Travis or your own equivalent (once Bitcoinj arrives in Maven Central or another controlled repo).

How do I check the checker?

Trust has to begin somewhere so I'm going to provide some signed declarations for each version in the GitHub repo. These can be validated against my public key 59A81D7B. Obviously, you can also compile this code yourself and obtain the same result.

Why not try to verify the certificate for version 0.0.1 for practice?

If you are at all concerned about dependency-chain attacks, or just want to be that little bit safer when building, then please take a look at the project and offer up some critique if it doesn't meet your standards.

This could save my department a fortune...

The project is not tied to Bitcoinj so developers in secure environments such as research, military or government establishments may also want to consider this as an additional layer of protection to those already in place.

It may even allow you to start using open source where once you were unable due to the dependency-chain risk saving your department vast amounts of money otherwise spent on validation fees.


Related Posts

Useful? Consider a small donation: 1KzTSfqjF2iKCduwz59nv2uqh1W2JsTxZH

Got an opinion? Comments are welcome!

Want more? Take a look at the following:

How to create a deterministic JAR
Dropwizard with Ember Data demo
How to deploy dynamic sites with git
How to deploy static sites with git
Dropwizard with OpenID
How to include Markdown with Freemarker in Dropwizard
How to implement a RuntimeExceptionMapper for Dropwizard
MultiBit Merchant: Implementing HMAC authentication in Dropwizard
How to deploy a Dropwizard project to Heroku
How to recover your bitcoins from a failed hard drive
How to build a Builder
How to accept bitcoins on your blog with no code

comments powered by Disqus