Description
Returning str(e) or an exception’s args in an HTTP response can disclose file paths, SQL errors, internal hostnames, or configuration values.
Potential impact
Responses may reveal database structure, service names, or the precise reason an authentication or permission check failed. These differences can help someone adjust subsequent inputs.
Remediation
- Use a shared Flask, FastAPI, or Django exception handler to return a generic message and an appropriate HTTP status. Disable framework debug error pages in production.
- Record necessary diagnostic details in access-controlled server logs. Keep passwords, tokens, and other secrets out of those logs.
Examples
Before
python
@app.route("/bad")
def server_bad():
try:
do_computation()
except Exception as e:
return str(e)
After
python
@app.route("/safe")
def server_safe():
try:
do_computation()
except Exception:
current_app.logger.exception("request failed")
return "Internal error", 500
In the Flask example, return str(e) puts the exception in the response body. The revised handler records the error with current_app.logger.exception and sends the client a fixed message with status 500.