Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

truncator: fix circular references #286

Merged
merged 2 commits into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Airbrake Ruby Changelog

### master

* Fixed circular references in the new truncator
([#286](https://github.com/airbrake/airbrake-ruby/pull/286)). All v2.6.0 users
are *highly recommended* to upgrade.

### [v2.6.0][v2.6.0] (November 9, 2017)

* Reworked truncation to not mutate given payload (params) and made it freeze it
Expand Down
16 changes: 14 additions & 2 deletions lib/airbrake-ruby/truncator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ class Truncator
# strings with +ENCODING_OPTIONS+
TEMP_ENCODING = 'utf-16'.freeze

# @return [String] what to append when something is a circular reference
CIRCULAR = '[Circular]'.freeze

# @return [String] what to append when something is truncated
TRUNCATED = '[Truncated]'.freeze

# @return [Array<Class>] The types that can contain references to itself
CIRCULAR_TYPES = [Array, Hash, Set].freeze

# @param [Integer] max_size maximum size of hashes, arrays and strings
def initialize(max_size)
@max_size = max_size
Expand All @@ -24,7 +33,10 @@ def initialize(max_size)
# @param [Set] seen The cache that helps to detect recursion
# @return [Object] truncated object
def truncate(object, seen = Set.new)
return '[Circular]'.freeze if seen.include?(object)
if seen.include?(object)
return CIRCULAR if CIRCULAR_TYPES.any? { |t| object.is_a?(t) }
return object
end
truncate_object(object, seen << object)
end

Expand All @@ -51,7 +63,7 @@ def truncate_object(object, seen)
def truncate_string(str)
fixed_str = replace_invalid_characters(str)
return fixed_str if fixed_str.length <= @max_size
(fixed_str.slice(0, @max_size) + '[Truncated]').freeze
(fixed_str.slice(0, @max_size) + TRUNCATED).freeze
end

def stringify_object(object)
Expand Down
23 changes: 23 additions & 0 deletions spec/truncator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,28 @@ def obj.to_json
expect(subject).to be_frozen
end
end

context "given an array with hashes and hash-like objects with identical keys" do
let(:hashie) { Class.new(Hash) }

let(:object) do
{
errors: [
{ file: 'a' },
hashie.new.merge(file: 'bcde')
]
}
end

it "truncates values" do
expect(subject).to eq(
errors: [
{ file: 'a' },
hashie.new.merge(file: 'bcd[Truncated]')
]
)
expect(subject).to be_frozen
end
end
end
end