Skip to content

Commit 9a718ee

Browse files
authored
Added Number value literal lookahead restrictions (#6823)
1 parent c3532bf commit 9a718ee

File tree

9 files changed

+4740
-43
lines changed

9 files changed

+4740
-43
lines changed

src/HotChocolate/Core/test/Types.Analyzers.Tests/packages.lock.json

Lines changed: 3051 additions & 0 deletions
Large diffs are not rendered by default.

src/HotChocolate/Diagnostics/test/Diagnostics.Tests/__snapshots__/ServerInstrumentationTests.Http_Post_parser_error.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
{
2727
"Name": "Error",
2828
"Tags": {
29-
"graphql.error.message": "Expected a `Name`-token, but found a `Integer`-token.",
29+
"graphql.error.message": "Found a NameStart character `n` (110) following a number, which is disallowed.",
3030
"graphql.error.code": "HC0011",
3131
"graphql.error.location.column": 37,
3232
"graphql.error.location.line": 10

src/HotChocolate/Diagnostics/test/Diagnostics.Tests/__snapshots__/ServerInstrumentationTests.Http_Post_parser_error__NET7.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"Tags": [
2929
{
3030
"Key": "graphql.error.message",
31-
"Value": "Expected a `Name`-token, but found a `Integer`-token."
31+
"Value": "Found a NameStart character `n` (110) following a number, which is disallowed."
3232
},
3333
{
3434
"Key": "graphql.error.code",

src/HotChocolate/Fusion/test/Shared/packages.lock.json

Lines changed: 1604 additions & 0 deletions
Large diffs are not rendered by default.

src/HotChocolate/Language/src/Language.Utf8/Properties/LangUtf8Resources.Designer.cs

Lines changed: 42 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/HotChocolate/Language/src/Language.Utf8/Properties/LangUtf8Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@
159159
<data name="UnexpectedCharacter" xml:space="preserve">
160160
<value>Unexpected character `{0}` ({1}).</value>
161161
</data>
162+
<data name="DisallowedNameCharacterAfterNumber" xml:space="preserve">
163+
<value>Found a NameStart character `{0}` ({1}) following a number, which is disallowed.</value>
164+
</data>
162165
<data name="InvalidCharacterEscapeSequence" xml:space="preserve">
163166
<value>Invalid character escape sequence: \{0}.</value>
164167
</data>

src/HotChocolate/Language/src/Language.Utf8/Utf8GraphQLReader.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,9 @@ private void ReadPunctuatorToken(byte code)
339339

340340
/// <summary>
341341
/// Reads int tokens as specified in
342-
/// http://facebook.github.io/graphql/October2016/#IntValue
342+
/// http://facebook.github.io/graphql/October2021/#IntValue
343343
/// or a float tokens as specified in
344-
/// http://facebook.github.io/graphql/October2016/#FloatValue
344+
/// http://facebook.github.io/graphql/October2021/#FloatValue
345345
/// from the current lexer state.
346346
/// </summary>
347347
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -378,7 +378,8 @@ private void ReadNumberToken(byte firstCode)
378378
code = ReadDigits(code);
379379
}
380380

381-
if ((code | 0x20) is GraphQLConstants.E)
381+
const byte lowerCaseBit = 0x20;
382+
if ((code | lowerCaseBit) is GraphQLConstants.E)
382383
{
383384
isFloat = true;
384385
_floatFormat = Language.FloatFormat.Exponential;
@@ -388,7 +389,18 @@ private void ReadNumberToken(byte firstCode)
388389
{
389390
code = _graphQLData[++_position];
390391
}
391-
ReadDigits(code);
392+
code = ReadDigits(code);
393+
}
394+
395+
// Lookahead for NameStart.
396+
// https://github.com/graphql/graphql-spec/pull/601
397+
// NOTE:
398+
// Not checking for Digit because there is no situation
399+
// where that hasn't been consumed at this point.
400+
if (code.IsLetterOrUnderscore() ||
401+
code == GraphQLConstants.Dot)
402+
{
403+
throw new SyntaxException(this, DisallowedNameCharacterAfterNumber, (char)code, code);
392404
}
393405

394406
_kind = isFloat

src/HotChocolate/Language/test/Language.Tests/Parser/ValueParserTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,25 @@ public void ZeroZeroIsNotAllowed()
4848

4949
private static IValueNode ParseValue(string value)
5050
=> Utf8GraphQLParser.Syntax.ParseValueLiteral(value, true);
51+
52+
// https://github.com/graphql/graphql-spec/pull/601#issuecomment-518954455
53+
// Int
54+
[InlineData("0xF1")]
55+
[InlineData("0b10")]
56+
[InlineData("123abc")]
57+
[InlineData("1_234")]
58+
// Float
59+
[InlineData("1.23f")]
60+
[InlineData("1.234_5")]
61+
[InlineData("1.2e3.")]
62+
[Theory]
63+
public void NameStartFollowingNumberIsNotAllowed(string input)
64+
{
65+
// arrange
66+
// act
67+
void Action() => ParseValue(input);
68+
69+
// assert
70+
Assert.Throws<SyntaxException>(Action);
71+
}
5172
}

src/HotChocolate/Language/test/Language.Tests/Parser/__snapshots__/KitchenSinkParserTests.ParseFacebookKitchenSinkQueryNullability.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ mutation likeStory @onMutation {
6565
}
6666
}
6767

68-
subscription StoryLikeSubscription($input: StoryLikeSubscribeInput) @onSubscription {
68+
subscription StoryLikeSubscription($input: StoryLikeSubscribeInput @onVariableDefinition) @onSubscription {
6969
storyLikeSubscribe(input: $input) {
7070
story {
7171
likers {

0 commit comments

Comments
 (0)