forked from gothinkster/spring-boot-realworld-example-app
-
Notifications
You must be signed in to change notification settings - Fork 14
bug: bound GraphQL query depth and complexity to prevent unauthenticated DoS #1066
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
Open
devin-ai-integration
wants to merge
1
commit into
master
Choose a base branch
from
devin/1788253799-graphql-query-limits
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/io/spring/graphql/GraphQLQueryLimitConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package io.spring.graphql; | ||
|
|
||
| import graphql.analysis.FieldComplexityCalculator; | ||
| import graphql.analysis.FieldComplexityEnvironment; | ||
| import graphql.analysis.MaxQueryComplexityInstrumentation; | ||
| import graphql.analysis.MaxQueryDepthInstrumentation; | ||
| import graphql.execution.instrumentation.Instrumentation; | ||
| import java.util.Map; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class GraphQLQueryLimitConfig { | ||
|
|
||
| @Bean | ||
| public Instrumentation maxQueryDepthInstrumentation( | ||
| @Value("${graphql.limit.max-query-depth:10}") int maxQueryDepth) { | ||
| return new MaxQueryDepthInstrumentation(maxQueryDepth); | ||
| } | ||
|
|
||
| @Bean | ||
| public Instrumentation maxQueryComplexityInstrumentation( | ||
| @Value("${graphql.limit.max-query-complexity:50000}") int maxQueryComplexity) { | ||
| return new MaxQueryComplexityInstrumentation(maxQueryComplexity, pageSizeAwareCalculator()); | ||
| } | ||
|
|
||
| static FieldComplexityCalculator pageSizeAwareCalculator() { | ||
| return (FieldComplexityEnvironment environment, int childComplexity) -> | ||
| 1 + pageSize(environment.getArguments()) * childComplexity; | ||
|
Comment on lines
+28
to
+30
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Comment on lines
+29
to
+30
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| private static int pageSize(Map<String, Object> arguments) { | ||
| Object requested = | ||
| arguments.get("first") != null ? arguments.get("first") : arguments.get("last"); | ||
| if (requested instanceof Number) { | ||
| int size = ((Number) requested).intValue(); | ||
| return size > 0 ? size : 1; | ||
| } | ||
| return 1; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/test/java/io/spring/graphql/GraphQLQueryLimitConfigTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package io.spring.graphql; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import graphql.ExecutionResult; | ||
| import graphql.GraphQL; | ||
| import graphql.execution.instrumentation.Instrumentation; | ||
| import graphql.schema.GraphQLSchema; | ||
| import graphql.schema.idl.RuntimeWiring; | ||
| import graphql.schema.idl.SchemaGenerator; | ||
| import graphql.schema.idl.SchemaParser; | ||
| import graphql.schema.idl.TypeDefinitionRegistry; | ||
| import java.util.stream.Collectors; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class GraphQLQueryLimitConfigTest { | ||
|
|
||
| private static final String SDL = | ||
| String.join( | ||
| "\n", | ||
| "type Query { articles(first: Int, after: String, last: Int, before: String): " | ||
| + "ArticlesConnection }", | ||
| "type ArticlesConnection { edges: [ArticleEdge] }", | ||
| "type ArticleEdge { cursor: String!, node: Article }", | ||
| "type Article { slug: String!, title: String!, comments(first: Int, after: String, " | ||
| + "last: Int, before: String): CommentsConnection }", | ||
| "type CommentsConnection { edges: [CommentEdge] }", | ||
| "type CommentEdge { cursor: String!, node: Comment }", | ||
| "type Comment { id: ID!, body: String!, article: Article! }"); | ||
|
|
||
| @Test | ||
| public void should_reject_queries_exceeding_maximum_depth() { | ||
| GraphQLQueryLimitConfig config = new GraphQLQueryLimitConfig(); | ||
| ExecutionResult executionResult = | ||
| graphWith(config.maxQueryDepthInstrumentation(10)).execute(depthQuery()); | ||
|
|
||
| assertThat(executionResult.getErrors()).isNotEmpty(); | ||
| assertThat(errorMessages(executionResult)).contains("maximum query depth exceeded"); | ||
| } | ||
|
|
||
| @Test | ||
| public void should_reject_queries_exceeding_maximum_complexity() { | ||
| GraphQLQueryLimitConfig config = new GraphQLQueryLimitConfig(); | ||
| ExecutionResult executionResult = | ||
| graphWith(config.maxQueryComplexityInstrumentation(50000)) | ||
| .execute( | ||
| "{ articles(first: 1000) { edges { node { comments(first: 1000) { " | ||
| + "edges { node { body } } } } } } }"); | ||
|
|
||
| assertThat(executionResult.getErrors()).isNotEmpty(); | ||
| assertThat(errorMessages(executionResult)).contains("maximum query complexity exceeded"); | ||
| } | ||
|
|
||
| @Test | ||
| public void should_allow_realistic_queries_within_both_limits() { | ||
| String query = | ||
| "{ articles(first: 20) { edges { node { title comments(first: 20) { " | ||
| + "edges { node { body } } } } } } }"; | ||
| GraphQLQueryLimitConfig config = new GraphQLQueryLimitConfig(); | ||
|
|
||
| assertThat(graphWith(config.maxQueryDepthInstrumentation(10)).execute(query).getErrors()) | ||
| .isEmpty(); | ||
| ExecutionResult executionResult = | ||
| graphWith(config.maxQueryComplexityInstrumentation(50000)).execute(query); | ||
| assertThat(executionResult.getErrors()).isEmpty(); | ||
| } | ||
|
|
||
| private static GraphQL graphWith(Instrumentation instrumentation) { | ||
| TypeDefinitionRegistry registry = new SchemaParser().parse(SDL); | ||
| GraphQLSchema schema = | ||
| new SchemaGenerator() | ||
| .makeExecutableSchema(registry, RuntimeWiring.newRuntimeWiring().build()); | ||
| return GraphQL.newGraphQL(schema).instrumentation(instrumentation).build(); | ||
| } | ||
|
|
||
| private static String depthQuery() { | ||
| return "{ articles { edges { node { " + cycle(5) + " } } } }"; | ||
| } | ||
|
|
||
| private static String cycle(int remaining) { | ||
| if (remaining == 0) { | ||
| return "slug"; | ||
| } | ||
| return "comments { edges { node { article { " + cycle(remaining - 1) + " } } } }"; | ||
| } | ||
|
|
||
| private static String errorMessages(ExecutionResult executionResult) { | ||
| return executionResult.getErrors().stream() | ||
| .map(error -> error.getMessage()) | ||
| .collect(Collectors.joining("\n")); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Info: Both limits reach production execution
DGS 4.9.21 collects every
Instrumentationbean into one chain. Both independently tested limits therefore run on production requests.Was this helpful? React with 👍 or 👎 to provide feedback.