class Labkit::Logging::Sanitizer

Sanitizer provides log message sanitization, removing confidential information from log messages

Constants

ALLOWED_SCHEMES
SCP_ANCHORED_URL_REGEXP
SCP_URL_REGEXP
URL_REGEXP

Public Class Methods

mask_scp_url(scp_url) click to toggle source

Ensures that URLs of the form user:password@hostname:project.git are sanitized to hide credentials

# File lib/labkit/logging/sanitizer.rb, line 51
def self.mask_scp_url(scp_url)
  scp_url = scp_url.to_s.strip
  m = SCP_ANCHORED_URL_REGEXP.match(scp_url)
  return "" unless m

  password = m[2]
  host = m[3]
  path = m[4]

  return "*****@#{host}:#{path}" unless password.present?

  "*****:*****@#{host}:#{path}"
end
mask_url(url) click to toggle source

Ensures that URLS are sanitized to hide credentials

# File lib/labkit/logging/sanitizer.rb, line 38
def self.mask_url(url)
  url = url.to_s.strip
  p = URI::DEFAULT_PARSER.parse(url)

  p.password = "*****" if p.password.present?
  p.user = "*****" if p.user.present?
  p.to_s
rescue URI::InvalidURIError
  ""
end
sanitize_field(content) click to toggle source
# File lib/labkit/logging/sanitizer.rb, line 20
def self.sanitize_field(content)
  content = content.gsub(URL_REGEXP) { |url| mask_url(url) }
  content.gsub(SCP_URL_REGEXP) { |scp_url| mask_scp_url(scp_url) }
end
sanitize_sql(sql) click to toggle source
# File lib/labkit/logging/sanitizer.rb, line 25
def self.sanitize_sql(sql)
  PgQuery.normalize(sql)
rescue PgQuery::ParseError
  ""
end
sql_fingerprint(normalized_sql) click to toggle source
# File lib/labkit/logging/sanitizer.rb, line 31
def self.sql_fingerprint(normalized_sql)
  PgQuery.parse(normalized_sql)&.fingerprint
rescue PgQuery::ParseError
  ""
end