Skip to content

Commit

Permalink
YDA-6041: log CSRF errors
Browse files Browse the repository at this point in the history
So that technical admins can see that a redirect occurred because
of a CSRF error.
  • Loading branch information
stsnel committed Dec 10, 2024
1 parent e9d34ed commit 893c825
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion general/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
__copyright__ = 'Copyright (c) 2021-2024, Utrecht University'
__license__ = 'GPLv3, see LICENSE'

from flask import Blueprint, redirect, render_template, Response, url_for
from flask import Blueprint, redirect, render_template, request, Response, session, url_for
from flask_wtf.csrf import CSRFError

from util import log_error

general_bp = Blueprint('general_bp', __name__,
template_folder='templates/general',
static_folder='static/general',
Expand All @@ -19,6 +21,8 @@ def index() -> Response:

@general_bp.app_errorhandler(CSRFError)
def csrf_error(e: Exception) -> Response:
username = session.get("login_username", "N/A")
log_error(f"CSRF error occurred for user {username} on path {request.path}.")
return redirect(url_for('user_bp.login'))


Expand Down

0 comments on commit 893c825

Please sign in to comment.