Skip to content

Commit

Permalink
feat: redact auth headers from rack env used to report errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Mar 14, 2018
1 parent 579fa39 commit 94f8f13
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
16 changes: 12 additions & 4 deletions lib/webmachine/convert_request_to_rack_env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ def self.call(request)
'SCRIPT_NAME' => '',
'rack.url_scheme' => request.uri.scheme,
'rack.input' => request.body.to_io ? StringIO.new(request.body.to_s) : nil
}
http_headers = request.headers.each do | key, value |
env[convert_http_header_name_to_rack_header_name(key)] = value
}.merge(convert_headers(request))
end

def self.convert_headers(request)
request.headers.each_with_object({}) do | (key, value), env |
v = redact?(key) ? '[Filtered]' : value
env[convert_http_header_name_to_rack_header_name(key)] = v
end
env
end

def self.redact?(http_header_name)
lower = http_header_name.downcase
lower == 'authorization' || lower.include?('token')
end

def self.convert_http_header_name_to_rack_header_name(http_header_name)
Expand Down
47 changes: 32 additions & 15 deletions spec/lib/webmachine/convert_request_to_rack_env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,36 @@ module Webmachine

let(:rack_env) do
{
"rack.input"=>StringIO.new('foo'),
"REQUEST_METHOD"=>"POST",
"SERVER_NAME"=>"example.org",
"SERVER_PORT"=>"80",
"QUERY_STRING"=>"",
"PATH_INFO"=>"/foo",
"rack.url_scheme"=>"http",
"SCRIPT_NAME"=>"",
"CONTENT_LENGTH"=>"0",
"HTTP_HOST"=>"example.org",
"CONTENT_TYPE"=>"application/x-www-form-urlencoded",
"rack.input" => StringIO.new('foo'),
"REQUEST_METHOD" => "POST",
"SERVER_NAME" => "example.org",
"SERVER_PORT" => "80",
"QUERY_STRING" => "",
"PATH_INFO" => "/foo",
"rack.url_scheme" => "http",
"SCRIPT_NAME" => "",
"CONTENT_LENGTH" => "0",
"HTTP_HOST" => "example.org",
"CONTENT_TYPE" => "application/x-www-form-urlencoded",
"HTTP_AUTHORIZATION" => "auth",
"HTTP_TOKEN" => "foo"
}
end

let(:expected_rack_env) do
{
"REQUEST_METHOD" => "POST",
"SERVER_NAME" => "example.org",
"SERVER_PORT" => "80",
"QUERY_STRING" => "",
"PATH_INFO" => "/foo",
"rack.url_scheme" => "http",
"SCRIPT_NAME" => "",
"CONTENT_LENGTH" => "0",
"HTTP_HOST" => "example.org",
"CONTENT_TYPE" => "application/x-www-form-urlencoded",
"HTTP_AUTHORIZATION" => "[Filtered]",
"HTTP_TOKEN" => "[Filtered]"
}
end

Expand All @@ -38,12 +57,10 @@ module Webmachine
subject { ConvertRequestToRackEnv.call(webmachine_request) }

describe ".call" do
it "" do
expected_env = rack_env.dup
expected_env.delete('rack.input')
it "returns a rack env hash created from the Webmachine::Request" do
actual_env = subject
actual_rack_input = actual_env.delete('rack.input')
expect(subject).to eq expected_env
expect(subject).to eq expected_rack_env
expect(actual_rack_input.string).to eq 'foo'
end
end
Expand Down

0 comments on commit 94f8f13

Please sign in to comment.