Skip to content
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

fix: value error exception for non numeric project id #2741

Merged
merged 1 commit into from
Nov 25, 2024

Conversation

ukanga
Copy link
Member

@ukanga ukanga commented Nov 25, 2024

Changes / Features implemented

Validates project ID and form ID are numeric when handling submissions.

Steps taken to verify this change does what is intended

Try post a submission to /projects/not_numeric/submission.

Side effects of implementing this change

N/A

Before submitting this PR for review, please make sure you have:

  • Included tests
  • Updated documentation

Closes #2719

try:
request, __ = get_request_and_username(self.context)
except ValueError as exc:
raise serializers.ValidationError(str(exc))

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.

Copilot Autofix AI about 2 months ago

To fix the problem, we should avoid exposing the detailed exception message to the end user. Instead, we should log the detailed error message on the server and return a generic error message to the user. This can be achieved by using Python's logging module to log the exception details and raising a serializers.ValidationError with a generic message.

Suggested changeset 1
onadata/libs/serializers/data_serializer.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/onadata/libs/serializers/data_serializer.py b/onadata/libs/serializers/data_serializer.py
--- a/onadata/libs/serializers/data_serializer.py
+++ b/onadata/libs/serializers/data_serializer.py
@@ -311,3 +311,6 @@
         except ValueError as exc:
-            raise serializers.ValidationError(str(exc))
+            import logging
+            logger = logging.getLogger(__name__)
+            logger.error("Error in getting request and username", exc_info=True)
+            raise serializers.ValidationError(_("An internal error has occurred. Please try again later."))
         if not request.FILES or "xml_submission_file" not in request.FILES:
EOF
@@ -311,3 +311,6 @@
except ValueError as exc:
raise serializers.ValidationError(str(exc))
import logging
logger = logging.getLogger(__name__)
logger.error("Error in getting request and username", exc_info=True)
raise serializers.ValidationError(_("An internal error has occurred. Please try again later."))
if not request.FILES or "xml_submission_file" not in request.FILES:
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
@ukanga ukanga merged commit e221c8b into main Nov 25, 2024
10 checks passed
@ukanga ukanga deleted the fix-value-error-submissions branch November 25, 2024 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ValueError: Field 'id' expected a number but got '146we'.
3 participants