首页 > 解决方案 > Apache CSP 设置

问题描述

我有一个像反向代理一样的 apache 2.4。在将用户代理到目标服务器之前,我使用简单的表单登录来验证用户身份。

登录页面非常简单:

<!doctype html>
<html lang="it">
<head><title>AUTENTICAZIONE</title>

</head>
<body>

<script type="text/javascript">
</script>

<form method="POST" action="/dologin2.html">
  Username: <input type="text" name="httpd_username" value="" />
  Password: <input type="password" name="httpd_password" value="" />
  <input type="submit" name="login" value="Login" />
  <input type="hidden" name="httpd_location" value="https://sgsvrsiimws11lx.sistemi.group/primoacc/sigma/app" />
</form>
</body>
</html>

我在这个页面上遇到了 Firefox 和 chrome 的问题:

拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“default-src 'self'”。启用内联执行需要“unsafe-inline”关键字、哈希(“sha256-AbpHGcgLb+kRsJGnwFEktk7uzpZOCcBY74+YBdrKVGs=”)或随机数(“nonce-...”)。另请注意,'script-src' 未明确设置,因此 'default-src' 用作后备。

在我的 httpd.conf 我已经设置了这个:

标头集 Content-Security-Policy "frame-ancestors 'unsafe-inline' 'self' sgsvrsiimws11lx.sistemi.group;"

我已经设置了它,因为后端的一些角度对象没有加载。

我尝试以多种方式设置 Content-Security-Policy,但登录页面中的 javascript 始终出现错误。

即使我有那个错误我也可以用chrome登录,用firefox没有。

如何在 apache 中正确设置 Content-Security-Policy 标头以与我的登录页面正常工作?

谢谢

标签: apachegoogle-chromefirefoxcontent-security-policy

解决方案


Header set Content-Security-Policy "frame-ancestors 'unsafe-inline' 'self' sgsvrsiimws11lx.sistemi.group;"不限制内联脚本的执行。
您可以删除'unsafe-inline'令牌,因为frame-ancestors指令不支持它。

看起来您的“目标服务器”发送了一个登录页面,其中包含有自己的内容安全策略default-src 'self'规则。

在开发工具中检查您在浏览器中真正拥有的 CSP 标头,这里是教程。
如果您的“目标服务器”发布了自己的 CSP,则必须添加'unsafe-inline'到其中,而不是添加到反向代理配置中。'unsafe-inline'应将令牌插入script-src指令中(或者如果default-srcscript-src使用)。

注意:我希望您知道这'unsafe-inline'会降低 CSP 保护的能力。您可以使用'nonce-value''sha256-AbpHGcgLb+kRsJGnwFEktk7uzpZOCcBY74+YBdrKVGs='(取自 Chrome 的警告)来安全地允许内联脚本。


推荐阅读