force_ssl_middleware 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6dd47638b3279fd42b528a604f5f134dd8751b68
4
+ data.tar.gz: 0b4f39f413fdd73a36269ad5624a5af1c10ceaad
5
+ SHA512:
6
+ metadata.gz: 8bb2d21b6b4e8a91ebce778bc8837363bb454b8a4dcef0a5d831084339330cc9c86a17e31ce1b7144dbdb1a3bcc6425eee35b323684bea8bde9a9069bd5ccc02
7
+ data.tar.gz: 89cb587690bc251d5dae76e7b90080b7f00d12108f0d1a0522b6e834d95f7ae9f4f440297987b7c8dc87acb8afe798d498b3de86f7d9e9d6b90fe37181e85851
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in force_ssl_middleware.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Donald Plummer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ # ForceSslMiddleware
2
+
3
+ Copied from Rails [ActionDispatch::SSL](https://github.com/rails/rails/blob/2468b161a6ada171cceeddaf418b540eb98f2d55/actionpack/lib/action_dispatch/middleware/ssl.rb),
4
+ add support for excluded_paths.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'force_ssl_middleware'
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ Insert into your middleware from `config/environments/production.rb`:
17
+
18
+ ```ruby
19
+ config.middleware.insert 0, ForceSslMiddleware, excluded_paths: ['/options']
20
+ ```
21
+
22
+ Same options as `ActionDispatch::SSL`, with the addition of `excluded_paths`.
23
+
24
+ `excluded_paths`: An array of:
25
+ * `String` - for matching the start of the path
26
+ * `Regexp` - to match against the path
27
+
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
31
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
32
+ prompt that will allow you to experiment.
33
+
34
+ To install this gem onto your local machine, run `bundle exec rake install`. To
35
+ release a new version, update the version number in `version.rb`, and then run
36
+ `bundle exec rake release`, which will create a git tag for the version, push
37
+ git commits and tags, and push the `.gem` file to
38
+ [rubygems.org](https://rubygems.org).
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/avvo/force_ssl_middleware.
43
+
44
+ ## License
45
+
46
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
47
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "force_ssl_middleware"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'force_ssl_middleware/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "force_ssl_middleware"
8
+ spec.version = ForceSslMiddleware::VERSION
9
+ spec.authors = ["Donald Plummer"]
10
+ spec.email = ["dplummer@avvo.com"]
11
+
12
+ spec.summary = %q{Same as ActionDispatch::SSL, add support for excluded_paths}
13
+ spec.homepage = "https://github.com/avvo/force_ssl_middleware"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
@@ -0,0 +1,96 @@
1
+ # copied from Rails https://github.com/rails/rails/blob/2468b161a6ada171cceeddaf418b540eb98f2d55/actionpack/lib/action_dispatch/middleware/ssl.rb#L9
2
+ # Add support for excluded_paths
3
+ class ForceSslMiddleware
4
+ YEAR = 31536000
5
+
6
+ def self.default_hsts_options
7
+ { :expires => YEAR, :subdomains => false }
8
+ end
9
+
10
+ def initialize(app, options = {})
11
+ @app = app
12
+
13
+ @hsts = options.fetch(:hsts, {})
14
+ @hsts = {} if @hsts == true
15
+ @hsts = self.class.default_hsts_options.merge(@hsts) if @hsts
16
+
17
+ @host = options[:host]
18
+ @port = options[:port]
19
+
20
+ @excluded_paths = options[:excluded_paths]
21
+ validate_excluded_paths
22
+ end
23
+
24
+ def call(env)
25
+ request = ActionDispatch::Request.new(env)
26
+
27
+ if excluded_path?(request.fullpath)
28
+ @app.call(env)
29
+ else
30
+ if request.ssl?
31
+ status, headers, body = @app.call(env)
32
+ headers.reverse_merge!(hsts_headers)
33
+ flag_cookies_as_secure!(headers)
34
+ [status, headers, body]
35
+ else
36
+ redirect_to_https(request)
37
+ end
38
+ end
39
+ end
40
+
41
+ private
42
+ def redirect_to_https(request)
43
+ host = @host || request.host
44
+ port = @port || request.port
45
+
46
+ location = "https://#{host}"
47
+ location << ":#{port}" if port != 80
48
+ location << request.fullpath
49
+
50
+ headers = { 'Content-Type' => 'text/html', 'Location' => location }
51
+
52
+ [301, headers, []]
53
+ end
54
+
55
+ # http://tools.ietf.org/html/draft-hodges-strict-transport-sec-02
56
+ def hsts_headers
57
+ if @hsts
58
+ value = "max-age=#{@hsts[:expires].to_i}"
59
+ value += "; includeSubDomains" if @hsts[:subdomains]
60
+ { 'Strict-Transport-Security' => value }
61
+ else
62
+ {}
63
+ end
64
+ end
65
+
66
+ def flag_cookies_as_secure!(headers)
67
+ if cookies = headers['Set-Cookie']
68
+ cookies = cookies.split("\n")
69
+
70
+ headers['Set-Cookie'] = cookies.map { |cookie|
71
+ if cookie !~ /;\s*secure\s*(;|$)/i
72
+ "#{cookie}; secure"
73
+ else
74
+ cookie
75
+ end
76
+ }.join("\n")
77
+ end
78
+ end
79
+
80
+ def excluded_path?(path)
81
+ string_paths, other_paths = @excluded_paths.partition {|p| p.is_a?(String)}
82
+ if string_paths.present? && path.start_with?(*string_paths)
83
+ true
84
+ else
85
+ other_paths.any? do |excluded_path|
86
+ path =~ excluded_path
87
+ end
88
+ end
89
+ end
90
+
91
+ def validate_excluded_paths
92
+ if @excluded_paths.any? {|p| !p.is_a?(String) && !p.is_a?(Regexp)}
93
+ raise "only String and Regexp supported for excluded_paths"
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,3 @@
1
+ class ForceSslMiddleware
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: force_ssl_middleware
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Donald Plummer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - dplummer@avvo.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - force_ssl_middleware.gemspec
72
+ - lib/force_ssl_middleware.rb
73
+ - lib/force_ssl_middleware/version.rb
74
+ homepage: https://github.com/avvo/force_ssl_middleware
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.5.1
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Same as ActionDispatch::SSL, add support for excluded_paths
98
+ test_files: []