> 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/custom-code-context-for-sast-rules.md).

# SASTルールのためのカスタムコードコンテキスト

Custom Code Context では、特定の SAST ルールに平易な言葉でのガイダンスを追加できます。Aikido はそのガイダンスを、 [SAST自動トリアージ](/docs/docs-ja/aikido-agent/sast-autotriage.md) より適切に真陽性と誤検知を判定するために使用します。コンテキストはグローバルにも、リポジトリ単位でも追加できます。

それは **しません** ルールが検出する内容を変更しません。Aikido が検出結果をより正確に解釈するのに役立ちます。

### ユースケース

ルールにコードベース固有の知識が必要な場合は、Custom Code Context を使用します。たとえば次のような場合です：

* 信頼できるサニタイズ用ライブラリ
* 危険な API を囲む社内の安全なラッパー
* データを信頼済みとしてマークする検証ヘルパー
* 1 つのリポジトリでのみ安全なパターン
* 信頼できる入力を使って隔離されたマシン上で実行されるスクリプト

<details>

<summary>具体例を表示</summary>

**信頼できるサニタイズ用ライブラリ**

{% code overflow="wrap" %}

```
<L> という公開ライブラリをサニタイズに頻繁に使用しています。このライブラリの関数が返すすべての変数は信頼済みと見なされます。
```

{% endcode %}

**社内の検証ヘルパー**

{% code overflow="wrap" %}

```
このルールでは、validateAndNormalizeUserInput() が返す値はサニタイズ済みと見なされます。このヘルパーは無効な文字を拒否し、厳格な許可リストを適用します。
```

{% endcode %}

**危険な API を囲む安全なラッパー**

{% code overflow="wrap" %}

```
このルールでは、safeRedirect() を介した呼び出しが想定されています。このラッパーは、承認済みドメインの許可リストにある URL へのリダイレクトのみを許可します。
```

{% endcode %}

</details>

### コンテキストを追加する方法

{% hint style="info" %}
Custom Code Context は、AutoTriage をサポートする SAST ルールでのみ利用できます。
{% endhint %}

**ステップ 1.** Repositories Checks ページを開き、「[SAST ルールを表示](https://app.aikido.dev/repositories/sast)'.

**ステップ 2.** 該当する SAST ルールを見つけてアクションメニューを開き、選択します **Custom Code Context**.<br>

<figure><img src="/files/a42773fd57b3dc9d72cd632c0a62d8461ccadc8f" alt=""><figcaption></figcaption></figure>

**手順 3。** 平易な言葉でコンテキストを追加します。

<figure><img src="/files/cb2a84eb50812aa989cbd70a4f64ddecbc448b46" alt=""><figcaption></figcaption></figure>

**手順 4。** コンテキストをグローバルに適用するか、そのリポジトリのみに適用するかを選びます。変更を保存します。

{% hint style="info" %}
新しいコンテキストを適用する前に、手動で再スキャンが必要です。
{% endhint %}

### 書き方のヒント

コンテキストは狭く、具体的に保ちます。

Do:

* 正確なライブラリ名、関数名、ラッパー名、またはフォルダー名を挙げる
* データが信頼できる、またはサニタイズ済みである理由を説明する
* 選択したルールに固有の内容にする

避けるべきこと：

* 「入力はたいていサニタイズされている」のような広い主張
* 関数名のない曖昧な記述
* すべてに当てはまる例外

<details>

<summary>良い例と弱い例を表示</summary>

**良い例**

{% code overflow="wrap" %}

```
この SQL インジェクションのルールでは、buildSafeQuery() が返す値は信頼済みです。このヘルパーは、パラメータ化クエリのみを作成し、生のユーザー入力を連結することはありません。
```

{% endcode %}

**弱い例**

{% code overflow="wrap" %}

```
私たちのチームは SQL クエリを慎重に扱っているので、これらの検出結果は誤検知であることが多いです。
```

{% endcode %}

</details>

### Custom Code Context とカスタム SAST ルール

既存のルールのトリアージを改善したい場合は、Custom Code Context を使用します。

使用 [カスタム SAST ルール](/docs/docs-ja/kdosukyan/scanning-practices/add-custom-sast-iac-rules.md) コードベースで新しいパターンを検出する必要がある場合に使います。

要するに：

* **Custom Code Context** 解釈を改善する
* **カスタム SAST ルール** 検出範囲を広げる


---

# 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/custom-code-context-for-sast-rules.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.
