class ActiveRecord::OpenTracing::SqlSanitizer::Base

Constants

MAX_SQL_LENGTH

Public Instance Methods

sanitize(sql) click to toggle source
# File lib/active_record/open_tracing/sql_sanitizer/base.rb, line 10
def sanitize(sql)
  scrubbed = scrub(sql.dup)
  apply_substitutions(scrubbed)
end

Private Instance Methods

apply_substitutions(str) click to toggle source
# File lib/active_record/open_tracing/sql_sanitizer/base.rb, line 21
def apply_substitutions(str)
  substitutions.inject(str.dup) do |memo, (regex, replacement)|
    if replacement.respond_to?(:call)
      memo.gsub(regex, &replacement)
    else
      memo.gsub(regex, replacement)
    end
  end.strip
end
encodings?(encodings = %w[UTF-8 binary]) click to toggle source
# File lib/active_record/open_tracing/sql_sanitizer/base.rb, line 31
def encodings?(encodings = %w[UTF-8 binary])
  encodings.all? do |enc|
    begin
      Encoding.find(enc)
    rescue StandardError
      false
    end
  end
end
scrub(str) click to toggle source
# File lib/active_record/open_tracing/sql_sanitizer/base.rb, line 43
def scrub(str)
  # safeguard - don't sanitize or scrub large SQL statements
  return "" if !str.is_a?(String) || str.length > MAX_SQL_LENGTH

  # Whatever encoding it is, it is valid and we can operate on it
  return str if str.valid_encoding?

  # Prefer scrub over convert
  if str.respond_to?(:scrub)
    str.scrub("_")
  elsif encodings?(%w[UTF-8 binary])
    str.encode("UTF-8", "binary", invalid: :replace, undef: :replace, replace: "_")
  else
    # Unable to scrub invalid sql encoding, returning empty string
    ""
  end
end
substitutions() click to toggle source
# File lib/active_record/open_tracing/sql_sanitizer/base.rb, line 17
def substitutions
  raise NotImplementedError
end