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.
HTTP の Content-Security-Policy
の base-uri
ディレクティブは、文書の <base>
要素で使用することができる URL を制限します。この値が存在しない場合は、任意の URI が許可されます。このディレクティブが存在しない場合、ユーザーエージェントは <base>
要素の値を使用します。
CSP バージョン | 2 |
---|---|
ディレクティブ種別 | 文書ディレクティブ |
default-src による代替 |
設定しないと、任意の URL が許可されます。 |
構文
http
Content-Security-Policy: base-uri 'none';
Content-Security-Policy: base-uri <source-expression-list>;
このディレクティブは、次のいずれかの値を指定することができます。
'none'
-
<base>
要素を使用してベース URI を設定することはできません。 単一引用符は必須です。 <source-expression-list>
-
ソース表現の値を空白で区切ったリストです。
<base>
要素は、その値が指定されたソース表現のいずれかと一致した場合、ベース URI を設定します。このディレクティブでは、以下のソース表現の値が適用できます。
例
Meta タグの設定
html
<meta http-equiv="Content-Security-Policy" content="base-uri 'self'" />
Apache の設定
apacheconf
<IfModule mod_headers.c>
Header set Content-Security-Policy "base-uri 'self'";
</IfModule>
Nginx の設定
nginx
add_header Content-Security-Policy "base-uri 'self';"
違反になる場合
ドメインが example.com
ではないので、 <base>
要素の href
を https://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 |