首页 > 解决方案 > 内容安全策略指令:“default-src 'self'”。请注意,'frame-src' 没有明确设置,所以 'default-src' 用作后备

问题描述

注销后,我使用 WsFedration(ADFS) 在我们的应用程序中集成了单点登录,它在成功注销并返回登录页面时重定向到页面。在 Windows 服务器中托管后,此操作正常,但在托管后,到 Nginx 服务器我遇到问题,它没有重定向到登录页面,控制台错误说,

拒绝框架“https://xxx-yyy.zzz.rr/”,因为它违反了以下内容安全策略指令:“default-src 'self'”。请注意,'frame-src' 没有明确设置,所以 'default-src' 用作后备

然后我对此进行搜索并将内容安全策略(CSP)添加到 Nginx 配置文件中,如下所示。

add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options SAMEORIGIN always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Content-Security-Policy "style-src-elem 'unsafe-inline' 'self' https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css  https://fonts.googleapis.com/css";
add_header Content-Security-Policy "style-src 'unsafe-inline' 'self' https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css  https://fonts.googleapis.com/css";
add_header Content-Security-Policy "frame-src 'unsafe-inline' 'self' none";
add_header Content-Security-Policy "default-src 'unsafe-inline'  'self'; https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css  https://fonts.googleapis.com/css  ";
add_header Content-Security-Policy "frame-ancestors 'self' 'unsafe-inline' none";

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header Content-Security-Policy "font-src 'self' 'unsafe-inline' https://netdna.bootstrapcdn.com  https://fonts.gstatic.com";    

我尝试了几种方法,但我无法弄清楚,如果有人可以帮助我解决这个问题,非常感谢。提前致谢。

标签: content-security-policy

解决方案


  1. 您同时发布多个 CSP,它们的工作方式与您想象的不同。如果发布了多个 CSP,则将它们与逻辑“AND”组合。
    但是您在每个 CSP 中巧妙地使用了独特的指令,因此如果不是该指令,整个集合将按预期工作default-src。如果它在单独的 CSP 中发布,则default-src覆盖所有其他后备指令。结果,您'unsafe-inline' 'self'对所有指令都有规则。

您必须将所有指令放在一个add_header Content-Security-Policy.

  1. 您在规则中有一些错误,例如:
  • https://fonts.googleapis.com/csssource 应该有尾随/,因为它是文件夹名,而不是文件名。
  • nonetoken 应该是单引号 - 'none',如果它与其他来源结合,它将被忽略。
  • "frame-src 'unsafe-inline' 'self' none"-frame-src不支持'unsafe-inline'令牌。
  • "frame-ancestors 'self' 'unsafe-inline' none"-frame-ancestors不支持'unsafe-inline'令牌。
  • "font-src 'self' 'unsafe-inline' https://netdna.bootstrapcdn.com https://fonts.gstatic.com"-font-src不支持'unsafe-inline'令牌。
  • "default-src 'unsafe-inline' 'self'; https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css https://fonts.googleapis.com/css "-;后面的(分号)'self'确实完成了default-src规则集,因此https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css算作指令名称。

这里是你的规则:

add_header Content-Security-Policy " \
default-src 'self' 'unsafe-inline' https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css https://fonts.googleapis.com/css/; \
font-src 'self' https://netdna.bootstrapcdn.com https://fonts.gstatic.com; \
frame-ancestors 'self'; \
frame-src 'self'; \
style-src 'self' 'unsafe-inline' https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css https://fonts.googleapis.com/css/; \
style-src-elem 'self' 'unsafe-inline' https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css https://fonts.googleapis.com/css/; \
"

推荐阅读