-
Notifications
You must be signed in to change notification settings - Fork 175
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
Validate UUID Format in Public API Requests to Prevent 500 Errors #4794
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
import re | ||
from collections import OrderedDict | ||
from functools import reduce | ||
from uuid import UUID | ||
|
||
from django.core.exceptions import ValidationError | ||
from django.db.models import Exists | ||
|
@@ -35,6 +36,7 @@ | |
from kolibri_public.search import get_available_metadata_labels | ||
from kolibri_public.stopwords import stopwords_set | ||
from le_utils.constants import content_kinds | ||
from rest_framework import status | ||
from rest_framework.permissions import AllowAny | ||
from rest_framework.response import Response | ||
|
||
|
@@ -45,7 +47,6 @@ | |
from contentcuration.viewsets.base import BaseValuesViewset | ||
from contentcuration.viewsets.base import ReadOnlyValuesViewset | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
|
@@ -697,8 +698,15 @@ def retrieve(self, request, pk=None): | |
:return: an object representing the parent with a pagination object as "children" | ||
""" | ||
|
||
queryset = self.get_tree_queryset(request, pk) | ||
try: | ||
UUID(pk) | ||
except ValueError: | ||
return Response( | ||
{"error": "Invalid UUID format."}, | ||
status=status.HTTP_400_BAD_REQUEST | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, can 404. As this is mostly just copy pasted from Kolibri, we could just update the (this is useful because it means we continue to keep the two implementations as parallel as possible). |
||
) | ||
|
||
queryset = self.get_tree_queryset(request, pk) | ||
# We explicitly order by lft here, so that the nodes are in tree traversal order, so we can iterate over them and build | ||
# out our nested representation, being sure that any ancestors have already been processed. | ||
nodes = self.serialize(queryset.order_by("lft")) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that this is a retrieve operation, a
404
would be fine here.Also, you could just put a try catch around:
Catch the value error and raise a 404 there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like in Kolibri that line was updated to use
self.get_object()
- not sure if that's catching the ValueError or not, but I suspect it may be: https://github.com/learningequality/kolibri/blob/develop/kolibri/core/content/public_api.py#L75