このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。
CSP: report-to
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Content-Security-Policy のreport-to ディレクティブは、 CSP 違反の報告に使用するエンドポイントの名前を示します。
CSP 違反が発生した場合、シリアライズされたCSPViolationReportBody オブジェクトのインスタンスが含まれているレポートが生成されます。このレポートは、レポート API で定義されている一般的なメカニズムを使用して、エンドポイント名に対応する URL に送信されます。
サーバーは、 HTTP のReporting-Endpoints レスポンスヘッダーに、エンドポイント名とそれに対応する URL の間の対応付けを別個に指定する必要があります。
In this article
構文
Content-Security-Policy: …; report-to <endpoint_name><endpoint_name> は、 HTTP のReporting-Endpoints レスポンスヘッダーで指定されたエンドポイントの名前です。また、Report-To非推奨; レスポンスヘッダーでサーバーが指定したグループの名前である場合もあります。
違反レポートの構文
CSP 違反レポートは、JSON 形式のReport オブジェクトのインスタンスであり、type プロパティに"csp-violation" という値が存在し、本体はCSPViolationReportBody オブジェクトをシリアライズした形式です(各オブジェクトのプロパティ定義については、それぞれのオブジェクトを参照してください)。レポートは、Content-Type がapplication/reports+json であるPOST 操作により、ターゲットエンドポイントに送信されます。
単一のレポートのJSONは、次のようになるでしょう。
{ "age": 53531, "body": { "blockedURL": "inline", "columnNumber": 39, "disposition": "enforce", "documentURL": "https://example.com/csp-report", "effectiveDirective": "script-src-elem", "lineNumber": 121, "originalPolicy": "default-src 'self'; report-to csp-endpoint-name", "referrer": "https://www.google.com/", "sample": "console.log(\"lo\")", "sourceFile": "https://example.com/csp-report", "statusCode": 200 }, "type": "csp-violation", "url": "https://example.com/csp-report", "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"}使用上のメモ
report-to ディレクティブはreport-uri を置き換えることを意図しており、report-to に対応しているブラウザーはreport-uri ディレクティブを無視します。しかし、report-to が広く対応されるまでは、どちらのディレクティブも表示させるように指定することができます。
Content-Security-Policy: …; report-uri https://endpoint.example.com; report-to endpoint_nameなお、このトピックの他の例ではreport-uri を示していません。
例
>CSP 違反レポートのエンドポイントの設定
サーバーは、 HTTP レスポンスのReporting-Endpoints ヘッダーを使用して、エンドポイント名と URL の割り当てられた対応を定義することができます。任意の名前を使用することができます。ここでは、name-of-endpoint という名前を選んでいます。
Reporting-Endpoints: name-of-endpoint="https://example.com/csp-reports"サーバーは、report-to ディレクティブを使用する際に、 CSP 違反レポートの送信対象としてこのエンドポイント名を設定することができます。
Content-Security-Policy: default-src 'self'; report-to name-of-endpoint仕様書
| Specification |
|---|
| Content Security Policy Level 3> # directive-report-to> |