Skip to content

Commit ec34db9

Browse files
authored
Add max_nesting of 10 to breadcrumbs data serialization (#2583)
This is [enforced in [relay](https://github.com/getsentry/relay/blob/bcbfa7e7db6e4a8f3f0383270fbb2b5cfe668770/relay-event-schema/src/protocol/breadcrumb.rs#L111) at a depth of 5. We allow upto 10 to have some leeway but if we have a really large data object we just drop it to avoid SystemStackError. Closes #2393 and #2397
1 parent bfa8b49 commit ec34db9

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Features
44

55
- Add new sidekiq config `report_only_dead_jobs` ([#2581](https://github.com/getsentry/sentry-ruby/pull/2581))
6+
- Add `max_nesting` of 10 to breadcrumbs data serialization ([#2583](https://github.com/getsentry/sentry-ruby/pull/2583))
67

78
### Bug Fixes
89

sentry-ruby/lib/sentry/breadcrumb.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module Sentry
44
class Breadcrumb
5+
MAX_NESTING = 10
56
DATA_SERIALIZATION_ERROR_MESSAGE = "[data were removed due to serialization issues]"
67

78
# @return [String, nil]
@@ -60,7 +61,7 @@ def level=(level) # needed to meet the Sentry spec
6061

6162
def serialized_data
6263
begin
63-
::JSON.parse(::JSON.generate(@data))
64+
::JSON.parse(::JSON.generate(@data, max_nesting: MAX_NESTING))
6465
rescue Exception => e
6566
Sentry.logger.debug(LOGGER_PROGNAME) do
6667
<<~MSG

sentry-ruby/spec/sentry/breadcrumb_spec.rb

+25-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
require "spec_helper"
44

55
RSpec.describe Sentry::Breadcrumb do
6+
let(:stringio) { StringIO.new }
7+
68
before do
7-
perform_basic_setup
9+
perform_basic_setup do |config|
10+
config.logger = ::Logger.new(stringio)
11+
end
812
end
913

1014
let(:crumb) do
@@ -77,6 +81,16 @@
7781
)
7882
end
7983

84+
let(:very_deep_crumb) do
85+
data = [[[[[ { a: [{ b: [[{ c: 4 }]] }] }]]]]]
86+
87+
Sentry::Breadcrumb.new(
88+
category: "cow",
89+
message: "I cause too much recursion",
90+
data: data
91+
)
92+
end
93+
8094
it "serializes data correctly" do
8195
result = crumb.to_hash
8296

@@ -91,6 +105,16 @@
91105
expect(result[:category]).to eq("baz")
92106
expect(result[:message]).to eq("I cause issues")
93107
expect(result[:data]).to eq("[data were removed due to serialization issues]")
108+
expect(stringio.string).to match(/can't serialize breadcrumb data because of error: nesting of 10 is too deep/)
109+
end
110+
111+
it "rescues data serialization issue for extremely nested data and ditch the data" do
112+
result = very_deep_crumb.to_hash
113+
114+
expect(result[:category]).to eq("cow")
115+
expect(result[:message]).to eq("I cause too much recursion")
116+
expect(result[:data]).to eq("[data were removed due to serialization issues]")
117+
expect(stringio.string).to match(/can't serialize breadcrumb data because of error: nesting of 10 is too deep/)
94118
end
95119
end
96120
end

0 commit comments

Comments
 (0)