<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Srujan</title>
    <description>The latest articles on DEV Community by Srujan (@srujan_g).</description>
    <link>https://dev.to/srujan_g</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F35841%2F691603d6-5a49-46b4-a09b-eeb9906e9fd6.jpeg</url>
      <title>DEV Community: Srujan</title>
      <link>https://dev.to/srujan_g</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/srujan_g"/>
    <language>en</language>
    <item>
      <title>Escaping a transitive dependency nightmare with some help from Gradle</title>
      <dc:creator>Srujan</dc:creator>
      <pubDate>Mon, 06 Sep 2021 16:23:01 +0000</pubDate>
      <link>https://dev.to/srujan_g/escaping-a-transitive-dependency-nightmare-with-some-help-from-gradle-374b</link>
      <guid>https://dev.to/srujan_g/escaping-a-transitive-dependency-nightmare-with-some-help-from-gradle-374b</guid>
      <description>&lt;p&gt;Every so often at work, our CI pipeline's dependency check job spits out another vulnerable dependency detected in our application. Not entirely surprising considering the rate at which new CVEs are published these days.&lt;/p&gt;

&lt;p&gt;This isn't much of a problem for direct (or first-level) dependencies, as long as there's a patch for it out already. All you do is upgrade your dependency to the latest patched version, ensure the update doesn't actually break your application, and you're good to go.&lt;/p&gt;

&lt;p&gt;If the vulnerability lies in a transitive dependency (a dependency of &lt;em&gt;another&lt;/em&gt; dependency, typically one that you don't directly declare), it starts to get a little messy. A couple of issues can arise here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You don't know where this dependency even came from.&lt;/li&gt;
&lt;li&gt;The parent dependency hasn't updated to the latest patched version yet.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Luckily, modern build tools help make this chore (an important chore nonetheless) a little less painful. In our case, this was with some help from Gradle.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tracking down transitive dependencies
&lt;/h3&gt;

&lt;p&gt;Identifying the top-level dependency where a vulnerable dependency is used is usually the first step to patching your application.&lt;/p&gt;

&lt;p&gt;Gradle's build scan can probably provide the most information about your project in one central place, but this involves publishing build details to Gradle's servers, something you might want to avoid if you don't want sensitive details related to your project out in public.&lt;/p&gt;

&lt;p&gt;Instead, a fast and easy way to do this locally on the command-line is with the &lt;code&gt;dependencyInsight&lt;/code&gt; task provided by Gradle. This visualizes the origin of a transitive dependency and, in the case of multiple conflicting versions, also shows why a particular version was chosen. &lt;/p&gt;

&lt;p&gt;The latter is what separates it from another similar task called &lt;code&gt;dependencies&lt;/code&gt; which only provides a raw visualization of every dependency in your project.&lt;/p&gt;

&lt;p&gt;You can run this task from the command-line as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ gradle -q dependencyInsight --dependency &amp;lt;dependency-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also provide an option &lt;code&gt;--configuration&lt;/code&gt; to specify the gradle configuration to scan. If nothing is provided, it scans &lt;code&gt;compileClasspath&lt;/code&gt; by default.&lt;/p&gt;

&lt;p&gt;In my case, I wanted to track down &lt;code&gt;commons-io&lt;/code&gt; (version 2.6 had the vulnerability CVE-2021-29425) in my project. Running the above gave the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;commons-io:commons-io:2.6 (selected by rule)
   variant "compile" [
      org.gradle.status              = release (not requested)
      org.gradle.usage               = java-api
      org.gradle.libraryelements     = jar (compatible with: classes+resources)
      org.gradle.category            = library

      Requested attributes not found in the selected variant:
         org.gradle.dependency.bundling = external
         org.gradle.jvm.version         = 11
   ]

commons-io:commons-io:2.6 
+--- &amp;lt;internal-library&amp;gt;:0.1.372
     +--- compileClasspath
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(&lt;em&gt;Simplified output only for representation, most large projects have much more complex dependency trees&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;This tells me that &lt;code&gt;commons-io v2.6&lt;/code&gt; is a dependency of an internal library that we used between teams. Now I could have waited for the team responsible for maintaining this internal library to patch it themselves, but that would mean blocking my CI pipeline from further deployments for an unknown period of time. Instead, I need a way to force-upgrade this dependency to a patched version until the maintainers patch their library as well.&lt;/p&gt;

&lt;p&gt;This is where another feature of Gradle comes into play.&lt;/p&gt;

&lt;h3&gt;
  
  
  Applying constraints on transitive dependencies
&lt;/h3&gt;

&lt;p&gt;Dependency constraints is the easiest (and the recommended) way to force resolve a particular version of a transitive dependency. Constraints apply on all direct and transitive dependencies defined in a project. &lt;/p&gt;

&lt;p&gt;A constraint on a dependency can be applied in your &lt;code&gt;build.gradle&lt;/code&gt; script like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dependencies {
    implementation 'org.apache.httpcomponents:httpclient'
    constraints {
        implementation('org.apache.httpcomponents:httpclient:4.5.3') {
            because 'previous versions have a bug impacting this application'
        }
        implementation('commons-codec:commons-codec:1.11') {
            because 'version 1.9 pulled from httpclient has bugs affecting this application'
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above constraints force Gradle to always use &lt;code&gt;httpclient v4.5.3&lt;/code&gt; and forces resolution of the &lt;code&gt;commons-codec&lt;/code&gt; library to v1.11 instead of v1.9 specified in &lt;code&gt;httpclient&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Therefore, to force upgrade to a patched version of &lt;code&gt;commons-io&lt;/code&gt;, I had to add the following constraint in my &lt;code&gt;build.gradle&lt;/code&gt; script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;constraints {
        implementation('commons-io:commons-io:2.10.0') {
            because('Versions &amp;lt; 2.7 allows limited path traversal with FileNameUtils.normalize method - CVE-2021-29425')
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;because&lt;/code&gt; closure allows you to explain why this version should be chosen and it will appear in the &lt;code&gt;dependencyInsight&lt;/code&gt; output.&lt;/p&gt;

&lt;p&gt;Now my &lt;code&gt;dependencyInsight&lt;/code&gt; output looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;commons-io:commons-io:2.10.0
   variant "compile" [
      org.gradle.status              = release (not requested)
      org.gradle.usage               = java-api
      org.gradle.libraryelements     = jar (compatible with: classes+resources)
      org.gradle.category            = library

      Requested attributes not found in the selected variant:
         org.gradle.dependency.bundling = external
         org.gradle.jvm.version         = 11
   ]
   Selection reasons:
      - By constraint : Versions &amp;lt; 2.7 allows limited path traversal with FileNameUtils.normalize method - CVE-2021-29425


commons-io:commons-io:2.6 -&amp;gt; 2.10.0
+--- &amp;lt;internal-library&amp;gt;:0.1.372
     +--- compileClasspath
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now I can rest assured that the vulnerable dependency always resolves to the patched version instead of the one declared inside the internal library.&lt;/p&gt;

</description>
      <category>gradle</category>
      <category>java</category>
    </item>
  </channel>
</rss>
