Skip to content

Commit

Permalink
fix: remove non UTF-8 chars from webhook response bodies before savin…
Browse files Browse the repository at this point in the history
…g the logs to the (UTF-8 encoded) database table
  • Loading branch information
bethesque committed Jan 15, 2018
1 parent 3a4339c commit 0ee5b0b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/pact_broker/domain/webhook_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ def log_response response, execution_logger
execution_logger.info "#{header.split("-").collect(&:capitalize).join('-')}: #{response[header]}"
end
logger.debug "body=#{response.body}"
execution_logger.info response.body
safe_body = nil
if response.body
safe_body = response.body.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
if response.body != safe_body
execution_logger.debug "Note that invalid UTF-8 byte sequences were removed from response body before saving the logs"
end
end
execution_logger.info safe_body
end

def log_completion_message options, execution_logger, success
Expand Down
19 changes: 19 additions & 0 deletions spec/lib/pact_broker/domain/webhook_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,25 @@ module Domain
end
end

context "when the response body contains a non UTF-8 character" do
let!(:http_request) do
stub_request(:post, "http://example.org/hook").
to_return(:status => 200, :body => "This has some \xC2 invalid chars")
end

it "removes the non UTF-8 characters before saving the logs so they don't blow up the database" do
result = subject.execute(pact, options)
expect(result.logs).to include "This has some invalid chars"
end

it "logs that it has cleaned the string to the execution logger" do
logger = double("logger").as_null_object
allow(Logger).to receive(:new).and_return(logger)
expect(logger).to receive(:debug).with(/Note that invalid UTF-8 byte sequences were removed/)
subject.execute(pact, options)
end
end

context "when an error occurs executing the request" do

class WebhookTestError < StandardError; end
Expand Down

0 comments on commit 0ee5b0b

Please sign in to comment.