Skip to content

Commit

Permalink
Fixed crash when missing CSRF token cookie in SessionAuthentication
Browse files Browse the repository at this point in the history
Fixes #1651
  • Loading branch information
FrancoisDupayrat committed Mar 21, 2023
1 parent 9f6bc63 commit 51e30d2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion tastypie/authentication.py
Expand Up @@ -312,7 +312,10 @@ def is_authenticated(self, request, **kwargs):
return request.user.is_authenticated
csrf_token = request.COOKIES.get(settings.CSRF_COOKIE_NAME, '')

csrf_token = check_token_format(csrf_token)
try:
csrf_token = check_token_format(csrf_token)
except InvalidTokenFormat:
return False

if request.is_secure():
referer = request.META.get('HTTP_REFERER')
Expand Down
2 changes: 2 additions & 0 deletions tests/core/tests/authentication.py
Expand Up @@ -471,6 +471,8 @@ def test_apikey_and_authentication_enforce_user(self):
self.assertEqual(session_auth.is_authenticated(request1), True)
# api key auth should fail because of invalid api key
self.assertEqual(isinstance(api_key_auth.is_authenticated(request2), HttpUnauthorized), True)
# multi auth should fail because there is no valid auth
self.assertEqual(isinstance(auth.is_authenticated(request2), HttpUnauthorized), True)

# multi auth shouldn't change users if api key auth fails
# multi auth passes since session auth is valid
Expand Down

0 comments on commit 51e30d2

Please sign in to comment.