From b1eef55bf0bccab649c8f35d04dffd6ddb4a77ab Mon Sep 17 00:00:00 2001 From: Zeke Gabrielse Date: Fri, 25 Oct 2024 15:41:44 -0500 Subject: [PATCH] update request logger to ignore binary --- app/controllers/concerns/request_logger.rb | 24 ++++++++++++++-------- config/initializers/mime_types.rb | 6 ++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/app/controllers/concerns/request_logger.rb b/app/controllers/concerns/request_logger.rb index ef83868e0a..e1af02accd 100644 --- a/app/controllers/concerns/request_logger.rb +++ b/app/controllers/concerns/request_logger.rb @@ -195,14 +195,22 @@ def request_log_response_body return unless body.present? - return body unless - response.content_type&.include?('json') - - params = JSON.parse(body) - filterer = ActiveSupport::ParameterFilter.new(%i[password digest token secret otp redirect]) - filtered = filterer.filter(params) - - filtered.to_json + mime_type, * = Mime::Type.parse(response.content_type.to_s) + filtered = case mime_type + in symbol: :jsonapi | :json + filterer = ActiveSupport::ParameterFilter.new(%i[password digest token secret otp redirect auth]) + params = JSON.parse(body) + + filterer.filter(params).to_json + in symbol: :text | :html + body + in symbol: :binary + nil # we don't ever want to store binary + else + nil + end + + filtered rescue => e Keygen.logger.exception(e) end diff --git a/config/initializers/mime_types.rb b/config/initializers/mime_types.rb index 622066223f..89eccd865e 100644 --- a/config/initializers/mime_types.rb +++ b/config/initializers/mime_types.rb @@ -4,3 +4,9 @@ Mime::Type.register 'application/vnd.api+json', :jsonapi, %W[ application/vnd.keygen+json ] + +# adds pattern matching +class Mime::Type + def deconstruct_keys(*) = { string:, symbol:, synonyms: } + def deconstruct = [symbol] +end