> 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 に 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 依存関係ロック](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.
