Skip to content

Commit

Permalink
fix: gracefully handle corrupt webhook metadata in pact URL
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Sep 21, 2020
1 parent 884f579 commit ba94c35
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/pact_broker/api/pact_broker_urls.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require 'erb'
require 'pact_broker/pacts/metadata'
require 'pact_broker/logging'

module PactBroker
module Api
module PactBrokerUrls

include PactBroker::Pacts::Metadata
include PactBroker::Logging
# TODO make base_url the last and optional argument for all methods, defaulting to ''

extend self
Expand Down Expand Up @@ -70,8 +72,13 @@ def encode_webhook_metadata(metadata)

def decode_webhook_metadata(metadata)
if metadata
Rack::Utils.parse_nested_query(Base64.strict_decode64(metadata)).each_with_object({}) do | (k, v), new_hash |
new_hash[k.to_sym] = v
begin
Rack::Utils.parse_nested_query(Base64.strict_decode64(metadata)).each_with_object({}) do | (k, v), new_hash |
new_hash[k.to_sym] = v
end
rescue StandardError => e
logger.warn("Exception parsing webhook metadata: #{metadata}", e)
{}
end
else
{}
Expand Down
15 changes: 15 additions & 0 deletions spec/lib/pact_broker/api/pact_broker_urls_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
module PactBroker
module Api
describe PactBrokerUrls do
before do
allow(PactBrokerUrls).to receive(:logger).and_return(logger)
end
let(:logger) { double('logger').as_null_object }

# Regex find all the URL parameter names
# \/\{[^\}\s\[\(\.]+\}
Expand Down Expand Up @@ -117,6 +121,17 @@ module Api
expect(PactBrokerUrls.decode_webhook_metadata(nil)).to eq({})
end
end

context "when the metadata is not valid base64" do
it "returns an empty hash" do
expect(PactBrokerUrls.decode_webhook_metadata("foo==,")).to eq({})
end

it "logs a warning" do
expect(logger).to receive(:warn).with("Exception parsing webhook metadata: foo==,", ArgumentError)
PactBrokerUrls.decode_webhook_metadata("foo==,")
end
end
end

describe "latest_verification_for_pact_url" do
Expand Down

0 comments on commit ba94c35

Please sign in to comment.