Skip to content

Commit

Permalink
fix: handle metadata that has had its padding removed
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Mar 28, 2021
1 parent dbb967e commit 06bd6d3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
33 changes: 27 additions & 6 deletions lib/pact_broker/api/pact_broker_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
16 changes: 13 additions & 3 deletions spec/lib/pact_broker/api/pact_broker_urls_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 06bd6d3

Please sign in to comment.