bugsnag 6.12.1 → 6.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. checksums.yaml +4 -4
  2. data/.buildkite/pipeline.yml +470 -0
  3. data/.rubocop.yml +55 -0
  4. data/.rubocop_todo.yml +530 -160
  5. data/CHANGELOG.md +73 -0
  6. data/CONTRIBUTING.md +1 -9
  7. data/Gemfile +14 -7
  8. data/TESTING.md +81 -0
  9. data/VERSION +1 -1
  10. data/docker-compose.yml +46 -0
  11. data/dockerfiles/Dockerfile.jruby-unit-tests +13 -0
  12. data/dockerfiles/Dockerfile.ruby-maze-runner +26 -0
  13. data/dockerfiles/Dockerfile.ruby-unit-tests +12 -0
  14. data/features/delayed_job.feature +6 -22
  15. data/features/fixtures/delayed_job/Dockerfile +2 -4
  16. data/features/fixtures/delayed_job/app/Gemfile +1 -1
  17. data/features/fixtures/delayed_job/app/Rakefile +18 -0
  18. data/features/fixtures/docker-compose.yml +32 -40
  19. data/features/fixtures/expected_breadcrumbs/active_job.json +9 -0
  20. data/features/fixtures/expected_breadcrumbs/mongo_failed.json +15 -0
  21. data/features/fixtures/expected_breadcrumbs/mongo_filtered_request.json +15 -0
  22. data/features/fixtures/expected_breadcrumbs/mongo_filtered_result.json +15 -0
  23. data/features/fixtures/expected_breadcrumbs/mongo_success.json +14 -0
  24. data/features/fixtures/expected_breadcrumbs/request.json +13 -0
  25. data/features/fixtures/expected_breadcrumbs/sql_with_bindings.json +12 -0
  26. data/features/fixtures/expected_breadcrumbs/sql_without_bindings.json +11 -0
  27. data/features/fixtures/plain/Dockerfile +2 -2
  28. data/features/fixtures/plain/app/app.rb +1 -3
  29. data/features/fixtures/plain/app/delivery/fork_threadpool.rb +3 -1
  30. data/features/fixtures/plain/app/report_modification/initiators/handled_on_error.rb +10 -0
  31. data/features/fixtures/plain/app/report_modification/initiators/unhandled_on_error.rb +11 -0
  32. data/features/fixtures/plain/app/stack_frame_modification/initiators/handled_on_error.rb +29 -0
  33. data/features/fixtures/plain/app/stack_frame_modification/initiators/unhandled_on_error.rb +26 -0
  34. data/features/fixtures/plain/app/unhandled/{Interrupt.rb → interrupt.rb} +0 -0
  35. data/features/fixtures/rack1/Dockerfile +2 -2
  36. data/features/fixtures/rack2/Dockerfile +2 -2
  37. data/features/fixtures/rails3/Dockerfile +2 -2
  38. data/features/fixtures/rails3/app/Gemfile +4 -0
  39. data/features/fixtures/rails3/app/config/initializers/bugsnag.rb +11 -2
  40. data/features/fixtures/rails4/Dockerfile +2 -5
  41. data/features/fixtures/rails4/app/config/initializers/bugsnag.rb +10 -1
  42. data/features/fixtures/rails5/Dockerfile +2 -2
  43. data/features/fixtures/rails5/app/Gemfile +3 -2
  44. data/features/fixtures/rails5/app/config/initializers/bugsnag.rb +10 -1
  45. data/features/fixtures/rails6/Dockerfile +2 -2
  46. data/features/fixtures/rails6/app/Gemfile +3 -2
  47. data/features/fixtures/rails6/app/app/controllers/mongo_controller.rb +22 -0
  48. data/features/fixtures/rails6/app/app/models/mongo_model.rb +6 -0
  49. data/features/fixtures/rails6/app/config/environments/development.rb +2 -0
  50. data/features/fixtures/rails6/app/config/environments/production.rb +1 -0
  51. data/features/fixtures/rails6/app/config/environments/rails_env.rb +1 -0
  52. data/features/fixtures/rails6/app/config/environments/test.rb +1 -0
  53. data/features/fixtures/rails6/app/config/initializers/bugsnag.rb +10 -1
  54. data/features/fixtures/rails6/app/config/mongoid.yml +23 -0
  55. data/features/fixtures/rails6/app/config/routes.rb +4 -0
  56. data/features/fixtures/resque/Dockerfile +2 -2
  57. data/features/fixtures/sidekiq/Dockerfile +5 -7
  58. data/features/fixtures/sidekiq/app/Gemfile +2 -1
  59. data/features/fixtures/sidekiq/app/Rakefile.rb +14 -0
  60. data/features/fixtures/sinatra1/Dockerfile +2 -2
  61. data/features/fixtures/sinatra2/Dockerfile +2 -2
  62. data/features/plain_features/add_tab.feature +30 -97
  63. data/features/plain_features/app_type.feature +6 -25
  64. data/features/plain_features/app_version.feature +6 -25
  65. data/features/plain_features/auto_notify.feature +4 -20
  66. data/features/plain_features/delivery.feature +12 -60
  67. data/features/plain_features/exception_data.feature +24 -94
  68. data/features/plain_features/filters.feature +9 -43
  69. data/features/plain_features/handled_errors.feature +16 -78
  70. data/features/plain_features/ignore_classes.feature +5 -23
  71. data/features/plain_features/ignore_report.feature +8 -24
  72. data/features/plain_features/proxies.feature +13 -56
  73. data/features/plain_features/release_stages.feature +9 -40
  74. data/features/plain_features/report_api_key.feature +11 -35
  75. data/features/plain_features/report_severity.feature +10 -35
  76. data/features/plain_features/report_stack_frames.feature +29 -93
  77. data/features/plain_features/report_user.feature +29 -96
  78. data/features/plain_features/unhandled_errors.feature +17 -88
  79. data/features/rails_features/api_key.feature +12 -58
  80. data/features/rails_features/app_type.feature +13 -58
  81. data/features/rails_features/app_version.feature +19 -80
  82. data/features/rails_features/auto_capture_sessions.feature +31 -112
  83. data/features/rails_features/auto_notify.feature +28 -105
  84. data/features/rails_features/before_notify.feature +18 -83
  85. data/features/rails_features/breadcrumbs.feature +40 -137
  86. data/features/rails_features/handled.feature +18 -82
  87. data/features/rails_features/ignore_classes.feature +12 -51
  88. data/features/rails_features/meta_data_filters.feature +9 -33
  89. data/features/rails_features/mongo_breadcrumbs.feature +22 -96
  90. data/features/rails_features/on_error.feature +29 -0
  91. data/features/rails_features/project_root.feature +19 -84
  92. data/features/rails_features/release_stage.feature +20 -82
  93. data/features/rails_features/send_code.feature +13 -55
  94. data/features/rails_features/send_environment.feature +7 -33
  95. data/features/rails_features/unhandled.feature +6 -31
  96. data/features/rails_features/user_info.feature +27 -65
  97. data/features/sidekiq.feature +12 -79
  98. data/features/steps/ruby_notifier_steps.rb +59 -15
  99. data/features/support/env.rb +12 -45
  100. data/lib/bugsnag.rb +109 -21
  101. data/lib/bugsnag/breadcrumbs/breadcrumbs.rb +0 -2
  102. data/lib/bugsnag/breadcrumbs/validator.rb +0 -6
  103. data/lib/bugsnag/cleaner.rb +129 -60
  104. data/lib/bugsnag/code_extractor.rb +137 -0
  105. data/lib/bugsnag/configuration.rb +58 -1
  106. data/lib/bugsnag/helpers.rb +2 -4
  107. data/lib/bugsnag/integrations/que.rb +7 -4
  108. data/lib/bugsnag/middleware/discard_error_class.rb +30 -0
  109. data/lib/bugsnag/middleware/exception_meta_data.rb +15 -9
  110. data/lib/bugsnag/middleware/ignore_error_class.rb +2 -0
  111. data/lib/bugsnag/middleware/rack_request.rb +2 -4
  112. data/lib/bugsnag/middleware_stack.rb +38 -3
  113. data/lib/bugsnag/on_error_callbacks.rb +33 -0
  114. data/lib/bugsnag/report.rb +4 -14
  115. data/lib/bugsnag/session_tracker.rb +3 -3
  116. data/lib/bugsnag/stacktrace.rb +28 -75
  117. data/spec/breadcrumbs/breadcrumb_spec.rb +1 -1
  118. data/spec/breadcrumbs/validator_spec.rb +1 -26
  119. data/spec/bugsnag_spec.rb +2 -2
  120. data/spec/cleaner_spec.rb +202 -10
  121. data/spec/code_extractor_spec.rb +129 -0
  122. data/spec/configuration_spec.rb +16 -1
  123. data/spec/fixtures/apps/rails-initializer-config/Gemfile +5 -1
  124. data/spec/fixtures/apps/rails-invalid-initializer-config/Gemfile +5 -1
  125. data/spec/fixtures/apps/rails-no-config/Gemfile +5 -1
  126. data/spec/fixtures/crashes/file1.rb +29 -0
  127. data/spec/fixtures/crashes/file2.rb +25 -0
  128. data/spec/fixtures/crashes/file_with_long_lines.rb +7 -0
  129. data/spec/fixtures/crashes/functions.rb +29 -0
  130. data/spec/fixtures/crashes/short_file.rb +2 -0
  131. data/spec/helper_spec.rb +0 -31
  132. data/spec/integrations/logger_spec.rb +1 -1
  133. data/spec/integrations/rack_spec.rb +8 -6
  134. data/spec/integrations/rake_spec.rb +1 -1
  135. data/spec/on_error_spec.rb +332 -0
  136. data/spec/report_spec.rb +331 -30
  137. data/spec/spec_helper.rb +14 -1
  138. data/spec/stacktrace_spec.rb +427 -74
  139. metadata +36 -7
  140. data/.travis.yml +0 -117
  141. data/features/plain_features/api_key.feature +0 -25
