|
| 1 | +# Detecting and resolving dependency vulnerabilities in Gradle projects |
| 2 | + |
| 3 | +This is a simple project demonstrating how to use the `dependency-submission` GitHub action to detect |
| 4 | +vulnerable dependencies in a Gradle project, and various techniques to address these vulnerabilities. |
| 5 | + |
| 6 | +## Setting up the repository to detect vulnerable dependencies |
| 7 | + |
| 8 | +In order to receive alerts about any vulnerable dependencies for this repository: |
| 9 | + |
| 10 | +1. Dependency graph and Dependabot alerts are enabled |
| 11 | +<img width="1284" alt="image" src="https://github.com/bigdaz/dependency-submission-demo/assets/179734/70947c93-af91-4afc-b683-1ee1d1ef7fb2"> |
| 12 | + |
| 13 | +2. A simple `dependency-submission` workflow is configured to run on any push to the `main` branch. |
| 14 | + |
| 15 | +https://github.com/bigdaz/dependency-submission-demo/blob/4606ae62102b56b47b8376f85fd0dc9a8fcffd74/.github/workflows/dependency-submission.yml#L1-L22 |
| 16 | + |
| 17 | +See the [full dependency-submission documentatio](https://github.com/gradle/actions/blob/main/dependency-submission/README.md)n for more details on adding a dependency-submission workflow. |
| 18 | + |
| 19 | +## Vulnerabilities reported for this repository |
| 20 | + |
| 21 | +This repository has 5 current Dependabot alerts for vulnerable dependencies. |
| 22 | +These are not publicly visible, but here is the list: |
| 23 | + |
| 24 | +<img width="969" alt="image" src="https://github.com/bigdaz/dependency-submission-demo/assets/179734/2b7403dd-d244-4f90-9e56-6d1dd8d9a71c"> |
| 25 | + |
| 26 | +In the following section we will step through the process of investigating, isolating and addressing |
| 27 | +these vulnerabilities. |
| 28 | + |
| 29 | +## Updating a direct dependency to non-vulnerable version |
| 30 | + |
| 31 | +In this example repository, a dependency on `org.apache.commons:commons-text:1.9` results in the following |
| 32 | +Dependabot alert: |
| 33 | + |
| 34 | +<img width="729" alt="image" src="https://github.com/bigdaz/dependency-submission-demo/assets/179734/c0bb34ec-9e93-407e-b257-63cf6c7a4670"> |
| 35 | + |
| 36 | +In this simple case, the vulnerable dependency is declared directly in the Gradle project, |
| 37 | +and a newer, non-vulnerable version is available. So the fix is as simple as bumping the version in the project. |
| 38 | +Here is an example pull-request that will address this vulnerability. |
| 39 | + |
| 40 | +https://github.com/bigdaz/dependency-submission-demo/pull/1/files |
| 41 | + |
| 42 | +## Updating a transitive dependency by updating a direct dependency |
| 43 | + |
| 44 | +We see 2 vulnerabilities reported for `org.apache.commons:commons-compress:1.24.0`, like this: |
| 45 | + |
| 46 | +<img width="938" alt="image" src="https://github.com/bigdaz/dependency-submission-demo/assets/179734/6d5bc043-6ff6-46f3-9166-d46096fe6bac"> |
| 47 | + |
| 48 | +But there isn't anywhere in our Gradle project where we add depend on `commons-compress`: this vulnerability |
| 49 | +must involve a _transitive_ dependency, and the first step is to work out which direct dependency is responsible. |
| 50 | +The easiest way to do this is with a Gradle Build Scan®, which is why our workflow is |
| 51 | +configured to automatically publish a Build Scan for every dependency submission. |
| 52 | + |
| 53 | +https://github.com/bigdaz/dependency-submission-demo/blob/4606ae62102b56b47b8376f85fd0dc9a8fcffd74/.github/workflows/dependency-submission.yml#L20-L22 |
| 54 | + |
| 55 | +[Looking at the build scan for the latest submission](https://scans.gradle.com/s/umqci7ktaxfd6/dependencies?dependencies=commons-compress&expandAll&focusedDependency=WzAsMSwyLFswLDAsWzBdXV0&focusedDependencyView=versions), we can see that `commons-compress` is required by `io.minio:minio:8.5.8`, which _is_ a direct dependency of our project. |
| 56 | + |
| 57 | +<img width="1066" alt="image" src="https://github.com/bigdaz/dependency-submission-demo/assets/179734/1a9f8b3a-2be4-4eeb-b438-df2de8090af0"> |
| 58 | + |
| 59 | +Searching for newer versions reveals that there's an updated version of `minio` available (`8.5.9`), |
| 60 | +and when we update our project to this version, the commons-compress library is updated to `1.26.0`. |
| 61 | + |
| 62 | +Here's the pull-request that will update the version of `minio`, resolving the 2 Dependabot alerts |
| 63 | +triggered by `org.apache.commons:commons-compress:1.24.0`. |
| 64 | + |
| 65 | +https://github.com/bigdaz/dependency-submission-demo/pull/5/files |
| 66 | + |
| 67 | +## Updating a transitive dependency using a dependency constraint |
| 68 | + |
| 69 | +There are times when you won't be able to update a direct dependency to resolve a transitive dependency vulnerability. |
| 70 | +Perhaps there isn't a newer version available with the vulnerability resolved, or perhaps your project isn't |
| 71 | +compatible with the newer version. |
| 72 | + |
| 73 | +In this case, you can directly influence the transitive dependency version with a [dependency constraint](https://docs.gradle.org/current/userguide/dependency_constraints.html). |
| 74 | +A dependency contraint allows you to specify a transitive dependency version to use, without adding a direct dependency on that version. |
| 75 | + |
| 76 | +This pull-request adds a dependency constraint that causes the build to use a newer version of `commons-compress`, |
| 77 | +thereby resolving the Dependabot alert. |
| 78 | + |
| 79 | +https://github.com/bigdaz/dependency-submission-demo/pull/6/files |
| 80 | + |
| 81 | +If you inspect [the resulting Build Scan](https://scans.gradle.com/s/iballo7cgijxw/dependencies?dependencies=commons-compress&expandAll&focusedDependency=WzAsMSw1LFswLDAsWzFdXV0&focusedDependencyView=versions), |
| 82 | +you can see that the version of `commons-compress` is updated, but the version of `minio` is not changed. |
| 83 | + |
| 84 | +## Updating a Plugin classpath dependency |
| 85 | + |
| 86 | +As well as the things you declare in a `dependencies` block, the various Gradle plugins that you apply to your build |
| 87 | +will often have library dependencies. |
| 88 | +Vulnerabilities in these dependencies are detected by the `dependency-submission` action. |
| 89 | + |
| 90 | +The final 2 Dependabot alerts in this project are due to `com.squareup.okio:okio-jvm:3.2.0` and `com.squareup.okio:okio:3.2.0`. |
| 91 | + |
| 92 | +<img width="947" alt="image" src="https://github.com/bigdaz/dependency-submission-demo/assets/179734/c9ca7b7b-2f07-4647-9201-7f00a0c167ce"> |
| 93 | + |
| 94 | +When inspecting the Build Scan for these vulnerable versions, [we note that they are not listed in the |
| 95 | +"Dependencies" section](https://scans.gradle.com/s/csber5edafgy2/dependencies?dependencies=okio&expandAll). |
| 96 | +This is because these vulnerable versions are actually brought in by the `com.github.ben-manes.versions` plugin: |
| 97 | +you can see this by [searching for 'okio' in the "Build Dependencies" section of the Build Scan](https://scans.gradle.com/s/iballo7cgijxw/build-dependencies?dependencies=okio&expandAll&focusedDependency=WzAsMCwyOSxbMCwwLFsyXV1d&focusedDependencyView=versions). |
| 98 | + |
| 99 | +<img width="1236" alt="image" src="https://github.com/bigdaz/dependency-submission-demo/assets/179734/fdd3392d-bc91-4fde-a790-8d17127bc3cb"> |
| 100 | + |
| 101 | +Although vulnerable plugin dependencies like this can be trickier to identify, they can be addressed in much the same way as |
| 102 | +regular transitive dependencies. |
| 103 | +In this case there is no newer version of the plugin available, so we can't simply update the plugin version. |
| 104 | +Instead, we must add a dependency constraint to force a newer version of `okio` to be used. |
| 105 | + |
| 106 | +By looking closely at the Build Scan and searching for released versions, we find that constraining the |
| 107 | +version of `com.squareup.okhttp3:okhttp:4.12.0`, both the `okio` and `okio-jvm` versions will be transitively |
| 108 | +updated to non-vulnerable versions. |
| 109 | + |
| 110 | +This pull request adds a dependency constraint to the `buildscript` classpath, which fixes the security |
| 111 | +vulnerablitity in `okio` that is introduced by the `com.github.ben-manes.versions` plugin. |
| 112 | + |
| 113 | +https://github.com/bigdaz/dependency-submission-demo/pull/4/files |
| 114 | + |
| 115 | +The [resulting Build Scan demonstrates](https://scans.gradle.com/s/csber5edafgy2/build-dependencies?dependencies=okio&expandAll&focusedDependency=WzAsMCwzNCxbMCwwLFsxXV1d&focusedDependencyView=versions) that the build dependencies no longer include the vulnerable dependency versions. |
| 116 | + |
| 117 | +<img width="1199" alt="image" src="https://github.com/bigdaz/dependency-submission-demo/assets/179734/c9a75b62-2500-4f28-aa74-dcb0dd3b88a7"> |
0 commit comments