Skip to content

Commit 31f0c91

Browse files
committed
Do not process ActionController if params is not a Hash
1 parent 6fac221 commit 31f0c91

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

lib/rails_semantic_logger/action_controller/log_subscriber.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,22 @@ def process_action(event)
1414

1515
# Unused, but needed for Devise 401 status code monkey patch to still work.
1616
::ActionController::Base.log_process_action(payload)
17+
18+
params = payload[:params]
1719

18-
# According to PR https://github.com/reidmorrison/rails_semantic_logger/pull/37/files
19-
# payload[:params] is not always a Hash.
20-
payload[:params] = payload[:params].to_unsafe_h unless payload[:params].is_a?(Hash)
21-
payload[:params] = payload[:params].except(*INTERNAL_PARAMS)
22-
payload.delete(:params) if payload[:params].empty?
20+
if params.kind_of?(Hash) || params.kind_of?(::ActionController::Parameters)
21+
# According to PR https://github.com/reidmorrison/rails_semantic_logger/pull/37/files
22+
# params is not always a Hash.
23+
payload[:params] = params.to_unsafe_h unless params.is_a?(Hash)
24+
payload[:params] = params.except(*INTERNAL_PARAMS)
25+
26+
if payload[:params].empty?
27+
payload.delete(:params)
28+
elsif params["file"]
29+
# When logging to JSON the entire tempfile is logged, so convert it to a string.
30+
payload[:params]["file"] = params["file"].inspect
31+
end
32+
end
2333

2434
format = payload[:format]
2535
payload[:format] = format.to_s.upcase if format.is_a?(Symbol)
@@ -48,12 +58,6 @@ def process_action(event)
4858
payload.delete(:request)
4959
payload.delete(:response)
5060

51-
params = payload[:params]
52-
if params
53-
# When logging to JSON the entire tempfile is logged, so convert it to a string.
54-
params["file"] = params["file"].inspect if params["file"]
55-
end
56-
5761
{
5862
message: "Completed ##{payload[:action]}",
5963
duration: event.duration,

test/action_controller_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require_relative "test_helper"
2+
3+
class ActionControllerTest < Minitest::Test
4+
describe "RailsSemanticLogger::ActionController::LogSubscriber" do
5+
let(:subscriber) { RailsSemanticLogger::ActionController::LogSubscriber.new }
6+
7+
describe "#process_action" do
8+
it "does not process if params is not a Hash nor an instance of ActionController::Parameters" do
9+
event = ActiveSupport::Notifications::Event.new(
10+
"start_processing.action_controller",
11+
5.seconds.ago,
12+
Time.zone.now,
13+
SecureRandom.uuid,
14+
{
15+
payload: "{}"
16+
}
17+
)
18+
19+
messages = semantic_logger_events do
20+
subscriber.process_action(event)
21+
end
22+
23+
assert_equal 1, messages.count, messages
24+
end
25+
end
26+
end
27+
end

0 commit comments

Comments
 (0)