Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed value overflows break query validation. #5522

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/HotChocolate/Core/src/Types/Types/Scalars/IntType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public IntType(
Description = description;
}

protected override int ParseLiteral(IntValueNode valueSyntax) =>
valueSyntax.ToInt32();
protected override int ParseLiteral(IntValueNode valueSyntax)
=> valueSyntax.ToInt32();

protected override IntValueNode ParseValue(int runtimeValue) =>
new(runtimeValue);
protected override IntValueNode ParseValue(int runtimeValue)
=> new(runtimeValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ protected IntegerTypeBase(

protected override bool IsInstanceOfType(IntValueNode valueSyntax)
{
return IsInstanceOfType(ParseLiteral(valueSyntax));
try
{
return IsInstanceOfType(ParseLiteral(valueSyntax));
}
catch (InvalidFormatException)
{
return false;
}
}

protected override bool IsInstanceOfType(TRuntimeType runtimeValue)
Expand Down
23 changes: 20 additions & 3 deletions src/HotChocolate/Core/src/Validation/Rules/ValueVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ protected override ISyntaxVisitorAction Enter(
inputObjectType));
}
else if (value.Value.Kind is SyntaxKind.Variable &&
!IsInstanceOfType(context, new NonNullType(field.Type), value.Value))
!TryIsInstanceOfType(context, new NonNullType(field.Type), value.Value))
{
context.ReportError(
context.OneOfVariablesMustBeNonNull(
Expand Down Expand Up @@ -332,7 +332,7 @@ protected override ISyntaxVisitorAction Enter(
if (context.Types.TryPeek(out var currentType) &&
currentType is IInputType locationType)
{
if (valueNode.IsNull() || IsInstanceOfType(context, locationType, valueNode))
if (valueNode.IsNull() || TryIsInstanceOfType(context, locationType, valueNode))
{
return Skip;
}
Expand All @@ -348,7 +348,7 @@ protected override ISyntaxVisitorAction Enter(
return Skip;
}

private bool TryCreateValueError(
private static bool TryCreateValueError(
IDocumentValidatorContext context,
IInputType locationType,
IValueNode valueNode,
Expand Down Expand Up @@ -390,6 +390,23 @@ private bool TryPeekLastDefiningSyntaxNode(
return false;
}

private bool TryIsInstanceOfType(
IDocumentValidatorContext context,
IInputType inputType,
IValueNode value)
{
try
{
return IsInstanceOfType(context, inputType, value);
}
// in the case a scalar IsInstanceOfType check is not done well an throws we will
// catch this here and make sure that the validation fails correctly.
catch
{
return false;
}
}

private bool IsInstanceOfType(
IDocumentValidatorContext context,
IInputType inputType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ public void GoodIntNegativeValue()
");
}

[Fact]
public void OverflowInt()
{
ExpectErrors($@"
{{
arguments {{
intArgField(intArg: {long.MaxValue})
}}
}}");
}

[Fact]
public void GoodNullToBooleanNullableValue()
{
Expand Down Expand Up @@ -1173,4 +1184,4 @@ query InvalidItem($a: [String] = [""one"", 2]) {
}
");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"Message": "The specified argument value does not match the argument type.",
"Code": null,
"Path": {
"Name": "intArgField",
"Parent": {
"Name": "arguments",
"Parent": {
"Parent": null,
"Depth": -1,
"IsRoot": true
},
"Depth": 0,
"IsRoot": false
},
"Depth": 1,
"IsRoot": false
},
"Locations": [
{
"Line": 4,
"Column": 37
}
],
"Extensions": {
"argument": "intArg",
"argumentValue": "9223372036854775807",
"locationType": "Int",
"specifiedBy": "http://spec.graphql.org/October2021/#sec-Values-of-Correct-Type"
},
"Exception": null,
"SyntaxNode": {
"Kind": "IntValue",
"Location": {
"Start": 77,
"End": 97,
"Line": 4,
"Column": 37
},
"Value": "9223372036854775807"
}
}
]