Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

Client-Side Security API

最終更新 Markdown で表示Agent セットアップ

Client-Side Security API(旧称 Page Shield API)を使い、Client-Side Security のリソース監視の有効化と無効化、設定の変更、検出済みスクリプトと接続の情報取得ができます。

API リクエストの認証には API トークン が必要です。必要な API トークン権限については、ロールと権限 を参照してください。

エンドポイント

完全なエンドポイントは、Cloudflare API のベース URL に Client-Side Security API のエンドポイントを付けて組み立てます。

https://api.cloudflare.com/client/v4

{zone_id} 引数はゾーン ID(16 進文字列)です。この値は Cloudflare ダッシュボード、または Cloudflare API の /zones エンドポイント で確認できます。

{script_id} 引数はスクリプト ID(16 進文字列)です。この値は、検出された各スクリプトについて List client-side security scripts 操作のレスポンスに含まれます。

{connection_id} 引数は接続 ID(16 進文字列)です。この値は、検出された各接続について List client-side security connections API 操作のレスポンスに含まれます。

利用できる操作は次のとおりです。

操作 メソッド + URL スタブ 説明
Get client-side security settings GET zones/{zone_id}/page_shield Client-Side Security の設定(ステータスを含む)を取得します。
Update client-side security settings PUT zones/{zone_id}/page_shield Client-Side Security の設定を更新します。
List client-side security scripts GET zones/{zone_id}/page_shield/scripts 検出済みスクリプトの一覧を取得します。
Get a client-side security script GET zones/{zone_id}/page_shield/scripts/{script_id} スクリプトの詳細を取得します。
List client-side security connections GET zones/{zone_id}/page_shield/connections 検出済み接続の一覧を取得します。
Get a client-side security connection GET zones/{zone_id}/page_shield/connections/{connection_id} 接続の詳細を取得します。
List client-side security cookies GET zones/{zone_id}/page_shield/cookies 検出済み Cookie の一覧を取得します。
Get a client-side security cookie GET zones/{zone_id}/page_shield/cookies/{cookie_id} Cookie の詳細を取得します。
List content security rules GET zones/{zone_id}/page_shield/policies 設定済みのコンテンツセキュリティルールをすべて取得します。
Get a content security rule GET zones/{zone_id}/page_shield/policies/{policy_id} コンテンツセキュリティルールの詳細を取得します。
Create a content security rule POST zones/{zone_id}/page_shield/policies 指定した設定でコンテンツセキュリティルールを作成します。
Update a content security rule PUT zones/{zone_id}/page_shield/policies/{policy_id} 既存のコンテンツセキュリティルールを更新します。
Delete a content security rule DELETE zones/{zone_id}/page_shield/policies/{policy_id} 既存のコンテンツセキュリティルールを削除します。

API に関する注意

悪意のあるスクリプトの分類(Malicious または Not malicious)は、API では直接取得できません。この分類を判断するには、スクリプトの js_integrity_score 値を分類しきい値と比較します。しきい値は現在 10 です。しきい値より低いスコアのスクリプトは、悪意があると見なされます。

よく使う API 呼び出し

Client-Side Security の設定を取得する

この例は、Cloudflare の Client-Side Security の現在の設定(ステータス(有効 / 無効)を含む)を取得します。

Required API token permissions

At least one of the following token permissions is required:
  • Page Shield
  • Domain Page Shield Read
  • Domain Page Shield
  • Page Shield Read
  • Zone Settings Write
  • Zone Settings Read
Get client-side security settingsbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/page_shield" \
	--request GET \
	--header "X-Auth-Email: $CLOUDFLARE_EMAIL" \
	--header "X-Auth-Key: $CLOUDFLARE_API_KEY"
{
	"result": {
		"enabled": true,
		"updated_at": "2023-05-14T11:47:55.677555Z",
		"use_cloudflare_reporting_endpoint": true,
		"use_connection_url_path": false
	},
	"success": true,
	"errors": [],
	"messages": []
}

Client-Side Security を有効にする