@@ -1,31 +1,75 @@
1
- When("I set environment variable {string} to the current IP") do |env_var|
1
+ Then(/^the "(.+)" of the top non-bugsnag stackframe equals (\d+|".+")$/) do |element, value|
2
+ stacktrace = read_key_path(Server.current_request[:body], 'events.0.exceptions.0.stacktrace')
3
+ frame_index = stacktrace.find_index { |frame| ! /.*lib\/bugsnag.*\.rb/.match(frame["file"]) }
2
4
  steps %Q{
3
- When I set environment variable "#{env_var}" to "#{current_ip}"
5
+ the "#{element}" of stack frame #{frame_index} equals #{value}
4
6
  }
5
7
  end
6
8
 
7
- When("I set environment variable {string} to the mock API port") do |env_var|
9
+ Then(/^the total sessionStarted count equals (\d+)$/) do |value|
10
+ session_counts = read_key_path(Server.current_request[:body], "sessionCounts")
11
+ total_count = session_counts.inject(0) { |count, session| count += session["sessionsStarted"] }
12
+ assert_equal(value, total_count)
13
+ end
14
+
15
+
16
+ # Due to an ongoing discussion on whether the `payload_version` needs to be present within the headers
17
+ # and body of the payload, this step is a local replacement for the similar step present in the main
18
+ # maze-runner library. Once the discussion is resolved this step should be removed and replaced in scenarios
19
+ # with the main library version.
20
+ Then("the request is valid for the error reporting API version {string} for the {string}") do |payload_version, notifier_name|
8
21
  steps %Q{
9
- When I set environment variable "#{env_var}" to "#{MOCK_API_PORT}"
22
+ Then the "Bugsnag-Api-Key" header equals "#{$api_key}"
23
+ And the payload field "apiKey" equals "#{$api_key}"
24
+ And the "Bugsnag-Payload-Version" header equals "#{payload_version}"
25
+ And the "Content-Type" header equals "application/json"
26
+ And the "Bugsnag-Sent-At" header is a timestamp
27
+
28
+ And the payload field "notifier.name" equals "#{notifier_name}"
29
+ And the payload field "notifier.url" is not null
30
+ And the payload field "notifier.version" is not null
31
+ And the payload field "events" is a non-empty array
32
+
33
+ And each element in payload field "events" has "severity"
34
+ And each element in payload field "events" has "severityReason.type"
35
+ And each element in payload field "events" has "unhandled"
36
+ And each element in payload field "events" has "exceptions"
10
37
  }
