Skip to content

Commit 5787e0d

Browse files
committed
feat(webhooks): do not redact header if it contains a parameter
1 parent 81821a3 commit 5787e0d

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

lib/pact_broker/webhooks/render.rb

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ class Render
77
TEMPLATE_PARAMETER_REGEXP = /\$\{pactbroker\.[^\}]+\}/
88
DEFAULT_ESCAPER = lambda { |it| it }
99

10+
def self.includes_parameter?(value)
11+
value =~ TEMPLATE_PARAMETER_REGEXP
12+
end
13+
1014
def self.call(template, params, &escaper)
1115
render_template(escape_params(params, escaper || DEFAULT_ESCAPER), template)
1216
end

lib/pact_broker/webhooks/webhook_request_template.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def display_password
5353

5454
def redacted_headers
5555
headers.each_with_object({}) do | (name, value), new_headers |
56-
redact = HEADERS_TO_REDACT.any?{ | pattern | name =~ pattern }
56+
redact = HEADERS_TO_REDACT.any?{ | pattern | name =~ pattern } && !PactBroker::Webhooks::Render.includes_parameter?(value)
5757
new_headers[name] = redact ? "**********" : value
5858
end
5959
end

spec/lib/pact_broker/webhooks/webhook_request_template_spec.rb

+50-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Webhooks
1111
password: "password",
1212
uuid: "1234",
1313
body: body,
14-
headers: {'headername' => 'headervalue'}
14+
headers: headers
1515
}
1616
end
1717

@@ -27,6 +27,7 @@ module Webhooks
2727
}
2828
end
2929

30+
let(:headers) { {'headername' => 'headervalue'} }
3031
let(:url) { "http://example.org/hook?foo=bar" }
3132
let(:base_url) { "http://broker" }
3233
let(:built_url) { "http://example.org/hook?foo=barBUILT" }
@@ -107,6 +108,54 @@ module Webhooks
107108
end
108109
end
109110
end
111+
112+
describe "redacted_headers" do
113+
subject { WebhookRequestTemplate.new(attributes) }
114+
115+
let(:headers) do
116+
{
117+
'Authorization' => 'foo',
118+
'X-authorization' => 'bar',
119+
'Token' => 'bar',
120+
'X-Auth-Token' => 'bar',
121+
'X-Authorization-Token' => 'bar',
122+
'OK' => 'ok'
123+
}
124+
end
125+
126+
let(:expected_headers) do
127+
{
128+
'Authorization' => '**********',
129+
'X-authorization' => '**********',
130+
'Token' => '**********',
131+
'X-Auth-Token' => '**********',
132+
'X-Authorization-Token' => '**********',
133+
'OK' => 'ok'
134+
}
135+
end
136+
137+
it "redacts sensitive headers" do
138+
expect(subject.redacted_headers).to eq expected_headers
139+
end
140+
141+
context "when there is a parameter in the value" do
142+
let(:headers) do
143+
{
144+
'Authorization' => '${pactbroker.secret}'
145+
}
146+
end
147+
148+
let(:expected_headers) do
149+
{
150+
'Authorization' => '${pactbroker.secret}'
151+
}
152+
end
153+
154+
it "does not redact it" do
155+
expect(subject.redacted_headers).to eq expected_headers
156+
end
157+
end
158+
end
110159
end
111160
end
112161
end

0 commit comments

Comments
 (0)