Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Reject non-numeric cursors for the ingredient sync endpoint

Roland Geider9c6465662370
diff --git a/wger/nutrition/tests/test_ingredient.py b/wger/nutrition/tests/test_ingredient.py
index 1a4009355..55ce6b27d 100644
--- a/wger/nutrition/tests/test_ingredient.py
+++ b/wger/nutrition/tests/test_ingredient.py
@@ -14,6 +14,7 @@
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
# Standard Library
+import base64
import datetime
import io
import json
@@ -904,6 +905,16 @@ class IngredientSyncViewSetTestCase(WgerTestCase):
self.assertEqual(response.data['results'], [])
self.assertIsNone(response.data['next'])
+ def test_malformed_cursor_is_rejected_cleanly(self):
+ """A cursor whose position is not a valid `id` returns 404, not a 500."""
+
+ # base64 of "p=not-a-number" — a non-numeric position for the integer
+ # ordering field, as produced by truncated or hand-crafted cursors.
+ bad_cursor = base64.b64encode(b'p=not-a-number').decode('ascii')
+ response = self.client.get(self.url, {'cursor': bad_cursor})
+
+ self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
+
def test_unprocessable_image_does_not_break_page(self):
"""An image that cannot be thumbnailed yields `thumbnails: None`, not a 500."""
diff --git a/wger/utils/pagination.py b/wger/utils/pagination.py
index 0d2d726e7..7b990c4bf 100644
--- a/wger/utils/pagination.py
+++ b/wger/utils/pagination.py
@@ -14,6 +14,7 @@
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
# Third Party
+from rest_framework.exceptions import NotFound
from rest_framework.pagination import (
CursorPagination,
LimitOffsetPagination,
@@ -52,3 +53,15 @@ class IngredientCursorPagination(CursorPagination):
page_size_query_param = 'page_size'
ordering = 'id'
cursor_query_param = 'cursor'
+
+ def decode_cursor(self, request):
+ cursor = super().decode_cursor(request)
+
+ # Reject any non-numeric cursors
+ if cursor is not None and cursor.position is not None:
+ try:
+ int(cursor.position)
+ except (TypeError, ValueError):
+ raise NotFound(self.invalid_cursor_message)
+
+ return cursor