この例は、指定したゾーンで Cloudflare の Client-Side Security を有効にします。

Required API token permissions

At least one of the following token permissions is required:
  • Page Shield
  • Domain Page Shield
  • Zone Settings Write
Update client-side security settingsbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/page_shield" \
	--request PUT \
	--header "X-Auth-Email: $CLOUDFLARE_EMAIL" \
	--header "X-Auth-Key: $CLOUDFLARE_API_KEY" \
	--json '{
		"enabled": true
	}'
{
	"result": {
		"enabled": true,
		"updated_at": "2023-05-14T11:50:41.756996Z"
	},
	"success": true,
	"errors": [],
	"messages": []
}

検出済みスクリプトの一覧を取得する

この GET リクエストは、ホスト名 example.net で Cloudflare の Client-Side Security が検出したスクリプトの一覧を取得します。1 ページあたり 15 件で最初のページを要求します。URL クエリ文字列には、絞り込みとページングのパラメーターが含まれます。

URL クエリ文字列に status フィルターパラメーターを指定しない場合、レスポンスにはデフォルトで active ステータスのスクリプトだけが含まれます。

Required API token permissions

At least one of the following token permissions is required:
  • Page Shield
  • Domain Page Shield Read
  • Domain Page Shield
  • Page Shield Read
  • Zone Settings Write
  • Zone Settings Read
List detected scriptsbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/page_shield/scripts?hosts=example.net&page=1&per_page=15" \
	--request GET \
	--header "X-Auth-Email: $CLOUDFLARE_EMAIL" \
	--header "X-Auth-Key: $CLOUDFLARE_API_KEY"
{
	"result": [
		{
			"id": "8337233faec2357ff84465a919534e4d",
			"url": "https://malicious.example.com/badscript.js",
			"added_at": "2023-05-18T10:51:10.09615Z",
			"first_seen_at": "2023-05-18T10:51:08Z",
			"last_seen_at": "2023-05-22T09:57:54Z",
			"host": "example.net",
			"domain_reported_malicious": false,
			"url_reported_malicious": true,
			"malicious_url_categories": ["Malware"],
			"first_page_url": "http://malicious.example.com/page_one.html",
			"status": "active",
			"url_contains_cdn_cgi_path": false,
			"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
			"js_integrity_score": 10,
			"obfuscation_score": 10,
			"dataflow_score": 8,
			"malware_score": 8,
			"cryptomining_score": 9,
			"magecart_score": 8,
			"fetched_at": "2023-05-21T16:58:07Z"
		}
		// (...)
	],
	"success": true,
	"errors": [],
	"messages": [],
	"result_info": {
		"page": 1,
		"per_page": 15,
		"count": 15,
		"total_count": 24,
		"total_pages": 2
	}
}

例のレスポンスに表示されているフィールドの一部は、Cloudflare プランによっては利用できないことがあります。

利用できる絞り込み、ページング、並べ替えのパラメーターについては、API リファレンス を参照してください。

報告頻度の低いスクリプトの一覧を取得する

この GET リクエストは、ホスト名 example.net で報告頻度の低いスクリプトの一覧を取得します。1 ページあたり 15 件で最初のページを要求します。URL クエリ文字列には、絞り込みとページングのパラメーターが含まれます。

Required API token permissions

At least one of the following token permissions is required:
  • Page Shield
  • Domain Page Shield Read
  • Domain Page Shield
  • Page Shield Read
  • Zone Settings Write
  • Zone Settings Read
