如何在 CSP header 中对部分源表达式(例如 URL)中的 CSP-policy 定界符(例如分号)进行编码?
How to encode CSP-policy delimiter characters (e.g. semicolon) within parts of source expressions (e.g. URL) in CSP header?
我目前正在 NGINX 中编写我的 CSP 策略,我需要提供一个 report-uri,其中包含 special 字符 ;
。请注意,;
对于 URI 路径有效。
default-src: 'self'; report-uri: /;index
但是,;
未被识别为有效字符,因此我在浏览器 (Chrome) 控制台中收到以下错误:
Unrecognized Content-Security-Policy directive 'index'.
有什么方法可以转义字符或将 URI 包装在字符串中(在 header 字符串中)?我已经尝试过 \;
和单引号换行(我使用双引号来换行 header 内容)。
您需要将分号百分号编码为 %3B
。 CSP 规范有 the following note:
Note: Characters like U+003B SEMICOLON (;
) and U+002C COMMA (,
) cannot appear in source expressions directly: if you’d like to include these characters in a source expression, they must be percent encoded as %3B
and %2C
respectively.
所以问题中显示的CSP政策应该这样写:
default-src: 'self'; report-uri: /%3Bindex
我目前正在 NGINX 中编写我的 CSP 策略,我需要提供一个 report-uri,其中包含 special 字符 ;
。请注意,;
对于 URI 路径有效。
default-src: 'self'; report-uri: /;index
但是,;
未被识别为有效字符,因此我在浏览器 (Chrome) 控制台中收到以下错误:
Unrecognized Content-Security-Policy directive 'index'.
有什么方法可以转义字符或将 URI 包装在字符串中(在 header 字符串中)?我已经尝试过 \;
和单引号换行(我使用双引号来换行 header 内容)。
您需要将分号百分号编码为 %3B
。 CSP 规范有 the following note:
Note: Characters like U+003B SEMICOLON (
;
) and U+002C COMMA (,
) cannot appear in source expressions directly: if you’d like to include these characters in a source expression, they must be percent encoded as%3B
and%2C
respectively.
所以问题中显示的CSP政策应该这样写:
default-src: 'self'; report-uri: /%3Bindex