如何为nginx(虚拟主机)找出合理的内容安全策略源?

Meg*_*jin 3 security webserver nginx http-headers content-security-policy

我目前正在尝试更多地了解nginx和作为网络服务器的给定安全性.我想象的设置是nginx,有3个虚拟主机.每个主机都运行一个博客.

在完成nginx的一些强化教程后,我确实发现自己陷入了困境http-headers......我不确定如果我有多个虚拟主机,通过nginx实施内容安全策略是否正确,这可能取决于不同的不同内容站点.

但是,我目前的立场是弄清楚如何在每个*-src参数中为nginx设置合理的内容安全策略白名单.

无论是指令引用和源列表的参考,也没有 最新的内容安全策略级别3做了回答"最佳实践白名单"所有的问题*-src.

假设我已经把'self'所有这些参数都放在了:

  • default-src:

如果设置为,这是唯一有意义的参数 'self'

  • script-src,style-src,img-src,connect-src,font-src,child-src:

其余的让我头疼.我怎么知道每个好消息来源?如果我将它们设置为'self'将使用户总是得到一个400 HTTP错误?

就像我之前说的那样,我不确定通过nginx实施内容安全策略是否正确.如果我正在运行一个拥有5个以上客户端的网络服务器,那么我就不可能知道这些客户的每个"好"来源.我想再次指出,我只是想知道这些源参数.其他HTTP-Headers(不仅仅是内容安全策略)对我来说很有意义,而且是完全合理的.

此致,Megajin

Meg*_*jin 13

更新:经过一些更多的研究

我确实在Github上找到了一个非常有用的回购.我将与你们分享.

简短说明: Nginx Server Configs是一组配置片段,可以帮助您的服务器提高网站的性能和安全性,同时确保资源以正确的内容类型提供,并且可以访问(如果需要)甚至是跨域.

链接到回购:https://github.com/h5bp/server-configs-nginx

但是,我建议您阅读并了解每个选项!我仍然使用我自己的CSP政策,就像我在原始答案中所示.我认为这些信息可能对nginx世界中的任何初学者都有用.


我去做了一些关于我的问题的研究.目前我对我的配置很满意,我将在这篇文章的底部分享.

这个Stackoverflow问题实际上是一个很好的起点:内容安全策略如何工作?

我的最后一个观点是保护所有内容,如果有任何用户正在获得CSP - 错误他们必须告诉服务器管理员他们想要向CSP添加另一个源.如果源看起来值得信赖它将被添加,否则被拒绝(我可能以某种方式自动化该过程).

如果有人对进一步的SSL配置感兴趣,你可以查看这个github页面:https://gist.github.com/plentz/6737338

这是我的nginx标头配置:

# don't send the nginx version number in error pages and Server header
  server_tokens off;

# config to don't allow the browser to render the page inside an frame or iframe
# and avoid clickjacking http://en.wikipedia.org/wiki/Clickjacking
# if you need to allow [i]frames, you can use SAMEORIGIN or even set an uri with ALLOW-FROM uri
# https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options
add_header X-Frame-Options SAMEORIGIN always;

# when serving user-supplied content, include a X-Content-Type-Options: nosniff header along with the Content-Type: header,
# to disable content-type sniffing on some browsers.
# https://www.owasp.org/index.php/List_of_useful_HTTP_headers
# currently suppoorted in IE > 8 http://blogs.msdn.com/b/ie/archive/2008/09/02/ie8-security-part-vi-beta-2-update.aspx
# http://msdn.microsoft.com/en-us/library/ie/gg622941(v=vs.85).aspx
# 'soon' on Firefox https://bugzilla.mozilla.org/show_bug.cgi?id=471020
add_header X-Content-Type-Options nosniff always;

# This header enables the Cross-site scripting (XSS) filter built into most recent web browsers.
# It's usually enabled by default anyway, so the role of this header is to re-enable the filter for
# this particular website if it was disabled by the user.
# https://www.owasp.org/index.php/List_of_useful_HTTP_headers
add_header X-XSS-Protection "1; mode=block" always;

# with Content Security Policy (CSP) enabled(and a browser that supports it(http://caniuse.com/#feat=contentsecuritypolicy),
# you can tell the browser that it can only download content from the domains you explicitly allow
# http://www.html5rocks.com/en/tutorials/security/content-security-policy/
# https://www.owasp.org/index.php/Content_Security_Policy
# I need to change our application code so we can increase security by disabling 'unsafe-inline' 'unsafe-eval'
# directives for css and js(if you have inline css or js, you will need to keep it too).
# more: http://www.html5rocks.com/en/tutorials/security/content-security-policy/#inline-code-considered-harmful
add_header Content-Security-Policy "default-src 'self' https://google.com https://youtube.com https://facebook.com https://fonts.google.com https://fonts.googleapis.com https://ajax.googleapis.com https://www.google-analytics.com https://cdnjs.cloudflare.com https://code.jquery.com https://connect.facebook.net https://s.imgur.com https://imgur.com https://i.imgur.com https://500px.com https://drscdn.500px.org https://www.reddit.com https://www.flickr.com https://c1.staticflickr.com https://maxcdn.bootstrapcdn.com http://code.ionicframework.com https://cdn.fontawesome.com/; script-src 'self' 'unsafe-inline'; style-src 'self'; img-src 'self' data:; connect-src 'self'; font-src 'self'; object-src 'none'; media-src 'self'; form-action 'self'; frame-ancestors 'self';" always;
Run Code Online (Sandbox Code Playgroud)

在我的用户的帮助下,这将是一个漫长的学习过程.我希望这也能帮助别人.