Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 9 additions & 5 deletions lib/devise/controllers/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
38 changes: 38 additions & 0 deletions test/integration/authenticatable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down