Class: Labkit::Logging::Sanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/labkit/logging/sanitizer.rb

Constant Summary collapse

ALLOWED_SCHEMES =
%w[http https ssh git].freeze

Class Method Summary collapse

Class Method Details

.masked_url(url) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/labkit/logging/sanitizer.rb', line 14

def self.masked_url(url)
  url = url.to_s.strip
  url = Addressable::URI.parse(url)

  url.password = '*****' if url.password.present?
  url.user = '*****' if url.user.present?
  url.to_s
rescue Addressable::URI::InvalidURIError
  ''
end

.sanitize_field(content) ⇒ Object



8
9
10
11
12
# File 'lib/labkit/logging/sanitizer.rb', line 8

def self.sanitize_field(content)
  regexp = URI::DEFAULT_PARSER.make_regexp(ALLOWED_SCHEMES)

  content.gsub(regexp) { |url| masked_url(url) }
end