-
Notifications
You must be signed in to change notification settings - Fork 158
fix: avoid recursive input message stack overflows in gRPC planner #1446
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
fengyuwusong
wants to merge
6
commits into
wundergraph:master
Choose a base branch
from
fengyuwusong:codex/issue-2649-recursive-input-stack-overflow
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.
+303
−12
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
abd86c1
fix(grpc-datasource): reuse recursive input messages
fengyuwusong 8e3caa8
fix: add cycle detection in formatRPCMessage to prevent stack overflow
fengyuwusong 5e0437b
fix: correct gci import grouping in recursive input test
fengyuwusong 7339388
Merge branch 'master' into codex/issue-2649-recursive-input-stack-ove…
fengyuwusong 4b5a365
Merge branch 'master' into codex/issue-2649-recursive-input-stack-ove…
Noroth 803c481
test: add nested and multi-argument recursive input test cases
fengyuwusong 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
270 changes: 270 additions & 0 deletions
270
v2/pkg/engine/datasource/grpc_datasource/execution_plan_recursive_input_test.go
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,270 @@ | ||
| package grpcdatasource | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/ast" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/astparser" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/asttransform" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/astvalidation" | ||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/operationreport" | ||
| ) | ||
|
|
||
| func TestMutationExecutionPlanWithRecursiveInputType(t *testing.T) { | ||
| schemaDoc := mustParseRecursiveInputSchema(t, ` | ||
| scalar JSON | ||
|
|
||
| type Query { | ||
| noop: Boolean | ||
| } | ||
|
|
||
| type Mutation { | ||
| updateNode(input: UpdateNodeInput!): Node! | ||
| } | ||
|
|
||
| type Node { | ||
| id: ID! | ||
| } | ||
|
|
||
| input UpdateNodeInput { | ||
| id: ID! | ||
| conditions: RecursiveFilterInput | ||
| } | ||
|
|
||
| input RecursiveFilterInput { | ||
| and: [RecursiveFilterInput!] | ||
| or: [RecursiveFilterInput!] | ||
| key: String | ||
| value: JSON | ||
| } | ||
| `) | ||
|
|
||
| queryDoc, report := astparser.ParseGraphqlDocumentString(` | ||
| mutation UpdateNode($input: UpdateNodeInput!) { | ||
| updateNode(input: $input) { | ||
| id | ||
| } | ||
| } | ||
| `) | ||
| require.False(t, report.HasErrors(), report.Error()) | ||
|
|
||
| plan, err := newRPCPlanVisitor(rpcPlanVisitorConfig{ | ||
| subgraphName: "Products", | ||
| mapping: &GRPCMapping{ | ||
| Service: "Products", | ||
| MutationRPCs: RPCConfigMap[RPCConfig]{ | ||
| "updateNode": { | ||
| RPC: "MutationUpdateNode", | ||
| Request: "MutationUpdateNodeRequest", | ||
| Response: "MutationUpdateNodeResponse", | ||
| }, | ||
| }, | ||
| }, | ||
| }).PlanOperation(&queryDoc, &schemaDoc) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, plan) | ||
| require.NotPanics(t, func() { _ = plan.String() }) | ||
| require.Len(t, plan.Calls, 1) | ||
|
|
||
| inputField := lookupField(plan.Calls[0].Request.Fields, "input") | ||
| require.NotNil(t, inputField) | ||
| require.NotNil(t, inputField.Message) | ||
|
|
||
| conditionsField := lookupField(inputField.Message.Fields, "conditions") | ||
| require.NotNil(t, conditionsField) | ||
| require.NotNil(t, conditionsField.Message) | ||
|
|
||
| andField := lookupField(conditionsField.Message.Fields, "and") | ||
| require.NotNil(t, andField) | ||
| require.True(t, andField.Repeated || andField.IsListType) | ||
| require.Same(t, conditionsField.Message, andField.Message) | ||
|
|
||
| orField := lookupField(conditionsField.Message.Fields, "or") | ||
| require.NotNil(t, orField) | ||
| require.True(t, orField.Repeated || orField.IsListType) | ||
| require.Same(t, conditionsField.Message, orField.Message) | ||
|
Contributor
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. When looking at the test, you haven't checked if something like this: |
||
|
|
||
| keyField := lookupField(conditionsField.Message.Fields, "key") | ||
| require.NotNil(t, keyField) | ||
|
|
||
| valueField := lookupField(conditionsField.Message.Fields, "value") | ||
| require.NotNil(t, valueField) | ||
| } | ||
|
|
||
| func TestMutationExecutionPlanWithNestedRecursiveInputTypes(t *testing.T) { | ||
| schemaDoc := mustParseRecursiveInputSchema(t, ` | ||
| type Query { | ||
| noop: Boolean | ||
| } | ||
|
|
||
| type Mutation { | ||
| processA(input: A!): Boolean! | ||
| } | ||
|
|
||
| input A { | ||
| b: B | ||
| } | ||
|
|
||
| input B { | ||
| c: C | ||
| } | ||
|
|
||
| input C { | ||
| a: A | ||
| b: B | ||
| } | ||
| `) | ||
|
|
||
| queryDoc, report := astparser.ParseGraphqlDocumentString(` | ||
| mutation ProcessA($input: A!) { | ||
| processA(input: $input) | ||
| } | ||
| `) | ||
| require.False(t, report.HasErrors(), report.Error()) | ||
|
|
||
| plan, err := newRPCPlanVisitor(rpcPlanVisitorConfig{ | ||
| subgraphName: "Products", | ||
| mapping: &GRPCMapping{ | ||
| Service: "Products", | ||
| MutationRPCs: RPCConfigMap[RPCConfig]{ | ||
| "processA": { | ||
| RPC: "MutationProcessA", | ||
| Request: "MutationProcessARequest", | ||
| Response: "MutationProcessAResponse", | ||
| }, | ||
| }, | ||
| }, | ||
| }).PlanOperation(&queryDoc, &schemaDoc) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, plan) | ||
| require.NotPanics(t, func() { _ = plan.String() }) | ||
| require.Len(t, plan.Calls, 1) | ||
|
|
||
| inputField := lookupField(plan.Calls[0].Request.Fields, "input") | ||
| require.NotNil(t, inputField) | ||
| require.NotNil(t, inputField.Message) | ||
|
|
||
| // A.b -> B | ||
| aMessage := inputField.Message | ||
| require.Equal(t, "A", aMessage.Name) | ||
| bFieldInA := lookupField(aMessage.Fields, "b") | ||
| require.NotNil(t, bFieldInA) | ||
| require.NotNil(t, bFieldInA.Message) | ||
|
|
||
| // B.c -> C | ||
| bMessage := bFieldInA.Message | ||
| require.Equal(t, "B", bMessage.Name) | ||
| cFieldInB := lookupField(bMessage.Fields, "c") | ||
| require.NotNil(t, cFieldInB) | ||
| require.NotNil(t, cFieldInB.Message) | ||
|
|
||
| // C.a -> A (same pointer as top-level A) | ||
| cMessage := cFieldInB.Message | ||
| require.Equal(t, "C", cMessage.Name) | ||
| aFieldInC := lookupField(cMessage.Fields, "a") | ||
| require.NotNil(t, aFieldInC) | ||
| require.NotNil(t, aFieldInC.Message) | ||
| require.Same(t, aMessage, aFieldInC.Message) | ||
|
|
||
| // C.b -> B (same pointer as A.b's message) | ||
| bFieldInC := lookupField(cMessage.Fields, "b") | ||
| require.NotNil(t, bFieldInC) | ||
| require.NotNil(t, bFieldInC.Message) | ||
| require.Same(t, bMessage, bFieldInC.Message) | ||
| } | ||
|
|
||
| func TestMutationExecutionPlanWithMultipleRecursiveArguments(t *testing.T) { | ||
| schemaDoc := mustParseRecursiveInputSchema(t, ` | ||
| type Query { | ||
| noop: Boolean | ||
| } | ||
|
|
||
| type Mutation { | ||
| processFilters(filter: RecursiveFilter!, exclude: RecursiveFilter!): Boolean! | ||
| } | ||
|
|
||
| input RecursiveFilter { | ||
| and: [RecursiveFilter!] | ||
| or: [RecursiveFilter!] | ||
| key: String | ||
| } | ||
| `) | ||
|
|
||
| queryDoc, report := astparser.ParseGraphqlDocumentString(` | ||
| mutation ProcessFilters($filter: RecursiveFilter!, $exclude: RecursiveFilter!) { | ||
| processFilters(filter: $filter, exclude: $exclude) | ||
| } | ||
| `) | ||
| require.False(t, report.HasErrors(), report.Error()) | ||
|
|
||
| plan, err := newRPCPlanVisitor(rpcPlanVisitorConfig{ | ||
| subgraphName: "Products", | ||
| mapping: &GRPCMapping{ | ||
| Service: "Products", | ||
| MutationRPCs: RPCConfigMap[RPCConfig]{ | ||
| "processFilters": { | ||
| RPC: "MutationProcessFilters", | ||
| Request: "MutationProcessFiltersRequest", | ||
| Response: "MutationProcessFiltersResponse", | ||
| }, | ||
| }, | ||
| }, | ||
| }).PlanOperation(&queryDoc, &schemaDoc) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, plan) | ||
| require.NotPanics(t, func() { _ = plan.String() }) | ||
| require.Len(t, plan.Calls, 1) | ||
|
|
||
| filterField := lookupField(plan.Calls[0].Request.Fields, "filter") | ||
| require.NotNil(t, filterField) | ||
| require.NotNil(t, filterField.Message) | ||
|
|
||
| excludeField := lookupField(plan.Calls[0].Request.Fields, "exclude") | ||
| require.NotNil(t, excludeField) | ||
| require.NotNil(t, excludeField.Message) | ||
|
|
||
| // Both arguments share the same RecursiveFilter message via cache | ||
| require.Same(t, filterField.Message, excludeField.Message) | ||
|
|
||
| // Verify self-referencing fields | ||
| andField := lookupField(filterField.Message.Fields, "and") | ||
| require.NotNil(t, andField) | ||
| require.True(t, andField.Repeated || andField.IsListType) | ||
| require.Same(t, filterField.Message, andField.Message) | ||
|
|
||
| orField := lookupField(filterField.Message.Fields, "or") | ||
| require.NotNil(t, orField) | ||
| require.True(t, orField.Repeated || orField.IsListType) | ||
| require.Same(t, filterField.Message, orField.Message) | ||
|
|
||
| keyField := lookupField(filterField.Message.Fields, "key") | ||
| require.NotNil(t, keyField) | ||
| } | ||
|
|
||
| func lookupField(fields RPCFields, name string) *RPCField { | ||
| for i := range fields { | ||
| if fields[i].Name == name { | ||
| return &fields[i] | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func mustParseRecursiveInputSchema(t *testing.T, schema string) ast.Document { | ||
| t.Helper() | ||
|
|
||
| doc, report := astparser.ParseGraphqlDocumentString(schema) | ||
| require.False(t, report.HasErrors(), report.Error()) | ||
|
|
||
| err := asttransform.MergeDefinitionWithBaseSchema(&doc) | ||
| require.NoError(t, err) | ||
|
|
||
| report = operationreport.Report{} | ||
| astvalidation.DefaultDefinitionValidator().Validate(&doc, &report) | ||
| require.False(t, report.HasErrors(), report.Error()) | ||
|
|
||
| return doc | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.