rack-ssl-1.4.1/0000755000076400007640000000000012433176562012301 5ustar pravipravirack-ssl-1.4.1/LICENSE0000644000076400007640000000203712433176562013310 0ustar pravipraviCopyright (c) 2010 Joshua Peek Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rack-ssl-1.4.1/README.md0000644000076400007640000000042512433176562013561 0ustar pravipraviRack::SSL ========= Force SSL/TLS in your app. 1. Redirects all "http" requests to "https" 2. Set `Strict-Transport-Security` header 3. Flag all cookies as "secure" Installation ------------ gem install rack-ssl Usage ----- require 'rack/ssl' use Rack::SSL rack-ssl-1.4.1/metadata.yml0000644000076400007640000000246712433176562014615 0ustar pravipravi--- !ruby/object:Gem::Specification name: rack-ssl version: !ruby/object:Gem::Version version: 1.4.1 platform: ruby authors: - Joshua Peek autorequire: bindir: bin cert_chain: [] date: 2014-03-23 00:00:00.000000000 Z dependencies: - !ruby/object:Gem::Dependency name: rack requirement: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' type: :runtime prerelease: false version_requirements: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' description: |2 Rack middleware to force SSL/TLS. email: josh@joshpeek.com executables: [] extensions: [] extra_rdoc_files: [] files: - lib/rack/ssl.rb - LICENSE - README.md homepage: https://github.com/josh/rack-ssl licenses: - MIT metadata: {} post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' required_rubygems_version: !ruby/object:Gem::Requirement requirements: - - '>=' - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: rack-ssl rubygems_version: 2.0.3 signing_key: specification_version: 4 summary: Force SSL/TLS in your app. test_files: [] has_rdoc: rack-ssl-1.4.1/lib/0000755000076400007640000000000012433176562013047 5ustar pravipravirack-ssl-1.4.1/lib/rack/0000755000076400007640000000000012433176562013767 5ustar pravipravirack-ssl-1.4.1/lib/rack/ssl.rb0000644000076400007640000000433312433176562015120 0ustar pravipravirequire 'rack' require 'rack/request' module Rack class SSL YEAR = 31536000 def self.default_hsts_options { :expires => YEAR, :subdomains => false } end def initialize(app, options = {}) @app = app @hsts = options[:hsts] @hsts = {} if @hsts.nil? || @hsts == true @hsts = self.class.default_hsts_options.merge(@hsts) if @hsts @exclude = options[:exclude] @host = options[:host] end def call(env) if @exclude && @exclude.call(env) @app.call(env) elsif scheme(env) == 'https' status, headers, body = @app.call(env) headers = hsts_headers.merge(headers) flag_cookies_as_secure!(headers) [status, headers, body] else redirect_to_https(env) end end private # Fixed in rack >= 1.3 def scheme(env) if env['HTTPS'] == 'on' 'https' elsif env['HTTP_X_FORWARDED_PROTO'] env['HTTP_X_FORWARDED_PROTO'].split(',')[0] else env['rack.url_scheme'] end end def redirect_to_https(env) req = Request.new(env) host = @host || req.host location = "https://#{host}#{req.fullpath}" status = %w[GET HEAD].include?(req.request_method) ? 301 : 307 headers = { 'Content-Type' => 'text/html', 'Location' => location } [status, headers, []] end # http://tools.ietf.org/html/draft-hodges-strict-transport-sec-02 def hsts_headers if @hsts value = "max-age=#{@hsts[:expires]}" value += "; includeSubDomains" if @hsts[:subdomains] { 'Strict-Transport-Security' => value } else {} end end def flag_cookies_as_secure!(headers) if cookies = headers['Set-Cookie'] # Rack 1.1's set_cookie_header! will sometimes wrap # Set-Cookie in an array unless cookies.respond_to?(:to_ary) cookies = cookies.split("\n") end headers['Set-Cookie'] = cookies.map { |cookie| if cookie !~ /; secure(;|$)/ "#{cookie}; secure" else cookie end }.join("\n") end end end end