Sha256: 20161c9f6a301e58ef6293370abc4f5e9588e2fbd94af005f6728cc129adbe86

Contents?: true

Size: 1.56 KB

Versions: 14

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true
module SecureHeaders
  class Middleware
    def initialize(app)
      @app = app
    end

    # merges the hash of headers into the current header set.
    def call(env)
      req = Rack::Request.new(env)
      status, headers, response = @app.call(env)

      config = SecureHeaders.config_for(req)
      flag_cookies!(headers, override_secure(env, config.cookies)) unless config.cookies == OPT_OUT
      headers.merge!(SecureHeaders.header_hash_for(req))
      [status, headers, response]
    end

    private

    # inspired by https://github.com/tobmatth/rack-ssl-enforcer/blob/6c014/lib/rack/ssl-enforcer.rb#L183-L194
    def flag_cookies!(headers, config)
      if cookies = headers["Set-Cookie"]
        # Support Rails 2.3 / Rack 1.1 arrays as headers
        cookies = cookies.split("\n") unless cookies.is_a?(Array)

        headers["Set-Cookie"] = cookies.map do |cookie|
          SecureHeaders::Cookie.new(cookie, config).to_s
        end.join("\n")
      end
    end

    # disable Secure cookies for non-https requests
    def override_secure(env, config = {})
      if scheme(env) != "https" && config != OPT_OUT
        config[:secure] = OPT_OUT
      end

      config
    end

    # derived from https://github.com/tobmatth/rack-ssl-enforcer/blob/6c014/lib/rack/ssl-enforcer.rb#L119
    def scheme(env)
      if env["HTTPS"] == "on" || env["HTTP_X_SSL_REQUEST"] == "on"
        "https"
      elsif env["HTTP_X_FORWARDED_PROTO"]
        env["HTTP_X_FORWARDED_PROTO"].split(",")[0]
      else
        env["rack.url_scheme"]
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
secure_headers-7.0.0 lib/secure_headers/middleware.rb
secure_headers-6.7.0 lib/secure_headers/middleware.rb
secure_headers-6.6.0 lib/secure_headers/middleware.rb
secure_headers-6.5.0 lib/secure_headers/middleware.rb
secure_headers-6.4.0 lib/secure_headers/middleware.rb
secure_headers-6.3.4 lib/secure_headers/middleware.rb
secure_headers-6.3.3 lib/secure_headers/middleware.rb
secure_headers-6.3.2 lib/secure_headers/middleware.rb
secure_headers-6.3.1 lib/secure_headers/middleware.rb
secure_headers-6.3.0 lib/secure_headers/middleware.rb
secure_headers-6.2.0 lib/secure_headers/middleware.rb
secure_headers-6.1.2 lib/secure_headers/middleware.rb
secure_headers-6.1.1 lib/secure_headers/middleware.rb
secure_headers-6.1.0 lib/secure_headers/middleware.rb