Module: Coolhand::BaseInterceptor

Included in:
NetHttpInterceptor
Defined in:
lib/coolhand/base_interceptor.rb

Overview

Base module with common functionality for all interceptors

Constant Summary collapse

SENSITIVE_HEADER_PATTERN =

Matches any header whose name signals sensitive content, regardless of provider — covers known keys (x-api-key, x-goog-api-key, openai-api-key), AWS SigV4 session tokens (x-amz-security-token), session/CSRF cookies (cookie, set-cookie), and future/unknown providers using a similarly-named header. Shared with LoggerService so the two logging paths (interceptor + webhook forwarding) stay consistent.

/key|token|secret|signature|authorization|cookie/i
SENSITIVE_QUERY_PARAM_PATTERN =

Matches query param names that signal sensitive content, the same way SENSITIVE_HEADER_PATTERN does for headers — covers exact params this gem already knew about (key/token/secret) as well as presigned-URL credential params used by AWS (X-Amz-Signature, X-Amz-Credential, X-Amz-Security-Token) and Google Cloud (X-Goog-Signature, X-Goog-Credential) storage APIs.

/key|token|secret|sig|credential|password|auth/i

Class Method Summary collapse

Class Method Details

.normalize_header_value(value) ⇒ Object

Helper: convert arrays -> joined string, otherwise to_s



65
66
67
68
69
70
71
# File 'lib/coolhand/base_interceptor.rb', line 65

def normalize_header_value(value)
  if value.is_a?(Array)
    value.join(", ")
  else
    value.to_s
  end
end

.parse_json(string) ⇒ Object



135
136
137
138
139
# File 'lib/coolhand/base_interceptor.rb', line 135

def parse_json(string)
  JSON.parse(string)
rescue JSON::ParserError, TypeError
  string
end

.sanitize_headers(headers) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/coolhand/base_interceptor.rb', line 16

def sanitize_headers(headers)
  return {} if headers.nil?

  # Normalize various header-like objects into a Hash{String => String}
  raw = if headers.is_a?(Hash)
    headers.transform_keys(&:to_s).transform_values { |v| normalize_header_value(v) }
  elsif headers.respond_to?(:to_hash)
    begin
      headers.to_hash.transform_keys(&:to_s).transform_values { |v| normalize_header_value(v) }
    rescue StandardError
      # fall through to other enumeration strategies
      nil
    end
  elsif headers.respond_to?(:each_header)
    h = {}
    headers.each_header { |k, v| h[k.to_s] = normalize_header_value(v) }
    h
  elsif headers.respond_to?(:each)
    h = {}
    headers.each { |k, v| h[k.to_s] = normalize_header_value(v) }
    h
  else
    { "raw" => headers.to_s }
  end

  raw ||= {} # in case to_hash raised and nothing was built

  sanitized = raw.dup

  sanitized.each do |k, v|
    next if v.nil?

    key_down = k.to_s.downcase

    if key_down == "authorization"
      sanitized[k] = if v.to_s.match?(/\ABearer\s+/i)
        v.to_s.gsub(/\ABearer\s+.+/i, "Bearer [REDACTED]")
      else
        "[REDACTED]"
      end
    elsif key_down.match?(SENSITIVE_HEADER_PATTERN)
      sanitized[k] = "[REDACTED]"
    end
  end

  sanitized
end

.sanitize_url(url) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/coolhand/base_interceptor.rb', line 81

def sanitize_url(url)
  uri = URI.parse(url)
  return url unless uri.query

  params = URI.decode_www_form(uri.query)
  redacted = false
  params.map! do |n, v|
    if n.match?(SENSITIVE_QUERY_PARAM_PATTERN)
      redacted = true
      [n, "[REDACTED]"]
    else
      [n, v]
    end
  end

  if redacted
    uri.query = URI.encode_www_form(params)
    uri.to_s
  else
    url
  end
rescue URI::InvalidURIError
  url
end

.send_complete_request_log(request_id:, method:, url:, request_headers:, request_body:, response_headers:, response_body:, status_code:, start_time:, end_time:, duration_ms:, is_streaming:, source_api: nil, model: nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/coolhand/base_interceptor.rb', line 106

def send_complete_request_log(request_id:, method:, url:, request_headers:, request_body:, response_headers:,
  response_body:, status_code:, start_time:, end_time:, duration_ms:, is_streaming:, source_api: nil, model: nil)
  raw_request = {
    id: request_id,
    timestamp: start_time.iso8601,
    method: method.to_s.downcase,
    url: sanitize_url(url),
    headers: sanitize_headers(request_headers),
    request_body: request_body,
    response_headers: sanitize_headers(response_headers),
    response_body: response_body,
    status_code: status_code,
    duration_ms: duration_ms,
    completed_at: end_time.iso8601,
    is_streaming: is_streaming
  }
  raw_request[:source_api] = source_api if Coolhand.required_field?(source_api)
  raw_request[:model] = model if Coolhand.required_field?(model)

  request_data = { raw_request: raw_request }

  api_service = Coolhand::ApiService.new
  api_service.send_llm_request_log(request_data)

  Coolhand.log "📤 Sent complete request/response log for #{request_id} (duration: #{duration_ms}ms)"
rescue StandardError => e
  Coolhand.log "❌ Error sending complete request log: #{e.message}"
end