Skip to content

Commit 8ebf3c9

Browse files
authored
feat: allow additional parser options in the gradle and maven plugins (#1925)
Enables configuration of the maxCharacters and maxParserDepth parser options for GraphQL Java. ### 📝 Description See [ParserOptions](https://github.com/graphql-java/graphql-java/blob/7c381cc9d61c1e1838a2487d9b24974c451f23a2/src/main/java/graphql/parser/ParserOptions.java#L13) for the values configured in graphql-java. ### 🔗 Related Issues Extensions of #1586 with additional parameters that have been added to graphql-java.
1 parent 13faa5c commit 8ebf3c9

File tree

6 files changed

+46
-0
lines changed

6 files changed

+46
-0
lines changed

integration/maven-plugin-integration-tests/integration/generate-client/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181
<parserOptions>
8282
<maxTokens>15000</maxTokens>
8383
<maxWhitespaceTokens>200000</maxWhitespaceTokens>
84+
<maxCharacters>1048576</maxCharacters>
85+
<maxRuleDepth>500</maxRuleDepth>
8486
<captureIgnoredChars>false</captureIgnoredChars>
8587
<captureLineComments>false</captureLineComments>
8688
<captureSourceLocation>true</captureSourceLocation>

plugins/graphql-kotlin-gradle-plugin/src/main/kotlin/com/expediagroup/graphql/plugin/gradle/actions/GenerateClientAction.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ abstract class GenerateClientAction : WorkAction<GenerateClientParameters> {
5151
parserOptions = {
5252
parserOptions.maxTokens?.let { maxTokens(it) }
5353
parserOptions.maxWhitespaceTokens?.let { maxWhitespaceTokens(it) }
54+
parserOptions.maxCharacters?.let { maxCharacters(it) }
55+
parserOptions.maxRuleDepth?.let { maxRuleDepth(it) }
5456
parserOptions.captureIgnoredChars?.let { captureIgnoredChars(it) }
5557
parserOptions.captureSourceLocation?.let { captureSourceLocation(it) }
5658
parserOptions.captureLineComments?.let { captureLineComments(it) }

plugins/graphql-kotlin-gradle-plugin/src/main/kotlin/com/expediagroup/graphql/plugin/gradle/config/GraphQLParserOptions.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ data class GraphQLParserOptions(
1111
var maxTokens: Int? = null,
1212
/** Modify the maximum number of whitespace tokens read to prevent processing extremely large queries */
1313
var maxWhitespaceTokens: Int? = null,
14+
/** Modify the maximum number of characters in a document to prevent malicious documents consuming CPU */
15+
val maxCharacters: Int? = null,
16+
/** Modify the maximum grammar rule depth to negate malicious documents that can cause stack overflows */
17+
val maxRuleDepth: Int? = null,
1418
/** Memory usage is significantly reduced by not capturing ignored characters, especially in SDL parsing. */
1519
var captureIgnoredChars: Boolean? = null,
1620
/** Single-line comments do not have any semantic meaning in GraphQL source documents and can be ignored */

plugins/graphql-kotlin-maven-plugin/src/main/kotlin/com/expediagroup/graphql/plugin/maven/GenerateClientAbstractMojo.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ abstract class GenerateClientAbstractMojo : AbstractMojo() {
8181
* <parserOptions>
8282
* <maxTokens>15000</maxTokens>
8383
* <maxWhitespaceTokens>200000</maxWhitespaceTokens>
84+
* <maxCharacters>1048576</maxCharacters>
85+
* <maxRuleDepth>500</maxRuleDepth>
8486
* <captureIgnoredChars>false</captureIgnoredChars>
8587
* <captureLineComments>false</captureLineComments>
8688
* <captureSourceLocation>true</captureSourceLocation>
@@ -137,6 +139,8 @@ abstract class GenerateClientAbstractMojo : AbstractMojo() {
137139
parserOptions?.apply {
138140
maxTokens?.let { maxTokens(it) }
139141
maxWhitespaceTokens?.let { maxWhitespaceTokens(it) }
142+
maxCharacters?.let { maxCharacters(it) }
143+
maxRuleDepth?.let { maxRuleDepth(it) }
140144
captureIgnoredChars?.let { captureIgnoredChars(it) }
141145
captureLineComments?.let { captureLineComments(it) }
142146
captureSourceLocation?.let { captureSourceLocation(it) }
@@ -178,6 +182,8 @@ abstract class GenerateClientAbstractMojo : AbstractMojo() {
178182
log.debug(" parserOptions")
179183
maxTokens?.let { log.debug(" maxTokens = $it") }
180184
maxWhitespaceTokens?.let { log.debug(" maxWhitespaceTokens = $it") }
185+
maxCharacters?.let { log.debug(" maxCharacters = $it") }
186+
maxRuleDepth?.let { log.debug(" maxRuleDepth = $it") }
181187
captureIgnoredChars?.let { log.debug(" captureIgnoredChars = $it") }
182188
captureLineComments?.let { log.debug(" captureLineComments = $it") }
183189
captureSourceLocation?.let { log.debug(" captureSourceLocation = $it") }
@@ -224,6 +230,14 @@ class ParserOptions {
224230
@Parameter
225231
var maxWhitespaceTokens: Int? = null
226232

233+
/** Modify the maximum number of characters in a document to prevent malicious documents consuming CPU */
234+
@Parameter
235+
val maxCharacters: Int? = null
236+
237+
/** Modify the maximum grammar rule depth to negate malicious documents that can cause stack overflows */
238+
@Parameter
239+
val maxRuleDepth: Int? = null
240+
227241
/** Memory usage is significantly reduced by not capturing ignored characters, especially in SDL parsing. */
228242
@Parameter
229243
var captureIgnoredChars: Boolean? = null

website/docs/plugins/gradle-plugin-tasks.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ graphql {
148148
maxTokens = 15000
149149
// Override the maximum number of whitespace tokens read to prevent processing extremely large queries.
150150
maxWhitespaceTokens = 200000
151+
// Modify the maximum number of characters in a document to prevent malicious documents consuming CPU
152+
maxCharacters = 1048576
153+
// Modify the maximum grammar rule depth to negate malicious documents that can cause stack overflows
154+
maxRuleDepth = 500
151155
// Single-line comments do not have any semantic meaning in GraphQL source documents and can be ignored
152156
captureLineComments = false
153157
// Memory usage is significantly reduced by not capturing ignored characters, especially in SDL parsing.
@@ -213,6 +217,10 @@ graphql {
213217
options.maxTokens = 15000
214218
// Override the maximum number of whitespace tokens read to prevent processing extremely large queries.
215219
options.maxWhitespaceTokens = 200000
220+
// Modify the maximum number of characters in a document to prevent malicious documents consuming CPU
221+
options.maxCharacters = 1048576
222+
// Modify the maximum grammar rule depth to negate malicious documents that can cause stack overflows
223+
options.maxRuleDepth = 500
216224
// Memory usage is significantly reduced by not capturing ignored characters, especially in SDL parsing.
217225
options.captureIgnoredChars = false
218226
// Memory usage is reduced by not setting SourceLocations on AST nodes, especially in SDL parsing.
@@ -318,6 +326,10 @@ for details on how to update this process to use `kotlinx.serialization` instead
318326
maxTokens = 15000
319327
// Override the maximum number of whitespace tokens read to prevent processing extremely large queries.
320328
maxWhitespaceTokens = 200000
329+
// Modify the maximum number of characters in a document to prevent malicious documents consuming CPU
330+
maxCharacters = 1048576
331+
// Modify the maximum grammar rule depth to negate malicious documents that can cause stack overflows
332+
maxRuleDepth = 500
321333
// Memory usage is significantly reduced by not capturing ignored characters, especially in SDL parsing.
322334
captureIgnoredChars = false
323335
// Memory usage is reduced by not setting SourceLocations on AST nodes, especially in SDL parsing.
@@ -388,6 +400,10 @@ for details on how to update this process to use `kotlinx.serialization` instead
388400
maxTokens = 15000
389401
// Override the maximum number of whitespace tokens read to prevent processing extremely large queries.
390402
maxWhitespaceTokens = 200000
403+
// Modify the maximum number of characters in a document to prevent malicious documents consuming CPU
404+
maxCharacters = 1048576
405+
// Modify the maximum grammar rule depth to negate malicious documents that can cause stack overflows
406+
maxRuleDepth = 500
391407
// Memory usage is significantly reduced by not capturing ignored characters, especially in SDL parsing.
392408
captureIgnoredChars = false
393409
// Memory usage is reduced by not setting SourceLocations on AST nodes, especially in SDL parsing.

website/docs/plugins/maven-plugin-goals.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ Generate GraphQL client code based on the provided GraphQL schema and target que
101101
<maxTokens>15000</maxTokens>
102102
<!-- Modify the maximum number of whitespace tokens read to prevent processing extremely large queries -->
103103
<maxWhitespaceTokens>200000</maxWhitespaceTokens>
104+
<!-- Modify the maximum number of characters in a document to prevent malicious documents consuming CPU -->
105+
<maxCharacters>1048576</maxCharacters>
106+
<!-- Modify the maximum grammar rule depth to negate malicious documents that can cause stack overflows -->
107+
<maxRuleDepth>500</maxRuleDepth>
104108
<!-- Memory usage is significantly reduced by not capturing ignored characters, especially in SDL parsing -->
105109
<captureIgnoredChars>false</captureIgnoredChars>
106110
<!-- Single-line comments do not have any semantic meaning in GraphQL source documents and can be ignored -->
@@ -209,6 +213,10 @@ Generate GraphQL test client code based on the provided GraphQL schema and targe
209213
<maxTokens>15000</maxTokens>
210214
<!-- Modify the maximum number of whitespace tokens read to prevent processing extremely large queries -->
211215
<maxWhitespaceTokens>200000</maxWhitespaceTokens>
216+
<!-- Modify the maximum number of characters in a document to prevent malicious documents consuming CPU -->
217+
<maxCharacters>1048576</maxCharacters>
218+
<!-- Modify the maximum grammar rule depth to negate malicious documents that can cause stack overflows -->
219+
<maxRuleDepth>500</maxRuleDepth>
212220
<!-- Memory usage is significantly reduced by not capturing ignored characters, especially in SDL parsing -->
213221
<captureIgnoredChars>false</captureIgnoredChars>
214222
<!-- Single-line comments do not have any semantic meaning in GraphQL source documents and can be ignored -->

0 commit comments

Comments
 (0)