Module: Dadata::SensitiveData

Included in:
ClientBase, Configuration, SecureLogger, SensitiveDataMiddleware
Defined in:
lib/dadata/sensitive_data.rb

Overview

The SensitiveData module provides functionality for sanitizing sensitive information in log messages and HTTP headers. It is used internally by the gem to ensure that sensitive data such as API keys and secrets are not exposed in logs.

Examples:

class MyLogger
  include SensitiveData

  def log_request(headers)
    puts sanitize_headers(headers)
  end
end

Constant Summary collapse

SENSITIVE_HEADERS =

List of headers that contain sensitive information and should be filtered

%w[Authorization X-Secret API-Key].freeze

Instance Method Summary collapse

Instance Method Details

#sanitize_headers(headers) ⇒ String

Sanitizes headers by replacing sensitive values with [FILTERED]

Examples:

headers = { 'API-Key' => 'secret', 'Content-Type' => 'application/json' }
sanitize_headers(headers) # => "API-Key: [FILTERED], Content-Type: application/json"

Parameters:

  • headers (Hash, nil)

    Headers to sanitize

Returns:

  • (String)

    Sanitized headers string



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dadata/sensitive_data.rb', line 27

def sanitize_headers(headers)
  return '' unless headers

  headers.map do |key, value|
    if SENSITIVE_HEADERS.include?(key)
      "#{key}: [FILTERED]"
    else
      "#{key}: #{value}"
    end
  end.join(', ')
end

#sanitize_message(msg) ⇒ String

Sanitizes a message by replacing sensitive information with [FILTERED]

Examples:

msg = "API-Key: secret123, Content-Type: application/json"
sanitize_message(msg) # => "API-Key: [FILTERED], Content-Type: application/json"

Parameters:

  • msg (String, nil)

    Message to sanitize

Returns:

  • (String)

    Sanitized message



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dadata/sensitive_data.rb', line 46

def sanitize_message(msg)
  return '' unless msg.is_a?(String)

  result = msg.dup
  SENSITIVE_HEADERS.each do |header|
    # Escape hyphens in the header name and handle any whitespace around the colon
    pattern = /#{Regexp.escape(header)}[\s]*:[\s]*[^\n,]+/
    result = result.gsub(pattern, "#{header}: [FILTERED]")
  end
  result
end