Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove HTTP Host header entirely #1672

Merged
merged 1 commit into from
Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class Application < Rails::Application
event.payload.except(:params, :headers)
end

config.middleware.insert_before 0, Rack::HeadersFilter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the rack-header-filter gem let's you call this with a blacklist. Why not do that here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh thanks, I'd missed that gem entirely, will update

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so that gem deletes outgoing headers, not incoming headers, I don't think we can use it here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frown town. Oh well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extremely frown town 😿

require 'headers_filter'
config.middleware.insert_before 0, HeadersFilter

config.middleware.insert_before 0, Rack::Cors do
allow do
Expand Down
21 changes: 21 additions & 0 deletions lib/headers_filter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'rack/headers_filter'

# Expands on Rack::HeadersFilter to delete additional headers
class HeadersFilter
HEADERS_TO_DELETE = Rack::HeadersFilter::SENSITIVE_HEADERS + %w[
HTTP_HOST
]

def initialize(app)
@app = app
end

def call(env)
HEADERS_TO_DELETE.each { |header| env.delete(header) }
app.call(env)
end

private

attr_reader :app
end
21 changes: 21 additions & 0 deletions spec/lib/headers_filter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'rails_helper'

RSpec.describe HeadersFilter do
let(:app) { double('App', call: nil) }

let(:middleware) { HeadersFilter.new(app) }

describe '#call' do
it 'removes untrusted headers from the env' do
env = {
'HTTP_HOST' => 'foobar.com',
'HTTP_X_FORWARDED_HOST' => 'evil.com',
}

middleware.call(env)

expect(env).to_not have_key('HTTP_HOST')
expect(env).to_not have_key('HTTP_X_FORWARDED_HOST')
end
end
end
6 changes: 6 additions & 0 deletions spec/requests/headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@

expect(response.body).to_not include('evil.com')
end

it 'does not blow up with a malicious host value' do
get root_path, headers: { 'Host' => "mTpvPME6'));select pg_sleep(9); --" }

expect(response.code.to_i).to eq(200)
end
end