Module: SecureHeaders

Included in:
ActionController::Base
Defined in:
lib/secure_headers.rb,
lib/secure_headers/railtie.rb,
lib/secure_headers/middleware.rb,
lib/secure_headers/hash_helper.rb,
lib/secure_headers/view_helper.rb,
lib/secure_headers/configuration.rb,
lib/secure_headers/headers/cookie.rb,
lib/secure_headers/utils/cookies_config.rb,
lib/secure_headers/headers/public_key_pins.rb,
lib/secure_headers/headers/referrer_policy.rb,
lib/secure_headers/headers/x_frame_options.rb,
lib/secure_headers/headers/x_xss_protection.rb,
lib/secure_headers/headers/policy_management.rb,
lib/secure_headers/headers/x_download_options.rb,
lib/secure_headers/headers/x_content_type_options.rb,
lib/secure_headers/headers/content_security_policy.rb,
lib/secure_headers/headers/strict_transport_security.rb,
lib/secure_headers/headers/content_security_policy_config.rb,
lib/secure_headers/headers/x_permitted_cross_domain_policies.rb

Overview

All headers (except for hpkp) have a default value. Provide SecureHeaders::OPT_OUT or ":optout_of_protection" as a config value to disable a given header

Defined Under Namespace

Modules: DynamicConfig, HashHelper, PolicyManagement, ViewHelpers Classes: Configuration, ContentSecurityPolicy, ContentSecurityPolicyConfig, ContentSecurityPolicyConfigError, ContentSecurityPolicyReportOnlyConfig, Cookie, CookiesConfig, CookiesConfigError, Middleware, NoOpHeaderConfig, PublicKeyPins, PublicKeyPinsConfigError, Railtie, ReferrerPolicy, ReferrerPolicyConfigError, STSConfigError, StrictTransportSecurity, XContentTypeOptions, XContentTypeOptionsConfigError, XDOConfigError, XDownloadOptions, XFOConfigError, XFrameOptions, XPCDPConfigError, XPermittedCrossDomainPolicies, XXssProtection, XXssProtectionConfigError

Constant Summary collapse

OPT_OUT =
NoOpHeaderConfig.instance
SECURE_HEADERS_CONFIG =
"secure_headers_request_config".freeze
NONCE_KEY =
"secure_headers_content_security_policy_nonce".freeze
HTTPS =
"https".freeze
CSP =
ContentSecurityPolicy
ALL_HEADER_CLASSES =
[
  ContentSecurityPolicyConfig,
  ContentSecurityPolicyReportOnlyConfig,
  StrictTransportSecurity,
  PublicKeyPins,
  ReferrerPolicy,
  XContentTypeOptions,
  XDownloadOptions,
  XFrameOptions,
  XPermittedCrossDomainPolicies,
  XXssProtection
].freeze
ALL_HEADERS_BESIDES_CSP =
(
  ALL_HEADER_CLASSES -
    [ContentSecurityPolicyConfig, ContentSecurityPolicyReportOnlyConfig]
).freeze
HTTP_HEADER_CLASSES =

Headers set on http requests (excludes STS and HPKP)

(ALL_HEADER_CLASSES - [StrictTransportSecurity, PublicKeyPins]).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.append_content_security_policy_directives(request, additions, target = nil) ⇒ Object

Public: appends source values to the current configuration. If no value is set for a given directive, the value will be merged with the default-src value. If a value exists for the given directive, the values will be combined.

additions - a hash containing directives. e.g. script_src: %w(another-host.com)



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/secure_headers.rb', line 110

def append_content_security_policy_directives(request, additions, target = nil)
  config, target = config_and_target(request, target)

  if [:both, :enforced].include?(target) && !config.csp.opt_out?
    config.csp.append(additions)
  end

  if [:both, :report_only].include?(target) && !config.csp_report_only.opt_out?
    config.csp_report_only.append(additions)
  end

  override_secure_headers_request_config(request, config)
end

.config_for(request, prevent_dup = false) ⇒ Object

Public: Retreives the config for a given header type:

Checks to see if there is an override for this request, then Checks to see if a named override is used for this request, then Falls back to the global config



223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/secure_headers.rb', line 223

def config_for(request, prevent_dup = false)
  config = request.env[SECURE_HEADERS_CONFIG] ||
    Configuration.get(Configuration::DEFAULT_CONFIG)


  # Global configs are frozen, per-request configs are not. When we're not
  # making modifications to the config, prevent_dup ensures we don't dup
  # the object unnecessarily. It's not necessarily frozen to begin with.
  if config.frozen? && !prevent_dup
    config.dup
  else
    config
  end
end

.content_security_policy_script_nonce(request) ⇒ Object

Public: gets or creates a nonce for CSP.

The nonce will be added to script_src

Returns the nonce



205
206
207
# File 'lib/secure_headers.rb', line 205

def content_security_policy_script_nonce(request)
  content_security_policy_nonce(request, ContentSecurityPolicy::SCRIPT_SRC)
end

.content_security_policy_style_nonce(request) ⇒ Object

Public: gets or creates a nonce for CSP.

The nonce will be added to style_src

Returns the nonce



214
215
216
# File 'lib/secure_headers.rb', line 214

def content_security_policy_style_nonce(request)
  content_security_policy_nonce(request, ContentSecurityPolicy::STYLE_SRC)
end

.header_hash_for(request) ⇒ Object

Public: Builds the hash of headers that should be applied base on the request.

StrictTransportSecurity and PublicKeyPins are not applied to http requests. See #config_for to determine which config is used for a given request.

