Skip to content

Commit

Permalink
fix: use URL safe base64 encoding for pact metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Mar 29, 2021
1 parent 06bd6d3 commit fa1a333
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
19 changes: 5 additions & 14 deletions lib/pact_broker/api/pact_broker_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def pact_version_url_with_webhook_metadata pact, base_url = ''
end

def encode_metadata(metadata)
Base64.strict_encode64(Rack::Utils.build_nested_query(metadata))
Base64.urlsafe_encode64(Rack::Utils.build_nested_query(metadata), padding: false)
end

def decode_pact_metadata(metadata)
Expand Down Expand Up @@ -382,20 +382,11 @@ def provider_name(thing)
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)
""
Base64.urlsafe_decode64(metadata)
rescue StandardError => e
logger.warn("Exception parsing webhook metadata: '#{metadata}'", e)
""
end

def parse_nested_metadata_query(query)
Expand Down
12 changes: 10 additions & 2 deletions spec/lib/pact_broker/api/pact_broker_urls_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,16 @@ module Api
end
end

context "with padding" do
let(:encoded_metadata) { Base64.urlsafe_encode64("foo=bar", padding: true) }

it "can still handle it" do
expect(PactBrokerUrls.decode_pact_metadata(encoded_metadata)).to eq("foo" => "bar")
end
end

context "when the padding has been removed" do
let(:encoded_metadata) { Base64.strict_encode64("foo=bar").chomp("=") }
let(:encoded_metadata) { Base64.urlsafe_encode64("foo=bar", padding: false) }

it "can still handle it" do
expect(PactBrokerUrls.decode_pact_metadata(encoded_metadata)).to eq("foo" => "bar")
Expand All @@ -142,7 +150,7 @@ module Api
end

it "logs a warning" do
expect(logger).to receive(:info).with("ArgumentError parsing webhook metadata: '%'. Trying non strict decode.")
expect(logger).to receive(:warn).with("Exception parsing webhook metadata: '%'", ArgumentError)
PactBrokerUrls.decode_pact_metadata(encoded_metadata)
end
end
Expand Down

0 comments on commit fa1a333

Please sign in to comment.