Sha256: cfe9d2167d35a1cdd0b37206b663acc734352aa9595b1d63367c4cb9ad0b4677

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: false

require "json"

##
# Module responsible for sanitizing log data
module LogDataFilter
  DEFAULT_MAX_LENGTH = 512
  DEFAULT_SENSITIVE_FIELDS = [].freeze

  def self.config
    @config ||= begin
      file_path = "application.json"
      config = {
        max_length: DEFAULT_MAX_LENGTH,
        sensitive_fields: DEFAULT_SENSITIVE_FIELDS
      }

      if File.exist?(file_path)
        json = JSON.parse(File.read(file_path))

        if json["macaw"] && json["macaw"]["log"]
          log_config = json["macaw"]["log"]
          config[:max_length] = log_config["max_length"] if log_config["max_length"]
          config[:sensitive_fields] = log_config["sensitive_fields"] if log_config["sensitive_fields"]
        end
      end

      config
    end
  end

  def self.sanitize_for_logging(data, sensitive_fields: config[:sensitive_fields])
    return "" if data.nil?

    data = data.to_s.force_encoding("UTF-8")
    data = data.gsub(/[\x00-\x1F\x7F]/, "")
    data = data.gsub(/\s+/, " ")
    data = data.gsub("/", "")
    data = data.slice(0, config[:max_length])

    sensitive_fields.each do |field|
      next unless data.include?(field.to_s)

      data = data.gsub(/(#{Regexp.escape(field.to_s)}\s*[:=]\s*)([^\s]+)/) do |_match|
        "#{::Regexp.last_match(1)}#{Digest::SHA256.hexdigest(::Regexp.last_match(2))}"
      end
    end

    data
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
macaw_framework-1.1.3 lib/macaw_framework/data_filters/log_data_filter.rb
macaw_framework-1.1.2 lib/macaw_framework/data_filters/log_data_filter.rb
macaw_framework-1.1.1 lib/macaw_framework/data_filters/log_data_filter.rb
macaw_framework-1.1.0 lib/macaw_framework/data_filters/log_data_filter.rb