Skip to content

Commit 8c9407e

Browse files
Fixed empty cursor string validation (#8240)
1 parent 50f7047 commit 8c9407e

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

src/HotChocolate/Core/src/Types.CursorPagination/IndexCursor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public static unsafe string Format(Span<byte> buffer)
1818

1919
public static unsafe bool TryParse(string cursor, out int index)
2020
{
21+
if (string.IsNullOrWhiteSpace(cursor))
22+
{
23+
index = -1;
24+
return false;
25+
}
26+
2127
fixed (char* cPtr = cursor)
2228
{
2329
var count = _utf8.GetByteCount(cPtr, cursor.Length);

src/HotChocolate/Core/test/Types.CursorPagination.Tests/IntegrationTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,30 @@ public async Task Invalid_After_Index_Cursor()
982982
result.MatchSnapshot();
983983
}
984984

985+
[Fact]
986+
public async Task Invalid_EmptyString_After_Index_Cursor()
987+
{
988+
var executor = await new ServiceCollection()
989+
.AddGraphQL()
990+
.AddQueryType<QueryType>()
991+
.Services.BuildServiceProvider()
992+
.GetRequestExecutorAsync();
993+
994+
var result = await executor.ExecuteAsync(
995+
"""
996+
{
997+
letters(first: 2 after: "") {
998+
edges {
999+
cursor
1000+
}
1001+
}
1002+
}
1003+
"""
1004+
);
1005+
1006+
result.MatchSnapshot();
1007+
}
1008+
9851009
[Fact]
9861010
public async Task Invalid_Before_Index_Cursor()
9871011
{
@@ -1007,6 +1031,31 @@ public async Task Invalid_Before_Index_Cursor()
10071031
result.MatchSnapshot();
10081032
}
10091033

1034+
[Fact]
1035+
public async Task Invalid_EmptyString_Before_Index_Cursor()
1036+
{
1037+
var executor =
1038+
await new ServiceCollection()
1039+
.AddGraphQL()
1040+
.AddQueryType<QueryType>()
1041+
.Services
1042+
.BuildServiceProvider()
1043+
.GetRequestExecutorAsync();
1044+
1045+
var result = await executor.ExecuteAsync(
1046+
"""
1047+
{
1048+
letters(first: 2 before: "") {
1049+
edges {
1050+
cursor
1051+
}
1052+
}
1053+
}
1054+
""");
1055+
1056+
result.MatchSnapshot();
1057+
}
1058+
10101059
[Fact]
10111060
public async Task Simple_EnumerableValueType_ReturnsError()
10121061
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"errors": [
3+
{
4+
"message": "The cursor specified in `after` has an invalid format.",
5+
"locations": [
6+
{
7+
"line": 2,
8+
"column": 3
9+
}
10+
],
11+
"path": [
12+
"letters"
13+
],
14+
"extensions": {
15+
"argument": "after",
16+
"cursor": "",
17+
"code": "HC0078"
18+
}
19+
}
20+
],
21+
"data": {
22+
"letters": null
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"errors": [
3+
{
4+
"message": "The cursor specified in `before` has an invalid format.",
5+
"locations": [
6+
{
7+
"line": 2,
8+
"column": 3
9+
}
10+
],
11+
"path": [
12+
"letters"
13+
],
14+
"extensions": {
15+
"argument": "before",
16+
"cursor": "",
17+
"code": "HC0078"
18+
}
19+
}
20+
],
21+
"data": {
22+
"letters": null
23+
}
24+
}

0 commit comments

Comments
 (0)