Module: Bugno::RequestDataExtractor

Included in:
Event
Defined in:
lib/bugno/request_data_extractor.rb

Constant Summary collapse

ALLOWED_HEADERS_REGEX =
/^HTTP_|^CONTENT_TYPE$|^CONTENT_LENGTH$/.freeze
ALLOWED_BODY_PARSEABLE_METHODS =
%w[POST PUT PATCH DELETE].freeze

Instance Method Summary collapse

Instance Method Details

#extract_request_data_from_rack(env) ⇒ Object



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
# File 'lib/bugno/request_data_extractor.rb', line 12

def extract_request_data_from_rack(env)
  rack_req = ::Rack::Request.new(env)
  sensitive_params = sensitive_params_list(env)

  post_params = scrub_params(post_params(rack_req), sensitive_params)
  get_params = scrub_params(get_params(rack_req), sensitive_params)
  route_params = scrub_params(route_params(env), sensitive_params)
  session = scrub_params(request_session(env), sensitive_params)
  cookies = scrub_params(request_cookies(rack_req), sensitive_params)
  person_data = scrub_params(person_data(env), sensitive_params)

  data = {
    url: request_url(env),
    ip_address: ip_address(env),
    headers: headers(env),
    http_method: request_method(env),
    params: get_params,
    route_params: route_params,
    session: session,
    cookies: cookies,
    person_data: person_data
  }
  data[:params] = post_params if data[:params].empty?

  data
end

#get_params(rack_req) ⇒ Object



121
122
123
124
125
# File 'lib/bugno/request_data_extractor.rb', line 121

def get_params(rack_req)
  rack_req.GET
rescue StandardError
  {}
end

#headers(env) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bugno/request_data_extractor.rb', line 78

def headers(env)
  env.keys.grep(ALLOWED_HEADERS_REGEX).map do |header|
    name = header.gsub(/^HTTP_/, '').split('_').map(&:capitalize).join('-')
    if name == 'Cookie'
      {}
    elsif sensitive_headers_list.include?(name)
      { name => Bugno::Filter::Params.scrub_value }
    else
      { name => env[header] }
    end
  end.inject(:merge)
end

#ip_address(env) ⇒ Object



117
118
119
# File 'lib/bugno/request_data_extractor.rb', line 117

def ip_address(env)
  ip_address_string = (env['action_dispatch.remote_ip'] || env['HTTP_X_REAL_IP'] || env['REMOTE_ADDR']).to_s
end

#person_data(env) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/bugno/request_data_extractor.rb', line 49

def person_data(env)
  current_user = Bugno.configuration.current_user_method
  controller = env['action_controller.instance']
  person_data = begin
                  controller.send(current_user).attributes
                rescue StandardError
                  {}
                end
  person_data
end

#post_params(rack_req) ⇒ Object



127
128
129
130
131
# File 'lib/bugno/request_data_extractor.rb', line 127

def post_params(rack_req)
  rack_req.POST
rescue StandardError
  {}
end

#request_cookies(rack_req) ⇒ Object



72
73
74
75
76
# File 'lib/bugno/request_data_extractor.rb', line 72

def request_cookies(rack_req)
  rack_req.cookies
rescue StandardError
  {}
end

#request_method(env) ⇒ Object



91
92
93
# File 'lib/bugno/request_data_extractor.rb', line 91

def request_method(env)
  env['REQUEST_METHOD'] || env[:method]
end

#request_session(env) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/bugno/request_data_extractor.rb', line 64

def request_session(env)
  session = env.fetch('rack.session', {})

  session.to_hash
rescue StandardError
  {}
end

#request_url(env) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/bugno/request_data_extractor.rb', line 95

def request_url(env)
  forwarded_proto = env['HTTP_X_FORWARDED_PROTO'] || env['rack.url_scheme'] || ''
  scheme = forwarded_proto.split(',').first

  host = env['HTTP_X_FORWARDED_HOST'] || env['HTTP_HOST'] || env['SERVER_NAME'] || ''
  host = host.split(',').first.strip unless host.empty?

  path = env['ORIGINAL_FULLPATH'] || env['REQUEST_URI']
  unless path.nil? || path.empty?
    path = '/' + path.to_s if path.to_s.slice(0, 1) != '/'
  end

  port = env['HTTP_X_FORWARDED_PORT']
  if port && !(!scheme.nil? && scheme.casecmp('http').zero? && port.to_i == 80) && \
     !(!scheme.nil? && scheme.casecmp('https').zero? && port.to_i == 443) && \
     !(host.include? ':')
    host = host + ':' + port
  end

  [scheme, '://', host, path].join
end

#route_params(env) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/bugno/request_data_extractor.rb', line 133

def route_params(env)
  return {} unless defined?(Rails)

  begin
    environment = { method: request_method(env) }

    ::Rails.application.routes.recognize_path(env['PATH_INFO'],
                                              environment)
  rescue StandardError
    {}
  end
end

#scrub_params(params, sensitive_params) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/bugno/request_data_extractor.rb', line 39

def scrub_params(params, sensitive_params)
  options = {
    params: params,
    config: Bugno.configuration.scrub_fields,
    extra_fields: sensitive_params,
    whitelist: Bugno.configuration.scrub_whitelist
  }
  Bugno::Filter::Params.call(options)
end

#sensitive_headers_listObject



146
147
148
# File 'lib/bugno/request_data_extractor.rb', line 146

def sensitive_headers_list
  Bugno.configuration.scrub_headers || []
end

#sensitive_params_list(env) ⇒ Object



60
61
62
# File 'lib/bugno/request_data_extractor.rb', line 60

def sensitive_params_list(env)
  Array(env['action_dispatch.parameter_filter'])
end