User Agent Blocking を使うと、特定のブラウザーまたは Web アプリケーションの User-Agent リクエストヘッダー ↗ をブロックできます。ユーザーエージェントルールは、個々のサブドメインではなく、ドメイン全体に適用されます。
ユーザーエージェントルールは、Zone Lockdown ルール のあとに適用されます。Zone Lockdown で IP アドレスを許可すると、その IP はユーザーエージェントルールをスキップします。
Cloudflare の User Agent Blocking は、すべてのプランで利用できます。User agent rules オプションは、ユーザーエージェントルールを 1 つ以上設定している場合にだけ表示されます。
利用できるユーザーエージェントルールの数は、Cloudflare のプランによって異なります。
| Free | Pro | Business | Enterprise | |
|---|---|---|---|---|
| 利用可否 | あり | あり | あり | あり |
| ルール数 | 10 | 50 | 250 | 1,000 |
-
Cloudflare ダッシュボードで Security rules ページを開きます。
Security rules を開く ↗ -
Create rule > User agent rules を選択します。
-
Name/Description に、わかりやすいルール名を入力します。
-
Action で、実行するアクションを選びます。Block、Non-Interactive Challenge、Managed Challenge、Interactive Challenge のいずれかです。
-
User Agent にユーザーエージェントの値を入力します(
*などのワイルドカードは使えません)。たとえば Bad Bot の Web スパイダーをブロックする場合は、BadBot/1.0.2 (+http://bad.bot)を入力します。 -
Save and Deploy blocking rule を選択します。
Create a User Agent Blocking rule 操作に対して、次のような POST リクエストを送ります。
Required API token permissions
At least one of the following token permissions is required:Firewall Services Write
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/firewall/ua_rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"description": "Block Bad Bot web spider",
"mode": "block",
"configuration": {
"target": "ua",
"value": "BadBot/1.0.2 (+http://bad.bot)"
}
}'User Agent Blocking ルールが Managed Challenge などのチャレンジアクションを使う場合、訪問者はチャレンジページを通過する必要があります。通過すると cf_clearance Cookie が設定されます。この Cookie の有効期間は、Challenge Passage の設定で決まります。