List detected scriptsbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/page_shield/scripts?hosts=example.net&page=1&per_page=15&status=infrequent" \
	--request GET \
	--header "X-Auth-Email: $CLOUDFLARE_EMAIL" \
	--header "X-Auth-Key: $CLOUDFLARE_API_KEY"
{
	"result": [
		{
			"id": "83c8da2267394ce8465b74c299658fea",
			"url": "https://scripts.example.com/anotherbadscript.js",
			"added_at": "2023-05-17T13:16:03.419619Z",
			"first_seen_at": "2023-05-17T13:15:23Z",
			"last_seen_at": "2023-05-18T09:05:20Z",
			"host": "example.net",
			"domain_reported_malicious": false,
			"url_reported_malicious": false,
			"first_page_url": "http://malicious.example.com/page_one.html",
			"status": "infrequent",
			"url_contains_cdn_cgi_path": false,
			"hash": "9245aad577e846dd9b990b1b32425a3fae4aad8b8a28441a8b80084b6bb75a45",
			"js_integrity_score": 48,
			"obfuscation_score": 49,
			"dataflow_score": 45,
			"malware_score": 45,
			"cryptomining_score": 37,
			"magecart_score": 49,
			"fetched_at": "2023-05-18T03:58:07Z"
		}
		// (...)
	],
	"success": true,
	"errors": [],
	"messages": [],
	"result_info": {
		"page": 1,
		"per_page": 15,
		"count": 15,
		"total_count": 17,
		"total_pages": 2
	}
}

例のレスポンスに表示されているフィールドの一部は、Cloudflare プランによっては利用できないことがあります。

利用できる絞り込み、ページング、並べ替えのパラメーターについては、API リファレンス を参照してください。

検出済みスクリプトの詳細を取得する

この GET リクエストは、スクリプト ID 8337233faec2357ff84465a919534e4d の、Cloudflare の Client-Side Security が検出したスクリプトの詳細を取得します。

Required API token permissions

At least one of the following token permissions is required:
  • Page Shield
  • Domain Page Shield Read
  • Domain Page Shield
  • Page Shield Read
  • Zone Settings Write
  • Zone Settings Read
Get a detected scriptbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/page_shield/scripts/8337233faec2357ff84465a919534e4d" \
	--request GET \
	--header "X-Auth-Email: $CLOUDFLARE_EMAIL" \
	--header "X-Auth-Key: $CLOUDFLARE_API_KEY"
{
	"result": {
		"id": "8337233faec2357ff84465a919534e4d",
		"url": "https://malicious.example.com/badscript.js",
		"added_at": "2023-05-18T10:51:10.09615Z",
		"first_seen_at": "2023-05-18T10:51:08Z",
		"last_seen_at": "2023-05-22T09:57:54Z",
		"host": "example.net",
		"domain_reported_malicious": false,
		"url_reported_malicious": true,
		"malicious_url_categories": ["Malware"],
		"first_page_url": "http://malicious.example.com/page_one.html",
		"status": "active",
		"url_contains_cdn_cgi_path": false,
		"hash": "9245aad577e846dd9b990b1b32425a3fae4aad8b8a28441a8b80084b6bb75a45",
		"js_integrity_score": 48,
		"obfuscation_score": 49,
		"dataflow_score": 45,
		"malware_score": 42,
		"cryptomining_score": 32,
		"magecart_score": 44,
		"fetched_at": "2023-05-21T16:58:07Z",
		"page_urls": [
			"http://malicious.example.com/page_two.html",
			"http://malicious.example.com/page_three.html",
			"http://malicious.example.com/page_four.html"
		],
		"versions": [
			{
				"hash": "9245aad577e846dd9b990b1b32425a3fae4aad8b8a28441a8b80084b6bb75a45",
				"js_integrity_score": 48,
				"obfuscation_score": 49,
				"dataflow_score": 45,
				"malware_score": 42,
				"cryptomining_score": 32,
				"magecart_score": 44,
				"fetched_at": "2023-05-21T16:58:07Z"
			}
		]
	},
	"success": true,
	"errors": [],
	"messages": []
}

例のレスポンスに表示されているフィールドの一部は、Cloudflare プランによっては利用できないことがあります。

検出済み接続の一覧を取得する

この GET リクエストは、Cloudflare の Client-Side Security が検出した接続の一覧を取得します。1 ページあたり 15 件で最初のページを要求します。

URL クエリ文字列に status フィルターパラメーターを指定しない場合、レスポンスにはデフォルトで active ステータスの接続だけが含まれます。

Required API token permissions

