-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Query complexity calculation and limits #315
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
95ed529
New complexity package
edsrzf 238a7e2
Add complexity support to codegen, handler
edsrzf 556b93a
Run go generate ./...
edsrzf cecd84c
Add complexity package tests
edsrzf 8c3aed7
Merge branch 'master' into query-complexity
edsrzf e834f6b
Query complexity docs
edsrzf e5265ac
Fix complexity template bug
edsrzf 8da5d61
Generate complexity for all fields. Fix bugs. Re-generate examples.
edsrzf 239b1d2
Don't emit complexity fields for reserved objects
edsrzf 6e408d5
Interfaces take max complexity of implementors
edsrzf 2ab857e
Merge branch 'master' into query-complexity
edsrzf 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 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 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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,85 @@ | ||
package complexity | ||
|
||
import ( | ||
"github.com/99designs/gqlgen/graphql" | ||
"github.com/vektah/gqlparser/ast" | ||
) | ||
|
||
func Calculate(es graphql.ExecutableSchema, op *ast.OperationDefinition, vars map[string]interface{}) int { | ||
walker := complexityWalker{ | ||
es: es, | ||
vars: vars, | ||
} | ||
typeName := "" | ||
switch op.Operation { | ||
case ast.Query: | ||
typeName = es.Schema().Query.Name | ||
case ast.Mutation: | ||
typeName = es.Schema().Mutation.Name | ||
case ast.Subscription: | ||
typeName = es.Schema().Subscription.Name | ||
} | ||
return walker.selectionSetComplexity(typeName, op.SelectionSet) | ||
} | ||
|
||
type complexityWalker struct { | ||
es graphql.ExecutableSchema | ||
vars map[string]interface{} | ||
} | ||
|
||
func (cw complexityWalker) selectionSetComplexity(typeName string, selectionSet ast.SelectionSet) int { | ||
var complexity int | ||
for _, selection := range selectionSet { | ||
switch s := selection.(type) { | ||
case *ast.Field: | ||
var childComplexity int | ||
switch s.ObjectDefinition.Kind { | ||
case ast.Object, ast.Interface, ast.Union: | ||
childComplexity = cw.selectionSetComplexity(s.ObjectDefinition.Name, s.SelectionSet) | ||
} | ||
|
||
args := s.ArgumentMap(cw.vars) | ||
if customComplexity, ok := cw.es.Complexity(typeName, s.Name, childComplexity, args); ok { | ||
complexity = safeAdd(complexity, customComplexity) | ||
} else { | ||
// default complexity calculation | ||
complexity = safeAdd(complexity, safeAdd(1, childComplexity)) | ||
} | ||
|
||
case *ast.FragmentSpread: | ||
complexity = safeAdd(complexity, cw.selectionSetComplexity(typeName, s.Definition.SelectionSet)) | ||
|
||
case *ast.InlineFragment: | ||
complexity = safeAdd(complexity, cw.selectionSetComplexity(typeName, s.SelectionSet)) | ||
} | ||
} | ||
return complexity | ||
} | ||
|
||
// safeAdd is a saturating add of a and b that ignores negative operands. | ||
// If a + b would overflow through normal Go addition, | ||
// it returns the maximum integer value instead. | ||
// | ||
// Adding complexities with this function prevents attackers from intentionally | ||
// overflowing the complexity calculation to allow overly-complex queries. | ||
// | ||
// It also helps mitigate the impact of custom complexities that accidentally | ||
// return negative values. | ||
func safeAdd(a, b int) int { | ||
// Ignore negative operands. | ||
if a <= 0 { | ||
if b < 0 { | ||
return 0 | ||
} | ||
return b | ||
} else if b <= 0 { | ||
return a | ||
} | ||
|
||
c := a + b | ||
if c < a { | ||
// Set c to maximum integer instead of overflowing. | ||
c = int(^uint(0) >> 1) | ||
} | ||
return c | ||
} |
This file contains 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 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 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
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.
This is basically copied from
args.gotpl
. There's probably a better way to do this with less duplication.In general, it seems like it might be too expensive to be doing all the argument conversion and unmarshaling twice, as well. (Now it will happen once for the complexity calculation, and then again for the actual query execution.)
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.
It should be pretty cheap as long as it only happens if there is a complexity function defined? Might be a bunch of extra generated code, but that's OK.
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.
What prevents you from calling
args.gotpl
directly?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.
It wants to
return graphql.Null
in error cases, which is the wrong type for this function.