Skip to content

Return 404 for a cursor whose position is invalid for the ordering field - #10124

Open
mohamed-alired wants to merge 2 commits into
encode:mainfrom
mohamed-alired:fix-cursor-pagination-invalid-position
Open

mohamed-alired wants to merge 2 commits into
encode:mainfrom
mohamed-alired:fix-cursor-pagination-invalid-position

Conversation

@mohamed-alired

Copy link
Copy Markdown
Contributor

Description

CursorPagination.decode_cursor() validates the offset and reverse parts of a cursor, but the position (p) is passed through unchecked and paginate_queryset() filters on it directly. A client can send a well-formed cursor whose position is not a valid value for the ordering field, and the request fails with a 500:

# ordering = 'id', request: GET /?cursor=cD1hYmM=   (base64 of 'p=abc')
ValueError: Field 'id' expected a number but got 'abc'.

# with a UUID ordering field
django.core.exceptions.ValidationError: ['"abc" is not a valid UUID.']

Neither exception is handled by the exception handler. This change treats such a cursor like any other malformed cursor and raises NotFound with invalid_cursor_message ("Invalid cursor"), which is what DRF already returns for a bad offset or bad base64.

Tests: test_invalid_cursor_position in CursorPaginationTestsMixin runs against both the mock queryset and the real model queryset. It fails on main and passes with this change.

CursorPagination.decode_cursor() validates the offset and reverse parts
of a cursor but passes the position through, and paginate_queryset()
filters on it directly. A client-supplied cursor such as base64('p=abc')
on an integer or UUID ordering field therefore raised ValueError or
django.core.exceptions.ValidationError from queryset.filter(), a 500.

Treat it like any other malformed cursor and raise NotFound with the
"Invalid cursor" message.
@auvipy
auvipy self-requested a review November 28, 2026 12:25

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants