Module: GetStreamRuby::LogRedaction
- Defined in:
- lib/getstream_ruby/log_redaction.rb
Overview
Redaction helpers for the SDK's structured log events. Shallow by design.
Constant Summary collapse
- REDACTED =
'<redacted>'- QUERY_PARAMS =
%w[api_key api_secret token].freeze
- BODY_KEYS =
%w[api_secret token password].freeze
- MESSAGE_SECRET_PATTERN =
Matches
key=valuefor the secret query params wherever they appear in a free-form string (e.g. a transport error message that embeds the request URL), value runs up to the next&, whitespace, or end of string. /\b(#{QUERY_PARAMS.join('|')})=[^&\s]*/i.freeze
- CONTROL_CHARS =
Control characters (C0, DEL, C1) plus the Unicode line/paragraph separators: anything a log sink or viewer may treat as a line break or terminal escape.
/[[:cntrl:] ]/.freeze
Class Method Summary collapse
- .redact_json_body(body) ⇒ Object
-
.redact_message(string) ⇒ Object
Redacts secret query-parameter values wherever they appear in a free string (e.g.
error.messagefrom a transport exception, which may embed the full request URL). - .redact_query(params) ⇒ Object
-
.sanitize(value) ⇒ Object
Makes a log line single-line and inert: every control character is replaced by its escaped form (CR ->
\r, LF ->\n, ESC ->\e, ...), so a value echoed into a log event (path, query, error message, body) can't forge extra log lines or inject terminal escape sequences.
Class Method Details
.redact_json_body(body) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/getstream_ruby/log_redaction.rb', line 30 def redact_json_body(body) return body if body.nil? || body.empty? data = JSON.parse(body) return body unless data.is_a?(Hash) changed = false BODY_KEYS.each do |key| if data.key?(key) data[key] = REDACTED changed = true end end changed ? JSON.generate(data) : body rescue JSON::ParserError body end |
.redact_message(string) ⇒ Object
Redacts secret query-parameter values wherever they appear in a free
string (e.g. error.message from a transport exception, which may embed
the full request URL). Case-insensitive on the key; the value is
replaced regardless of its own case.
54 55 56 57 58 |
# File 'lib/getstream_ruby/log_redaction.rb', line 54 def (string) return string if string.nil? || string.empty? string.gsub(MESSAGE_SECRET_PATTERN) { "#{Regexp.last_match(1)}=#{REDACTED}" } end |
.redact_query(params) ⇒ Object
24 25 26 27 28 |
# File 'lib/getstream_ruby/log_redaction.rb', line 24 def redact_query(params) return params if params.nil? || params.empty? params.to_h { |k, v| [k, QUERY_PARAMS.include?(k.to_s.downcase) ? REDACTED : v] } end |
.sanitize(value) ⇒ Object
Makes a log line single-line and inert: every control character is
replaced by its escaped form (CR -> \r, LF -> \n, ESC -> \e, ...),
so a value echoed into a log event (path, query, error message, body)
can't forge extra log lines or inject terminal escape sequences.
Non-UTF-8 input (e.g. a binary response body) is scrubbed first so the
match itself can't raise.
66 67 68 69 70 71 |
# File 'lib/getstream_ruby/log_redaction.rb', line 66 def sanitize(value) string = value.to_s string = string.dup.force_encoding(Encoding::UTF_8) unless string.encoding == Encoding::UTF_8 string = string.scrub unless string.valid_encoding? string.gsub(CONTROL_CHARS) { |char| char.dump[1..-2] } end |