Skip to content

Commit fe05919

Browse files
committed
fix: ensure webhook hosts cannot contain templated parameters
1 parent c91d04e commit fe05919

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

lib/pact_broker/api/contracts/webhook_contract.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,21 @@ def allowed_webhook_host?(url)
9797
end
9898
end
9999

100+
def non_templated_host?(url)
101+
parse_uri(url).host == parse_uri(url, 'differentplaceholder').host
102+
end
103+
100104
def host_whitelist
101105
PactBroker.configuration.webhook_host_whitelist
102106
end
103107

104-
def parse_uri(uri_string)
105-
URI(uri_string.gsub(/\$\{pactbroker\.[^\}]+\}/, 'placeholder'))
108+
def parse_uri(uri_string, placeholder = 'placeholder')
109+
URI(uri_string.gsub(/\$\{pactbroker\.[^\}]+\}/, placeholder))
106110
end
107111
end
108112

109113
required(:http_method).filled(:valid_method?, :allowed_webhook_method?)
110-
required(:url).filled(:valid_url?, :allowed_webhook_scheme?, :allowed_webhook_host?)
114+
required(:url).filled(:valid_url?, :allowed_webhook_scheme?, :allowed_webhook_host?, :non_templated_host?)
111115
end
112116
end
113117

lib/pact_broker/locale/en.yml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ en:
77
valid_version_number?: "Version number '%{value}' cannot be parsed to a version number. The expected format (unless this configuration has been overridden) is a semantic version. eg. 1.3.0 or 2.0.4.rc1"
88
name_in_path_matches_name_in_pact?: "does not match %{left} name in path ('%{right}')."
99
valid_consumer_version_number?: "Consumer version number '%{value}' cannot be parsed to a version number. The expected format (unless this configuration has been overridden) is a semantic version. eg. 1.3.0 or 2.0.4.rc1"
10+
non_templated_host?: "cannot have a template parameter in the host"
1011

1112
pact_broker:
1213
messages:

spec/lib/pact_broker/api/contracts/webhook_contract_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ def valid_webhook_with
191191
expect(subject.errors).to be_empty
192192
end
193193
end
194+
195+
context "with a URL that has templated parameters in the host" do
196+
let(:json) do
197+
valid_webhook_with do |hash|
198+
hash['request']['url'] = 'https://${pactbroker.consumerVersionNumber}/commits'
199+
end
200+
end
201+
202+
it "contains an error" do
203+
expect(subject.errors[:"request.url"]).to eq ["cannot have a template parameter in the host"]
204+
end
205+
end
194206
end
195207
end
196208
end

0 commit comments

Comments
 (0)