问题:当 Web 服务器响应标头时
Content-Security-Policy: sandbox; default-src 'none'; img-src 'self'; style-src 'self';
这似乎会导致 Chromium 添加标头
Sec-Fetch-Site: cross-site
当从与包含的 html 页面相同的来源(和目录)请求样式表时。相反,预计上述 CSP 会导致浏览器提交
Sec-Fetch-Site: same-origin
。
使用 nginx+Chromium 进行重现:
将以下 4 行添加到 nginx 配置文件中的
location
指令中:
add_header Content-Security-Policy "sandbox; default-src 'none'; img-src 'self'; style-src 'self';";
if ($http_sec_fetch_site = 'cross-site') {
return 403;
}
从该位置提供以下 2 个静态文件
report.html
和 report.css
。
report.html
:
<!DOCTYPE html>
<html>
<head>
<title>Report</title>
<link rel="stylesheet" href="report.css">
</head>
<body>
<h1>Report</h1>
</body>
</html>
和
report.css
:
body { font-family: sans-serif }
以下是使用开发者工具网络窗口发出的
report.html
和后续 report.css
请求的屏幕截图:
请注意,由于请求中的
report.css
标头不正确,因此对 403 Forbidden
的请求为 Sec-Fetch-Site: cross-site
。
问题:为什么 Chromium 为基于允许同源样式表的给定 CSP 应该是
Sec-Fetch-Site: cross-site
的文件提交 same-origin
?
注意:如果问题似乎没有重现,请验证当请求
report.html
时,服务器响应中是否可以看到以下标头:
Content-Security-Policy: sandbox; default-src 'none'; img-src 'self'; style-src 'self';
以便为后续请求设置 CSP
report.css
。
Chromium 版本 113.0.5672.126(官方版本)Arch Linux(64 位)
这似乎是服务器端的配置错误。标题:
Content-Security-Policy: sandbox; default-src 'none'; img-src 'self'; style-src 'self';
其实是自相矛盾的。指令
default-src 'none'; img-src 'self'; style-src 'self';
尝试允许 same-origin
图像和样式表。但是,没有 sandbox
的 allow-same-origin
会导致资源无法满足 same-origin
策略:
allow-same-origin
如果不使用此令牌,资源将被视为来自始终无法满足同源策略的特殊来源(可能会阻止访问数据存储/cookies和一些 JavaScript API)。
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/sandbox
因此,为了保留
sandbox
并且仅允许从同一来源提供图像和样式表,最安全的调整是将 allow-same-origin
添加到 sandbox
指令中:
Content-Security-Policy: sandbox allow-same-origin; default-src 'none'; img-src 'self'; style-src 'self';
对于在 Jenkins 中遇到此问题的任何人,预计将在 Jenkins 2.417 中发布修复。
最近已使用此信息更新了其他解决方法 https://www.jenkins.io/doc/book/security/configuring-content-security-policy/