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), 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/i
Class Method Summary collapse
-
.normalize_header_value(value) ⇒ Object
Helper: convert arrays -> joined string, otherwise to_s.
- .parse_json(string) ⇒ Object
- .sanitize_headers(headers) ⇒ Object
- .sanitize_url(url) ⇒ Object
- .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:) ⇒ Object
Class Method Details
.normalize_header_value(value) ⇒ Object
Helper: convert arrays -> joined string, otherwise to_s
64 65 66 67 68 69 70 |
# File 'lib/coolhand/base_interceptor.rb', line 64 def normalize_header_value(value) if value.is_a?(Array) value.join(", ") else value.to_s end end |
.parse_json(string) ⇒ Object
125 126 127 128 129 |
# File 'lib/coolhand/base_interceptor.rb', line 125 def parse_json(string) JSON.parse(string) rescue JSON::ParserError, TypeError string end |
.sanitize_headers(headers) ⇒ Object
15 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 |
# File 'lib/coolhand/base_interceptor.rb', line 15 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/coolhand/base_interceptor.rb', line 72 def sanitize_url(url) uri = URI.parse(url) return url unless uri.query sensitive = %w[key api_key apikey token access_token secret] params = URI.decode_www_form(uri.query) redacted = false params.map! do |n, v| if sensitive.include?(n.downcase) 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:) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/coolhand/base_interceptor.rb', line 98 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:) request_data = { raw_request: { id: request_id, timestamp: start_time.iso8601, method: method.to_s.downcase, url: sanitize_url(url), headers: request_headers, request_body: request_body, response_headers: response_headers, response_body: response_body, status_code: status_code, duration_ms: duration_ms, completed_at: end_time.iso8601, is_streaming: is_streaming } } 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.}" end |