Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

secure_headers

> 安全
Open source

Manages application of security headers with many safe defaults

3.2K stars0 likes1 views
WebsiteGitHub

About

Manages application of security headers with many safe defaults

Secure Headers

main branch represents 7.x line. See the upgrading to 4.x doc, upgrading to 5.x doc, upgrading to 6.x doc or upgrading to 7.x doc for instructions on how to upgrade. Bug fixes should go in the 6.x branch for now.

The gem will automatically apply several headers that are related to security. This includes:

  • Content Security Policy (CSP) - Helps detect/prevent XSS, mixed-content, and other classes of attack. CSP 2 Specification
    • https://csp.withgoogle.com
    • https://csp.withgoogle.com/docs/strict-csp.html
    • https://csp-evaluator.withgoogle.com
  • HTTP Strict Transport Security (HSTS) - Ensures the browser never visits the http version of a website. Protects from SSLStrip/Firesheep attacks. HSTS Specification
  • X-Frame-Options (XFO) - Prevents your content from being framed and potentially clickjacked. X-Frame-Options Specification
  • X-XSS-Protection - Cross site scripting heuristic filter for IE/Chrome
  • X-Content-Type-Options - Prevent content type sniffing
  • x-download-options - Prevent file downloads opening
  • x-permitted-cross-domain-policies - Restrict Adobe Flash Player's access to data
  • referrer-policy - Referrer Policy draft
  • expect-ct - Only use certificates that are present in the certificate transparency logs. expect-ct draft specification.
  • clear-site-data - Clearing browser data for origin. clear-site-data specification.

It can also mark all http cookies with the Secure, HttpOnly and SameSite attributes. This is on default but can be turned off by using config.cookies = SecureHeaders::OPT_OUT.

secure_headers is a library with a global config, per request overrides, and rack middleware that enables you customize your application settings.

Documentation

  • Named overrides and appends
  • Per action configuration
  • Cookies
  • Hashes
  • Sinatra Config

Configuration

If you do not supply a default configuration, exceptions will be raised. If you would like to use a default configuration (which is fairly locked down), just call SecureHeaders::Configuration.default without any arguments or block.

All nil values will fallback to their default values. SecureHeaders::OPT_OUT will disable the header entirely.

Word of caution: The following is not a default configuration per se. It serves as a sample implementation of the configuration. You should read more about these headers and determine what is appropriate for your requirements.

…

CSP Reporting

SecureHeaders supports both the legacy report-uri and the modern report-to directives for CSP violation reporting:

report-uri (Legacy)

The report-uri directive sends violations to a URL endpoint. It's widely supported but limited to POST requests with JSON payloads.

ruby
config.csp = {
  default_src: %w('self'),
  report_uri: %w(https://example.com/csp-report)
}

report-to (Modern)

The report-to directive specifies a named reporting endpoint defined in the Reporting-Endpoints header. This enables more flexible reporting through the HTTP Reporting API standard.

ruby
config.csp = {
  default_src: %w('self'),
  report_to: "csp-endpoint"
}

config.reporting_endpoints = {
  "csp-endpoint": "https://example.com/reports"
}

Recommendation: Use both report-uri and report-to for maximum compatibility while transitioning to the modern approach.

Deprecated Configuration Values

  • block_all_mixed_content - this value is deprecated in favor of upgrade_insecure_requests. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/block-all-mixed-content for more information.

Default values

All headers except for PublicKeyPins and ClearSiteData have a default value. The default set of headers is:

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: 0

API configurations

Which headers you decide to use for API responses is entirely a personal choice. Things like X-Frame-Options seem to have no place in an API response and would be wasting bytes. While this is true, browsers can do funky things with non-html responses. At the minimum, we suggest CSP:

ruby
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

However, I would consider these headers anyways depending on your load and bandwidth requirements.

Disabling secure_headers

If you want to disable secure_headers entirely (e.g., for specific environments or deployment scenarios), you can use Configuration.disable!:

ruby
if ENV["ENABLE_STRICT_HEADERS"]
  SecureHeaders::Configuration.default do |config|
    # your configuration here
  end
else
  SecureHeaders::Configuration.disable!
end

Important: This configuration must be set during application startup (e.g., in an initializer). Once you call either Configuration.default or Configuration.disable!, the choice cannot be changed at runtime. Attempting to call disable! after default (or vice versa) will raise an AlreadyConfiguredError.

When disabled, no security headers will be set by the gem. This is useful when:

  • You're gradually rolling out secure_headers across different customers or deployments
  • You need to migrate existing custom headers to secure_headers
  • You want environment-specific control over security headers

Note: When disable! is used, you don't need to configure a default configuration. The gem will not raise a NotYetConfiguredError.

Acknowledgements

This project originated within the Security team at Twitter. An archived fork from the point of transition is here: https://github.com/twitter-archive/secure_headers.

Contributors include:

  • Neil Matatall @oreoshake
  • Chris Aniszczyk
  • Artur Dryomov
  • Bjørn Mæland
  • Arthur Chiu
  • Jonathan Viney
  • Jeffrey Horn
  • David Collazo
  • Brendon Murphy
  • William Makley
  • Reed Loden
  • Noah Kantrowitz
  • Wyatt Anderson
  • Salimane Adjao Moustapha
  • Francois Chagnon
  • Jeff Hodges
  • Ian Melven
  • Darío Javier Cravero
  • Logan Hasson
  • Raul E Rangel
  • Steve Agalloco
  • Nate Collings
  • Josh Kalderimis
  • Alex Kwiatkowski
  • Julich Mera
  • Jesse Storimer
  • Tom Daniels
  • Kolja Dummann
  • Jean-Philippe Doyle
  • Blake Hitchcock
  • vanderhoorn
  • orthographic-pedant
  • Narsimham Chelluri

If you've made a contribution and see your name missing from the list, make a PR and add it!

Similar libraries

  • Rack rack-secure_headers
  • Node.js (express) helmet and hood
  • Node.js (hapi) blankie
  • ASP.NET - NWebsec
  • Python - django-csp + commonware; django-security, secure
  • Go - secureheader
  • Elixir secure_headers
  • Dropwizard dropwizard-web-security
  • Ember.js ember-cli-content-security-policy
  • PHP secure-headers

Issues· 13 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubycontent-security-policycookiecsphsts

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category安全
PricingOpen source

> Related tools

O
OWASP ZAP
开源 Web 应用安全扫描器
O
owasp-wstg-tracker
Simple web app to track OWASP WSTG security testing progress
H
homebridge-mi-gateway-security
XiaoMi Gateway Security plugin for HomeBridge.