> 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/zen-firewall/zen-installation-instructions/zen-firewall-for-ruby/rails.md).

# Ruby on Rails

このガイドでは、アプリケーション向けに Aikido の Zen Firewall をインストールして設定する方法を順を追って説明します。アプリケーションを保護するために、以下の手順に従ってください。

問題や不具合が発生した場合は、サポートチャットまたは GitHub の issue でお気軽にご連絡ください

<https://github.com/AikidoSec/firewall-ruby>

## 要件

* Ruby 2.7 以上。
* Ruby on Rails 7.x または 8.x。
* [Aikidoアカウント](/docs/docs-ja/hajimeni/setting-up-your-account.md) & [Zen Firewallトークン](/docs/docs-ja/zen-firewall/zen-installation-instructions/creating-an-aikido-zen-firewall-token.md)

## インストールと設定

{% stepper %}
{% step %}
**AikidoによるZen Firewallをインストール**

RubyアプリにZenをインストールします：

```bash
bundle add aikido-zen
```

Zenを次より前に読み込む `Bundler.require` で `config/application.rb`:

```ruby
require "aikido-zen"
Aikido::Zen.protect!

Bundler.require(*Rails.groups)
```

{% endstep %}

{% step %}
**リクエストブロックとユーザー識別を有効にする**

このミドルウェアを使用して現在のユーザーを識別し、ユーザーブロックなどのユーザーに基づく機能を有効にします。

Zen Firewall が攻撃をブロックするためにこのミドルウェアを必要とすることはありません。基本の攻撃保護はこれなしで動作します。Zen にどのユーザーがリクエストを送っているかを認識させたい場合に、このミドルウェアを追加してください。

アプリケーションがユーザーを識別する方法に合わせて、例を調整してください。

```ruby
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  private

  def authenticate_user!
    # 認証ロジック
    # ...

    Aikido::Zen.set_user(
      id: current_user.id,
      name: current_user.name
    )
  end
end
```

{% endstep %}

{% step %}
**Zen Firewall をドライ / 検出のみモードで開始する**

```bash
AIKIDO_BLOCK=false AIKIDO_TOKEN=AIK_RUNTIME_ bundle exec rails s
```

トークンを環境変数として設定し、Aikido Zen エージェントがそれを取得できるようにしてください。まだトークンがない場合は、次に従ってください [こちらの手順](/docs/docs-ja/zen-firewall/zen-installation-instructions/creating-an-aikido-zen-firewall-token.md).

```bash
AIKIDO_TOKEN=AIK_RUNTIME_
```

アプリが期待どおりに動作し、いかなるリクエストもブロックしないことを確認するため、まずは dry モードでアプリを起動することを推奨します。誤検知を避けるため、Zen Firewall はステージング環境で 2 週間実行することをお勧めします。

```bash
AIKIDO_BLOCK=false
```

{% hint style="info" %}
次を使用できます `AIKIDO_DEBUG=true` エージェントの動作について、より詳細な情報を得るためにデバッグモードを有効にするためのものです。環境変数の詳細については: [環境変数による設定](/docs/docs-ja/zen-firewall/zen-installation-instructions/configuration-via-environment-variables.md)
{% endhint %}

トークンに暗号化された認証情報を使いたい場合は:

```yaml
# config/credentials.yml.enc
zen:
  token: "AIKIDO_RUNTIME_..."
```

```ruby
# config/initializers/zen.rb
Rails.application.config.zen.token = Rails.application.credentials.zen.token
```

{% endstep %}

{% step %}
**アプリをテスト**

アプリケーションを開いて、いくつかの操作を行うか、いくつかのページを開いてください。Zen がアプリケーション内のルートを自動的に検出します。

{% hint style="info" %}
Zen は10分ごとに Aikido にデータを送信します
{% endhint %}

Zen アプリケーションの次のページを見ることで、エージェントが正常に動作しているか確認できます:

* **イベント**: 「Application started」イベントが表示されるはずです。
* **ルート**: しばらくすると、アプリケーションのルートがここにメソッド、ルート、リクエストとともに表示され始めます。
* **インスタンス**: Zen がインストールされているアプリケーションのアクティブなインスタンス数が表示されるはずです。

<figure><img src="https://715870456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyKbzcQGrx7UtrG0nPZZ7%2Fuploads%2Fgit-blob-79af8470fd5feef0b378588fc5de01c3ea89d9ae%2FScreenshot%202025-06-23%20at%2010.08.35.png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**ダッシュボードでレート制限を設定**

Zen Firewall ミドルウェアを追加したら、Aikido ダッシュボードでレート制限を設定することで、ルートをブルートフォース攻撃から保護するテストができます。

1. 作成したアプリをクリックします。
2. へ移動 **ルート** タブを選択します。
3. 制限したいルートを見つけてクリック **レート制限の設定**.
4. 手順に従ってレート制限を設定します（例：1分あたり5リクエスト）。

![保護およびレート制限のオプション付きで認証ルートを表示する API ルート管理インターフェース。](https://715870456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyKbzcQGrx7UtrG0nPZZ7%2Fuploads%2Fgit-blob-4f588cb82dce7a932262f90046f280ee5115a13d%2FScreenshot%202025-10-30%20at%2010.38.17.png?alt=media)

![POST /auth/login に対して、1分あたり5リクエストのレート制限を設定します。](https://715870456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyKbzcQGrx7UtrG0nPZZ7%2Fuploads%2Fgit-blob-2633dcea71b485b0257ecf6b607323a9646dae06%2Fucarecdn-34b1af76-facb-4b6d-9c87-155236236b8f.png?alt=media)

**レート制限の確認**

アプリを起動し、レート制限を設定したルートに1分以内に5回アクセスしてみてください。5回目の試行後、レート制限エラーが表示されるはずです：

```
Aikido ファイアウォールによりレート制限されています。（あなたのIP: 1.2.3.4）
```

{% endstep %}

{% step %}
**次のステップ**

Zen Firewall のインストールに成功しました。おめでとうございます。問題が発生した場合、ご不明点や機能の要望がある場合は、遠慮なくサポートまでご連絡ください。

これで、Zen Firewall が提供する多彩な機能をぜひご確認ください：

* [Zen FirewallでBotトラフィックをブロックする](/docs/docs-ja/zen-firewall/zen-features/blocking-bot-traffic-with-zen-firewall.md)
* [Zen FirewallでTorトラフィックをブロックまたは監視する](/docs/docs-ja/zen-firewall/zen-features/blocking-tor-traffic-with-zen-firewall.md)
* [Zen Firewallでユーザーを追跡する](/docs/docs-ja/zen-firewall/zen-features/blocking-users-with-zen-firewall.md)
* [Zen Firewallで既知の脅威アクターをブロックする](/docs/docs-ja/zen-firewall/zen-features/blocking-known-threat-actors-with-zen-firewall.md)
* [Zen Firewall で国別にトラフィックをブロックする](/docs/docs-ja/zen-firewall/zen-features/blocking-traffic-by-country-with-zen-firewall.md)
* [ルートのレート制限を設定する](/docs/docs-ja/zen-firewall/zen-features/setting-up-rate-limiting-for-routes.md)
* [アウトバウンドドメインを監視する](/docs/docs-ja/zen-firewall/zen-features/monitor-outbound-domains.md)

追加情報：

* [Zen のパフォーマンスと信頼性](/docs/docs-ja/zen-firewall/miscellaneous/how-zen-works-performance-reliability.md)
* [Zen Firewall のブロックモードと検知モード](/docs/docs-ja/zen-firewall/zen-features/blocking-vs-detection-mode-in-zen-firewall.md)
* [Zen の統計を理解する](/docs/docs-ja/zen-firewall/zen-features/understanding-your-zen-statistics.md)
  {% endstep %}
  {% endstepper %}


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.aikido.dev/docs/docs-ja/zen-firewall/zen-installation-instructions/zen-firewall-for-ruby/rails.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
