From d5a4ebdcc20c7880b071dfdfa9523884bb5407e2 Mon Sep 17 00:00:00 2001 From: Iain Beeston Date: Fri, 30 Jun 2017 14:00:36 +0100 Subject: [PATCH] Data that looks like a URI but is invalid should be treated as a string There's a bug where if you pass a string as data and it looks like a URI, we try to parse it and load the data from that location. However, if parsing fails, we should give up trying to load remote data and treat the data as a string instead. --- CHANGELOG.md | 1 + lib/json-schema/validator.rb | 2 ++ test/initialize_data_test.rb | 2 ++ 3 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1be4860a..7dc7fb09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Rescue URI error when initializing a data string that contains a colon - Fragments with an odd number of components no longer raise an `undefined method `validate'` error +- Data that looks like a URI but is invalid is now treated as a string ## [2.8.0] - 2017-02-07 diff --git a/lib/json-schema/validator.rb b/lib/json-schema/validator.rb index c439b613..ecb1cf4e 100644 --- a/lib/json-schema/validator.rb +++ b/lib/json-schema/validator.rb @@ -594,6 +594,8 @@ def custom_open(uri) if uri.absolute? && Util::URI::SUPPORTED_PROTOCOLS.include?(uri.scheme) begin open(uri.to_s).read + rescue URI::InvalidURIError => e + raise JSON::Schema::UriError, e.message rescue OpenURI::HTTPError, Timeout::Error => e raise JSON::Schema::JsonLoadError, e.message end diff --git a/test/initialize_data_test.rb b/test/initialize_data_test.rb index cf08cbe1..46b4c601 100644 --- a/test/initialize_data_test.rb +++ b/test/initialize_data_test.rb @@ -109,6 +109,8 @@ def test_parse_invalid_uri_string end assert_raises(JSON::Schema::JsonLoadError) { JSON::Validator.validate(schema, data, :uri => true) } + + assert(JSON::Validator.validate(schema, "http://a/%%30%30")) end def test_parse_invalid_scheme_string