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-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

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
base-uri

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報