-
Notifications
You must be signed in to change notification settings - Fork 475
Add EventNotificationHandler example #1701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| handler.handle(webhook_body, sig_header) | ||
| return jsonify(success=True), 200 | ||
| except Exception as e: | ||
| return jsonify(error=str(e)), 500 |
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium
Stack trace information
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 10 days ago
To fix the information exposure, we should avoid returning internal error messages or exception details (like str(e)) to API clients. Instead, log the actual exception—including the traceback if desired—using server-side logging (for example, using Python's standard logging module), and return a generic error message to the client.
The fix requires:
- Importing
loggingat the top of the file if not already present. - Optionally configuring the logger if desired, or just using the root logger.
- In the exception handler, log the full exception and traceback using
logging.exception()(or similar). - Return a generic message in the response, such as
"An internal error has occurred.", rather than user-facing exception details.
The code to change is in the exception handler in the webhook() function, specifically on lines 54–55. Logging should be done inside the except block, before returning the generic message.
-
Copy modified line R14 -
Copy modified lines R55-R56
| @@ -11,7 +11,7 @@ | ||
|
|
||
| import os | ||
| from flask import Flask, request, jsonify | ||
|
|
||
| import logging | ||
| from stripe import StripeClient, UnhandledNotificationDetails | ||
| from stripe.v2.core import EventNotification | ||
| from stripe.events import V1BillingMeterErrorReportTriggeredEventNotification | ||
| @@ -52,4 +52,5 @@ | ||
| handler.handle(webhook_body, sig_header) | ||
| return jsonify(success=True), 200 | ||
| except Exception as e: | ||
| return jsonify(error=str(e)), 500 | ||
| logging.exception("Exception occurred while handling webhook") | ||
| return jsonify(error="An internal error has occurred."), 500 |
Why?
The hosted docs were a little late merging, so I wanted to make sure they got linked in the changelog. Plus I added the fully-working example from the original PR to the actual code. I also took the opportunity to make sure the hosted docs matched these working examples.
What?
See Also