2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-21 08:15:51 -04:00
|
|
|
module Gitlab
|
2016-05-18 22:16:36 -04:00
|
|
|
class UrlSanitizer
|
2017-10-02 02:45:58 -04:00
|
|
|
ALLOWED_SCHEMES = %w[http https ssh git].freeze
|
2017-09-29 09:45:00 -04:00
|
|
|
|
2016-05-18 22:16:36 -04:00
|
|
|
def self.sanitize(content)
|
2018-09-21 02:55:02 -04:00
|
|
|
regexp = URI::DEFAULT_PARSER.make_regexp(ALLOWED_SCHEMES)
|
2016-05-18 22:16:36 -04:00
|
|
|
|
|
|
|
content.gsub(regexp) { |url| new(url).masked_url }
|
2016-07-11 03:01:09 -04:00
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
content.gsub(regexp, '')
|
2016-05-18 22:16:36 -04:00
|
|
|
end
|
|
|
|
|
2016-06-30 08:30:07 -04:00
|
|
|
def self.valid?(url)
|
2017-09-06 07:29:14 -04:00
|
|
|
return false unless url.present?
|
2018-12-09 02:23:39 -05:00
|
|
|
return false unless url.is_a?(String)
|
2017-03-09 10:39:09 -05:00
|
|
|
|
2017-09-29 09:45:00 -04:00
|
|
|
uri = Addressable::URI.parse(url.strip)
|
2016-06-30 08:30:07 -04:00
|
|
|
|
2017-09-29 09:45:00 -04:00
|
|
|
ALLOWED_SCHEMES.include?(uri.scheme)
|
2016-06-30 08:30:07 -04:00
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2016-03-21 10:11:05 -04:00
|
|
|
def initialize(url, credentials: nil)
|
2017-09-06 10:20:25 -04:00
|
|
|
%i[user password].each do |symbol|
|
|
|
|
credentials[symbol] = credentials[symbol].presence if credentials&.key?(symbol)
|
|
|
|
end
|
|
|
|
|
2016-03-21 10:11:05 -04:00
|
|
|
@credentials = credentials
|
2017-09-08 10:48:10 -04:00
|
|
|
@url = parse_url(url)
|
2016-03-21 08:15:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def sanitized_url
|
|
|
|
@sanitized_url ||= safe_url.to_s
|
|
|
|
end
|
|
|
|
|
2016-05-18 22:16:36 -04:00
|
|
|
def masked_url
|
|
|
|
url = @url.dup
|
2017-09-06 07:29:14 -04:00
|
|
|
url.password = "*****" if url.password.present?
|
|
|
|
url.user = "*****" if url.user.present?
|
2016-05-18 22:16:36 -04:00
|
|
|
url.to_s
|
|
|
|
end
|
|
|
|
|
2016-03-21 08:15:51 -04:00
|
|
|
def credentials
|
2017-09-06 07:29:14 -04:00
|
|
|
@credentials ||= { user: @url.user.presence, password: @url.password.presence }
|
2016-03-21 08:15:51 -04:00
|
|
|
end
|
|
|
|
|
2016-03-21 10:11:05 -04:00
|
|
|
def full_url
|
|
|
|
@full_url ||= generate_full_url.to_s
|
|
|
|
end
|
|
|
|
|
2016-03-21 08:15:51 -04:00
|
|
|
private
|
|
|
|
|
2017-09-08 10:48:10 -04:00
|
|
|
def parse_url(url)
|
|
|
|
url = url.to_s.strip
|
|
|
|
match = url.match(%r{\A(?:git|ssh|http(?:s?))\://(?:(.+)(?:@))?(.+)})
|
|
|
|
raw_credentials = match[1] if match
|
|
|
|
|
|
|
|
if raw_credentials.present?
|
|
|
|
url.sub!("#{raw_credentials}@", '')
|
|
|
|
|
2018-07-10 16:00:21 -04:00
|
|
|
user, _, password = raw_credentials.partition(':')
|
2017-09-08 10:48:10 -04:00
|
|
|
@credentials ||= { user: user.presence, password: password.presence }
|
|
|
|
end
|
|
|
|
|
|
|
|
url = Addressable::URI.parse(url)
|
|
|
|
url.password = password if password.present?
|
|
|
|
url.user = user if user.present?
|
|
|
|
url
|
|
|
|
end
|
|
|
|
|
2016-03-21 10:11:05 -04:00
|
|
|
def generate_full_url
|
2016-03-29 09:23:32 -04:00
|
|
|
return @url unless valid_credentials?
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2018-07-18 01:50:08 -04:00
|
|
|
@url.dup.tap do |generated|
|
|
|
|
generated.password = encode_percent(credentials[:password]) if credentials[:password].present?
|
|
|
|
generated.user = encode_percent(credentials[:user]) if credentials[:user].present?
|
|
|
|
end
|
2016-03-21 10:11:05 -04:00
|
|
|
end
|
|
|
|
|
2016-03-21 08:15:51 -04:00
|
|
|
def safe_url
|
|
|
|
safe_url = @url.dup
|
|
|
|
safe_url.password = nil
|
|
|
|
safe_url.user = nil
|
|
|
|
safe_url
|
|
|
|
end
|
2016-03-29 09:23:32 -04:00
|
|
|
|
|
|
|
def valid_credentials?
|
|
|
|
credentials && credentials.is_a?(Hash) && credentials.any?
|
|
|
|
end
|
2018-07-18 01:50:08 -04:00
|
|
|
|
|
|
|
def encode_percent(string)
|
|
|
|
# CGI.escape converts spaces to +, but this doesn't work for git clone
|
|
|
|
CGI.escape(string).gsub('+', '%20')
|
|
|
|
end
|
2016-03-21 08:15:51 -04:00
|
|
|
end
|
2016-03-21 12:29:19 -04:00
|
|
|
end
|