Skip to content

Commit

Permalink
Fix FrozenError (can't modify frozen String: "[FILTERED]") (#199)
Browse files Browse the repository at this point in the history
* + reproduce issues in a spec example

* + do not scrub bearer token or basic credentials when already the whole authorization header is scrubbed

* + bump version
  • Loading branch information
Franca Rast authored May 12, 2021
1 parent 59e417f commit c654c88
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
16 changes: 14 additions & 2 deletions lib/lhc/scrubbers/headers_scrubber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,26 @@ def scrub_auth_headers!
end

def scrub_basic_authentication_headers!
return if auth_options[:basic].blank? || scrubbed['Authorization'].blank?
return if !scrub_basic_authentication_headers?

scrubbed['Authorization'].gsub!(auth_options[:basic][:base_64_encoded_credentials], SCRUB_DISPLAY)
end

def scrub_bearer_authentication_headers!
return if auth_options[:bearer].blank? || scrubbed['Authorization'].blank?
return if !scrub_bearer_authentication_headers?

scrubbed['Authorization'].gsub!(auth_options[:bearer_token], SCRUB_DISPLAY)
end

def scrub_basic_authentication_headers?
auth_options[:basic].present? &&
scrubbed['Authorization'].present? &&
scrubbed['Authorization'].include?(auth_options[:basic][:base_64_encoded_credentials])
end

def scrub_bearer_authentication_headers?
auth_options[:bearer].present? &&
scrubbed['Authorization'].present? &&
scrubbed['Authorization'].include?(auth_options[:bearer_token])
end
end
2 changes: 1 addition & 1 deletion lib/lhc/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module LHC
VERSION ||= '15.0.0'
VERSION ||= '15.0.1'
end
34 changes: 18 additions & 16 deletions spec/request/scrubbed_headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,20 @@
let(:authorization_header) { { 'Authorization' => "Bearer #{bearer_token}" } }
let(:auth) { { bearer: -> { bearer_token } } }

it 'provides srubbed request headers' do
it 'scrubs only the bearer token' do
expect(request.scrubbed_headers).to include('Authorization' => "Bearer #{LHC::Scrubber::SCRUB_DISPLAY}")
expect(request.headers).to include(authorization_header)
end

context 'when nothing should get scrubbed' do
before :each do
LHC.config.scrubs = {}
end
it 'scrubs whole "Authorization" header' do
LHC.config.scrubs[:headers] << 'Authorization'
expect(request.scrubbed_headers).to include('Authorization' => LHC::Scrubber::SCRUB_DISPLAY)
expect(request.headers).to include(authorization_header)
end

it 'does not filter beaerer auth' do
expect(request.scrubbed_headers).to include(authorization_header)
end
it 'scrubs nothing' do
LHC.config.scrubs = {}
expect(request.scrubbed_headers).to include(authorization_header)
end
end

Expand All @@ -82,19 +83,20 @@
let(:authorization_header) { { 'Authorization' => "Basic #{credentials_base_64_codiert}" } }
let(:auth) { { basic: { username: username, password: password } } }

it 'provides srubbed request headers' do
it 'scrubs only credentials' do
expect(request.scrubbed_headers).to include('Authorization' => "Basic #{LHC::Scrubber::SCRUB_DISPLAY}")
expect(request.headers).to include(authorization_header)
end

context 'when nothing should get scrubbed' do
before :each do
LHC.config.scrubs = {}
end
it 'scrubs whole "Authorization" header' do
LHC.config.scrubs[:headers] << 'Authorization'
expect(request.scrubbed_headers).to include('Authorization' => LHC::Scrubber::SCRUB_DISPLAY)
expect(request.headers).to include(authorization_header)
end

it 'does not filter basic auth' do
expect(request.scrubbed_headers).to include(authorization_header)
end
it 'scrubs nothing' do
LHC.config.scrubs = {}
expect(request.scrubbed_headers).to include(authorization_header)
end
end
end
Expand Down

0 comments on commit c654c88

Please sign in to comment.