Skip to content

Commit

Permalink
csrf: improve validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcastro2 authored and kpsherva committed Jul 4, 2024
1 parent b564e88 commit 8f10125
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions invenio_rest/csrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ def _abort400(reason):

def csrf_validate():
"""Check CSRF cookie against request headers."""
# If the cookie is not set, we don't need to check anything.
if not request.cookies:
return

csrf_token = _get_csrf_token()
if csrf_token is None:
return _abort400(REASON_NO_CSRF_COOKIE)

request_csrf_token = _get_submitted_csrf_token()
if not request_csrf_token:
_abort400(REASON_BAD_TOKEN)

if request.is_secure:
referer = request.referrer

Expand All @@ -163,14 +175,6 @@ def csrf_validate():
reason = REASON_BAD_REFERER % referer.geturl()
return _abort400(reason)

csrf_token = _get_csrf_token()
if csrf_token is None:
return _abort400(REASON_NO_CSRF_COOKIE)

request_csrf_token = _get_submitted_csrf_token()
if not request_csrf_token:
_abort400(REASON_BAD_TOKEN)

decoded_request_csrf_token = _decode_csrf(request_csrf_token)

if csrf_token != decoded_request_csrf_token:
Expand Down

0 comments on commit 8f10125

Please sign in to comment.