舞台裏

Qiita が表でこっちが裏。こっそりやっていく。

20.1.7 Content Security Policy (CSP)

20.1.7 Content Security Policy (CSP)

Content Security Policy (CSP) is a mechanism that web applications can leverage to mitigate content injection vulnerabilities, such as cross-site scripting (XSS).

Content Security Policy は、 Web アプリケーションがコンテンツインジェクション脆弱性を緩和するのに利用できるようにするメカニズムです。 例えば、 XSS などがそうです。

CSP is a declarative policy that provides a facility for web application authors to declare and ultimately inform the client (user-agent) about the sources from which the web application expects to load resources.

CSPは、Webアプリケーション作成者が宣言し、最終的にWebアプリケーションがリソースを読み込むソースについてクライアント(ユーザーエージェント)に通知するための機能を提供する宣言型ポリシーです。

Content Security Policy is not intended to solve all content injection vulnerabilities.

CSP は全てのコンテンツインジェクション脆弱性を解決することを意図していません。

Instead, CSP can be leveraged to help reduce the harm caused by content injection attacks.

代わりに、 CSP はコンテンツインジェクション攻撃を原因とする問題を減らすのに活用できます。

As a first line of defense, web application authors should validate their input and encode their output.

最初の防衛ラインとして、 Web アプリケーションの制作者は彼らの入力を検証し、出力をエンコードすべきです。

A web application may employ the use of CSP by including one of the following HTTP headers in the response:

Webアプリケーションは、レスポンスに次のHTTPヘッダーのいずれかを含めることで、CSPを使用することができます。

  • Content-Security-Policy
  • Content-Security-Policy-Report-Only

Each of these headers are used as a mechanism to deliver a security policy to the client.

これらのヘッダーは、クライアントにセキュリティポリシーを提供するためのメカニズムとして使用されます。

A security policy contains a set of security policy directives (for example, script-src and object-src), each responsible for declaring the restrictions for a particular resource representation.

セキュリティポリシーは、特定のリソース表現についての制限を宣言したセキュリティポリシーディレクティブのセット(例えば script-srcobject-src)を含んでいます。

For example, a web application can declare that it expects to load scripts from specific, trusted sources, by including the following header in the response:

例えば、 Web アプリケーションは、次のレスポンスヘッダを含めることで、アプリケーションが特定の信頼された場所からスクリプトを読み込むことを期待していることを宣言できます。

Content-Security-Policy: script-src https://trustedscripts.example.com

An attempt to load a script from another source other than what is declared in the script-src directive will be blocked by the user-agent.

script-src ディレクティブで宣言された場所とは異なる場所からスクリプトをロードしようとすると、ユーザエージェント(※ブラウザとか)によってブロックされます。

Additionally, if the report-uri directive is declared in the security policy, then the violation will be reported by the user-agent to the declared URL.

さらに、もし report-uri ディレクティブがセキュリティポリシーに宣言されていた場合、違反行為はユーザエージェントによって宣言された URL に報告されます。

For example, if a web application violates the declared security policy, the following response header will instruct the user-agent to send violation reports to the URL specified in the policy’s report-uri directive.

例えば、もし Web アプリケーションが宣言されたセキュリティポリシーに違反した場合、以下のレスポンスヘッダーはユーザーエージェントに違反のレポートをポリシーの report-uri ディレクティブで指定された URL に対して送信するよう指示します。

Content-Security-Policy: script-src https://trustedscripts.example.com; report-uri /csp-report-endpoint/

Violation reports are standard JSON structures that can be captured either by the web application’s own API or by a publicly hosted CSP violation reporting service, such as, REPORT-URI.

違反のレポートは Web アプリケーション自身の API か、 CSP 違反レポートサービス(例えば REPORT-URI)によってホストされたポリシーのいずれかでキャプチャー可能な標準的な JSON の構造をしています。

The Content-Security-Policy-Report-Only header provides the capability for web application authors and administrators to monitor security policies, rather than enforce them.

Content-Security-Policy-Report-Only ヘッダーは Web アプリケーションの作者と管理者に代わってセキュリティポリシーを監視する能力を提供します。

This header is typically used when experimenting and/or developing security policies for a site.

このヘッダーは通常、サイトのセキュリティポリシーを検証したり開発しているときに使用します。

When a policy is deemed effective, it can be enforced by using the Content-Security-Policy header field instead.

ポリシーが有効とみなされると、代わりに Content-Security-Policy ヘッダーフィールドが使用されるようになります。

Given the following response header, the policy declares that scripts may be loaded from one of two possible sources.

以下のレスポンスヘッダーを与えて、ポリシーがスクリプトは2つの有効なソースのうち1つからロードされるということを宣言しています。

Content-Security-Policy-Report-Only: script-src 'self' https://trustedscripts.example.com; report-uri /csp-report-endpoint/

If the site violates this policy, by attempting to load a script from evil.com, the user-agent will send a violation report to the declared URL specified by the report-uri directive, but still allow the violating resource to load nevertheless.

evil.com からロードしようとしたためにサイトがこのポリシーに違反した場合、ユーザーエージェントは違反レポートを report-uri ディレクティブで宣言された URL に送信します。 しかし、それでもまだ違反したリソースをロードすることは許可されています。

Configuring Content Security Policy

It’s important to note that Spring Security does not add Content Security Policy by default.

Spring Security は Content Security Policy をデフォルトでは追加しないという点に注意してください。

The web application author must declare the security policy(s) to enforce and/or monitor for the protected resources.

Web アプリケーションの作者は、リソースの保護について実行するのかもしくは監視するのかについてセキュリティポリシーを宣言しなければなりません。

For example, given the following security policy:

例えば、次のセキュリティポリシーを与えるとした場合、

script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/

You can enable the CSP header using XML configuration with the <content-security-policy> element as shown below:

あなたは xml 設定を使った場合は <content-security-policy> 要素を以下に見るように使うことで CSP ヘッダーを有効にできます。

<http>
    <!-- ... -->

    <headers>
        <content-security-policy
            policy-directives="script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/" />
    </headers>
</http>

To enable the CSP ‘report-only’ header, configure the element as follows:

CSP の report-only ヘッダーを有効にするには、要素を次のように設定します。

<http>
    <!-- ... -->

    <headers>
        <content-security-policy
            policy-directives="script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/"
            report-only="true" />
    </headers>
</http>

Similarly, you can enable the CSP header using Java configuration as shown below:

同様に、あなたは Java Configuration を次のように使うことで CSP ヘッダーを有効にできます。

@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    // ...
    .headers()
        .contentSecurityPolicy("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/");
}
}

To enable the CSP ‘report-only’ header, provide the following Java configuration:

CSP の report-only ヘッダーを有効にするには、 Java Configuration では次のように提供します。

@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    // ...
    .headers()
        .contentSecurityPolicy("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/")
        .reportOnly();
}
}

Additional Resources

Applying Content Security Policy to a web application is often a non-trivial undertaking.

Web アプリケーションに Content Security Policy を適用することは、しばしば重要ではないことがあります。

The following resources may provide further assistance in developing effective security policies for your site.

以下のリソースは効果的なセキュリティポリシーをサイトに組み込むときの助けを提供します。

An Introduction to Content Security Policy

CSP Guide - Mozilla Developer Network

W3C Candidate Recommendation