CSP: base-uri

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since August 2016.

We’d love to hear your thoughts on the next set of proposals for the JavaScript language. You can find a description of the proposals here.
Please take two minutes to fill out our short survey.

HTTP の Content-Security-Policybase-uri ディレクティブは、文書の <base> 要素で使用することができる URL を制限します。この値が存在しない場合は、任意の URI が許可されます。このディレクティブが存在しない場合、ユーザーエージェントは <base> 要素の値を使用します。

CSP バージョン 2
ディレクティブ種別 Document directive
default-src による代替 設定しないと、任意の URL が許可されます。

構文

1 つまたは複数のソースを base-uri ポリシーに使用することができます。

http
Content-Security-Policy: base-uri <source>;
Content-Security-Policy: base-uri <source> <source>;

ソース

このディレクティブは、他の CSP ディレクティブと同じように、引数のソース値のほとんどを使用します。 CSP のソース値

なお、 base-uri ではいくつかの値、たとえば 'unsafe-inline''strict-dynamic' などのキーワードは意味がありません。

Meta タグの設定

html
<meta http-equiv="Content-Security-Policy" content="base-uri 'self'" />

Apache の設定

<IfModule mod_headers.c>
Header set Content-Security-Policy "base-uri 'self'";
</IfModule>

Nginx の設定

add_header Content-Security-Policy "base-uri 'self';"

違反になる場合

ドメインが example.com ではないので、 <base> 要素の hrefhttps://example.com に設定すると、 CSP 違反となります。

html
<meta http-equiv="Content-Security-Policy" content="base-uri 'self'" />
<base href="https://example.com/" />

<!--
// Error: Refused to set the document's base URI to 'https://example.com/'
// because it violates the following Content Security Policy
// directive: "base-uri 'self'"
-->

仕様書

Specification
Content Security Policy Level 3
# directive-base-uri

ブラウザーの互換性

関連情報