rack-headers_filter 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e94f67fc75befa98bd6f8cb63d027f11821d8d58
4
+ data.tar.gz: 59a2aa9bd0b1ef58d8a05ca09ac197e0f0ebfa67
5
+ SHA512:
6
+ metadata.gz: 9ada28c5d3f476f3c22b3aba6f07ae3fc2f0bc1477a5660ef12cb1f85a3445df9141cbd7812b39ffd5477c6eaf625b45cc20c4273dfc3f9545abc81dc8f85685
7
+ data.tar.gz: c56a4f8fde21147350419ce5876bc766ee3a7a488abd531f8f699c6e09345ab0cad5ae363a274829bc5a7f23f7b5b3e2a453b71db0004aedbfdf4ae6e386b40e
@@ -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 rack-headers_filter.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Pusher Ltd.
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,79 @@
1
+ # Rack::HeadersFilter
2
+
3
+ Sanitizes some of the "trusted" headers. Useful when you don't control your
4
+ frontend router like on Heroku. This is the next best thing.
5
+
6
+ The are some funky heuristics happening inside of Rack::Request. For example
7
+ `request.ip` and `request.host_with_port` use HTTP_X_FORWARDED_HOST which is
8
+ forgeable by the client on Heroku. This is then trusted by other code like
9
+ `ActionController::ForceSSL` as a canonical source of truth. The result is
10
+ that if something can set the `X-Forwarded-Host` header, and `config.force_ssl
11
+ = true` is set in rails, the user gets redirected to that random host.
12
+
13
+ This middleware creates a list of "dangerous" headers that are used by
14
+ `Rack::Request` and filters out the ones that are not controlled by the
15
+ router. By default it is configured to be deployed on Heroku.
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'rack-headers_filter'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install rack-headers_filter
32
+
33
+ ## Usage
34
+
35
+ This middleware is designed to be installed first in the chain. Either add
36
+ this to the `config.ru`:
37
+
38
+ ```ruby
39
+ require 'rack/headers_filter'
40
+ use Rack::HeadersFilter
41
+ ```
42
+
43
+ Or in `config/environments/production.rb`:
44
+
45
+ ```ruby
46
+ # First thing, filter out bad headers (0 == first)
47
+ config.middleware.insert_before(0, Rack::HeadersFilter)
48
+ ```
49
+
50
+ By default it is configured with the Heroku router headers. It's also possible
51
+ to specify them manually:
52
+
53
+ ```ruby
54
+ require 'rack/headers_filter'
55
+ use Rack::HeadersFilter, trusted_headers: %[HTTP_HOST]
56
+ ```
57
+
58
+ ## Development
59
+
60
+ After checking out the repo, run `script/setup` to install dependencies. Then,
61
+ run `rake spec` to run the tests. You can also run `script/console` for an
62
+ interactive prompt that will allow you to experiment.
63
+
64
+ To install this gem onto your local machine, run `bundle exec rake install`.
65
+ To release a new version, update the version number in `version.rb`, and then
66
+ run `bundle exec rake release`, which will create a git tag for the version,
67
+ push git commits and tags, and push the `.gem` file to
68
+ [rubygems.org](https://rubygems.org).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at
73
+ https://github.com/pusher/rack-headers_filter.
74
+
75
+ ## License
76
+
77
+ The gem is available as open source under the terms of the
78
+ [MIT License](http://opensource.org/licenses/MIT).
79
+
@@ -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,39 @@
1
+ module Rack
2
+ class HeadersFilter
3
+ SENSITIVE_HEADERS = %w[
4
+ HTTP_X_FORWARDED_FOR
5
+ HTTP_X_FORWARDED_HOST
6
+ HTTP_X_FORWARDED_PORT
7
+ HTTP_X_FORWARDED_PROTO
8
+ HTTP_X_FORWARDED_SCHEME
9
+ HTTP_X_FORWARDED_SSL
10
+ ]
11
+
12
+ # Headers sent by the Heroku router
13
+ HEROKU_HEADERS = %w[
14
+ HTTP_CONNECTION
15
+ HTTP_CONNECT_TIME
16
+ HTTP_HOST
17
+ HTTP_TOTAL_ROUTE_TIME
18
+ HTTP_UPGRADE_INSECURE_REQUESTS
19
+ HTTP_VIA
20
+ HTTP_X_FORWARDED_FOR
21
+ HTTP_X_FORWARDED_PROTO
22
+ HTTP_X_FORWARDED_PROTO
23
+ HTTP_X_REQUEST_ID
24
+ HTTP_X_REQUEST_START
25
+ ]
26
+
27
+ attr_reader :remove_headers
28
+
29
+ def initialize(app, trusted_headers: HEROKU_HEADERS)
30
+ @remove_headers = SENSITIVE_HEADERS - trusted_headers
31
+ @app = app
32
+ end
33
+
34
+ def call(env)
35
+ @remove_headers.each(&env.method(:delete))
36
+ @app.call(env)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = "rack-headers_filter"
4
+ spec.version = '0.0.1'
5
+ spec.authors = ["zimbatm"]
6
+ spec.email = ["zimbatm@zimbatm.com"]
7
+
8
+ spec.summary = %q{Removes sensitive untrusted headers from the request}
9
+ spec.description = %q{
10
+ The goal of this gem is to avoid depending on potentially forgeable
11
+ headers down the line by configuring the deploy target properly.
12
+ }
13
+ spec.homepage = "https://github.com/pusher/rack-headers_filter"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.11"
20
+ spec.add_development_dependency "rack", "~> 1.6"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.0"
23
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rack/headers_filter"
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
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-headers_filter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - zimbatm
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-22 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: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: "\n The goal of this gem is to avoid depending on potentially forgeable\n
70
+ \ headers down the line by configuring the deploy target properly.\n "
71
+ email:
72
+ - zimbatm@zimbatm.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - lib/rack/headers_filter.rb
85
+ - rack-headers_filter.gemspec
86
+ - script/console
87
+ - script/setup
88
+ homepage: https://github.com/pusher/rack-headers_filter
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.6.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Removes sensitive untrusted headers from the request
112
+ test_files: []