Secure Headers
master 表示6.x 线。查看升级到4.x文档,升级到5.x文档, 或者 升级到6.x文档, 以获取有关如何升级的说明。现在,错误修复应该在5.x分支中。
gem 将自动应用几个与安全性相关的标头。 这包括:
- Content Security Policy(CSP) -- 帮助检测/阻止XSS,混合内容和其他类型的攻击。 CSP 2规范
- HTTP Strict Transport Security(HSTS) -- 确保浏览器不会访问网站的http版本。防止SSLStrip/Firesheep攻击。 HSTS规范
- X-Frame-Options(XFO) -- 防止您的内容被构架并可能被点击劫持。 X-Frame-Options Specification
- X-XSS-Protection -- 用于IE跨站脚本启发式滤波器/铬
- X-Content-Type-Options -- 防止内容类型嗅探
- X-Download-Options -- 防止文件下载打开
- X-Permitted-Cross-Domain-Policies -- 限制Adobe Flash Player访问数据
- Referrer-Policy -- Referrer Policy draft
- Expect-CT -- 仅使用证书透明度日志中存在的证书。 Expect-CT草案规范。
- Clear-Site-Data -- 清除浏览器数据的来源。 清除网站数据规范。
它也可以用 Secure、HttpOnly 和 SameSite 属性标记所有 HTTP cookie。这是默认的,但可以通过使用 config.cookies=SecureHeaders::OPT_OUT 来关闭。
secure_headers 是一个具有全局配置,每个请求覆盖和 rack 中间件的库,可让您自定义应用程序设置。
文档
配置
如果您不提供默认配置,则会引发异常。 如果您想使用默认配置(相当严格锁定),只需调用 SecureHeaders::Configuration.default,无需任何参数或阻止。
所有nil值都将回退到其默认值。 SecureHeaders::OPT_OUT 将完全禁用标头。
注意事项:以下内容不是默认配置。 它用作配置的示例实现。 您应该阅读有关这些标头的更多信息,并确定哪些适合您的要求。
SecureHeaders::Configuration.default do |config| config.cookies = { secure: true, # mark all cookies as "Secure" httponly: true, # mark all cookies as "HttpOnly" samesite: { lax: true # mark all cookies as SameSite=lax } } # Add "; preload" and submit the site to hstspreload.org for best protection. config.hsts = "max-age=#{1.week.to_i}" config.x_frame_options = "DENY" config.x_content_type_options = "nosniff" config.x_xss_protection = "1; mode=block" config.x_download_options = "noopen" config.x_permitted_cross_domain_policies = "none" config.referrer_policy = %w(origin-when-cross-origin strict-origin-when-cross-origin) config.csp = { # "meta" values. these will shape the header, but the values are not included in the header. preserve_schemes: true, # default: false. Schemes are removed from host sources to save bytes and discourage mixed content. # directive values: these values will directly translate into source directives default_src: %w('none'), base_uri: %w('self'), block_all_mixed_content: true, # see http://www.w3.org/TR/mixed-content/ child_src: %w('self'), # if child-src isn't supported, the value for frame-src will be set. connect_src: %w(wss:), font_src: %w('self' data:), form_action: %w('self' github.com), frame_ancestors: %w('none'), img_src: %w(mycdn.com data:), manifest_src: %w('self'), media_src: %w(utoob.com), object_src: %w('self'), sandbox: true, # true and [] will set a maximally restrictive setting plugin_types: %w(application/x-shockwave-flash), script_src: %w('self'), style_src: %w('unsafe-inline'), worker_src: %w('self'), upgrade_insecure_requests: true, # see https://www.w3.org/TR/upgrade-insecure-requests/ report_uri: %w(https://report-uri.io/example-csp) } # This is available only from 3.5.0; use the `report_only: true` setting for 3.4.1 and below. config.csp_report_only = config.csp.merge({ img_src: %w(somewhereelse.com), report_uri: %w(https://report-uri.io/example-csp-report-only) }) end
默认值
除 PublicKeyPins 和 ClearSiteData 之外的所有标头都有默认值。 默认的标头集是:
Content-Security-Policy: default-src 'self' https:; font-src 'self' https: data:; img-src 'self' https: data:; object-src 'none'; script-src https:; style-src 'self' https: 'unsafe-inline' Strict-Transport-Security: max-age=631138519 X-Content-Type-Options: nosniff X-Download-Options: noopen X-Frame-Options: sameorigin X-Permitted-Cross-Domain-Policies: none X-Xss-Protection: 1; mode=block
API 配置
您决定使用哪些标头进行 API 响应完全是个人选择。 像 X-Frame-Options 这样的东西似乎在 API 响应中没有位置,并且会浪费字节。 虽然这是事实,但浏览器可以使用非 HTML 响应做出时髦的事情。 至少,我们建议 CSP:
SecureHeaders::Configuration.override(:api) do |config| config.csp = { default_src: 'none' } config.hsts = SecureHeaders::OPT_OUT config.x_frame_options = SecureHeaders::OPT_OUT config.x_content_type_options = SecureHeaders::OPT_OUT config.x_xss_protection = SecureHeaders::OPT_OUT config.x_permitted_cross_domain_policies = SecureHeaders::OPT_OUT end
但是,我会考虑这些标头,具体取决于您的负载和带宽要求。
类似的库
- Rack rack-secure_headers
- Node.js(express)helmet 和 hood
- Node.js(hapi) blankie
- J2EE Servlet >=3.0 headlines
- ASP.NET -- NWebsec
- Python -- django-csp + commonware ; django-security
- Go -- secureheader
- Elixir secure_headers
- Dropwizard dropwizard-web-security
- Ember.js ember-cli-content-security-policy
- PHP secure-headers
许可证
版权所有2013-2014 Twitter,Inc和其他贡献者。
根据 Apache 许可证版本2.0获得许可: http://www.apache.org/licenses/LICENSE-2.0