At least one of the following token permissions is required:
  • Page Shield
  • Domain Page Shield Read
  • Domain Page Shield
  • Page Shield Read
  • Zone Settings Write
  • Zone Settings Read
List detected connectionsbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/page_shield/connections?page=1&per_page=15" \
	--request GET \
	--header "X-Auth-Email: $CLOUDFLARE_EMAIL" \
	--header "X-Auth-Key: $CLOUDFLARE_API_KEY"
{
	"result": [
		{
			"id": "0a7bb628776f4e50a50d8594c4a01740",
			"url": "https://malicious.example.com",
			"added_at": "2022-09-18T10:51:10.09615Z",
			"first_seen_at": "2022-09-18T10:51:08Z",
			"last_seen_at": "2022-09-02T09:57:54Z",
			"host": "example.net",
			"domain_reported_malicious": true,
			"malicious_domain_categories": ["Malware", "Spyware"],
			"url_reported_malicious": false,
			"malicious_url_categories": [],
			"first_page_url": "https://example.net/one.html",
			"status": "active",
			"url_contains_cdn_cgi_path": false
		}
		// (...)
	],
	"success": true,
	"errors": [],
	"messages": [],
	"result_info": {
		"page": 1,
		"per_page": 15,
		"count": 15,
		"total_count": 16,
		"total_pages": 2
	}
}

利用できる絞り込み、ページング、並べ替えのパラメーターについては、API リファレンス を参照してください。

検出済み接続の詳細を取得する

この GET リクエストは、接続 ID 0a7bb628776f4e50a50d8594c4a01740 の、Cloudflare の Client-Side Security が検出した接続の詳細を取得します。

Required API token permissions

At least one of the following token permissions is required:
  • Page Shield
  • Domain Page Shield Read
  • Domain Page Shield
  • Page Shield Read
  • Zone Settings Write
  • Zone Settings Read
Get a detected connectionbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/page_shield/connections/0a7bb628776f4e50a50d8594c4a01740" \
	--request GET \
	--header "X-Auth-Email: $CLOUDFLARE_EMAIL" \
	--header "X-Auth-Key: $CLOUDFLARE_API_KEY"
{
	"result": {
		"id": "0a7bb628776f4e50a50d8594c4a01740",
		"url": "https://malicious.example.com",
		"added_at": "2022-09-18T10:51:10.09615Z",
		"first_seen_at": "2022-09-18T10:51:08Z",
		"last_seen_at": "2022-09-02T09:57:54Z",
		"host": "example.net",
		"domain_reported_malicious": true,
		"malicious_domain_categories": ["Malware", "Spyware"],
		"url_reported_malicious": false,
		"malicious_url_categories": [],
		"first_page_url": "https://example.net/one.html",
		"status": "active",
		"url_contains_cdn_cgi_path": false
	},
	"success": true,
	"errors": [],
	"messages": []
}

この GET リクエストは、Cloudflare の Client-Side Security が検出した Cookie の一覧を取得します。1 ページあたり 15 件で最初のページを要求します。

URL クエリ文字列に status フィルターパラメーターを指定しない場合、レスポンスにはデフォルトで active ステータスの Cookie だけが含まれます。

Required API token permissions

At least one of the following token permissions is required:
  • Page Shield
  • Domain Page Shield Read
  • Domain Page Shield
  • Page Shield Read
  • Zone Settings Write
  • Zone Settings Read
List detected cookiesbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/page_shield/cookies?page=1&per_page=15" \
	--request GET \
	--header "X-Auth-Email: $CLOUDFLARE_EMAIL" \
	--header "X-Auth-Key: $CLOUDFLARE_API_KEY"
{
	"result": [
		{
			"id": "beee03ada7e047e79f076785d8cd8b8e",
			"type": "first_party",
			"name": "PHPSESSID",
			"host": "example.net",
			"domain_attribute": "example.net",
			"expires_attribute": "2024-10-21T12:28:20Z",
			"http_only_attribute": true,
			"max_age_attribute": null,
			"path_attribute": "/store",
			"same_site_attribute": "strict",
			"secure_attribute": true,
			"first_seen_at": "2024-05-06T10:51:08Z",
			"last_seen_at": "2024-05-07T11:56:01Z",
			"first_page_url": "example.net/store/products",
			"page_urls": ["example.net/store/products/1"]
		}
		// (...)
	],
	"success": true,
	"errors": [],
	"messages": [],
	"result_info": {
		"page": 1,
		"per_page": 15,
		"count": 15,
		"total_count": 16,
		"total_pages": 2
	}
}

