Skip to content

Commit

Permalink
feat(webhooks): do not redact header if it contains a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jul 19, 2019
1 parent 81821a3 commit 5787e0d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/pact_broker/webhooks/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class Render
TEMPLATE_PARAMETER_REGEXP = /\$\{pactbroker\.[^\}]+\}/
DEFAULT_ESCAPER = lambda { |it| it }

def self.includes_parameter?(value)
value =~ TEMPLATE_PARAMETER_REGEXP
end

def self.call(template, params, &escaper)
render_template(escape_params(params, escaper || DEFAULT_ESCAPER), template)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/webhooks/webhook_request_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def display_password

def redacted_headers
headers.each_with_object({}) do | (name, value), new_headers |
redact = HEADERS_TO_REDACT.any?{ | pattern | name =~ pattern }
redact = HEADERS_TO_REDACT.any?{ | pattern | name =~ pattern } && !PactBroker::Webhooks::Render.includes_parameter?(value)
new_headers[name] = redact ? "**********" : value
end
end
Expand Down
51 changes: 50 additions & 1 deletion spec/lib/pact_broker/webhooks/webhook_request_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Webhooks
password: "password",
uuid: "1234",
body: body,
headers: {'headername' => 'headervalue'}
headers: headers
}
end

Expand All @@ -27,6 +27,7 @@ module Webhooks
}
end

let(:headers) { {'headername' => 'headervalue'} }
let(:url) { "http://example.org/hook?foo=bar" }
let(:base_url) { "http://broker" }
let(:built_url) { "http://example.org/hook?foo=barBUILT" }
Expand Down Expand Up @@ -107,6 +108,54 @@ module Webhooks
end
end
end

describe "redacted_headers" do
subject { WebhookRequestTemplate.new(attributes) }

let(:headers) do
{
'Authorization' => 'foo',
'X-authorization' => 'bar',
'Token' => 'bar',
'X-Auth-Token' => 'bar',
'X-Authorization-Token' => 'bar',
'OK' => 'ok'
}
end

let(:expected_headers) do
{
'Authorization' => '**********',
'X-authorization' => '**********',
'Token' => '**********',
'X-Auth-Token' => '**********',
'X-Authorization-Token' => '**********',
'OK' => 'ok'
}
end

it "redacts sensitive headers" do
expect(subject.redacted_headers).to eq expected_headers
end

context "when there is a parameter in the value" do
let(:headers) do
{
'Authorization' => '${pactbroker.secret}'
}
end

let(:expected_headers) do
{
'Authorization' => '${pactbroker.secret}'
}
end

it "does not redact it" do
expect(subject.redacted_headers).to eq expected_headers
end
end
end
end
end
end

0 comments on commit 5787e0d

Please sign in to comment.