コンテンツにスキップ

ページシールドAPI

ページシールドを有効または無効にし、その設定を構成し、検出されたスクリプトや接続に関する情報を取得するには、ページシールドAPIを使用します。

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

エンドポイント

完全なエンドポイントは、ページシールドAPIのエンドポイントをCloudflare APIのベースURLに追加することで取得できます:

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

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

{script_id}引数はスクリプトID(16進数の文字列)です。この値は、検出された各スクリプトのページシールドスクリプトのリスト操作のレスポンスに含まれています。

{connection_id}引数は接続ID(16進数の文字列)です。この値は、検出された各接続のページシールド接続API操作のレスポンスに含まれています。

以下の表は、利用可能な操作をまとめたものです:

操作メソッド + URLスラブノート
ページシールド設定を取得GET zones/{zone_id}/page_shieldページシールドの設定を取得します(ステータスを含む)。
ページシールド設定を更新PUT zones/{zone_id}/page_shieldページシールドの設定を更新します。
ページシールドスクリプトのリストGET zones/{zone_id}/page_shield/scripts検出されたスクリプトのリストを取得します。
ページシールドスクリプトを取得GET zones/{zone_id}/page_shield/scripts/{script_id}スクリプトの詳細を取得します。
ページシールド接続のリストGET zones/{zone_id}/page_shield/connections検出された接続のリストを取得します。
ページシールド接続を取得GET zones/{zone_id}/page_shield/connections/{connection_id}接続の詳細を取得します。
ページシールドクッキーのリストGET zones/{zone_id}/page_shield/cookies検出されたクッキーのリストを取得します。
ページシールドクッキーを取得GET zones/{zone_id}/page_shield/cookies/{cookie_id}クッキーの詳細を取得します。
ページシールドポリシーのリストGET zones/{zone_id}/page_shield/policies設定されたすべてのCSPポリシーのリストを取得します。
ページシールドポリシーを取得GET zones/{zone_id}/page_shield/policies/{policy_id}CSPポリシーの詳細を取得します。
ページシールドポリシーを作成POST zones/{zone_id}/page_shield/policies提供された設定でCSPポリシーを作成します。
ページシールドポリシーを更新PUT zones/{zone_id}/page_shield/policies/{policy_id}既存のCSPポリシーを更新します。
ページシールドポリシーを削除DELETE zones/{zone_id}/page_shield/policies/{policy_id}既存のCSPポリシーを削除します。

APIノート

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

  • APIは、悪意のあるスクリプト/接続カテゴリに対して2つの別々のプロパティを提供します:malicious_domain_categoriesmalicious_url_categories、これはそれぞれdomain_reported_maliciousおよびurl_reported_maliciousプロパティに関連しています。Cloudflareダッシュボードは、すべてのカテゴリを単一の悪意のあるカテゴリフィールドに表示します。詳細については、悪意のあるスクリプトと接続カテゴリを参照してください。

一般的なAPI呼び出し

ページシールド設定を取得

この例では、ページシールドの現在の設定を取得し、ステータス(有効/無効)を含めます。

リクエスト
curl "https://api.cloudflare.com/client/v4/zones/{zone_id}/page_shield" \
--header "Authorization: Bearer <API_TOKEN>"
レスポンス
{
"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": []
}

ページシールドを有効にする

この例では、指定されたゾーンでページシールドを有効にします。

リクエスト
curl --request PUT \
"https://api.cloudflare.com/client/v4/zones/{zone_id}/page_shield" \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{ "enabled": true }'
レスポンス
{
"result": {
"enabled": true,
"updated_at": "2023-05-14T11:50:41.756996Z"
},
"success": true,
"errors": [],
"messages": []
}

検出されたスクリプトのリストを取得

このGETリクエストは、ホスト名example.netでページシールドによって検出されたスクリプトのリストを取得し、1ページあたり15アイテムの最初のページを要求します。URLクエリ文字列にはフィルタリングとページングパラメータが含まれています。

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

リクエスト
curl "https://api.cloudflare.com/api/v4/zones/{zone_id}/page_shield/scripts?hosts=example.net&page=1&per_page=15" \
--header "Authorization: Bearer <API_TOKEN>"
レスポンス
{
"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,
"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クエリ文字列にはフィルタリングとページングパラメータが含まれています。

リクエスト
curl "https://api.cloudflare.com/api/v4/zones/{zone_id}/page_shield/scripts?status=infrequent&hosts=example.net&page=1&per_page=15" \
--header "Authorization: Bearer <API_TOKEN>"
レスポンス
{
"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,
"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リクエストは、スクリプトID8337233faec2357ff84465a919534e4dのページシールドによって検出されたスクリプトの詳細を取得します。

リクエスト
curl "https://api.cloudflare.com/api/v4/zones/{zone_id}/page_shield/scripts/8337233faec2357ff84465a919534e4d" \
--header "Authorization: Bearer <API_TOKEN>"
レスポンス
{
"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,
"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": 50,
"fetched_at": "2023-05-21T16:58:07Z"
}
]
},
"success": true,
"errors": [],
"messages": []
}

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

検出された接続のリストを取得

このGETリクエストは、ページシールドによって検出された接続のリストを取得し、1ページあたり15アイテムの最初のページを要求します。

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

リクエスト
curl "https://api.cloudflare.com/api/v4/zones/{zone_id}/page_shield/connections?page=1&per_page=15" \
--header "Authorization: Bearer <API_TOKEN>"
レスポンス
{
"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リクエストは、接続ID0a7bb628776f4e50a50d8594c4a01740のページシールドによって検出された接続の詳細を取得します。

リクエスト
curl "https://api.cloudflare.com/api/v4/zones/{zone_id}/page_shield/connections/0a7bb628776f4e50a50d8594c4a01740" \
--header "Authorization: Bearer <API_TOKEN>"
レスポンス
{
"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リクエストは、ページシールドによって検出されたクッキーのリストを取得し、1ページあたり15アイテムの最初のページを要求します。

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

リクエスト
curl "https://api.cloudflare.com/api/v4/zones/{zone_id}/page_shield/cookies?page=1&per_page=15" \
--header "Authorization: Bearer <API_TOKEN>"
レスポンス
{
"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 を持つ Page Shield によって検出されたクッキーの詳細を取得します。

Request
curl "https://api.cloudflare.com/api/v4/zones/{zone_id}/page_shield/cookies/beee03ada7e047e79f076785d8cd8b8e" \
--header "Authorization: Bearer <API_TOKEN>"
Response
{
"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 アクションを持つ Page Shield ポリシーを作成し、ホスティングされている場所に基づいて次のスクリプトを許可します:

  • myapp.example.com にホスティングされているスクリプト(example.com にホスティングされているスクリプトは含まれません)。
  • cdnjs.cloudflare.com にホスティングされているスクリプト。
  • Google Analytics スクリプトの完全な URL を使用。
  • 同じオリジン内のすべてのスクリプト(同じ HTTP または HTTPS スキームとホスト名)。

他のすべてのスクリプトはポリシー違反を引き起こしますが、それらのスクリプトはブロックされません。

コンテンツセキュリティポリシー (CSP) の指示と値に関する詳細は、MDN ドキュメントを参照してください。

Request
curl "https://api.cloudflare.com/api/v4/zones/{zone_id}/page_shield/policies" \
--header "Authorization: Bearer <API_TOKEN>" \
--header "Content-Type: application/json" \
--data '{
"description": "My first policy 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'\''"
}'
Response
{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "<POLICY_ID>",
"description": "My first policy 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" を使用します。そのようなポリシーの場合、ポリシーによって許可されていないすべてのスクリプトはブロックされます。