diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ccb0882a4..005d6a06b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +### Unreleased + +* bug fixes + * Record the 401 status of a request halted by Warden in the `process_action.action_controller` payload, instead of writing it into the log payload. Rails 8.2 logs from a frozen event payload, so the old way raised `FrozenError` and lost the log line. [#5877](https://github.com/heartcombo/devise/pull/5877) + ### 5.0.4 - 2026-05-08 * security fixes diff --git a/lib/devise/controllers/helpers.rb b/lib/devise/controllers/helpers.rb index 68e8e8d1d6..29cd998044 100644 --- a/lib/devise/controllers/helpers.rb +++ b/lib/devise/controllers/helpers.rb @@ -79,11 +79,6 @@ def current_#{group_name.to_s.pluralize} end METHODS end - - def log_process_action(payload) - payload[:status] ||= 401 unless payload[:exception] - super - end end # Define authentication filters and accessor helpers based on mappings. @@ -279,6 +274,15 @@ def expire_data_after_sign_out! Devise.mappings.each { |_,m| instance_variable_set("@current_#{m.name}", nil) } super end + + # When authentication fails, Warden throws past the controller, so Rails + # records no status for the request. A completed action and a raised + # exception have both set one by now, so a missing status means Warden + # took over, and the failure app answers with 401 or a redirect to sign in. + def append_info_to_payload(payload) + super + payload[:status] ||= 401 + end end end diff --git a/test/integration/authenticatable_test.rb b/test/integration/authenticatable_test.rb index 28d00399bb..0e25ae096c 100644 --- a/test/integration/authenticatable_test.rb +++ b/test/integration/authenticatable_test.rb @@ -559,6 +559,44 @@ class AuthenticationOthersTest < Devise::IntegrationTest end end +class AuthenticationInstrumentationTest < Devise::IntegrationTest + def process_action_statuses + statuses = [] + callback = ->(event) { statuses << event.payload[:status] } + ActiveSupport::Notifications.subscribed(callback, "process_action.action_controller") { yield } + statuses + end + + test 'a request Warden halts is instrumented with a 401 status' do + statuses = process_action_statuses { get users_path } + assert_redirected_to new_user_session_path + assert_equal [401], statuses + end + + test 'a request Warden halts is logged as 401' do + log = StringIO.new + logger, ActionController::Base.logger = ActionController::Base.logger, ActiveSupport::Logger.new(log) + get users_path + assert_match "Completed 401 Unauthorized", log.string + ensure + ActionController::Base.logger = logger + end + + test 'a request that raises keeps the status of its exception' do + HomeController.any_instance.stubs(:index).raises(ActiveRecord::RecordNotFound) + statuses = process_action_statuses do + assert_raise(ActiveRecord::RecordNotFound) { get root_path } + end + assert_equal [404], statuses + end + + test 'a completed request keeps its own status' do + sign_in_as_user + statuses = process_action_statuses { get users_path } + assert_equal [200], statuses + end +end + class AuthenticationKeysTest < Devise::IntegrationTest test 'missing authentication keys cause authentication to abort' do swap Devise, authentication_keys: [:subdomain] do