如何使用特定模式 CSP 标头 URL

gkr*_*hna 5 java security header content-security-policy

http://aa.bb.dd.com http://aa.bb1.dd.com这样的 URL 很少

我添加了 CSP 白名单 url,其模式如下所示,

http://*.bb*.*.com
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误

The source list for Content Security Policy directive 'script-src' contains an invalid source: 'https://*.bb*.*.com'. It will be ignored
Run Code Online (Sandbox Code Playgroud)

如何添加模式以便允许 bb*(aa.bb1.dd.com、aa.bb2.dd.com 等)?

Bar*_*ard 7

你不能。

该规范将主机列出如下:

; Hosts: "example.com" / "*.example.com" / "https://*.example.com:12/path/to/file.js"
host-source = [ scheme-part "://" ] host-part [ port-part ] [ path-part ]
scheme-part = scheme
              ; scheme is defined in section 3.1 of RFC 3986.
host-part   = "*" / [ "*." ] 1*host-char *( "." 1*host-char )
host-char   = ALPHA / DIGIT / "-"
port-part   = ":" ( 1*DIGIT / "*" )
path-part   = path-abempty
              ; path-abempty is defined in section 3.3 of RFC 3986.
Run Code Online (Sandbox Code Playgroud)

也就是说,主机可以是 *,也可以是 * 开头。它们的中间或末尾不能有 *。

所以你可以有*.dd.com(但不是*.*.dd.com)。

老实说,根据需要使用通配符会打开安全问题并击败使用 CSP 的意义,因为我可以通过使用包含 bb 的子域(例如http://www.bb.baddomain.com)从任何域加载资源.