hooopo / hooopo.github.com

octopress
hooopo.github.com
5 stars 0 forks source link

Three Default Secure HTTP Headers By Rails4 #2

Open hooopo opened 11 years ago

hooopo commented 11 years ago

Rails4 引入了三个默认HTTP Headers

config.action_dispatch.default_headers = {
      'X-Frame-Options' => 'SAMEORIGIN',
      'X-XSS-Protection' => '1; mode=block',
      'X-Content-Type-Options' => 'nosniff'
    }

X-Content-Type-Options

X-Content-Type-Options'头设置为 nosniff'会阻止浏览器通过内容猜测文件的MIME Type的行为。如果不阻止,浏览器的这一行为会被黑客利用,增加XSS的机率。

X-XSS-Protection

IE8以及之后的版本支持X-XSS-Protection头,设置为1;mode=block会激活IE内建的XSS过滤机制,阻止常见的XSS攻击。

X-Frame-Options

X-Frame-Options 头可以决定网页是否允许被嵌入(iframe/frame)到其他页面。设置为DENY之后会阻止所有页面内嵌你的网页,,设置为SAMEORIGIN会允许自己域下的网站内嵌你的网页。 还可以设置指定允许内嵌你的页面的域。不允许别人内嵌你的页面是减少click-jacking的有效途径。

在Rails3里增加这三个头很简单,只要加一个全局的filter:

# application_controller.rb
before_filter :set_secure_headers

def set_secure_headers
  response.headers.merge!(
      'X-Frame-Options'        => 'SAMEORIGIN',
      'X-XSS-Protection'       => '1; mode=block',
      'X-Content-Type-Options' => 'nosniff' 
    )
end

refs:

  1. http://recxltd.blogspot.co.uk/2012/03/seven-web-server-http-headers-that.html
  2. https://blog.whitehatsec.com/x-frame-options/
  3. http://blogs.msdn.com/b/ie/archive/2008/07/02/ie8-security-part-iv-the-xss-filter.aspx
  4. http://homakov.blogspot.com/2012/06/saferweb-with-new-features-come-new.html
  5. http://msdn.microsoft.com/en-us/library/ie/gg622941(v=vs.85).aspx