-
Notifications
You must be signed in to change notification settings - Fork 0
Fix security #25
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
Merged
Merged
Fix security #25
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,33 @@ | ||
| import logging | ||
| import traceback | ||
| from typing import Optional | ||
| from rest_framework.response import Response | ||
| from rest_framework import status | ||
|
|
||
| def error_response(message, http_status=status.HTTP_400_BAD_REQUEST): | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def error_response( | ||
| message: Optional[str] = None, | ||
| http_status: int = status.HTTP_400_BAD_REQUEST, | ||
| *, | ||
| exc: Optional[Exception] = None | ||
| ): | ||
| """ | ||
| Generates a response with the ‘error’ field and the specified HTTP status | ||
| Generates a generalized Response with the 'error' field. | ||
| If exc is passed, it logs the stack trace on the server | ||
| """ | ||
| return Response({"error": message}, status=http_status) | ||
| if exc is not None: | ||
| # Logging of a full stack trade | ||
| logger.error("Internal error: %s\n%s", exc, traceback.format_exc()) | ||
|
|
||
| # We return only a general message to the client | ||
| safe_message = message or "An internal server error occurred" | ||
| return Response({"error": safe_message}, status=http_status) | ||
|
|
||
|
|
||
| def status_response(message, http_status=None): | ||
| """ | ||
| Generates a response with the ‘status’ field and the specified HTTP status | ||
| """ | ||
| return Response({"status": message}, status=http_status) | ||
| """ | ||
| return Response({"status": message}, status=http_status) | ||
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
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
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
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.
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium
Copilot Autofix
AI 12 months ago
To fix the issue, we need to ensure that sensitive exception details are not exposed to external users. Instead of directly passing
str(e)as themessageargument toerror_response, we should always provide a generic error message for the client. The detailed exception information should only be logged on the server for debugging purposes. This can be achieved by modifying the calls toerror_responseinusers/views.pyto use a generic error message, while still passing the exception object (exc) for logging purposes.