From b0bb6044f02e218fe27b24b9fa38d0523474b3df Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 23 Aug 2018 20:04:44 +1000 Subject: [PATCH] fix: gracefully handle scenario where URL supplied in JSON body is not a String --- lib/pact_broker/api/contracts/request_validations.rb | 2 +- lib/pact_broker/api/contracts/verification_contract.rb | 2 +- lib/pact_broker/api/contracts/webhook_contract.rb | 4 ++-- lib/rack/pact_broker/invalid_uri_protection.rb | 2 +- .../api/contracts/verification_contract_spec.rb | 8 ++++++++ 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/pact_broker/api/contracts/request_validations.rb b/lib/pact_broker/api/contracts/request_validations.rb index af3c965c8..35520c837 100644 --- a/lib/pact_broker/api/contracts/request_validations.rb +++ b/lib/pact_broker/api/contracts/request_validations.rb @@ -25,7 +25,7 @@ def url_valid? def uri URI(url) - rescue URI::InvalidURIError + rescue URI::InvalidURIError, ArgumentError nil end end diff --git a/lib/pact_broker/api/contracts/verification_contract.rb b/lib/pact_broker/api/contracts/verification_contract.rb index 1bb52216a..742c1fae8 100644 --- a/lib/pact_broker/api/contracts/verification_contract.rb +++ b/lib/pact_broker/api/contracts/verification_contract.rb @@ -21,7 +21,7 @@ def not_blank? value def valid_url? url URI(url) - rescue URI::InvalidURIError + rescue URI::InvalidURIError, ArgumentError nil end diff --git a/lib/pact_broker/api/contracts/webhook_contract.rb b/lib/pact_broker/api/contracts/webhook_contract.rb index 20eecc5aa..2557b9598 100644 --- a/lib/pact_broker/api/contracts/webhook_contract.rb +++ b/lib/pact_broker/api/contracts/webhook_contract.rb @@ -109,8 +109,8 @@ def valid_method?(http_method) def valid_url?(url) uri = parse_uri(url) uri.scheme && uri.host - rescue URI::InvalidURIError - false + rescue URI::InvalidURIError, ArgumentError + nil end def allowed_webhook_method?(http_method) diff --git a/lib/rack/pact_broker/invalid_uri_protection.rb b/lib/rack/pact_broker/invalid_uri_protection.rb index 680aa109d..02e44849c 100644 --- a/lib/rack/pact_broker/invalid_uri_protection.rb +++ b/lib/rack/pact_broker/invalid_uri_protection.rb @@ -23,7 +23,7 @@ def valid_uri? env begin parse(::Rack::Request.new(env).url) true - rescue URI::InvalidURIError + rescue URI::InvalidURIError, ArgumentError false end end diff --git a/spec/lib/pact_broker/api/contracts/verification_contract_spec.rb b/spec/lib/pact_broker/api/contracts/verification_contract_spec.rb index e05cee229..44f056393 100644 --- a/spec/lib/pact_broker/api/contracts/verification_contract_spec.rb +++ b/spec/lib/pact_broker/api/contracts/verification_contract_spec.rb @@ -64,6 +64,14 @@ def modify hash, options its(:errors) { is_expected.to be_empty } end + context "when the buildURL is not stringable" do + let(:build_url) { {} } + + it "has an error" do + expect(subject.errors[:build_url]).to include(match("URL")) + end + end + context "when the providerApplicationVersion is not present" do let(:params) { modify valid_params, without: :providerApplicationVersion } it "has an error" do