利用できる絞り込み、ページング、並べ替えのパラメーターについては、API を呼び出す を参照してください。

この GET リクエストは、ID beee03ada7e047e79f076785d8cd8b8e の、Cloudflare の Client-Side Security が検出した Cookie の詳細を取得します。

Required API token permissions

At least one of the following token permissions is required:
  • Page Shield
  • Domain Page Shield Read
  • Domain Page Shield
  • Page Shield Read
  • Zone Settings Write
  • Zone Settings Read
Get a detected cookiebash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/page_shield/cookies/beee03ada7e047e79f076785d8cd8b8e" \
	--request GET \
	--header "X-Auth-Email: $CLOUDFLARE_EMAIL" \
	--header "X-Auth-Key: $CLOUDFLARE_API_KEY"
{
	"result": {
		"id": "beee03ada7e047e79f076785d8cd8b8e",
		"type": "first_party",
		"name": "PHPSESSID",
		"host": "example.net",
		"domain_attribute": "example.net",
		"expires_attribute": "2024-10-21T12:28:20Z",
		"http_only_attribute": true,
		"max_age_attribute": null,
		"path_attribute": "/store",
		"same_site_attribute": "strict",
		"secure_attribute": true,
		"first_seen_at": "2024-05-06T10:51:08Z",
		"last_seen_at": "2024-05-07T11:56:01Z",
		"first_page_url": "example.net/store/products",
		"page_urls": ["example.net/store/products/1"]
	},
	"success": true,
	"errors": [],
	"messages": []
}

コンテンツセキュリティルールを作成する

この POST リクエストは、Log アクションのコンテンツセキュリティルール(以前はポリシーと呼ばれていました)を作成し、ホスト場所に基づいて次のスクリプトを許可します。

  • myapp.example.com でホストされているスクリプト(example.com のスクリプトは含みません)。
  • cdnjs.cloudflare.com でホストされているスクリプト。
  • 完全な URL で指定した Google Analytics スクリプト。
  • 同じオリジン(同じ HTTP または HTTPS スキームとホスト名)のすべてのスクリプト。

それ以外のスクリプトはルール違反を引き起こしますが、ブロックはされません。

Content Security Policy(CSP) のディレクティブと値については、MDN のドキュメント を参照してください。

Required API token permissions

At least one of the following token permissions is required:
  • Page Shield
  • Domain Page Shield
  • Zone Settings Write
Create a content security rulebash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/page_shield/policies" \
	--request POST \
	--header "X-Auth-Email: $CLOUDFLARE_EMAIL" \
	--header "X-Auth-Key: $CLOUDFLARE_API_KEY" \
	--json '{
		"description": "My first content security rule in log mode",
		"action": "log",
		"expression": "http.host eq \"myapp.example.com\"",
		"enabled": "true",
		"value": "script-src myapp.example.com cdnjs.cloudflare.com https://www.google-analytics.com/analytics.js '\''self'\''"
	}'
{
	"success": true,
	"errors": [],
	"messages": [],
	"result": {
		"id": "<RULE_ID>",
		"description": "My first content security rule in log mode",
		"action": "log",
		"expression": "http.host eq \"myapp.example.com\"",
		"enabled": "true",
		"value": "script-src myapp.example.com cdnjs.cloudflare.com https://www.google-analytics.com/analytics.js 'self'"
	}
}

Log ではなく Allow アクションのコンテンツセキュリティルールを作成する場合は、リクエスト本文で "action": "allow" を使います。その場合、ルールで許可されていないスクリプトはすべてブロックされます。

役に立ちましたか?