Sha256: 161562f4de11ab934e828ee5a8387f7e129d8fced15775c6a1ea6649ec990be7

Contents?: true

Size: 1.67 KB

Versions: 17

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  module OpenTracing
    module SqlSanitizer
      class Base
        require "active_record/open_tracing/sql_sanitizer/regexes"
        include ActiveRecord::OpenTracing::SqlSanitizer::Regexes

        def sanitize(sql)
          scrubbed = scrub(sql.dup)
          apply_substitutions(scrubbed)
        end

        private

        def substitutions
          raise NotImplementedError
        end

        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

        def encodings?(encodings = %w[UTF-8 binary])
          encodings.all? do |enc|
            begin
              Encoding.find(enc)
            rescue StandardError
              false
            end
          end
        end

        MAX_SQL_LENGTH = 16384

        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
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
activerecord-instrumentation-0.6.2 lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.6.1 lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.6.1.pre lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.6.0 lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.6.0.jhemphill4 lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.6.0.jhemphill3 lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.6.0.jhemphill2 lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.6.0.jhemphill1 lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.5.2 lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.5.2.pre lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.5.1 lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.5.1.pre lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.4.0 lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.4.0.jlauer4 lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.4.0.jlauer3 lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.4.0.jlauer2 lib/active_record/open_tracing/sql_sanitizer/base.rb
activerecord-instrumentation-0.4.0.jlauer1 lib/active_record/open_tracing/sql_sanitizer/base.rb