Skip to content

Commit 2f89cd3

Browse files
Ensure CSRF errors handled to return unauthorized response.#8065
1 parent 4132028 commit 2f89cd3

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

web/pgadmin/__init__.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from werkzeug.local import LocalProxy
3737
from werkzeug.utils import find_modules
3838
from jinja2 import select_autoescape
39+
from flask_wtf.csrf import CSRFError
3940

4041
from pgadmin.model import db, Role, Server, SharedServer, ServerGroup, \
4142
User, Keys, Version, SCHEMA_VERSION as CURRENT_SCHEMA_VERSION
@@ -45,7 +46,8 @@
4546
from pgadmin.utils.versioned_template_loader import VersionedTemplateLoader
4647
from datetime import timedelta, datetime
4748
from pgadmin.setup import get_version, set_version, check_db_tables
48-
from pgadmin.utils.ajax import internal_server_error, make_json_response
49+
from pgadmin.utils.ajax import internal_server_error, make_json_response, \
50+
unauthorized
4951
from pgadmin.utils.csrf import pgCSRFProtect
5052
from pgadmin import authenticate
5153
from pgadmin.utils.security_headers import SecurityHeaders
@@ -915,13 +917,15 @@ def all_exception_handler(e):
915917
@app.errorhandler(HTTPException)
916918
def http_exception_handler(e):
917919
current_app.logger.error(e, exc_info=True)
918-
if e.code == 400 and\
919-
e.description == 'The CSRF session token is missing.':
920-
error = str(e.description) + 'Please refresh the page.'
921-
return internal_server_error(errormsg=gettext(error))
922920
return e
923921

924-
# Intialize the key manager
922+
# Send unauthorized response if CSRF errors occurs.
923+
@app.errorhandler(CSRFError)
924+
def handle_csrf_error(error):
925+
err_msg = str(error.description) + ' You need to refresh the page.'
926+
return unauthorized(errormsg=gettext(err_msg))
927+
928+
# Initialize the key manager
925929
app.keyManager = KeyManager()
926930

927931
##########################################################################

0 commit comments

Comments
 (0)