如何编写带通配符的 CSP?

How to write a CSP with wildcard?

我正在为我的网站编写 CSP,header 是通过 AWS 上的 Lambda@Edge 为我在 lightsail 上的网站添加的。我已经将 CSP 设置如下,一直在努力让它工作:content-security-policy: default-src 'self' *.thetechcapsule.com thetechcapsule.com; img-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'

我的域是 thetechcapsule.com,我通过 www.thetechcapsule.com 设置了云端 CDN。我以为通配符会允许所有子域,但它不起作用,出现脚本错误,但它们应该使用默认源?

例如控制台错误

Refused to load the script '<URL>' because it violates the following Content Security Policy directive: "script-src 'self'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.

谢谢

浏览器替换为确切的元组,而不是 'self':加载页面的方案+主机名+端口。

如果您从 Url http://example.com 加载页面,'self' 将是 http://example.com 并且不允许 http://www.example.com.

如果页面加载为 https://www.example.com'self' 变为 https://www.example.com 并且它将不允许 https://example.comhttp://www.example.com(最后一种情况的方案不同).

因此,如果您的页面同时从 site.comwww.site.com 加载资源,您需要在 script-src 指令中执行与在 default-src 中完全相同的操作:

script-src 'self' *.thetechcapsule.com thetechcapsule.com;

如果混合使用 site.comwww.site.com 进行资源加载,styles/images 可能需要相同的内容。

注意:在兼容 CSP3 的浏览器中,'self' 令牌还涵盖了 ws:wss: 方案。 http://example.com 允许升级到 https://example.com.