11
38
  end
12
39
 
13
- When("I set environment variable {string} to the proxy settings with credentials {string}") do |env_var, credentials|
40
+ Given("I start the rails service") do
41
+ rails_version = ENV["RAILS_VERSION"]
14
42
  steps %Q{
15
- When I set environment variable "#{env_var}" to "#{credentials}@#{current_ip}:#{MOCK_API_PORT}"
43
+ When I start the service "rails#{rails_version}"
44
+ And I wait for the host "rails#{rails_version}" to open port "3000"
16
45
  }
17
46
  end
18
47
 
19
- Then(/^the "(.+)" of the top non-bugsnag stackframe equals (\d+|".+")(?: for request (\d+))?$/) do |element, value, request_index|
20
- stacktrace = read_key_path(find_request(request_index)[:body], 'events.0.exceptions.0.stacktrace')
21
- frame_index = stacktrace.find_index { |frame| ! /.*lib\/bugsnag.*\.rb/.match(frame["file"]) }
48
+ When("I navigate to the route {string} on the rails app") do |route|
49
+ rails_version = ENV["RAILS_VERSION"]
22
50
  steps %Q{
23
- the "#{element}" of stack frame #{frame_index} equals #{value}
51
+ When I open the URL "http://rails#{rails_version}:3000#{route}"
24
52
  }
25
53
  end
26
54
 
