Skip to content

Commit

Permalink
Added Number value literal lookahead restrictions (#6823)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonC9018 authored Jan 16, 2024
1 parent c3532bf commit 9a718ee
Show file tree
Hide file tree
Showing 9 changed files with 4,740 additions and 43 deletions.
3,051 changes: 3,051 additions & 0 deletions src/HotChocolate/Core/test/Types.Analyzers.Tests/packages.lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
{
"Name": "Error",
"Tags": {
"graphql.error.message": "Expected a `Name`-token, but found a `Integer`-token.",
"graphql.error.message": "Found a NameStart character `n` (110) following a number, which is disallowed.",
"graphql.error.code": "HC0011",
"graphql.error.location.column": 37,
"graphql.error.location.line": 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"Tags": [
{
"Key": "graphql.error.message",
"Value": "Expected a `Name`-token, but found a `Integer`-token."
"Value": "Found a NameStart character `n` (110) following a number, which is disallowed."
},
{
"Key": "graphql.error.code",
Expand Down
1,604 changes: 1,604 additions & 0 deletions src/HotChocolate/Fusion/test/Shared/packages.lock.json

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@
<data name="UnexpectedCharacter" xml:space="preserve">
<value>Unexpected character `{0}` ({1}).</value>
</data>
<data name="DisallowedNameCharacterAfterNumber" xml:space="preserve">
<value>Found a NameStart character `{0}` ({1}) following a number, which is disallowed.</value>
</data>
<data name="InvalidCharacterEscapeSequence" xml:space="preserve">
<value>Invalid character escape sequence: \{0}.</value>
</data>
Expand Down
20 changes: 16 additions & 4 deletions src/HotChocolate/Language/src/Language.Utf8/Utf8GraphQLReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,9 @@ private void ReadPunctuatorToken(byte code)

/// <summary>
/// Reads int tokens as specified in
/// http://facebook.github.io/graphql/October2016/#IntValue
/// http://facebook.github.io/graphql/October2021/#IntValue
/// or a float tokens as specified in
/// http://facebook.github.io/graphql/October2016/#FloatValue
/// http://facebook.github.io/graphql/October2021/#FloatValue
/// from the current lexer state.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -378,7 +378,8 @@ private void ReadNumberToken(byte firstCode)
code = ReadDigits(code);
}

if ((code | 0x20) is GraphQLConstants.E)
const byte lowerCaseBit = 0x20;
if ((code | lowerCaseBit) is GraphQLConstants.E)
{
isFloat = true;
_floatFormat = Language.FloatFormat.Exponential;
Expand All @@ -388,7 +389,18 @@ private void ReadNumberToken(byte firstCode)
{
code = _graphQLData[++_position];
}
ReadDigits(code);
code = ReadDigits(code);
}

// Lookahead for NameStart.
// https://github.com/graphql/graphql-spec/pull/601
// NOTE:
// Not checking for Digit because there is no situation
// where that hasn't been consumed at this point.
if (code.IsLetterOrUnderscore() ||
code == GraphQLConstants.Dot)
{
throw new SyntaxException(this, DisallowedNameCharacterAfterNumber, (char)code, code);
}

_kind = isFloat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,25 @@ public void ZeroZeroIsNotAllowed()

private static IValueNode ParseValue(string value)
=> Utf8GraphQLParser.Syntax.ParseValueLiteral(value, true);

// https://github.com/graphql/graphql-spec/pull/601#issuecomment-518954455
// Int
[InlineData("0xF1")]
[InlineData("0b10")]
[InlineData("123abc")]
[InlineData("1_234")]
// Float
[InlineData("1.23f")]
[InlineData("1.234_5")]
[InlineData("1.2e3.")]
[Theory]
public void NameStartFollowingNumberIsNotAllowed(string input)
{
// arrange
// act
void Action() => ParseValue(input);

// assert
Assert.Throws<SyntaxException>(Action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ mutation likeStory @onMutation {
}
}

subscription StoryLikeSubscription($input: StoryLikeSubscribeInput) @onSubscription {
subscription StoryLikeSubscription($input: StoryLikeSubscribeInput @onVariableDefinition) @onSubscription {
storyLikeSubscribe(input: $input) {
story {
likers {
Expand Down

0 comments on commit 9a718ee

Please sign in to comment.