-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Restrict the number of ENFs created and take advantage in GoodFaith introspection #3539
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b94f152
add the ability to restrict the number of ENFs created
andimarek 9d37ffc
handle aliases correctly for good faith check
andimarek 83a21e9
Added good faith code to create ENO that is a fixed size
bbakerman 2c29b53
Uppped limit as discussed
bbakerman bd6ec10
Fixed tests and also added unit test for depth in good faith
bbakerman 55f2e5d
Added benchmark code
bbakerman 70895d7
Check max depth seen more often
bbakerman 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
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
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
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
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
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 |
|---|---|---|
|
|
@@ -64,6 +64,7 @@ | |
| import static graphql.util.FpKit.filterSet; | ||
| import static graphql.util.FpKit.groupingBy; | ||
| import static graphql.util.FpKit.intersection; | ||
| import static java.util.Collections.max; | ||
|
Member
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. I think this import is not used. |
||
| import static java.util.Collections.singleton; | ||
| import static java.util.Collections.singletonList; | ||
| import static java.util.stream.Collectors.toCollection; | ||
|
|
@@ -80,24 +81,28 @@ public static class Options { | |
| private final GraphQLContext graphQLContext; | ||
| private final Locale locale; | ||
| private final int maxChildrenDepth; | ||
| private final int maxFieldsCount; | ||
|
|
||
| private final boolean deferSupport; | ||
|
|
||
| private Options(GraphQLContext graphQLContext, | ||
| Locale locale, | ||
| int maxChildrenDepth, | ||
| int maxFieldsCount, | ||
| boolean deferSupport) { | ||
| this.graphQLContext = graphQLContext; | ||
| this.locale = locale; | ||
| this.maxChildrenDepth = maxChildrenDepth; | ||
| this.deferSupport = deferSupport; | ||
| this.maxFieldsCount = maxFieldsCount; | ||
| } | ||
|
|
||
| public static Options defaultOptions() { | ||
| return new Options( | ||
| GraphQLContext.getDefault(), | ||
| Locale.getDefault(), | ||
| Integer.MAX_VALUE, | ||
| Integer.MAX_VALUE, | ||
| false); | ||
| } | ||
|
|
||
|
|
@@ -111,7 +116,7 @@ public static Options defaultOptions() { | |
| * @return new options object to use | ||
| */ | ||
| public Options locale(Locale locale) { | ||
| return new Options(this.graphQLContext, locale, this.maxChildrenDepth, this.deferSupport); | ||
| return new Options(this.graphQLContext, locale, this.maxChildrenDepth, this.maxFieldsCount, this.deferSupport); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -124,7 +129,7 @@ public Options locale(Locale locale) { | |
| * @return new options object to use | ||
| */ | ||
| public Options graphQLContext(GraphQLContext graphQLContext) { | ||
| return new Options(graphQLContext, this.locale, this.maxChildrenDepth, this.deferSupport); | ||
| return new Options(graphQLContext, this.locale, this.maxChildrenDepth, this.maxFieldsCount, this.deferSupport); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -136,7 +141,19 @@ public Options graphQLContext(GraphQLContext graphQLContext) { | |
| * @return new options object to use | ||
| */ | ||
| public Options maxChildrenDepth(int maxChildrenDepth) { | ||
| return new Options(this.graphQLContext, this.locale, maxChildrenDepth, this.deferSupport); | ||
| return new Options(this.graphQLContext, this.locale, maxChildrenDepth, this.maxFieldsCount, this.deferSupport); | ||
| } | ||
|
|
||
| /** | ||
| * Controls the maximum number of ENFs created. Can be used to prevent | ||
| * against malicious operations. | ||
| * | ||
| * @param maxFieldsCount the max number of ENFs created | ||
| * | ||
| * @return new options object to use | ||
| */ | ||
| public Options maxFieldsCount(int maxFieldsCount) { | ||
| return new Options(this.graphQLContext, this.locale, maxChildrenDepth, maxFieldsCount, this.deferSupport); | ||
|
Member
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. Should this have been this.maxChildrenDepth? |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -148,7 +165,7 @@ public Options maxChildrenDepth(int maxChildrenDepth) { | |
| */ | ||
| @ExperimentalApi | ||
| public Options deferSupport(boolean deferSupport) { | ||
| return new Options(this.graphQLContext, this.locale, this.maxChildrenDepth, deferSupport); | ||
| return new Options(this.graphQLContext, this.locale, this.maxChildrenDepth, this.maxFieldsCount, deferSupport); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -178,6 +195,10 @@ public int getMaxChildrenDepth() { | |
| return maxChildrenDepth; | ||
| } | ||
|
|
||
| public int getMaxFieldsCount() { | ||
| return maxFieldsCount; | ||
| } | ||
|
|
||
| /** | ||
| * @return whether support for defer is enabled | ||
| * | ||
|
|
@@ -266,13 +287,36 @@ public static ExecutableNormalizedOperation createExecutableNormalizedOperation( | |
| OperationDefinition operationDefinition, | ||
| Map<String, FragmentDefinition> fragments, | ||
| CoercedVariables coercedVariableValues) { | ||
| return createExecutableNormalizedOperation(graphQLSchema, | ||
| operationDefinition, | ||
| fragments, | ||
| coercedVariableValues, | ||
| Options.defaultOptions()); | ||
| } | ||
|
|
||
| /** | ||
| * This will create a runtime representation of the graphql operation that would be executed | ||
| * in a runtime sense. | ||
| * | ||
| * @param graphQLSchema the schema to be used | ||
| * @param operationDefinition the operation to be executed | ||
| * @param fragments a set of fragments associated with the operation | ||
| * @param coercedVariableValues the coerced variables to use | ||
| * | ||
| * @return a runtime representation of the graphql operation. | ||
| */ | ||
| public static ExecutableNormalizedOperation createExecutableNormalizedOperation(GraphQLSchema graphQLSchema, | ||
| OperationDefinition operationDefinition, | ||
| Map<String, FragmentDefinition> fragments, | ||
| CoercedVariables coercedVariableValues, | ||
| Options options) { | ||
| return new ExecutableNormalizedOperationFactoryImpl( | ||
| graphQLSchema, | ||
| operationDefinition, | ||
| fragments, | ||
| coercedVariableValues, | ||
| null, | ||
| Options.defaultOptions() | ||
| options | ||
| ).createNormalizedQueryImpl(); | ||
| } | ||
|
|
||
|
|
@@ -386,6 +430,8 @@ private static class ExecutableNormalizedOperationFactoryImpl { | |
| private final ImmutableMap.Builder<ExecutableNormalizedField, MergedField> normalizedFieldToMergedField = ImmutableMap.builder(); | ||
| private final ImmutableMap.Builder<ExecutableNormalizedField, QueryDirectives> normalizedFieldToQueryDirectives = ImmutableMap.builder(); | ||
| private final ImmutableListMultimap.Builder<FieldCoordinates, ExecutableNormalizedField> coordinatesToNormalizedFields = ImmutableListMultimap.builder(); | ||
| private int fieldCount = 0; | ||
| private int maxDepthSeen = 0; | ||
|
|
||
| private ExecutableNormalizedOperationFactoryImpl( | ||
| GraphQLSchema graphQLSchema, | ||
|
|
@@ -420,10 +466,11 @@ private ExecutableNormalizedOperation createNormalizedQueryImpl() { | |
| updateFieldToNFMap(topLevel, fieldAndAstParents); | ||
| updateCoordinatedToNFMap(topLevel); | ||
|
|
||
| buildFieldWithChildren( | ||
| int depthSeen = buildFieldWithChildren( | ||
| topLevel, | ||
| fieldAndAstParents, | ||
| 1); | ||
| maxDepthSeen = Math.max(maxDepthSeen,depthSeen); | ||
| } | ||
| // getPossibleMergerList | ||
| for (PossibleMerger possibleMerger : possibleMergerList) { | ||
|
|
@@ -437,7 +484,9 @@ private ExecutableNormalizedOperation createNormalizedQueryImpl() { | |
| fieldToNormalizedField.build(), | ||
| normalizedFieldToMergedField.build(), | ||
| normalizedFieldToQueryDirectives.build(), | ||
| coordinatesToNormalizedFields.build() | ||
| coordinatesToNormalizedFields.build(), | ||
| fieldCount, | ||
| maxDepthSeen | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -448,15 +497,14 @@ private void captureMergedField(ExecutableNormalizedField enf, MergedField merge | |
| normalizedFieldToMergedField.put(enf, mergedFld); | ||
| } | ||
|
|
||
| private void buildFieldWithChildren(ExecutableNormalizedField executableNormalizedField, | ||
| private int buildFieldWithChildren(ExecutableNormalizedField executableNormalizedField, | ||
| ImmutableList<FieldAndAstParent> fieldAndAstParents, | ||
| int curLevel) { | ||
| if (curLevel > this.options.getMaxChildrenDepth()) { | ||
| throw new AbortExecutionException("Maximum query depth exceeded " + curLevel + " > " + this.options.getMaxChildrenDepth()); | ||
| } | ||
| checkMaxDepthExceeded(curLevel); | ||
|
|
||
| CollectNFResult nextLevel = collectFromMergedField(executableNormalizedField, fieldAndAstParents, curLevel + 1); | ||
|
|
||
| int maxDepthSeen = curLevel; | ||
| for (ExecutableNormalizedField childENF : nextLevel.children) { | ||
| executableNormalizedField.addChild(childENF); | ||
| ImmutableList<FieldAndAstParent> childFieldAndAstParents = nextLevel.normalizedFieldToAstFields.get(childENF); | ||
|
|
@@ -467,9 +515,19 @@ private void buildFieldWithChildren(ExecutableNormalizedField executableNormaliz | |
| updateFieldToNFMap(childENF, childFieldAndAstParents); | ||
| updateCoordinatedToNFMap(childENF); | ||
|
|
||
| buildFieldWithChildren(childENF, | ||
| int depthSeen = buildFieldWithChildren(childENF, | ||
| childFieldAndAstParents, | ||
| curLevel + 1); | ||
| maxDepthSeen = Math.max(maxDepthSeen,depthSeen); | ||
|
|
||
| checkMaxDepthExceeded(maxDepthSeen); | ||
| } | ||
| return maxDepthSeen; | ||
| } | ||
|
|
||
| private void checkMaxDepthExceeded(int depthSeen) { | ||
| if (depthSeen > this.options.getMaxChildrenDepth()) { | ||
| throw new AbortExecutionException("Maximum query depth exceeded. " + depthSeen + " > " + this.options.getMaxChildrenDepth()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -578,6 +636,11 @@ private void createNFs(ImmutableList.Builder<ExecutableNormalizedField> nfListBu | |
| private ExecutableNormalizedField createNF(CollectedFieldGroup collectedFieldGroup, | ||
| int level, | ||
| ExecutableNormalizedField parent) { | ||
|
|
||
| this.fieldCount++; | ||
| if (this.fieldCount > this.options.getMaxFieldsCount()) { | ||
| throw new AbortExecutionException("Maximum field count exceeded. " + this.fieldCount + " > " + this.options.getMaxFieldsCount()); | ||
| } | ||
| Field field; | ||
| Set<GraphQLObjectType> objectTypes = collectedFieldGroup.objectTypes; | ||
| field = collectedFieldGroup.fields.iterator().next().field; | ||
|
|
@@ -590,7 +653,6 @@ private ExecutableNormalizedField createNF(CollectedFieldGroup collectedFieldGro | |
| normalizedArgumentValues = ValuesResolver.getNormalizedArgumentValues(fieldDefinition.getArguments(), field.getArguments(), this.normalizedVariableValues); | ||
| } | ||
| ImmutableList<String> objectTypeNames = map(objectTypes, GraphQLObjectType::getName); | ||
|
|
||
| return ExecutableNormalizedField.newNormalizedField() | ||
| .alias(field.getAlias()) | ||
| .resolvedArguments(argumentValues) | ||
|
|
@@ -763,8 +825,8 @@ private void collectInlineFragment(List<CollectedField> result, | |
|
|
||
| private NormalizedDeferredExecution buildDeferredExecution( | ||
| List<Directive> directives, | ||
| Set<GraphQLObjectType> newPossibleObjects) { | ||
| if(!options.deferSupport) { | ||
| Set<GraphQLObjectType> newPossibleObjects) { | ||
| if (!options.deferSupport) { | ||
| return null; | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
minor consistency issue - uses this.