|
36 | 36 | from werkzeug.local import LocalProxy |
37 | 37 | from werkzeug.utils import find_modules |
38 | 38 | from jinja2 import select_autoescape |
| 39 | +from flask_wtf.csrf import CSRFError |
39 | 40 |
|
40 | 41 | from pgadmin.model import db, Role, Server, SharedServer, ServerGroup, \ |
41 | 42 | User, Keys, Version, SCHEMA_VERSION as CURRENT_SCHEMA_VERSION |
|
45 | 46 | from pgadmin.utils.versioned_template_loader import VersionedTemplateLoader |
46 | 47 | from datetime import timedelta, datetime |
47 | 48 | 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 |
49 | 51 | from pgadmin.utils.csrf import pgCSRFProtect |
50 | 52 | from pgadmin import authenticate |
51 | 53 | from pgadmin.utils.security_headers import SecurityHeaders |
@@ -915,13 +917,16 @@ def all_exception_handler(e): |
915 | 917 | @app.errorhandler(HTTPException) |
916 | 918 | def http_exception_handler(e): |
917 | 919 | 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)) |
922 | 920 | return e |
923 | 921 |
|
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) + \ |
| 926 | + gettext(' You need to refresh the page.') |
| 927 | + return unauthorized(errormsg=err_msg) |
| 928 | + |
| 929 | + # Initialize the key manager |
925 | 930 | app.keyManager = KeyManager() |
926 | 931 |
|
927 | 932 | ########################################################################## |
|
0 commit comments