feat: harden global exception handlers — prevent traceback leakage and unify error responses#434
Open
Arijit429 wants to merge 2 commits intofireform-core:mainfrom
Open
Conversation
- Add HTTPException handler for consistent error shape across all routes
- Add RequestValidationError handler with human-readable error messages
- Add catch-all Exception handler to prevent stack trace leakage
- Fix duplicate get_template() call in forms.py (was querying DB twice)
- Wrap Controller errors in AppError for safe client-facing messages
- All errors now return uniform {success, error: {code, message}} envelope
Author
ContextThe existing Also fixed a duplicate |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #82
Closes #145
Closes #311
Closes #394
Closes #295
Closes #374
Summary
Hardens the global exception handling layer to prevent internal stack traces
from leaking to API clients, and unifies all error responses into a consistent
JSON envelope.
Problem
The current
handlers.pyonly catchesAppError. Three other error categoriesare completely unhandled:
error arrays with internal field paths
templates.pyroutes use HTTPException which returnsa different shape than
forms.pywhich uses AppErrorController()crashes, the full Python stacktrace including file paths and line numbers is returned to the client
This is a security risk (OWASP: Security Misconfiguration).
Changes
api/errors/handlers.pyHTTPExceptionhandler for consistent error shapeRequestValidationErrorhandler with human-readable messagesExceptionhandler — logs server-side, returns generic message{"success": false, "error": {"code": "...", "message": "..."}}api/errors/base.pysuper().__init__(message)for proper exception chainingapi/routes/forms.pyget_template()call (was querying database twice per request)Controller.fill_form()in try/except for crash protectionapi/main.pyregister_exception_handlers(app)is called at startupTesting
Verified all four error paths return uniform JSON:
422— validation errors (malformed body)404— application errors (template not found)400— HTTP errors (non-PDF upload)500— unhandled errors (generic message, no stack trace)Existing test suite passes without modification.
Real-world impact
Prevents internal stack traces from leaking to the frontend — important for
CAL FIRE deployment where error logs may be reviewed by non-technical staff.