diff --git a/.changesets/normalize-uploadedfile-objects.md b/.changesets/normalize-uploadedfile-objects.md new file mode 100644 index 00000000..e8182c4a --- /dev/null +++ b/.changesets/normalize-uploadedfile-objects.md @@ -0,0 +1,16 @@ +--- +bump: patch +type: change +--- + +Normalize Rack and Rails `UploadedFile` objects. Instead of displaying the Ruby class name, it will now show object details like the filename and content type. + +``` +# Before +# +# + +# After +# +# +``` diff --git a/lib/appsignal/utils/sample_data_sanitizer.rb b/lib/appsignal/utils/sample_data_sanitizer.rb index 82d41158..4f973b02 100644 --- a/lib/appsignal/utils/sample_data_sanitizer.rb +++ b/lib/appsignal/utils/sample_data_sanitizer.rb @@ -26,7 +26,21 @@ def sanitize_value(value, filter_keys, seen) when Date "#" else - inspected(value) + if defined?(::Rack::Multipart::UploadedFile) && + value.is_a?(::Rack::Multipart::UploadedFile) + "#" + elsif defined?(::ActionDispatch::Http::UploadedFile) && + value.is_a?(::ActionDispatch::Http::UploadedFile) + "#" + else + inspected(value) + end end end diff --git a/spec/lib/appsignal/utils/sample_data_sanitizer_spec.rb b/spec/lib/appsignal/utils/sample_data_sanitizer_spec.rb index 9030abcf..092f4ef4 100644 --- a/spec/lib/appsignal/utils/sample_data_sanitizer_spec.rb +++ b/spec/lib/appsignal/utils/sample_data_sanitizer_spec.rb @@ -32,17 +32,6 @@ def sanitize(value, filter_keys = []) .to eq(:date => "#") end - it "normalizes unsupported objects" do - expect(sanitize(:file => uploaded_file)[:file]) - .to include("::UploadedFile") - - expect(sanitize(:file => [:file => uploaded_file])[:file].first[:file]) - .to include("::UploadedFile") - - expect(sanitize(:object => Object.new)) - .to eq(:object => "#") - end - it "normalizes Time objects" do expect(sanitize(:time_in_utc => Time.utc(2024, 9, 12, 13, 14, 15))) .to eq(:time_in_utc => "#") @@ -51,6 +40,39 @@ def sanitize(value, filter_keys = []) .to eq(:time_with_timezone => "#") end + it "normalizes Rack UploadedFile objects" do + normalized_file = + "#" + expect(sanitize(:file => rack_uploaded_file)) + .to eq(:file => normalized_file) + + expect(sanitize(:file => [:file => rack_uploaded_file])[:file].first[:file]) + .to eq(normalized_file) + end + + if DependencyHelper.rails_present? + it "normalizes Rails ActionDispatch::Http::UploadedFile objects" do + normalized_file = + "#" + expect(sanitize(:file => rails_uploaded_file)) + .to eq(:file => normalized_file) + + expect(sanitize(:file => [:file => rails_uploaded_file])[:file].first[:file]) + .to eq(normalized_file) + end + end + + it "normalizes unsupported objects" do + expect(sanitize(:object => Object.new)) + .to eq(:object => "#") + end + it "accepts nested Hash values" do expect(sanitize(:abc => 123)).to eq(:abc => 123) expect(sanitize("abc" => [456])).to eq("abc" => [456]) diff --git a/spec/support/fixtures/uploaded_file.txt b/spec/support/fixtures/uploaded_file.txt index e69de29b..9f358a4a 100644 --- a/spec/support/fixtures/uploaded_file.txt +++ b/spec/support/fixtures/uploaded_file.txt @@ -0,0 +1 @@ +123456 diff --git a/spec/support/helpers/transaction_helpers.rb b/spec/support/helpers/transaction_helpers.rb index 2af338f2..10ed47db 100644 --- a/spec/support/helpers/transaction_helpers.rb +++ b/spec/support/helpers/transaction_helpers.rb @@ -1,12 +1,30 @@ module TransactionHelpers def uploaded_file if DependencyHelper.rails_present? - ActionDispatch::Http::UploadedFile.new(:tempfile => "/tmp") + rails_uploaded_file else - ::Rack::Multipart::UploadedFile.new(File.join(fixtures_dir, "/uploaded_file.txt")) + rack_uploaded_file end end + def rails_uploaded_file + tempfile = Tempfile.new(uploaded_file_path) + tempfile.write(File.read(uploaded_file_path)) + ActionDispatch::Http::UploadedFile.new( + :tempfile => tempfile, + :type => "text/plain", + :filename => File.basename(uploaded_file_path) + ) + end + + def rack_uploaded_file + ::Rack::Multipart::UploadedFile.new(uploaded_file_path) + end + + def uploaded_file_path + File.join(fixtures_dir, "uploaded_file.txt") + end + def default_namespace Appsignal::Transaction::HTTP_REQUEST end