From ae5b83be2fb36ba699955524392f509a1f83c2ca Mon Sep 17 00:00:00 2001 From: Noemi <45180344+unflxw@users.noreply.github.com> Date: Tue, 12 Nov 2024 16:30:07 +0100 Subject: [PATCH] Ensure the original hashes are not modified The customer might pass a hash that is used someplace else in their application. We should not modify the given hash. (We just saw this issue with appsignal/appsignal-javascript#639) --- lib/appsignal/logger.rb | 2 +- spec/lib/appsignal/logger_spec.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/appsignal/logger.rb b/lib/appsignal/logger.rb index 71dc3b5b..a0dc2fb4 100644 --- a/lib/appsignal/logger.rb +++ b/lib/appsignal/logger.rb @@ -162,7 +162,7 @@ def silence(_severity = ERROR, &block) attr_reader :default_attributes def add_with_attributes(severity, message, group, attributes) - Thread.current[:appsignal_logger_attributes] = default_attributes.merge!(attributes) + Thread.current[:appsignal_logger_attributes] = default_attributes.merge(attributes) add(severity, message, group) ensure Thread.current[:appsignal_logger_attributes] = nil diff --git a/spec/lib/appsignal/logger_spec.rb b/spec/lib/appsignal/logger_spec.rb index 22121d29..752b8b86 100644 --- a/spec/lib/appsignal/logger_spec.rb +++ b/spec/lib/appsignal/logger_spec.rb @@ -190,6 +190,17 @@ Appsignal::Utils::Data.generate({ :other_key => "other_value", :some_key => "some_value" })) logger.error("Some message", { :other_key => "other_value" }) end + + it "does not modify the original attribute hashes passed" do + default_attributes = { :some_key => "some_value" } + logger = Appsignal::Logger.new("group", :attributes => default_attributes) + + line_attributes = { :other_key => "other_value" } + logger.error("Some message", line_attributes) + + expect(default_attributes).to eq({ :some_key => "some_value" }) + expect(line_attributes).to eq({ :other_key => "other_value" }) + end end describe "#error with exception object" do