> For the complete documentation index, see [llms.txt](https://help.aikido.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.aikido.dev/docs/docs-ja/kdosukyan/scanning-practices/scala-dependency-management-and-scanning-for-buildsbt.md).

# Scala: build.sbt の依存関係管理とスキャン

## 課題 <a href="#the-challenge" id="the-challenge"></a>

Aikidoは、Scalaの依存関係における既知の脆弱性（CVE）だけでなく、それらの依存関係で使われているマルウェアや危険なライセンスも検出できます。

Aikido はそれらの依存関係と、その推移的なサブ依存関係をどのように見つけるのでしょうか？

Scalaの場合は、次のものをスキャンします。 `build.sbt` 依存関係についてファイルを確認します。なお、 `build.sbt` 一部の依存関係について、ファイルに正確なバージョンが含まれていない場合があります。そのため、Aikidoがアプリケーション内のリスクをすべて検出できないことがあります。

そのため、次のものを使用することをおすすめします。 `build.sbt.lock` 各依存関係とそのサブ依存関係の正確なバージョンを含むロックファイル。

Aikido によるセキュリティスキャンを容易にする以外にも、lockfile を使う理由があります:

* lockfile を使うことで、悪意のあるパッケージを介したサプライチェーン攻撃から身を守れます。この種の攻撃はますます一般的になっています
* ロックファイルを使うことで、全員がパッケージのまったく同じマイナーバージョンを使用するため、ビルドがより予測しやすくなります。「自分の環境では動く」問題が起きる可能性が低くなります。
* ビルド時間の短縮: もはや依存関係の解決は不要です

## 例 <a href="#example" id="example"></a>

これらの課題を示す最近の事例を見てみましょう。

```
// ロックファイルなしの元の build.sbt
libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-http" % "10.2.+",
  "org.apache.spark" %% "spark-core" % "3.+",
  "com.datastax.cassandra" % "cassandra-driver-core" % "latest.release"
)
```

Aikidoが報告した内容：

* Akka HTTPに重大なCVEが3件
* Sparkに高深刻度の脆弱性が2件
* Cassandraドライバーに重大な脆弱性が1件

調査の結果、すべて誤検知でした。スキャナーは、実際に本番環境で使用されていたものとは異なるバージョンを照合していたのです：

```
// スキャナーが実行されていると判断したもの
akka-http 10.2.0   // 脆弱
spark-core 3.0.0   // 脆弱
cassandra-driver 4.0.0  // 脆弱


// 実際に本番環境で実行されていたもの
akka-http 10.2.10  // 安全
spark-core 3.3.2   // 安全
cassandra-driver 4.15.0  // 安全
```

## 解決策：ロックファイルを追加する <a href="#solution-add-a-lockfile" id="solution-add-a-lockfile"></a>

次を使用してください [SBT Dependency Lock](https://github.com/stringbean/sbt-dependency-lock) プロジェクトのロックファイルを生成するためのプラグイン。

**ステップ1：SBT Dependency Lockプラグインを追加する**

```
// plugins.sbt に記載
addSbtPlugin("software.purpledragon" % "sbt-dependency-lock" % "1.5.1")
```

**ステップ2：ロックファイルを生成する**

```
// このコマンドを実行して依存関係を解決し、ロックファイルを生成します
sbt "dependencyLockWrite"
```

生成されたロックファイル（build.sbt.lock）は、すべての依存関係を明示的に定義します：

```
{
  "com.typesafe.akka:akka-http_2.13": "10.2.10",
  "org.apache.spark:spark-core_2.13": "3.3.2",
  "com.datastax.cassandra:cassandra-driver-core": "4.15.0"
}
```

**ステップ3：ロックされた依存関係を強制する**

```
// このコマンドを実行して依存関係を解決し、ロックファイルと照合します
sbt "dependencyLockCheck"
```

### 別の回避策：コンテナスキャン <a href="#alternative-workaround-container-scanning" id="alternative-workaround-container-scanning"></a>

ロックファイルはソースレベルで優れた依存関係管理を提供しますが、コンテナスキャンはセキュリティ検証のもう一つの強力な手法です。コンテナにはコンパイル済みの成果物が含まれているため、本番環境の実際の状態を表します。

#### コンテナスキャンの利点 <a href="#benefits-of-container-scanning" id="benefits-of-container-scanning"></a>

```
# スキャン対象の例を示す Dockerfile
FROM openjdk:11-jre-slim


# ここにコンパイル済みの成果物があります
COPY target/scala-2.13/your-app.jar /app/
COPY target/scala-2.13/lib/* /app/lib/


# これらが本番環境で実際に実行されるバージョンです
RUN ls -la /app/lib/

# akka-http_2.13-10.2.10.jar
# spark-core_2.13-3.3.2.jar
# cassandra-driver-core-4.15.0.jar
```

#### コンテナスキャンを使うタイミング <a href="#when-to-use-container-scanning" id="when-to-use-container-scanning"></a>

コンテナスキャンは、次のような場合に特に有効です：

* 本番投入可能な成果物を検証する必要がある
* ビルドプロセスが複数の段階に分かれている
* 本番環境で実行されている正確なバージョンを確認したい
* アプリケーション本体と実行環境の両方をスキャンする必要がある

## コンテナスキャン vs. ロックファイル <a href="#container-scanning-vs-lock-files" id="container-scanning-vs-lock-files"></a>

| **項目**   | **コンテナスキャン** | **ロックファイル** |
| -------- | ------------ | ----------- |
| 検証のタイミング | ビルド後         | ビルド前        |
| 確認対象     | コンパイル済みの成果物  | ソースの依存関係    |
| 正確性      | 本番環境と完全一致    | 開発環境と完全一致   |
| 統合       | CI/CDパイプライン  | 開発ワークフロー    |

> **ベストプラクティス**：両方のアプローチを使いましょう。コンテナスキャンは有用ですが、ロックファイルと併用すると最も効果的です。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://help.aikido.dev/docs/docs-ja/kdosukyan/scanning-practices/scala-dependency-management-and-scanning-for-buildsbt.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
