diff options
Diffstat (limited to 'lib/rubygems/uri.rb')
-rw-r--r-- | lib/rubygems/uri.rb | 75 |
1 files changed, 45 insertions, 30 deletions
diff --git a/lib/rubygems/uri.rb b/lib/rubygems/uri.rb index ba30fac2f5..a44aaceba5 100644 --- a/lib/rubygems/uri.rb +++ b/lib/rubygems/uri.rb @@ -5,6 +5,44 @@ # class Gem::Uri + ## + # Parses and redacts uri + + def self.redact(uri) + new(uri).redacted + end + + ## + # Parses uri, raising if it's invalid + + def self.parse!(uri) + require_relative "vendor/uri/lib/uri" + + raise Gem::URI::InvalidURIError unless uri + + return uri unless uri.is_a?(String) + + # Always escape URI's to deal with potential spaces and such + # It should also be considered that source_uri may already be + # a valid URI with escaped characters. e.g. "{DESede}" is encoded + # as "%7BDESede%7D". If this is escaped again the percentage + # symbols will be escaped. + begin + Gem::URI.parse(uri) + rescue Gem::URI::InvalidURIError + Gem::URI.parse(Gem::URI::DEFAULT_PARSER.escape(uri)) + end + end + + ## + # Parses uri, returning the original uri if it's invalid + + def self.parse(uri) + parse!(uri) + rescue Gem::URI::InvalidURIError + uri + end + def initialize(source_uri) @parsed_uri = parse(source_uri) end @@ -26,9 +64,9 @@ class Gem::Uri end def redact_credentials_from(text) - return text unless valid_uri? && password? + return text unless valid_uri? && password? && text.include?(to_s) - text.sub(password, 'REDACTED') + text.sub(password, "REDACTED") end def method_missing(method_name, *args, &blk) @@ -50,43 +88,20 @@ class Gem::Uri private - ## - # Parses the #uri, raising if it's invalid - def parse!(uri) - require "uri" - - raise URI::InvalidURIError unless uri - - # Always escape URI's to deal with potential spaces and such - # It should also be considered that source_uri may already be - # a valid URI with escaped characters. e.g. "{DESede}" is encoded - # as "%7BDESede%7D". If this is escaped again the percentage - # symbols will be escaped. - begin - URI.parse(uri) - rescue URI::InvalidURIError - URI.parse(URI::DEFAULT_PARSER.escape(uri)) - end + self.class.parse!(uri) end - ## - # Parses the #uri, returning the original uri if it's invalid - def parse(uri) - return uri unless uri.is_a?(String) - - parse!(uri) - rescue URI::InvalidURIError - uri + self.class.parse(uri) end def with_redacted_user - clone.tap {|uri| uri.user = 'REDACTED' } + clone.tap {|uri| uri.user = "REDACTED" } end def with_redacted_password - clone.tap {|uri| uri.password = 'REDACTED' } + clone.tap {|uri| uri.password = "REDACTED" } end def valid_uri? @@ -98,7 +113,7 @@ class Gem::Uri end def oauth_basic? - password == 'x-oauth-basic' + password == "x-oauth-basic" end def token? |