2016-03-21 08:15:51 -04:00
|
|
|
module Gitlab
|
2016-05-18 22:16:36 -04:00
|
|
|
class UrlSanitizer
|
|
|
|
def self.sanitize(content)
|
2017-02-22 12:46:57 -05:00
|
|
|
regexp = URI::Parser.new.make_regexp(%w(http https ssh git))
|
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-03-09 10:39:09 -05:00
|
|
|
return false unless url
|
|
|
|
|
2016-06-30 08:30:07 -04:00
|
|
|
Addressable::URI.parse(url.strip)
|
|
|
|
|
|
|
|
true
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2016-03-21 10:11:05 -04:00
|
|
|
def initialize(url, credentials: nil)
|
2016-06-30 07:17:37 -04:00
|
|
|
@url = Addressable::URI.parse(url.strip)
|
2016-03-21 10:11:05 -04:00
|
|
|
@credentials = credentials
|
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
|
|
|
|
url.password = "*****" unless url.password.nil?
|
|
|
|
url.user = "*****" unless url.user.nil?
|
|
|
|
url.to_s
|
|
|
|
end
|
|
|
|
|
2016-03-21 08:15:51 -04:00
|
|
|
def credentials
|
|
|
|
@credentials ||= { user: @url.user, password: @url.password }
|
|
|
|
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
|
|
|
|
|
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?
|
2016-03-21 10:11:05 -04:00
|
|
|
@full_url = @url.dup
|
2016-03-21 13:09:47 -04:00
|
|
|
@full_url.user = credentials[:user]
|
|
|
|
@full_url.password = credentials[:password]
|
2016-03-21 10:11:05 -04:00
|
|
|
@full_url
|
|
|
|
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
|
2016-03-21 08:15:51 -04:00
|
|
|
end
|
2016-03-21 12:29:19 -04:00
|
|
|
end
|