lib/bugsnag/cleaner.rb in bugsnag-6.26.2 vs lib/bugsnag/cleaner.rb in bugsnag-6.26.3

- old
+ new

@@ -28,29 +28,23 @@ return url if @configuration.meta_data_filters.empty? && @configuration.redacted_keys.empty? return url unless url.include?('?') begin uri = URI(url) - rescue URI::InvalidURIError - pre_query_string, _query_string = url.split('?', 2) - return "#{pre_query_string}?#{FILTERED}" - end - - return url unless uri.query - - query_params = uri.query.split('&').map { |pair| pair.split('=') } - query_params.map! do |key, val| - if filters_match?(key) - "#{key}=#{FILTERED}" + if uri.is_a?(URI::MailTo) + clean_mailto_url(url, uri) else - "#{key}=#{val}" + clean_generic_url(url, uri) end - end + rescue URI::InvalidURIError + pre_query_string, _query_string = url.split('?', 2) - uri.query = query_params.join('&') - uri.to_s + "#{pre_query_string}?#{FILTERED}" + rescue StandardError + FILTERED + end end ## # @param key [String, #to_s] # @return [Boolean] @@ -205,9 +199,37 @@ # @param scope [String] # @return [Boolean] def scope_should_be_filtered?(scope) @configuration.scopes_to_filter.any? do |scope_to_filter| scope.start_with?("#{scope_to_filter}.") + end + end + + def clean_generic_url(original_url, uri) + return original_url unless uri.query + + query_params = uri.query.split('&').map { |pair| pair.split('=') } + + uri.query = filter_uri_parameter_array(query_params).join('&') + uri.to_s + end + + def clean_mailto_url(original_url, uri) + return original_url unless uri.headers + + # headers in mailto links can't contain square brackets so we replace + # filtered parameters with 'FILTERED' instead of '[FILTERED]' + uri.headers = filter_uri_parameter_array(uri.headers, 'FILTERED').join('&') + uri.to_s + end + + def filter_uri_parameter_array(parameters, replacement = FILTERED) + parameters.map do |key, value| + if filters_match?(key) + "#{key}=#{replacement}" + else + "#{key}=#{value}" + end end end end end