GreedyBear icon indicating copy to clipboard operation
GreedyBear copied to clipboard

Logging out does not end the Django admin session of a superuser

Open Prasad8830 opened this issue 1 month ago • 3 comments

When a superuser logs in, LoginView.post in authentication/views.py also calls Django's login(request, request.user), which creates a server-side sessionid. Logout is the plain certego LogoutView, which only deletes the durin token and the token cookie. It never calls Django's logout(), so the sessionid stays valid on the server.

Reproduction on current develop, with a superuser:

# log in, keep the cookies
curl -s -c jar -X POST http://localhost/api/auth/login \
  -H 'Content-Type: application/json' \
  -d '{"username":"<superuser>","password":"<password>"}'

# log out (204)
CSRF=$(awk '/csrftoken/{print $7}' jar)
curl -s -b jar -c jar -X POST http://localhost/api/auth/logout -H "X-CSRFToken: $CSRF"

# the API now rejects the cookie (401) ...
curl -s -b jar http://localhost/api/auth/authentication

# ... but the same cookie jar, which still holds the sessionid, gets into the admin
curl -s -b jar -o /dev/null -w '%{http_code}\n' http://localhost/admin/greedybear/ioc/

The last request returns 200 with the real admin changelist. Without the cookie it is a 302 to /admin/login/.

The same applies to "Revoke other sessions" (revoke_other_tokens): it only deletes AuthToken rows, so the admin sessions of the other devices stay alive. Changing the password is not affected, I checked that Django's session auth hash invalidates the old admin sessions in that case (302 afterwards).

So a stolen or left-behind sessionid keeps full admin access until the session expires (Django's default is two weeks), even after the user logged out or revoked their sessions.

I searched the existing issues and PRs (#1725, #1282, #893 and so on) and they only deal with tokens and the frontend logout callback, not this.

A possible fix: override LogoutView locally to also call django.contrib.auth.logout(request), and make revoke_other_tokens delete the user's other rows from django_session as well. Happy to work on this if the approach sounds ok.

Prasad8830 avatar Oct 31 '26 12:10 Prasad8830

Hi @regulartim, could you please validate this bug? Thanks.

Prasad8830 avatar Oct 31 '26 12:10 Prasad8830

Hey @Prasad8830 ! Thanks, great catch! Your proposed fix seems the right thing to do, I'll assign you.

regulartim avatar Nov 06 '26 07:11 regulartim

Hi @regulartim, raised a PR according to my proposed approach.

Prasad8830 avatar Nov 13 '26 05:11 Prasad8830