Skip to content

Commit becf20c

Browse files
committed
fix: temporarily redact webhook response body from UI for security purposes
1 parent cf3bc3a commit becf20c

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

lib/pact_broker/api/decorators/webhook_execution_result_decorator.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,15 @@ def body
3434
represented.body
3535
end
3636
end
37-
3837
end
3938

40-
property :error, :extend => ErrorDecorator
41-
property :response, :extend => HTTPResponseDecorator
39+
property :message, exec_context: :decorator
40+
# property :error, :extend => ErrorDecorator
41+
# property :response, :extend => HTTPResponseDecorator
42+
43+
def message
44+
"Webhook response has been redacted temporarily for security purposes. Please see the Pact Broker application logs for the response body."
45+
end
4246

4347
link :webhook do | options |
4448
{

lib/pact_broker/domain/webhook_request.rb

+12-11
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,19 @@ def log_response response, execution_logger
114114
execution_logger.info(" ")
115115
logger.info "Received response for webhook #{uuid} status=#{response.code}"
116116
execution_logger.info "HTTP/#{response.http_version} #{response.code} #{response.message}"
117-
response.each_header do | header |
118-
execution_logger.info "#{header.split("-").collect(&:capitalize).join('-')}: #{response[header]}"
119-
end
117+
#response.each_header do | header |
118+
# execution_logger.info "#{header.split("-").collect(&:capitalize).join('-')}: #{response[header]}"
119+
#end
120120
logger.debug "body=#{response.body}"
121-
safe_body = nil
122-
if response.body
123-
safe_body = response.body.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
124-
if response.body != safe_body
125-
execution_logger.debug "Note that invalid UTF-8 byte sequences were removed from response body before saving the logs"
126-
end
127-
end
128-
execution_logger.info safe_body
121+
# safe_body = nil
122+
# if response.body
123+
# safe_body = response.body.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
124+
# if response.body != safe_body
125+
# execution_logger.debug "Note that invalid UTF-8 byte sequences were removed from response body before saving the logs"
126+
# end
127+
# end
128+
#execution_logger.info safe_body
129+
execution_logger.info "Webhook response has been redacted temporarily for security purposes. Please see the Pact Broker application logs for the response body."
129130
end
130131

131132
def log_completion_message options, execution_logger, success

spec/lib/pact_broker/api/decorators/webhook_execution_result_decorator_spec.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ module Decorators
3030
expect(subject[:_links][:webhook][:href]).to eq 'http://example.org/webhooks/some-uuid'
3131
end
3232

33-
context "when there is an error" do
33+
it "includes a message about the response being redacted" do
34+
expect(subject[:message]).to match /redacted/
35+
end
36+
37+
context "when there is an error", pending: "temporarily disabled" do
3438
let(:error) { double('error', message: 'message', backtrace: ['blah','blah']) }
3539

3640
it "includes the message" do
@@ -42,7 +46,7 @@ module Decorators
4246
end
4347
end
4448

45-
context "when there is a response" do
49+
context "when there is a response", pending: "temporarily disabled" do
4650
it "includes the response code" do
4751
expect(subject[:response][:status]).to eq 200
4852
end

spec/lib/pact_broker/domain/webhook_request_spec.rb

+16-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ module Domain
88
before do
99
allow(PactBroker::Api::PactBrokerUrls).to receive(:pact_url).and_return('http://example.org/pact-url')
1010
allow(PactBroker.configuration).to receive(:base_url).and_return('http://example.org')
11+
allow(PactBroker.logger).to receive(:info).and_call_original
12+
allow(PactBroker.logger).to receive(:debug).and_call_original
13+
allow(PactBroker.logger).to receive(:warn).and_call_original
1114
end
1215

1316
let(:username) { nil }
@@ -121,6 +124,14 @@ module Domain
121124
subject.execute(pact, options)
122125
end
123126

127+
it "does not write the response body to the exeuction log for security purposes" do
128+
expect(logs).to_not include "An error"
129+
end
130+
131+
it "logs a message about why there is no response information" do
132+
expect(logs).to include "Webhook response has been redacted temporarily for security purposes"
133+
end
134+
124135
describe "execution logs" do
125136

126137
it "logs the request method and path" do
@@ -143,12 +154,12 @@ module Domain
143154
expect(logs).to include "HTTP/1.0 200"
144155
end
145156

146-
it "logs the response headers" do
147-
expect(logs).to include "Content-Type: text/foo, blah"
157+
it "does not log the response headers" do
158+
expect(logs).to_not include "Content-Type: text/foo, blah"
148159
end
149160

150-
it "logs the response body" do
151-
expect(logs).to include "respbod"
161+
it "does not log the response body" do
162+
expect(logs).to_not include "respbod"
152163
end
153164

154165
context "when the response code is a success" do
@@ -280,7 +291,7 @@ module Domain
280291
end
281292
end
282293

283-
context "when the response body contains a non UTF-8 character" do
294+
context "when the response body contains a non UTF-8 character", pending: "execution logs disabled temporarily for security purposes" do
284295
let!(:http_request) do
285296
stub_request(:post, "http://example.org/hook").
286297
to_return(:status => 200, :body => "This has some \xC2 invalid chars")

0 commit comments

Comments
 (0)