Skip to content

Commit 3b40be3

Browse files
committed
Fixed value overflows break query validation.
1 parent 1e562c2 commit 3b40be3

File tree

5 files changed

+91
-12
lines changed

5 files changed

+91
-12
lines changed

src/HotChocolate/Core/src/Types/Types/Scalars/IntType.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public IntType(
5050
Description = description;
5151
}
5252

53-
protected override int ParseLiteral(IntValueNode valueSyntax) =>
54-
valueSyntax.ToInt32();
53+
protected override int ParseLiteral(IntValueNode valueSyntax)
54+
=> valueSyntax.ToInt32();
5555

56-
protected override IntValueNode ParseValue(int runtimeValue) =>
57-
new(runtimeValue);
56+
protected override IntValueNode ParseValue(int runtimeValue)
57+
=> new(runtimeValue);
5858
}

src/HotChocolate/Core/src/Types/Types/Scalars/IntegerTypeBase.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ protected IntegerTypeBase(
2727

2828
protected override bool IsInstanceOfType(IntValueNode valueSyntax)
2929
{
30-
return IsInstanceOfType(ParseLiteral(valueSyntax));
30+
try
31+
{
32+
return IsInstanceOfType(ParseLiteral(valueSyntax));
33+
}
34+
catch (InvalidFormatException)
35+
{
36+
return false;
37+
}
3138
}
3239

3340
protected override bool IsInstanceOfType(TRuntimeType runtimeValue)

src/HotChocolate/Core/src/Validation/Rules/ValueVisitor.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ protected override ISyntaxVisitorAction Enter(
222222
inputObjectType));
223223
}
224224
else if (value.Value.Kind is SyntaxKind.Variable &&
225-
!IsInstanceOfType(context, new NonNullType(field.Type), value.Value))
225+
!TryIsInstanceOfType(context, new NonNullType(field.Type), value.Value))
226226
{
227227
context.ReportError(
228228
context.OneOfVariablesMustBeNonNull(
@@ -331,7 +331,7 @@ protected override ISyntaxVisitorAction Enter(
331331
if (context.Types.TryPeek(out IType? currentType) &&
332332
currentType is IInputType locationType)
333333
{
334-
if (valueNode.IsNull() || IsInstanceOfType(context, locationType, valueNode))
334+
if (valueNode.IsNull() || TryIsInstanceOfType(context, locationType, valueNode))
335335
{
336336
return Skip;
337337
}
@@ -347,7 +347,7 @@ protected override ISyntaxVisitorAction Enter(
347347
return Skip;
348348
}
349349

350-
private bool TryCreateValueError(
350+
private static bool TryCreateValueError(
351351
IDocumentValidatorContext context,
352352
IInputType locationType,
353353
IValueNode valueNode,
@@ -389,6 +389,23 @@ private bool TryPeekLastDefiningSyntaxNode(
389389
return false;
390390
}
391391

392+
private bool TryIsInstanceOfType(
393+
IDocumentValidatorContext context,
394+
IInputType inputType,
395+
IValueNode value)
396+
{
397+
try
398+
{
399+
return IsInstanceOfType(context, inputType, value);
400+
}
401+
// in the case a scalar IsInstanceOfType check is not done well an throws we will
402+
// catch this here and make sure that the validation fails correctly.
403+
catch
404+
{
405+
return false;
406+
}
407+
}
408+
392409
private bool IsInstanceOfType(
393410
IDocumentValidatorContext context,
394411
IInputType inputType,

src/HotChocolate/Core/test/Validation.Tests/ValuesOfCorrectTypeRuleTests.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,21 @@ public void GoodIntNegativeValue()
211211
");
212212
}
213213

214-
[Fact]
215-
public void GoodNullToBooleanNullableValue()
216-
{
217-
ExpectValid(@"
214+
[Fact]
215+
public void OverflowInt()
216+
{
217+
ExpectErrors($@"
218+
{{
219+
arguments {{
220+
intArgField(intArg: {long.MaxValue})
221+
}}
222+
}}");
223+
}
224+
225+
[Fact]
226+
public void GoodNullToBooleanNullableValue()
227+
{
228+
ExpectValid(@"
218229
{
219230
arguments {
220231
booleanArgField(booleanArg: true)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[
2+
{
3+
"Message": "The specified argument value does not match the argument type.",
4+
"Code": null,
5+
"Path": {
6+
"Name": "intArgField",
7+
"Parent": {
8+
"Name": "arguments",
9+
"Parent": {
10+
"Parent": null,
11+
"Depth": -1,
12+
"IsRoot": true
13+
},
14+
"Depth": 0,
15+
"IsRoot": false
16+
},
17+
"Depth": 1,
18+
"IsRoot": false
19+
},
20+
"Locations": [
21+
{
22+
"Line": 4,
23+
"Column": 37
24+
}
25+
],
26+
"Extensions": {
27+
"argument": "intArg",
28+
"argumentValue": "9223372036854775807",
29+
"locationType": "Int",
30+
"specifiedBy": "http://spec.graphql.org/October2021/#sec-Values-of-Correct-Type"
31+
},
32+
"Exception": null,
33+
"SyntaxNode": {
34+
"Kind": "IntValue",
35+
"Location": {
36+
"Start": 77,
37+
"End": 97,
38+
"Line": 4,
39+
"Column": 37
40+
},
41+
"Value": "9223372036854775807"
42+
}
43+
}
44+
]

0 commit comments

Comments
 (0)