Skip to content

Commit

Permalink
Use RFC2396 URI parser
Browse files Browse the repository at this point in the history
As a side-effect of updating activesupport to v8, the uri gem was added
with its latest version (1.0.2). Since v1 the default URI parser was
switched from RFC2396 to RFC3986.

Thus the following changes were done:
- URI::DEFAULT_PARSER replaced with URI::RFC2396_PARSER
- URI::REGEXP replaced with URI::RFC2396_REGEXP
- As some other gems (e.g. fog-local) wrongly assume that
  URI::DEFAULT_PARSER is the RFC2396 variant, the default parser was
  changed with a monkey patch.
  • Loading branch information
philippthun committed Jan 23, 2025
1 parent b30ea21 commit e7ec04b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/messages/validators/url_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module VCAP::CloudController::Validators
class UrlValidator < ActiveModel::Validator
def validate(record)
if URI::DEFAULT_PARSER.make_regexp(%w[https http]).match?(record.url.to_s)
if URI::RFC2396_PARSER.make_regexp(%w[https http]).match?(record.url.to_s)
record.errors.add(:url, 'must not contain authentication') if URI(record.url).user
else
record.errors.add(:url, "'#{record.url}' must be a valid url")
Expand Down
8 changes: 8 additions & 0 deletions lib/cloud_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,11 @@ module VCAP::CloudController; end
require 'cloud_controller/errands/rotate_database_key'

require 'services'

# Switch default URI parser globally as some other gems (e.g. fog-local) wrongly assume that URI::DEFAULT_PARSER is the RFC2396 variant.
module URI
remove_const(:DEFAULT_PARSER) if const_defined?(:DEFAULT_PARSER)
const_set(:DEFAULT_PARSER, RFC2396_PARSER)

self.parser = RFC2396_PARSER
end
2 changes: 1 addition & 1 deletion lib/cloud_controller/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def escape_userinfo(value)
end

def valid_in_userinfo?(value)
URI::REGEXP::PATTERN::USERINFO.match(value)
URI::RFC2396_REGEXP::PATTERN::USERINFO.match(value)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/sequel_plugins/vcap_validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module InstanceMethods
def validates_url(attr, opts={})
return unless send(attr)

validates_format(URI::DEFAULT_PARSER.make_regexp(%w[http https]), attr, message: opts.fetch(:message, :url))
validates_format(URI::RFC2396_PARSER.make_regexp(%w[http https]), attr, message: opts.fetch(:message, :url))
end
end
end
2 changes: 1 addition & 1 deletion lib/utils/uri_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module UriUtils
class InvalidDockerURI < StandardError; end

def self.is_uri?(candidate)
!!(candidate.is_a?(String) && /\A#{URI::DEFAULT_PARSER.make_regexp}\Z/ =~ candidate && URI(candidate))
!!(candidate.is_a?(String) && /\A#{URI::RFC2396_PARSER.make_regexp}\Z/ =~ candidate && URI(candidate))
rescue StandardError
false
end
Expand Down
6 changes: 3 additions & 3 deletions lib/vcap/rest_api/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def self.schema_doc(schema)
schema.deparse
end

URL = UrlDecorator.new(URI::DEFAULT_PARSER.make_regexp(%w[http https]))
HTTPS_URL = HttpsUrlDecorator.new(URI::DEFAULT_PARSER.make_regexp('https'))
GIT_URL = GitUrlDecorator.new(URI::DEFAULT_PARSER.make_regexp(%w[http https git]))
URL = UrlDecorator.new(URI::RFC2396_PARSER.make_regexp(%w[http https]))
HTTPS_URL = HttpsUrlDecorator.new(URI::RFC2396_PARSER.make_regexp('https'))
GIT_URL = GitUrlDecorator.new(URI::RFC2396_PARSER.make_regexp(%w[http https git]))

# The block will be evaluated in the context of the schema validator used
# by class `JsonMessage` viz. `Membrane`.
Expand Down

0 comments on commit e7ec04b

Please sign in to comment.