Class: Blouson::RavenParameterFilterProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/blouson/raven_parameter_filter_processor.rb

Class Method Summary collapse

Class Method Details

.create(filters, header_filters) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/blouson/raven_parameter_filter_processor.rb', line 3

def self.create(filters, header_filters)
  Class.new(Raven::Processor) do
    @filters = filters
    @header_filters = header_filters.map(&:downcase)

    def self.filters
      @filters
    end

    def self.header_filters
      @header_filters
    end

    def initialize(client = nil)
      # ActionDispatch::Http::ParameterFilter is deprecated and will be removed from Rails 6.1.
      parameter_filter_klass = if defined?(ActiveSupport::ParameterFilter)
          ActiveSupport::ParameterFilter
        else
          ActionDispatch::Http::ParameterFilter
        end
      @parameter_filter = parameter_filter_klass.new(self.class.filters)
    end

    def process(value)
      process_query_string(value)
      process_request_body(value)
      process_request_header(value)
      process_cookie(value)
    ensure
      return value
    end

    def process_request_body(value)
      if value[:request] && value[:request][:data].present?
        data = value[:request][:data]
        if data.is_a?(String)
          # Maybe JSON request
          begin
            data = JSON.parse(data)
            value[:request][:data] = JSON.dump(@parameter_filter.filter(data))
          rescue JSON::ParserError => e
            # Record parser error to extra field
            value[:extra]['BlousonError'] = e.message
          end
        else
          value[:request][:data] = @parameter_filter.filter(data)
        end
      end
    end

    def process_query_string(value)
      if value[:request] && value[:request][:query_string].present?
        query    = Rack::Utils.parse_query(value[:request][:query_string])
        filtered = @parameter_filter.filter(query)

        value[:request][:query_string] = Rack::Utils.build_query(filtered)
      end
    end

    def process_request_header(value)
      if value[:request] && value[:request][:headers]
        headers = value[:request][:headers]
        headers.each_key do |k|
          if self.class.header_filters.include?(k.downcase)
            headers[k] = 'FILTERED'
          end
        end
      end
    end

    def process_cookie(value)
      if (cookies = value.dig(:request, :cookies))
        value[:request][:cookies] = @parameter_filter.filter(cookies)
      end

      if value[:request] && value[:request][:headers] && value[:request][:headers]['Cookie']
        cookies  = Hash[value[:request][:headers]['Cookie'].split('; ').map { |pair| pair.split('=', 2) }]
        filtered = @parameter_filter.filter(cookies)

        value[:request][:headers]['Cookie'] = filtered.map { |pair| pair.join('=') }.join('; ')
      end
    end
  end
end