gpt4 book ai didi

content-security-policy - 如何编写带通配符的 CSP?

转载 作者:行者123 更新时间:2023-12-05 03:42:40 24 4
gpt4 key购买 nike

我正在为我的网站编写 CSP, header 是通过 AWS 上的 Lambda@Edge 为我在 lightsail 上的网站添加的。我已经将 CSP 设置如下,一直在努力让它工作:content-security-policy: default-src 'self' *.thetechcapsule.com thetechcapsule.com; img-src ' self ';脚本源“ self ”;样式源“ self ”;对象源“无”

我的域是 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.com 加载资源和 www.site.com , 你需要在 script-src 中做指令正是你在 default-src 中所做的:

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

如果混合使用,样式/图像可能需要相同的内容site.comwww.site.com用于资源加载。

注意:在 CSP3 兼容的浏览器中,'self' 标记还覆盖了 ws:wss:计划。和 http://example.com允许升级到 https://example.com .

关于content-security-policy - 如何编写带通配符的 CSP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67139124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com