From 06bd6d3dc7e8859fb6afd5744104a6f5358a6a95 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 29 Mar 2021 08:47:13 +1100 Subject: [PATCH] fix: handle metadata that has had its padding removed --- lib/pact_broker/api/pact_broker_urls.rb | 33 +++++++++++++++---- .../pact_broker/api/pact_broker_urls_spec.rb | 16 +++++++-- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/lib/pact_broker/api/pact_broker_urls.rb b/lib/pact_broker/api/pact_broker_urls.rb index 4b9cffd4a..7fb87fcae 100644 --- a/lib/pact_broker/api/pact_broker_urls.rb +++ b/lib/pact_broker/api/pact_broker_urls.rb @@ -78,12 +78,7 @@ def encode_metadata(metadata) def decode_pact_metadata(metadata) if metadata && metadata != '' - begin - Rack::Utils.parse_nested_query(Base64.strict_decode64(metadata)) - rescue StandardError => e - logger.warn("Exception parsing webhook metadata: #{metadata}", e) - {} - end + parse_nested_metadata_query(base64_decode_metadata(metadata)) else {} end @@ -385,6 +380,32 @@ def provider_name(thing) nil end end + + def base64_decode_metadata(metadata) + Base64.strict_decode64(metadata) + # Some people remove the == padding on the end + rescue ArgumentError => e + logger.info("ArgumentError parsing webhook metadata: '#{metadata}'. Trying non strict decode.") + begin + Base64.decode64(metadata) + # Not sure that this can even happen, as invalid base64 chars are ignored + rescue StandardError => e + logger.warn("Exception parsing webhook metadata: '#{metadata}'", e) + "" + end + rescue StandardError => e + logger.warn("Exception parsing webhook metadata: '#{metadata}'", e) + "" + end + + def parse_nested_metadata_query(query) + begin + Rack::Utils.parse_nested_query(query) + rescue StandardError => e + logger.warn("Could not parse query: '#{query}'", e) + {} + end + end end end end diff --git a/spec/lib/pact_broker/api/pact_broker_urls_spec.rb b/spec/lib/pact_broker/api/pact_broker_urls_spec.rb index b1feab59c..e987f3864 100644 --- a/spec/lib/pact_broker/api/pact_broker_urls_spec.rb +++ b/spec/lib/pact_broker/api/pact_broker_urls_spec.rb @@ -126,14 +126,24 @@ module Api end end + context "when the padding has been removed" do + let(:encoded_metadata) { Base64.strict_encode64("foo=bar").chomp("=") } + + it "can still handle it" do + expect(PactBrokerUrls.decode_pact_metadata(encoded_metadata)).to eq("foo" => "bar") + end + end + context "when the metadata is not valid base64" do + let(:encoded_metadata) { "%" } + it "returns an empty hash" do - expect(PactBrokerUrls.decode_pact_metadata("foo==,")).to eq({}) + expect(PactBrokerUrls.decode_pact_metadata(encoded_metadata)).to eq({}) end it "logs a warning" do - expect(logger).to receive(:warn).with("Exception parsing webhook metadata: foo==,", ArgumentError) - PactBrokerUrls.decode_pact_metadata("foo==,") + expect(logger).to receive(:info).with("ArgumentError parsing webhook metadata: '%'. Trying non strict decode.") + PactBrokerUrls.decode_pact_metadata(encoded_metadata) end end end