Skip to content

Commit

Permalink
Replace recursive values instead of omitting them
Browse files Browse the repository at this point in the history
Instead of ignoring recursive values, replace them with a placeholder
string.
  • Loading branch information
unflxw committed Jul 3, 2023
1 parent 74fd723 commit 85c155a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ bump: "patch"
type: "change"
---

When sanitizing an array or hash, ignore recursively nested values. This fixes a SystemStackError issue when sanitising arrays and hashes.
When sanitizing an array or hash, replace recursively nested values with a placeholder string. This fixes a SystemStackError issue when sanitising arrays and hashes.
16 changes: 10 additions & 6 deletions lib/appsignal/utils/hash_sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Utils
# @api private
class HashSanitizer
FILTERED = "[FILTERED]"
RECURSIVE = "[RECURSIVE VALUE]"

class << self
def sanitize(value, filter_keys = [])
Expand All @@ -31,10 +32,10 @@ def sanitize_hash(source, filter_keys, seen)

{}.tap do |hash|
source.each_pair do |key, value|
next if seen.include?(value.object_id)

hash[key] =
if filter_keys.include?(key.to_s)
if seen.include?(value.object_id)
RECURSIVE
elsif filter_keys.include?(key.to_s)
FILTERED
else
sanitize_value(value, filter_keys, seen)
Expand All @@ -48,9 +49,12 @@ def sanitize_array(source, filter_keys, seen)

[].tap do |array|
source.each_with_index do |item, index|
next if seen.include?(item.object_id)

array[index] = sanitize_value(item, filter_keys, seen)
array[index] =
if seen.include?(item.object_id)
RECURSIVE
else
sanitize_value(item, filter_keys, seen)
end
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions spec/lib/appsignal/utils/hash_sanitizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,22 @@
expect(subject[:file]).to include "::UploadedFile"
end

it "ignores a recursive array" do
expect(subject[:recursive_array]).to be_nil
it "replaces a recursive array" do
expect(subject[:recursive_array]).to eq("[RECURSIVE VALUE]")
end

it "ignores a recursive hash" do
expect(subject[:recursive_hash]).to be_nil
it "replaces a recursive hash" do
expect(subject[:recursive_hash]).to eq("[RECURSIVE VALUE]")
end
end

describe "nested array" do
it "ignores a recursive array" do
expect(sanitized_params[:hash][:nested_array][4]).to be_nil
it "replaces a recursive array" do
expect(sanitized_params[:hash][:nested_array][4]).to eq("[RECURSIVE VALUE]")
end

it "ignores a recursive hash" do
expect(sanitized_params[:hash][:nested_array][5]).to be_nil
it "replaces a recursive hash" do
expect(sanitized_params[:hash][:nested_array][5]).to eq("[RECURSIVE VALUE]")
end
end
end
Expand Down

0 comments on commit 85c155a

Please sign in to comment.