27
- Then(/^the total sessionStarted count equals (\d+)(?: for request (\d+))?$/) do |value, request_index|
28
- session_counts = read_key_path(find_request(request_index)[:body], "sessionCounts")
29
- total_count = session_counts.inject(0) { |count, session| count += session["sessionsStarted"] }
30
- assert_equal(value, total_count)
31
- end
55
+ Then("the payload field {string} matches the appropriate Sidekiq handled payload") do |field|
56
+ if ENV["SIDEKIQ_VERSION"] == "~> 2"
57
+ created_at_present = "false"
58
+ else
59
+ created_at_present = "true"
60
+ end
61
+ steps %Q{
62
+ And the payload field "#{field}" matches the JSON fixture in "features/fixtures/sidekiq/payloads/handled_metadata_ca_#{created_at_present}.json"
63
+ }
64
+ end
65
+
66
+ Then("the payload field {string} matches the appropriate Sidekiq unhandled payload") do |field|
67
+ if ENV["SIDEKIQ_VERSION"] == "~> 2"
68
+ created_at_present = "false"
69
+ else
70
+ created_at_present = "true"
71
+ end
72
+ steps %Q{
73
+ And the payload field "#{field}" matches the JSON fixture in "features/fixtures/sidekiq/payloads/unhandled_metadata_ca_#{created_at_present}.json"
74
+ }
75
+ end
@@ -1,56 +1,23 @@
1
1
  require 'fileutils'
2
2
 
3
- Before do
4
- find_default_docker_compose
5
- end
6
-
7
- def output_logs
8
- $docker_services.each do |service|
9
- logged_service = service[:service] == :all ? '' : service[:service]
10
- command = "logs -t #{logged_service}"
11
- begin
12
- response = run_docker_compose_command(service[:file], command)
13
- rescue => exception
14
- response = "Couldn't retreive logs for #{service[:file]}:#{logged_service}"
15
- end
16
- STDOUT.puts response.is_a?(String) ? response : response.to_a
17
- end
18
- end
19
-
20
3
  def install_fixture_gems
21
- gem_dir = File.expand_path('../../../', __FILE__)
22
- Dir.chdir(gem_dir) do
23
- `rm bugsnag-*.gem` unless Dir.glob('bugsnag-*.gem').empty?
24
- `gem build bugsnag.gemspec`
25
- Dir.entries('features/fixtures').reject { |entry| ['.', '..'].include?(entry) }.each do |entry|
26
- target_dir = "features/fixtures/#{entry}"
27
- if File.directory?(target_dir)
28
- `cp bugsnag-*.gem #{target_dir}`
29
- `gem unpack #{target_dir}/bugsnag-*.gem --target #{target_dir}/temp-bugsnag-lib`
30
- end
31
- end
32
- `rm bugsnag-*.gem`
33
- end
34
- end
35
-
36
- # Added to ensure that multiple versions of Gems do not exist within the fixture folders,
37
- # which can be difficult to track down and clear up
38
- def remove_installed_gems
39
- removal_targets = ['temp-bugsnag-lib', 'bugsnag-*.gem']
4
+ throw Error.new("Bugsnag.gem not found. Is this running in a docker-container?") unless File.exist?("/app/bugsnag.gem")
40
5
  Dir.entries('features/fixtures').reject { |entry| ['.', '..'].include?(entry) }.each do |entry|
41
6
  target_dir = "features/fixtures/#{entry}"
42
- target_entries = []
43
- removal_targets.each do |r_target|
44
- target_entries += Dir.glob("#{target_dir}/#{r_target}")
45
- end
46
- target_entries.each do |d_target|
47
- FileUtils.rm_rf(d_target)
7
+ if File.directory?(target_dir)
8
+ `cp /app/bugsnag.gem #{target_dir}`
9
+ `gem unpack #{target_dir}/bugsnag.gem --target #{target_dir}/temp-bugsnag-lib`
48
10
  end
49
11
  end
50
12
  end
51
13
 
52
- at_exit do
53
- remove_installed_gems
14
+ AfterConfiguration do |config|
15
+ install_fixture_gems
54
16
  end
55
17
 
56
- install_fixture_gems
18
+ Before do
19
+ Docker.compose_project_name = "#{rand.to_s}:#{Time.new.strftime("%s")}"
20
+ Runner.environment.clear
21
+ Runner.environment["BUGSNAG_API_KEY"] = $api_key
22
+ Runner.environment["BUGSNAG_ENDPOINT"] = "http://maze-runner:#{MOCK_API_PORT}"
23
+ end
@@ -33,6 +33,7 @@ require "bugsnag/breadcrumbs/validator"
33
33
  require "bugsnag/breadcrumbs/breadcrumb"
34
34
  require "bugsnag/breadcrumbs/breadcrumbs"
35
35
 
36
+ # rubocop:todo Metrics/ModuleLength
36
37
  module Bugsnag
37
38
  LOCK = Mutex.new
38
39
  INTEGRATIONS = [:resque, :sidekiq, :mailman, :delayed_job, :shoryuken, :que, :mongo]
