CSP:base-uri
HTTP Content-Security-Policy 的 base-uri 指令限制了文件中 <base> 元素中可以使用的 URL。如果此值不存在,則允許使用任何 URI。如果此指令不存在,使用者代理將使用 <base> 元素中的值。
| CSP 版本 | 2 |
|---|---|
| 指令型別 | 文件指令 |
default-src 回退 |
沒有。不設定此項允許任何 URL。 |
語法
可以為 base-uri 策略允許一個或多個源
http
Content-Security-Policy: base-uri <source>;
Content-Security-Policy: base-uri <source> <source>;
來源
此指令使用與其他 CSP 指令相同的 CSP 源值 語法作為引數。但是,只有與 URL 匹配的值對 base-uri 有意義,包括 <host-source>、<scheme-source>、'self' 和 'none'。
示例
元標籤配置
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,因此具有 href 設定為 https://example.com 的 <base> 元素會導致 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'"
-->
規範
| 規範 |
|---|
| 內容安全策略第 3 級 # directive-base-uri |
瀏覽器相容性
BCD 表格僅在瀏覽器中載入