2016-06-02 11:05:42 +05:30
|
|
|
module Gitlab
|
|
|
|
class UrlSanitizer
|
|
|
|
def self.sanitize(content)
|
2017-08-17 22:00:37 +05:30
|
|
|
regexp = URI::Parser.new.make_regexp(%w(http https ssh git))
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
content.gsub(regexp) { |url| new(url).masked_url }
|
2016-08-24 12:49:21 +05:30
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
content.gsub(regexp, '')
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.valid?(url)
|
2017-08-17 22:00:37 +05:30
|
|
|
return false unless url
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
Addressable::URI.parse(url.strip)
|
|
|
|
|
|
|
|
true
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
false
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(url, credentials: nil)
|
2016-08-24 12:49:21 +05:30
|
|
|
@url = Addressable::URI.parse(url.strip)
|
2016-06-02 11:05:42 +05:30
|
|
|
@credentials = credentials
|
|
|
|
end
|
|
|
|
|
|
|
|
def sanitized_url
|
|
|
|
@sanitized_url ||= safe_url.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def masked_url
|
|
|
|
url = @url.dup
|
|
|
|
url.password = "*****" unless url.password.nil?
|
|
|
|
url.user = "*****" unless url.user.nil?
|
|
|
|
url.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def credentials
|
|
|
|
@credentials ||= { user: @url.user, password: @url.password }
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_url
|
|
|
|
@full_url ||= generate_full_url.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def generate_full_url
|
|
|
|
return @url unless valid_credentials?
|
|
|
|
@full_url = @url.dup
|
|
|
|
@full_url.user = credentials[:user]
|
|
|
|
@full_url.password = credentials[:password]
|
|
|
|
@full_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def safe_url
|
|
|
|
safe_url = @url.dup
|
|
|
|
safe_url.password = nil
|
|
|
|
safe_url.user = nil
|
|
|
|
safe_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_credentials?
|
|
|
|
credentials && credentials.is_a?(Hash) && credentials.any?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|