@@ -47,6 +48,12 @@ module Bugsnag
47
48
  def configure(validate_api_key=true)
48
49
  yield(configuration) if block_given?
49
50
 
51
+ # Create the session tracker if sessions are enabled to avoid the overhead
52
+ # of creating it on the first request. We skip this if we're not validating
53
+ # the API key as we use this internally before the user's configure block
54
+ # has run, so we don't know if sessions are enabled yet.
55
+ session_tracker if validate_api_key && configuration.auto_capture_sessions
56
+
50
57
  check_key_valid if validate_api_key
51
58
  check_endpoint_setup
52
59
 
@@ -63,7 +70,7 @@ module Bugsnag
63
70
  auto_notify = false
64
71
  end
65
72
 
66
- return unless deliver_notification?(exception, auto_notify)
73
+ return unless should_deliver_notification?(exception, auto_notify)
67
74
 
68
75
  exception = NIL_EXCEPTION_DESCRIPTION if exception.nil?
69
76
 
@@ -71,6 +78,7 @@ module Bugsnag
71
78
 
72
79
  # If this is an auto_notify we yield the block before the any middleware is run
73
80
  yield(report) if block_given? && auto_notify
81
+
74
82
  if report.ignore?
75
83
  configuration.debug("Not notifying #{report.exceptions.last[:errorClass]} due to ignore being signified in auto_notify block")
76
84
  return
@@ -97,6 +105,7 @@ module Bugsnag
97
105
  # If this is not an auto_notify then the block was provided by the user. This should be the last
98
106
  # block that is run as it is the users "most specific" block.
99
107
  yield(report) if block_given? && !auto_notify
108
+
100
109
  if report.ignore?
101
110
  configuration.debug("Not notifying #{report.exceptions.last[:errorClass]} due to ignore being signified in user provided block")
102
111
  return
@@ -111,13 +120,7 @@ module Bugsnag
111
120
  report.severity_reason = initial_reason
112
121
  end
113
122
 
114
- # Deliver
115
- configuration.info("Notifying #{configuration.notify_endpoint} of #{report.exceptions.last[:errorClass]}")
116
- options = {:headers => report.headers}
117
- payload = ::JSON.dump(Bugsnag::Helpers.trim_if_needed(report.as_json))
118
- Bugsnag::Delivery[configuration.delivery_method].deliver(configuration.notify_endpoint, payload, configuration, options)
119
- report_summary = report.summary
120
- leave_breadcrumb(report_summary[:error_class], report_summary, Bugsnag::Breadcrumbs::ERROR_BREADCRUMB_TYPE, :auto)
123
+ deliver_notification(report)
121
124
  end
122
125
  end
123
126
 
@@ -147,6 +150,7 @@ module Bugsnag
147
150
  # Configuration getters
148
151
  ##
149
152
  # Returns the client's Configuration object, or creates one if not yet created.
153
+ # @return [Configuration]
150
154
  def configuration
151
155
  @configuration = nil unless defined?(@configuration)
152
156
  @configuration || LOCK.synchronize { @configuration ||= Bugsnag::Configuration.new }
@@ -171,6 +175,8 @@ module Bugsnag
171
175
  # Allow access to "before notify" callbacks as an array.
172
176
  #
173
177
  # These callbacks will be called whenever an error notification is being made.
178
+ #
179
+ # @deprecated Use {Bugsnag#add_on_error} instead
174
180
  def before_notify_callbacks
175
181
  Bugsnag.configuration.request_data[:before_callbacks] ||= []
176
182
  end
@@ -211,27 +217,67 @@ module Bugsnag
211
217
  validator.validate(breadcrumb)
212
218
 
213
219
  # Skip if it's already invalid
214
- unless breadcrumb.ignore?
215
- # Run callbacks
216
- configuration.before_breadcrumb_callbacks.each do |c|
217
- c.arity > 0 ? c.call(breadcrumb) : c.call
218
- break if breadcrumb.ignore?
219
- end
220
+ return if breadcrumb.ignore?
220
221
 
221
- # Return early if ignored
222
- return if breadcrumb.ignore?
222
+ # Run callbacks
223
+ configuration.before_breadcrumb_callbacks.each do |c|
224
+ c.arity > 0 ? c.call(breadcrumb) : c.call
225
+ break if breadcrumb.ignore?
226
+ end
227
+
228
+ # Return early if ignored
229
+ return if breadcrumb.ignore?
230
+
231
+ # Validate again in case of callback alteration
232
+ validator.validate(breadcrumb)
233
+
234
+ # Add to breadcrumbs buffer if still valid
235
+ configuration.breadcrumbs << breadcrumb unless breadcrumb.ignore?
236
+ end
237
+
238
+ ##
239
+ # Add the given callback to the list of on_error callbacks
240
+ #
241
+ # The on_error callbacks will be called when an error is captured or reported
242
+ # and are passed a {Bugsnag::Report} object
243
+ #
244
+ # Returning false from an on_error callback will cause the error to be ignored
245
+ # and will prevent any remaining callbacks from being called
246
+ #
247
+ # @param callback [Proc]
248
+ # @return [void]
249
+ def add_on_error(callback)
250
+ configuration.add_on_error(callback)
251
+ end
223
252
 
