Skip to content

Commit

Permalink
fix: ensure non utf-8 characters in the webook response do not cause …
Browse files Browse the repository at this point in the history
…an error in the Pact Broker response body
  • Loading branch information
bethesque committed Jun 19, 2018
1 parent 273078b commit a5ae5bf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
33 changes: 25 additions & 8 deletions lib/pact_broker/domain/webhook_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'pact_broker/api/pact_broker_urls'
require 'pact_broker/build_http_options'
require 'cgi'
require 'delegate'

module PactBroker

Expand All @@ -22,6 +23,24 @@ def initialize message, response

end

class WebhookResponseWithUtf8SafeBody < SimpleDelegator
def body
if unsafe_body
unsafe_body.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
else
unsafe_body
end
end

def unsafe_body
__getobj__().body
end

def unsafe_body?
unsafe_body != body
end
end

class WebhookRequest

include PactBroker::Logging
Expand Down Expand Up @@ -110,9 +129,10 @@ def build_request uri, pact, verification, execution_logger
def do_request uri, req
logger.info "Making webhook #{uuid} request #{to_s}"
options = PactBroker::BuildHttpOptions.call(uri)
Net::HTTP.start(uri.hostname, uri.port, :ENV, options) do |http|
response = Net::HTTP.start(uri.hostname, uri.port, :ENV, options) do |http|
http.request req
end
WebhookResponseWithUtf8SafeBody.new(response)
end

def log_response response, execution_logger, options
Expand All @@ -131,7 +151,7 @@ def response_body_hidden_message
def log_response_to_application_logger response
logger.info "Received response for webhook #{uuid} status=#{response.code}"
logger.debug "headers=#{response.to_hash}"
logger.debug "body=#{response.body}"
logger.debug "body=#{response.unsafe_body}"
end

def log_response_to_execution_logger response, execution_logger
Expand All @@ -140,15 +160,12 @@ def log_response_to_execution_logger response, execution_logger
execution_logger.info "#{header.split("-").collect(&:capitalize).join('-')}: #{response[header]}"
end

safe_body = nil

if response.body
safe_body = response.body.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
if response.body != safe_body
if response.unsafe_body?
execution_logger.debug "Note that invalid UTF-8 byte sequences were removed from response body before saving the logs"
end
execution_logger.info response.body
end
execution_logger.info safe_body
end

def log_completion_message options, execution_logger, success
Expand Down Expand Up @@ -182,7 +199,7 @@ def http_request(uri)
end

def build_uri(pact, verification)
URI(PactBroker::Webhooks::Render.call(url, pact, verification){ | value | CGI::escape(value)} )
URI(PactBroker::Webhooks::Render.call(url, pact, verification){ | value | CGI::escape(value) if !value.nil? } )
end

def url_with_credentials pact, verification
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/pact_broker/domain/webhook_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ module Domain
end

it "sets the response on the result" do
expect(execute.response).to be_instance_of(Net::HTTPOK)
expect(execute.response).to be_instance_of(WebhookResponseWithUtf8SafeBody)
end
end

Expand All @@ -316,7 +316,7 @@ module Domain
end

it "sets the response on the result" do
expect(execute.response).to be_instance_of(Net::HTTPInternalServerError)
expect(execute.response).to be_instance_of(WebhookResponseWithUtf8SafeBody)
end
end

Expand Down

0 comments on commit a5ae5bf

Please sign in to comment.