Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add validation for incorrect cursor shape (#19153)

Lukasz Ostrowski46456e7317ca
diff --git a/saleor/graphql/core/connection.py b/saleor/graphql/core/connection.py
index 844f151e2a..f3fee597df 100644
--- a/saleor/graphql/core/connection.py
+++ b/saleor/graphql/core/connection.py
@@ -44,7 +44,10 @@ def to_global_cursor(values):
def from_global_cursor(cursor) -> list[str]:
values = unbase64(cursor)
- return orjson.loads(values)
+ parsed = orjson.loads(values)
+ if not isinstance(parsed, list):
+ raise ValueError("Cursor must decode to a JSON array.")
+ return parsed
def get_field_value(instance: DjangoModel, field_name: str):
diff --git a/saleor/graphql/core/tests/test_pagination.py b/saleor/graphql/core/tests/test_pagination.py
index 3d3e141855..fe7c28bec6 100644
--- a/saleor/graphql/core/tests/test_pagination.py
+++ b/saleor/graphql/core/tests/test_pagination.py
@@ -2,6 +2,7 @@ import base64
import math
import graphene
+import orjson
import pytest
from ....tests.models import Book
@@ -265,6 +266,16 @@ def test_pagination_invalid_cursor_and_valid_base64(books):
assert str(result.errors[0]) == "Received cursor is invalid."
+def test_pagination_cursor_decodes_to_non_list(books):
+ cursor = base64.b64encode(orjson.dumps({"id": "abc"})).decode("utf-8")
+ variables = {"first": 5, "after": cursor}
+
+ result = schema.execute(QUERY_PAGINATION_TEST, variables=variables)
+
+ assert len(result.errors) == 1
+ assert str(result.errors[0]) == "Received cursor is invalid."
+
+
QUERY_PAGINATION_WITH_FRAGMENTS = """
fragment BookFragment on BookType {
name