224
- # Validate again in case of callback alteration
225
- validator.validate(breadcrumb)
253
+ ##
254
+ # Remove the given callback from the list of on_error callbacks
255
+ #
256
+ # Note that this must be the same Proc instance that was passed to
257
+ # {Bugsnag#add_on_error}, otherwise it will not be removed
258
+ #
259
+ # @param callback [Proc]
260
+ # @return [void]
261
+ def remove_on_error(callback)
262
+ configuration.remove_on_error(callback)
263
+ end
226
264
 
227
- # Add to breadcrumbs buffer if still valid
228
- configuration.breadcrumbs << breadcrumb unless breadcrumb.ignore?
265
+ ##
266
+ # Returns the client's Cleaner object, or creates one if not yet created.
267
+ #
268
+ # @api private
269
+ #
270
+ # @return [Cleaner]
271
+ def cleaner
272
+ @cleaner = nil unless defined?(@cleaner)
273
+ @cleaner || LOCK.synchronize do
274
+ @cleaner ||= Bugsnag::Cleaner.new(configuration)
229
275
  end
230
276
  end
231
277
 
232
278
  private
233
279
 
234
- def deliver_notification?(exception, auto_notify)
280
+ def should_deliver_notification?(exception, auto_notify)
235
281
  reason = abort_reason(exception, auto_notify)
236
282
  configuration.debug(reason) unless reason.nil?
237
283
  reason.nil?
@@ -249,6 +295,32 @@ module Bugsnag
249
295
  end
250
296
  end
251
297
 
298
+ ##
299
+ # Deliver the notification to Bugsnag
300
+ #
301
+ # @param report [Report]
302
+ # @return void
303
+ def deliver_notification(report)
304
+ configuration.info("Notifying #{configuration.notify_endpoint} of #{report.exceptions.last[:errorClass]}")
305
+
306
+ payload = report_to_json(report)
307
+ options = {:headers => report.headers}
308
+
309
+ Bugsnag::Delivery[configuration.delivery_method].deliver(
310
+ configuration.notify_endpoint,
311
+ payload,
312
+ configuration,
313
+ options
314
+ )
315
+
316
+ leave_breadcrumb(
317
+ report.summary[:error_class],
318
+ report.summary,
319
+ Bugsnag::Breadcrumbs::ERROR_BREADCRUMB_TYPE,
320
+ :auto
321
+ )
322
+ end
323
+
252
324
  # Check if the API key is valid and warn (once) if it is not
253
325
  def check_key_valid
254
326
  @key_warning = false unless defined?(@key_warning)
@@ -273,7 +345,23 @@ module Bugsnag
273
345
  raise ArgumentError, "The session endpoint cannot be modified without the notify endpoint"
274
346
  end
275
347
  end
348
+
349
+ ##
350
+ # Convert the Report object to JSON
351
+ #
352
+ # We ensure the report is safe to send by removing recursion, fixing
353
+ # encoding errors and redacting metadata according to "meta_data_filters"
354
+ #
355
+ # @param report [Report]
356
+ # @return string
357
+ def report_to_json(report)
358
+ cleaned = cleaner.clean_object(report.as_json)
359
+ trimmed = Bugsnag::Helpers.trim_if_needed(cleaned)
360
+
361
+ ::JSON.dump(trimmed)
362
+ end
276
363
  end
277
364
  end
365
+ # rubocop:enable Metrics/ModuleLength
278
366
 
279
367
  Bugsnag.load_integrations unless ENV["BUGSNAG_DISABLE_AUTOCONFIGURE"]
@@ -1,6 +1,4 @@
1
1
  module Bugsnag::Breadcrumbs
2
- MAX_NAME_LENGTH = 30
3
-
4
2
  VALID_BREADCRUMB_TYPES = [
5
3
  ERROR_BREADCRUMB_TYPE = "error",
6
4
  MANUAL_BREADCRUMB_TYPE = "manual",
@@ -15,12 +15,6 @@ module Bugsnag::Breadcrumbs
15
15
  #
16
16
  # @param breadcrumb [Bugsnag::Breadcrumbs::Breadcrumb] the breadcrumb to be validated
17
17
  def validate(breadcrumb)
18
- # Check name length
19
- if breadcrumb.name.size > Bugsnag::Breadcrumbs::MAX_NAME_LENGTH
20
- @configuration.debug("Breadcrumb name trimmed to length #{Bugsnag::Breadcrumbs::MAX_NAME_LENGTH}. Original name: #{breadcrumb.name}")
21
- breadcrumb.name = breadcrumb.name.slice(0...Bugsnag::Breadcrumbs::MAX_NAME_LENGTH)
22
- end
23
-
24
18
  # Check meta_data hash doesn't contain complex values
25
19
  breadcrumb.meta_data = breadcrumb.meta_data.select do |k, v|
26
20
  if valid_meta_data_type?(v)
@@ -1,20 +1,76 @@
1
1
  require 'uri'
2
2
 
3
3
  module Bugsnag
4
+ # @api private
4
5
  class Cleaner
5
- ENCODING_OPTIONS = {:invalid => :replace, :undef => :replace}.freeze
6
6
  FILTERED = '[FILTERED]'.freeze
7
7
  RECURSION = '[RECURSION]'.freeze
8
8
  OBJECT = '[OBJECT]'.freeze
9
9
  RAISED = '[RAISED]'.freeze
10
+ OBJECT_WITH_ID_AND_CLASS = '[OBJECT]: [Class]: %<class_name>s [ID]: %<id>d'.freeze
10
11
 
11
- def initialize(filters)
12
- @filters = Array(filters)
13
- @deep_filters = @filters.any? {|f| f.kind_of?(Regexp) && f.to_s.include?("\\.".freeze) }
12
+ ##
13
+ # @param configuration [Configuration]
14
+ def initialize(configuration)
15
+ @configuration = configuration
14
16
  end
15
17
 
16
- def clean_object(obj)
17
- traverse_object(obj, {}, nil)
18
+ def clean_object(object)
19
+ @deep_filters = deep_filters?
20
+
21
+ traverse_object(object, {}, nil)
22
+ end
23
+
24
+ ##
25
+ # @param url [String]
26
+ # @return [String]
27
+ def clean_url(url)
28
+ return url if @configuration.meta_data_filters.empty?
29
+
30
+ uri = URI(url)
31
+ return url unless uri.query
32
+
33
+ query_params = uri.query.split('&').map { |pair| pair.split('=') }
34
+ query_params.map! do |key, val|
35
+ if filters_match?(key)
36
+ "#{key}=#{FILTERED}"
37
+ else
38
+ "#{key}=#{val}"
39
+ end
40
+ end
41
+
42
+ uri.query = query_params.join('&')
43
+ uri.to_s
44
+ end
45
+
46
+ private
47
+
48
+ ##
49
+ # This method calculates whether we need to filter deeply or not; i.e. whether
50
+ # we should match both with and without 'request.params'
51
+ #
52
+ # This is cached on the instance variable '@deep_filters' for performance
53
+ # reasons
54
+ #
55
+ # @return [Boolean]
56
+ def deep_filters?
57
+ @configuration.meta_data_filters.any? do |filter|
58
+ filter.is_a?(Regexp) && filter.to_s.include?("\\.".freeze)
59
+ end
60
+ end
61
+
62
+ def clean_string(str)
63
+ if defined?(str.encoding) && defined?(Encoding::UTF_8)
64
+ if str.encoding == Encoding::UTF_8
65
+ str.valid_encoding? ? str : str.encode('utf-16', invalid: :replace, undef: :replace).encode('utf-8')
66
+ else
67
+ str.encode('utf-8', invalid: :replace, undef: :replace)
68
+ end
69
+ elsif defined?(Iconv)
70
+ Iconv.conv('UTF-8//IGNORE', 'UTF-8', str) || str
71
+ else
72
+ str
73
+ end
18
74
  end
19
75
 
20
76
  def traverse_object(obj, seen, scope)
@@ -29,11 +85,22 @@ module Bugsnag
29
85
  value = case obj
30
86
  when Hash
31
87
  clean_hash = {}
32
- obj.each do |k,v|
33
- if filters_match_deeply?(k, scope)
34
- clean_hash[k] = FILTERED
35
- else
36
- clean_hash[k] = traverse_object(v, seen, [scope, k].compact.join('.'))
88
+ obj.each do |k, v|
89
+ begin
90
+ current_scope = [scope, k].compact.join('.')
91
+
92
+ if filters_match_deeply?(k, current_scope)
93
+ clean_hash[k] = FILTERED
94
+ else
95
+ clean_hash[k] = traverse_object(v, seen, current_scope)
96
+ end
97
+ # If we get an error here, we assume the key needs to be filtered
98
+ # to avoid leaking things we shouldn't. We also remove the key itself
99
+ # because it may cause issues later e.g. when being converted to JSON
100
+ rescue StandardError
101
+ clean_hash[RAISED] = FILTERED
102
+ rescue SystemStackError
103
+ clean_hash[RECURSION] = FILTERED
37
104
  end
38
105
  end
39
106
  clean_hash
@@ -44,10 +111,23 @@ module Bugsnag
44
111
  when String
45
112
  clean_string(obj)
46
113
  else
47
- str = obj.to_s rescue RAISED
114
+ # guard against objects that raise or blow the stack when stringified
115
+ begin
116
+ str = obj.to_s
117
+ rescue StandardError
118
+ str = RAISED
119
+ rescue SystemStackError
120
+ str = RECURSION
121
+ end
122
+
48
123
  # avoid leaking potentially sensitive data from objects' #inspect output
49
124
  if str =~ /#<.*>/
50
- OBJECT
125
+ # Use id of the object if available
126
+ if obj.respond_to?(:id)
127
+ format(OBJECT_WITH_ID_AND_CLASS, class_name: obj.class, id: obj.id)
128
+ else
129
+ OBJECT
130
+ end
51
131
  else
52
132
  clean_string(str)
53
133
  end
@@ -57,67 +137,56 @@ module Bugsnag
57
137
  value
58
138
  end
59
139
 
60
- def clean_string(str)
61
- if defined?(str.encoding) && defined?(Encoding::UTF_8)
62
- if str.encoding == Encoding::UTF_8
63
- str.valid_encoding? ? str : str.encode('utf-16', ENCODING_OPTIONS).encode('utf-8')
140
+ ##
141
+ # @param key [String, #to_s]
142
+ # @return [Boolean]
143
+ def filters_match?(key)
144
+ str = key.to_s
145
+
146
+ @configuration.meta_data_filters.any? do |filter|
147
+ case filter
148
+ when Regexp
149
+ str.match(filter)
64
150
  else
65
- str.encode('utf-8', ENCODING_OPTIONS)
151
+ str.include?(filter.to_s)
66
152
  end
67
- elsif defined?(Iconv)
68
- Iconv.conv('UTF-8//IGNORE', 'UTF-8', str) || str
69
- else
70
- str
71
153
  end
72
154
  end
73
155
 
74
- def self.clean_object_encoding(obj)
75
- new(nil).clean_object(obj)
76
- end
156
+ ##
157
+ # If someone has a Rails filter like /^stuff\.secret/, it won't match
158
+ # "request.params.stuff.secret", so we try it both with and without the
159
+ # "request.params." bit.
160
+ #
161
+ # @param key [String, #to_s]
162
+ # @param scope [String]
163
+ # @return [Boolean]
164
+ def filters_match_deeply?(key, scope)
165
+ return false unless scope_should_be_filtered?(scope)
77
166
 
78
- def clean_url(url)
79
- return url if @filters.empty?
167
+ return true if filters_match?(key)
168
+ return false unless @deep_filters
80
169
 
81
- uri = URI(url)
82
- return url unless uri.query
170
+ return true if filters_match?(scope)
83
171
 
84
- query_params = uri.query.split('&').map { |pair| pair.split('=') }
85
- query_params.map! do |key, val|
86
- if filters_match?(key)
87
- "#{key}=#{FILTERED}"
172
+ @configuration.scopes_to_filter.any? do |scope_to_filter|
173
+ if scope.start_with?("#{scope_to_filter}.request.params.")
174
+ filters_match?(scope.sub("#{scope_to_filter}.request.params.", ''))
88
175
  else
89
- "#{key}=#{val}"
176
+ filters_match?(scope.sub("#{scope_to_filter}.", ''))
90
177
  end
91
178
  end
92
-
93
- uri.query = query_params.join('&')
94
- uri.to_s
95
179
  end
96
180
 
97
- private
98
-
99
- def filters_match?(key)
100
- str = key.to_s
101
-
102
- @filters.any? do |f|
103
- case f
104
- when Regexp
105
- str.match(f)
106
- else
107
- str.include?(f.to_s)
108
- end
181
+ ##
182
+ # Should the given scope be filtered?
183
+ #
184
+ # @param scope [String]
185
+ # @return [Boolean]
186
+ def scope_should_be_filtered?(scope)
187
+ @configuration.scopes_to_filter.any? do |scope_to_filter|
188
+ scope.start_with?("#{scope_to_filter}.")
109
189
  end
110
190
  end
111
-
112
- # If someone has a Rails filter like /^stuff\.secret/, it won't match "request.params.stuff.secret",
113
- # so we try it both with and without the "request.params." bit.
114
- def filters_match_deeply?(key, scope)
115
- return true if filters_match?(key)
116
- return false unless @deep_filters
117
-
118
- long = [scope, key].compact.join('.')
119
- short = long.sub(/^request\.params\./, '')
120
- filters_match?(long) || filters_match?(short)
121
- end
122
191
  end
123
192
  end