Skip to content

Preventing stack overflow exceptions via limiting the depth of the parser rules #3112

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

Merged
merged 2 commits into from
Mar 20, 2023

Conversation

bbakerman
Copy link
Member

@bbakerman bbakerman commented Mar 13, 2023

This puts in a limit on how deep grammar rules can get.

As ANLTR is a recursive-descent parser it will build up a call stack as it moves along. This stack is limited and can blow up with just the wrong input. Even if the max tokens checking is in place, it can still blow the JVM stack before the max tokens are reached.

Right now it's 500 deep as presented here. Is this the right value? More?? Less ?? I changed it from 1000

@bbakerman bbakerman added this to the 20.1 milestone Mar 13, 2023
…rser rules - moving to 500 deep as a starting point
ParserOptions.setDefaultParserOptions(defaultOptions)
ParserOptions.setDefaultOperationParserOptions(defaultOperationOptions)
ParserOptions.setDefaultSdlParserOptions(defaultSdlOptions)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to the Stress test class along with other stress tests

@@ -93,10 +76,6 @@ class ParserTest extends Specification {
return AstComparator.isEqual(node1, node2)
}

boolean isEqual(List<Node> node1, List<Node> node2) {
return AstComparator.isEqual(node1, node2)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never used

@@ -404,7 +383,8 @@ class ParserTest extends Specification {
.build()

when:
def document = new Parser().parseDocument(input, parserOptionsWithoutCaptureLineComments)
def parserEnvironment = newParserEnvironment().document(input).parserOptions(parserOptionsWithoutCaptureLineComments).build()
def document = new Parser().parseDocument(parserEnvironment)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed deprecated methods

@@ -1158,112 +1142,14 @@ triple3 : """edge cases \\""" "" " \\"" \\" edge cases"""

when:
options = ParserOptions.newParserOptions().captureSourceLocation(false).build()
document = new Parser().parseDocument("{ f }", options)
def parserEnvironment = newParserEnvironment().document("{ f }").parserOptions(options).build()
document = new Parser().parseDocument(parserEnvironment)

then:
!options.isCaptureSourceLocation()
document.getSourceLocation() == SourceLocation.EMPTY
document.getDefinitions()[0].getSourceLocation() == SourceLocation.EMPTY
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the following the ParserStressTest

Copy link
Member

@andimarek andimarek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see one comment about Internal exception ... otherwise LGTM

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Internal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be public because the Parser throws it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well it has to be since its in another package (with the others)

exceptions
  InvalidUnicodeSyntaxException
  MoreTokensSyntaxException
  ParseCancelledException
  ParseCancelledTooDeepException

@bbakerman bbakerman added this pull request to the merge queue Mar 20, 2023
@bbakerman bbakerman merged commit 80e3135 into master Mar 20, 2023
bbakerman added a commit that referenced this pull request Mar 20, 2023
Merge pull request #3112 from graphql-java/prevent-stackoverflow-in-parser

Preventing stack overflow exceptions via limiting the depth of the parser rules

# Conflicts:
#	src/main/java/graphql/parser/Parser.java
#	src/main/java/graphql/parser/ParserOptions.java
#	src/main/resources/i18n/Parsing.properties
#	src/test/groovy/graphql/parser/ParserTest.groovy
bbakerman added a commit that referenced this pull request Mar 20, 2023
Merge pull request #3112 from graphql-java/prevent-stackoverflow-in-parser

Preventing stack overflow exceptions via limiting the depth of the parser rules

# Conflicts:
#	src/main/java/graphql/parser/Parser.java
#	src/main/java/graphql/parser/ParserOptions.java
#	src/main/resources/i18n/Parsing.properties
#	src/test/groovy/graphql/parser/ParserTest.groovy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants