CSP: base-uri

HTTP Content-Security-Policy(内容安全策略)中的 base-uri 指令限制了可以应用于一个文档的 <base> 元素的 URL。假如指令值为空,那么任何 URL 都是允许的。如果指令不存在,那么用户代理会使用 <base> 元素中的值。

CSP 版本 2
指令类型 文档指令
default-src 回落 无。没有设置则允许任何 URL。

语法

base-uri 安全策略可以设置一个或多个源:

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

此指令与其他 CSP 指令一样,使用大部分与参数相同的源值:CSP 源值 (en-US)

然而,请注意,一些值对 base-uri 没有意义,例如关键字 'unsafe-inline''strict-dynamic'

示例

Meta tag 配置

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

Apache 配置

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

Nginx 配置

bash
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="http://example.com/" />

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

规范

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

浏览器兼容性

BCD tables only load in the browser

参见