- mavenで導入されたライブラリの確認方法
- mavenで導入されたライブラリで内部的に利用しているライブラリのバージョンアップ手順
昨今では、ライブラリの導入にmavenを利用している方が多いと思います。
mavenは依存関係を自動で解決してくれるので便利なツールですが、mavenをちゃんと理解せずに使っているかたも多いのではないでしょうか。
導入したライブラリのなかで利用しているライブラリに脆弱性が発見された場合、使い方を理解していないと対応方法がわからず困ります。
この記事では、mavenで導入したライブラリの脆弱性を対応する手順を具体的に解説しています。
実際の開発の現場で役立つ内容となっています。
ぜひ最後までご覧ください。
テスト用に利用するpom.xml
今回は、spring-webライブラリを導入したプロジェクトで、spring-webが内部で使用しているspring-coreに脆弱性があったとして解説していきます。
pom.xmlはこのように定義。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-web-example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring.version>5.1.4.RELEASE</spring.version> <!-- 古めのバージョンを指定 -->
</properties>
<dependencies>
<!-- Spring Web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
mvn dependency:treeを実行して依存関係を確認してみます。
実行結果はこちら。
[INFO] -------------------< com.example:spring-web-example >-------------------
[INFO] Building spring-web-example 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- dependency:3.7.0:tree (default-cli) @ spring-web-example ---
[INFO] com.example:spring-web-example:jar:1.0-SNAPSHOT
[INFO] \- org.springframework:spring-web:jar:5.1.4.RELEASE:compile
[INFO] +- org.springframework:spring-beans:jar:5.1.4.RELEASE:compile
[INFO] \- org.springframework:spring-core:jar:5.1.4.RELEASE:compile
[INFO] \- org.springframework:spring-jcl:jar:5.1.4.RELEASE:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
spring-webは、内部的にspring-coreなどのライブラリを利用しているため、自動的にダウンロードされます。
内部的に仕様しているライブラリの脆弱性対応方法
では、ライブラリの中で利用しているライブラリに脆弱性があった時の対応手順を解説していきます。
例として、spring-coreライブラリの5.1.4のバージョンに脆弱性が発見されたと仮定します。
spring-coreライブラリを脆弱性対応版バージョンにアップデートします。
内部的なライブラリのバージョンアップは、mavenの「依存関係のオーバーライド」機能を利用します。
具体的な対応手順を解説していきます。
依存関係のオーバーライド
対象プロジェクトのpom.xmlにバージョンアップしたいモジュールを定義します。今回の場合は、spring-coreライブラリをバージョンアップしたいのでその記述を追加します。
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-web-example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring.version>5.1.4.RELEASE</spring.version>
<spring-core.version>5.3.24</spring-core.version> <!-- 最新バージョンを指定 -->
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring Core のバージョンを最新にオーバーライド -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-core.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring Web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
依存関係のアップデートの確認
対象のプロジェクトのspring-coreにオーバーライドしたバージョンが適用されているか確認します。
mvn dependency:treeを実行して依存関係を確認してみます。
実行結果はこちら。
[INFO] -------------------< com.example:spring-web-example >-------------------
[INFO] Building spring-web-example 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- dependency:3.7.0:tree (default-cli) @ spring-web-example ---
[INFO] com.example:spring-web-example:jar:1.0-SNAPSHOT
[INFO] \- org.springframework:spring-web:jar:5.1.4.RELEASE:compile
[INFO] +- org.springframework:spring-beans:jar:5.1.4.RELEASE:compile
[INFO] \- org.springframework:spring-core:jar:5.3.24:compile
[INFO] \- org.springframework:spring-jcl:jar:5.3.24:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
上記結果から、spring-coreライブラリとそれに依存するライブラリのバージョンがあがっていることが確認できました。
これで脆弱性の対応は完了です。Mavenで導入したライブラリの脆弱性はこの様に対応していきます。
mvn clean install でビルドを再ビルド
依存関係がアップデートされていることを確認出来たら、以下のコマンドを実行しプロジェクト全体を再ビルドします。
mvn clean installまとめ
- 依存関係の確認は「mvn dependency:tree」コマンドを利用する
- 脆弱性の対応は、pom.xmlのオーバーライド機能を利用してライブラリを上書きする
- 修正結果は「mvn dependency:tree」コマンドで対応後の依存関係を確認する
脆弱性の対応は、システム開発中やシステムリリース後に必ず発生します。
脆弱性の対応はセキュリティに、関わる対応のため、素早い対応を求められることが多いです。
対応手順をあらかじめ知っておくことで迅速な対応が可能になります。
本記事が皆様の参考になれば幸いです。



コメント