05/02/2026 updated code for security 2d
This commit is contained in:
+10
-9
@@ -110,31 +110,32 @@ def create_app(config_name: str = 'development') -> Flask:
|
||||
# Without this, Flask returns an HTML 500 page which breaks JSON.parse()
|
||||
# in the extension and surfaces as "Unexpected token '<'" to the user.
|
||||
import logging as _logging
|
||||
from flask import request as _request, jsonify as _jsonify
|
||||
_api_log = _logging.getLogger(__name__)
|
||||
|
||||
@app.errorhandler(404)
|
||||
def not_found(e):
|
||||
if request.path.startswith('/api/'):
|
||||
return jsonify({'error': 'Endpoint not found'}), 404
|
||||
if _request.path.startswith('/api/'):
|
||||
return _jsonify({'error': 'Endpoint not found'}), 404
|
||||
return render_template('auth/login.html'), 404
|
||||
|
||||
@app.errorhandler(405)
|
||||
def method_not_allowed(e):
|
||||
if request.path.startswith('/api/'):
|
||||
return jsonify({'error': 'Method not allowed'}), 405
|
||||
if _request.path.startswith('/api/'):
|
||||
return _jsonify({'error': 'Method not allowed'}), 405
|
||||
return render_template('auth/login.html'), 405
|
||||
|
||||
@app.errorhandler(Exception)
|
||||
def handle_exception(e):
|
||||
from werkzeug.exceptions import HTTPException
|
||||
if isinstance(e, HTTPException):
|
||||
if request.path.startswith('/api/'):
|
||||
return jsonify({'error': e.description}), e.code
|
||||
if _request.path.startswith('/api/'):
|
||||
return _jsonify({'error': e.description}), e.code
|
||||
return e
|
||||
# Unhandled exception — log it and return JSON for API routes.
|
||||
_api_log.exception('[PassKeeper] Unhandled exception on %s %s', request.method, request.path)
|
||||
if request.path.startswith('/api/'):
|
||||
return jsonify({'error': 'An internal server error occurred. Please try again.'}), 500
|
||||
_api_log.exception('[PassKeeper] Unhandled exception on %s %s', _request.method, _request.path)
|
||||
if _request.path.startswith('/api/'):
|
||||
return _jsonify({'error': 'An internal server error occurred. Please try again.'}), 500
|
||||
return render_template('auth/login.html'), 500
|
||||
|
||||
# Page-serving routes
|
||||
|
||||
Reference in New Issue
Block a user