Skip to content

Commit 795fb70

Browse files
authored
Merge pull request #116 from MindscapeHQ/fix-rack-request-data-reading
Correctly record raw request data for Rack based apps
2 parents 31115d8 + 8b7d069 commit 795fb70

File tree

6 files changed

+62
-12
lines changed

6 files changed

+62
-12
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 2.0.0 (20/04/2017)
2+
3+
Bugfixes:
4+
- Fix broken handling of raw request body reading in Rack applications ([#116](https://github.com/MindscapeHQ/raygun4ruby/pull/116))
5+
- This is a breaking change to how raw data was being read before so it requires a major version bump
6+
- Raw request data reading is now disabled by default and can be enabled via the `record_raw_data` configuration option
17
## 1.5.0 (16/03/2017)
28

39
Features

lib/raygun/client.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,21 @@ def rack_params(env)
132132
end
133133

134134
def raw_data(rack_env)
135+
return unless Raygun.configuration.record_raw_data
136+
135137
request = Rack::Request.new(rack_env)
136-
unless request.form_data?
137-
form_params(rack_env)
138+
input = rack_env['rack.input']
139+
140+
if input && !request.form_data?
141+
current_position = input.pos
142+
input.rewind
143+
144+
body = (input.read || '').slice(0, 4096)
145+
input.seek(current_position)
146+
147+
body
148+
else
149+
{}
138150
end
139151
end
140152

lib/raygun/configuration.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ def self.proc_config_option(name)
7070
# Override this if you wish to connect to a different Raygun API than the standard one
7171
config_option :api_url
7272

73+
# Should Raygun include the raw request body in the payload? This will not include
74+
# form submissions and will not be filtered by the blacklist
75+
config_option :record_raw_data
76+
7377
# Exception classes to ignore by default
7478
IGNORE_DEFAULT = ['ActiveRecord::RecordNotFound',
7579
'ActionController::RoutingError',
@@ -119,7 +123,8 @@ def initialize
119123
whitelist_payload_shape: DEFAULT_WHITELIST_PAYLOAD_SHAPE,
120124
proxy_settings: {},
121125
debug: false,
122-
api_url: 'https://api.raygun.io/'
126+
api_url: 'https://api.raygun.io/',
127+
record_raw_data: false
123128
})
124129
end
125130

lib/raygun/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Raygun
2-
VERSION = "1.5.0"
2+
VERSION = "2.0.0"
33
end

test/unit/client_test.rb

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def to_s
2929
def setup
3030
super
3131
@client = Raygun::Client.new
32+
Raygun.configuration.record_raw_data = true
3233
fake_successful_entry
3334

3435
# Force NZ time zone for utcOffset tests
@@ -222,15 +223,36 @@ def test_getting_request_information_with_nil_env
222223
assert_equal({}, @client.send(:request_information, nil))
223224
end
224225

225-
def test_non_form_parameters
226-
put_body_env_hash = sample_env_hash.merge({
227-
"REQUEST_METHOD"=>"PUT",
228-
"action_dispatch.request.parameters"=> { "a" => "b", "c" => "4945438", "password" => "swordfish" }
226+
def test_raw_post_body
227+
env_hash = sample_env_hash.merge({
228+
"CONTENT_TYPE" => "application/json",
229+
"REQUEST_METHOD" => "POST",
230+
"rack.input" => StringIO.new('{"foo": "bar"}')
229231
})
230232

231-
expected_form_hash = { "a" => "b", "c" => "4945438", "password" => "[FILTERED]" }
233+
assert_equal '{"foo": "bar"}', @client.send(:request_information, env_hash)[:rawData]
234+
end
235+
236+
def test_raw_post_body_with_more_than_4096_chars
237+
input = "0" * 5000;
238+
env_hash = sample_env_hash.merge({
239+
"CONTENT_TYPE" => "application/json",
240+
"REQUEST_METHOD" => "POST",
241+
"rack.input" => StringIO.new(input)
242+
})
232243

233-
assert_equal expected_form_hash, @client.send(:request_information, put_body_env_hash)[:rawData]
244+
assert_equal input.slice(0, 4096), @client.send(:request_information, env_hash)[:rawData]
245+
end
246+
247+
def test_raw_post_body_with_config_disabled
248+
Raygun.configuration.record_raw_data = false
249+
env_hash = sample_env_hash.merge({
250+
"CONTENT_TYPE" => "application/json",
251+
"REQUEST_METHOD" => "POST",
252+
"rack.input" => StringIO.new('{"foo": "bar"}')
253+
})
254+
255+
assert_equal(nil, @client.send(:request_information, env_hash)[:rawData])
234256
end
235257

236258
def test_error_raygun_custom_data
@@ -461,6 +483,7 @@ def test_filter_payload_with_whitelist_default_request_post
461483
Raygun.configuration.filter_payload_with_whitelist = true
462484

463485
post_body_env_hash = sample_env_hash.merge(
486+
"CONTENT_TYPE" => 'application/x-www-form-urlencoded',
464487
"REQUEST_METHOD" => "POST",
465488
"rack.input"=>StringIO.new("a=b&c=4945438&password=swordfish")
466489
)
@@ -475,7 +498,7 @@ def test_filter_payload_with_whitelist_default_request_post
475498
queryString: { },
476499
headers: { "Version"=>"HTTP/1.1", "Host"=>"localhost:3000", "Cookie"=>"cookieval" },
477500
form: { "a" => "[FILTERED]", "c" => "[FILTERED]", "password" => "[FILTERED]" },
478-
rawData: nil
501+
rawData: {}
479502
}
480503

481504
assert_equal expected_hash, details[:request]
@@ -506,7 +529,7 @@ def test_filter_payload_with_whitelist_request_post_except_formkey
506529
queryString: { },
507530
headers: { "Version"=>"HTTP/1.1", "Host"=>"localhost:3000", "Cookie"=>"cookieval" },
508531
form: { "username" => "foo", "password" => "[FILTERED]" },
509-
rawData: nil
532+
rawData: {}
510533
}
511534

512535
assert_equal expected_hash, details[:request]

test/unit/configuration_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,8 @@ def test_setting_custom_data_to_hash
129129
def test_api_url_default
130130
assert_equal "https://api.raygun.io/", Raygun.configuration.api_url
131131
end
132+
133+
def test_record_raw_data_default
134+
assert_equal false, Raygun.configuration.record_raw_data
135+
end
132136
end

0 commit comments

Comments
 (0)