-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1672 from 18F/margolis-host-headers
Remove HTTP Host header entirely
- Loading branch information
Showing
4 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters