> 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 によるセキュリティスキャンを容易にする以外にも、ロックファイルを使用する理由はあります:

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

## 例 <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 に 3 件の重大な CVE
* 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.
