> 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 における重大な 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.
