Skip to content

Commit b0a7870

Browse files
Adan Amarillascarlosantoniodasilva
authored andcommitted
Return 401 for sessions#destroy action with no user signed in (#4878)
It's an unauthenticated request, so return 401 Unauthorized like most other similar requests. Signed-off-by: Carlos Antonio da Silva <[email protected]>
1 parent 05bbc71 commit b0a7870

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
* Handle `on` and `ON` as true values to check params [#5514](https://github.com/heartcombo/devise/pull/5514)
5454
* Fix passing `format` option to `devise_for` [#5732](https://github.com/heartcombo/devise/pull/5732)
5555
* Use `ActiveRecord::SecurityUtils.secure_compare` in `Devise.secure_compare` to match two empty strings correctly. [#4829](https://github.com/heartcombo/devise/pull/4829)
56+
* Respond with `401 Unauthorized` for non-navigational requests to destroy the session when there is no authenticated resource. [#4878](https://github.com/heartcombo/devise/pull/4878)
5657
5758
5859
Please check [4-stable](https://github.com/heartcombo/devise/blob/4-stable/CHANGELOG.md)

app/controllers/devise/sessions_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def destroy
2828
signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
2929
set_flash_message! :notice, :signed_out if signed_out
3030
yield if block_given?
31-
respond_to_on_destroy
31+
respond_to_on_destroy(status: :no_content)
3232
end
3333

3434
protected
@@ -62,7 +62,7 @@ def verify_signed_out_user
6262
if all_signed_out?
6363
set_flash_message! :notice, :already_signed_out
6464

65-
respond_to_on_destroy
65+
respond_to_on_destroy(status: :unauthorized)
6666
end
6767
end
6868

@@ -72,11 +72,11 @@ def all_signed_out?
7272
users.all?(&:blank?)
7373
end
7474

75-
def respond_to_on_destroy
75+
def respond_to_on_destroy(status: status)
7676
# We actually need to hardcode this as Rails default responder doesn't
7777
# support returning empty response on GET request
7878
respond_to do |format|
79-
format.all { head :no_content }
79+
format.all { head status }
8080
format.any(*navigational_formats) { redirect_to after_sign_out_path_for(resource_name), status: Devise.responder.redirect_status }
8181
end
8282
end

test/controllers/sessions_controller_test.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class SessionsControllerTest < Devise::ControllerTestCase
7474
assert_template "devise/sessions/new"
7575
end
7676

77-
test "#destroy doesn't set the flash if the requested format is not navigational" do
77+
test "#destroy doesn't set the flash and returns 204 status if the requested format is not navigational" do
7878
request.env["devise.mapping"] = Devise.mappings[:user]
7979
user = create_user
8080
user.confirm
@@ -87,4 +87,16 @@ class SessionsControllerTest < Devise::ControllerTestCase
8787
assert flash[:notice].blank?, "flash[:notice] should be blank, not #{flash[:notice].inspect}"
8888
assert_equal 204, @response.status
8989
end
90+
91+
test "#destroy returns 401 status if user is not signed in and the requested format is not navigational" do
92+
request.env["devise.mapping"] = Devise.mappings[:user]
93+
delete :destroy, format: 'json'
94+
assert_equal 401, @response.status
95+
end
96+
97+
test "#destroy returns 302 status if user is not signed in and the requested format is navigational" do
98+
request.env["devise.mapping"] = Devise.mappings[:user]
99+
delete :destroy
100+
assert_equal 302, @response.status
101+
end
90102
end

0 commit comments

Comments
 (0)