Returns a hash of header names => header values. The value returned is meant to be merged into the header value from @app.call(env) in Rack middleware.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/secure_headers.rb', line 163

def header_hash_for(request)
  config = config_for(request, prevent_dup = true)
  headers = config.cached_headers
  user_agent = UserAgent.parse(request.user_agent)

  if !config.csp.opt_out? && config.csp.modified?
    headers = update_cached_csp(config.csp, headers, user_agent)
  end

  if !config.csp_report_only.opt_out? && config.csp_report_only.modified?
    headers = update_cached_csp(config.csp_report_only, headers, user_agent)
  end

  header_classes_for(request).each_with_object({}) do |klass, hash|
    if header = headers[klass::CONFIG_KEY]
      header_name, value = if [ContentSecurityPolicyConfig, ContentSecurityPolicyReportOnlyConfig].include?(klass)
        csp_header_for_ua(header, user_agent)
      else
        header
      end
      hash[header_name] = value
    end
  end
end

.opt_out_of_all_protection(request) ⇒ Object

Public: opts out of setting all headers by telling secure_headers to use the NOOP configuration.



150
151
152
# File 'lib/secure_headers.rb', line 150

def opt_out_of_all_protection(request)
  use_secure_headers_override(request, Configuration::NOOP_CONFIGURATION)
end

.opt_out_of_header(request, header_key) ⇒ Object

Public: opts out of setting a given header by creating a temporary config and setting the given headers config to OPT_OUT.



142
143
144
145
146
# File 'lib/secure_headers.rb', line 142

def opt_out_of_header(request, header_key)
  config = config_for(request)
  config.opt_out(header_key)
  override_secure_headers_request_config(request, config)
end

.override_content_security_policy_directives(request, additions, target = nil) ⇒ Object

Public: override a given set of directives for the current request. If a value already exists for a given directive, it will be overridden.

If CSP was previously OPT_OUT, a new blank policy is used.

additions - a hash containing directives. e.g. script_src: %w(another-host.com)



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/secure_headers.rb', line 82

def override_content_security_policy_directives(request, additions, target = nil)
  config, target = config_and_target(request, target)

  if [:both, :enforced].include?(target)
    if config.csp.opt_out?
      config.csp = ContentSecurityPolicyConfig.new({})
    end

    config.csp.merge!(additions)
  end

  if [:both, :report_only].include?(target)
    if config.csp_report_only.opt_out?
      config.csp_report_only = ContentSecurityPolicyReportOnlyConfig.new({})
    end

    config.csp_report_only.merge!(additions)
  end

  override_secure_headers_request_config(request, config)
end

.override_x_frame_options(request, value) ⇒ Object

Public: override X-Frame-Options settings for this request.

value - deny, sameorigin, or allowall

Returns the current config



134
135
136
137
138
# File 'lib/secure_headers.rb', line 134

def override_x_frame_options(request, value)
  config = config_for(request)
  config.update_x_frame_options(value)
  override_secure_headers_request_config(request, config)
end

.use_content_security_policy_named_append(request, name) ⇒ Object



124
125
126
127
# File 'lib/secure_headers.rb', line 124

def use_content_security_policy_named_append(request, name)
  additions = SecureHeaders::Configuration.named_appends(name).call(request)
  append_content_security_policy_directives(request, additions)
end

.use_secure_headers_override(request, name) ⇒ Object

Public: specify which named override will be used for this request. Raises an argument error if no named override exists.

name - the name of the previously configured override.



192
193
194
195
196
197
198
# File 'lib/secure_headers.rb', line 192

def use_secure_headers_override(request, name)
  if config = Configuration.get(name)
    override_secure_headers_request_config(request, config)
  else
    raise ArgumentError.new("no override by the name of #{name} has been configured")
  end
end

Instance Method Details

#append_content_security_policy_directives(additions) ⇒ Object



331
332
333
# File 'lib/secure_headers.rb', line 331

def append_content_security_policy_directives(additions)
  SecureHeaders.append_content_security_policy_directives(request, additions)
end

#content_security_policy_script_nonce ⇒ Object



319
320
321
# File 'lib/secure_headers.rb', line 319

def content_security_policy_script_nonce
  SecureHeaders.content_security_policy_script_nonce(request)
end

#content_security_policy_style_nonce ⇒ Object



323
324
325
# File 'lib/secure_headers.rb', line 323

def content_security_policy_style_nonce
  SecureHeaders.content_security_policy_style_nonce(request)
end

#opt_out_of_header(header_key) ⇒ Object



327
328
329
# File 'lib/secure_headers.rb', line 327

def opt_out_of_header(header_key)
  SecureHeaders.opt_out_of_header(request, header_key)
end

#override_content_security_policy_directives(additions) ⇒ Object



335
336
337
# File 'lib/secure_headers.rb', line 335

def override_content_security_policy_directives(additions)
  SecureHeaders.override_content_security_policy_directives(request, additions)
end

#override_x_frame_options(value) ⇒ Object



339
340
341
# File 'lib/secure_headers.rb', line 339

def override_x_frame_options(value)
  SecureHeaders.override_x_frame_options(request, value)
end

#use_content_security_policy_named_append(name) ⇒ Object



343
344
345
# File 'lib/secure_headers.rb', line 343

def use_content_security_policy_named_append(name)
  SecureHeaders.use_content_security_policy_named_append(request, name)
end

#use_secure_headers_override(name) ⇒ Object

These methods are mixed into controllers and delegate to the class method with the same name.



315
316
317
# File 'lib/secure_headers.rb', line 315

def use_secure_headers_override(name)
  SecureHeaders.use_secure_headers_override(request, name)
end