-
Notifications
You must be signed in to change notification settings - Fork 158
fix: grpc datasource stack overflows on recursive input #1466
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
dkorittki
merged 11 commits into
master
from
dominik/eng-9232-router-grpc-datasource-stack-overflows-on-recursive-input
Apr 20, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ce26b96
chore: add tests
dkorittki c275ee2
fix: use existing rpc methods if already visited
dkorittki 7622a4c
Merge branch 'master' into dominik/eng-9232-router-grpc-datasource-st…
dkorittki be40dc7
chore: use predefined size for lookup map
dkorittki a94d2b0
chore: run gci formatter
dkorittki 23adc8a
fix: prevent stack overflow in stringer
dkorittki 09bbc35
chore: assert method name in all recursive tests
dkorittki b3911b8
chore: use json path instead of index to find fields
dkorittki ad8b422
chore: strengthen stringer test assertion
dkorittki 84385cd
chore: add grpctest
dkorittki 059ca57
Merge branch 'master' into dominik/eng-9232-router-grpc-datasource-st…
Noroth 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
242 changes: 242 additions & 0 deletions
242
v2/pkg/engine/datasource/grpc_datasource/execution_plan_recursive_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,242 @@ | ||
| package grpcdatasource | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/wundergraph/graphql-go-tools/v2/pkg/astparser" | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| func TestExecutionPlan_RecursiveInputTypes_String(t *testing.T) { | ||
| // Verify stringer method does not overflow on recursive inputs | ||
| t.Parallel() | ||
|
|
||
| schema := ` | ||
| type Query { | ||
| search(conditions: ConditionsInput): [Result!]! | ||
| } | ||
|
|
||
| type Result { | ||
| id: ID! | ||
| name: String! | ||
| } | ||
|
|
||
| input ConditionsInput { | ||
| and: [ConditionsInput!] | ||
| or: [ConditionsInput!] | ||
| key: String | ||
| value: String | ||
| }` | ||
|
|
||
| mapping := &GRPCMapping{ | ||
| Service: "Search", | ||
| QueryRPCs: map[string]RPCConfig{ | ||
| "search": { | ||
| RPC: "Search", | ||
| Request: "SearchRequest", | ||
| Response: "SearchResponse", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| query := `query SearchQuery($conditions: ConditionsInput) { search(conditions: $conditions) { id name } }` | ||
|
|
||
| plan := planRecursiveTest(t, query, schema, mapping) | ||
|
|
||
| result := plan.String() | ||
| // formatRPCMessage must emit the recursive placeholder instead of overflowing. | ||
| require.Contains(t, result, "ConditionsInput") | ||
| require.Contains(t, result, "<recursive: ConditionsInput>") | ||
| } | ||
|
|
||
| func TestExecutionPlan_RecursiveInputTypes(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("Should not stack overflow on recursive input object with and/or fields", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| schema := ` | ||
| type Query { | ||
| search(conditions: ConditionsInput): [Result!]! | ||
| } | ||
|
|
||
| type Result { | ||
| id: ID! | ||
| name: String! | ||
| } | ||
|
|
||
| input ConditionsInput { | ||
| and: [ConditionsInput!] | ||
| or: [ConditionsInput!] | ||
| key: String | ||
| value: String | ||
| }` | ||
|
|
||
| mapping := &GRPCMapping{ | ||
| Service: "Search", | ||
| QueryRPCs: map[string]RPCConfig{ | ||
| "search": { | ||
| RPC: "Search", | ||
| Request: "SearchRequest", | ||
| Response: "SearchResponse", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| query := `query SearchQuery($conditions: ConditionsInput) { search(conditions: $conditions) { id name } }` | ||
|
|
||
| plan := planRecursiveTest(t, query, schema, mapping) | ||
|
|
||
| require.Len(t, plan.Calls, 1) | ||
| call := plan.Calls[0] | ||
| require.Equal(t, "Search", call.MethodName) | ||
|
|
||
| // The request should have a conditions field with a recursive message. | ||
| require.Len(t, call.Request.Fields, 1) | ||
| conditionsField := call.Request.Fields[0] | ||
| require.Equal(t, "conditions", conditionsField.JSONPath) | ||
| require.NotNil(t, conditionsField.Message) | ||
| require.Equal(t, "ConditionsInput", conditionsField.Message.Name) | ||
| require.Len(t, conditionsField.Message.Fields, 4) | ||
|
|
||
| // The and/or fields should reference the same ConditionsInput message (cycle). | ||
| andField := findField(t, conditionsField.Message.Fields, "and") | ||
| orField := findField(t, conditionsField.Message.Fields, "or") | ||
| require.True(t, andField.Message == conditionsField.Message, "and field should reference the same ConditionsInput message") | ||
| require.True(t, orField.Message == conditionsField.Message, "or field should reference the same ConditionsInput message") | ||
| }) | ||
|
|
||
| t.Run("Should not stack overflow on self-referencing input object", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| schema := ` | ||
| type Query { | ||
| filter(input: FilterInput): [Item!]! | ||
| } | ||
|
|
||
| type Item { | ||
| id: ID! | ||
| } | ||
|
|
||
| input FilterInput { | ||
| child: FilterInput | ||
| value: String | ||
| }` | ||
|
|
||
| mapping := &GRPCMapping{ | ||
| Service: "Items", | ||
| QueryRPCs: map[string]RPCConfig{ | ||
| "filter": { | ||
| RPC: "Filter", | ||
| Request: "FilterRequest", | ||
| Response: "FilterResponse", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| query := `query FilterQuery($input: FilterInput) { filter(input: $input) { id } }` | ||
|
|
||
| plan := planRecursiveTest(t, query, schema, mapping) | ||
|
|
||
| require.Len(t, plan.Calls, 1) | ||
| call := plan.Calls[0] | ||
| require.Equal(t, "Filter", call.MethodName) | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| require.Len(t, call.Request.Fields, 1) | ||
| inputField := call.Request.Fields[0] | ||
| require.Equal(t, "input", inputField.JSONPath) | ||
| require.NotNil(t, inputField.Message) | ||
| require.Equal(t, "FilterInput", inputField.Message.Name) | ||
| require.Len(t, inputField.Message.Fields, 2) | ||
|
|
||
| // The child field should reference the same FilterInput message. | ||
| childField := findField(t, inputField.Message.Fields, "child") | ||
| require.True(t, childField.Message == inputField.Message, "child field should reference the same FilterInput message") | ||
| }) | ||
|
|
||
| t.Run("Should not stack overflow on mutually recursive input objects", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| schema := ` | ||
| type Query { | ||
| evaluate(expr: ExprInput): Boolean! | ||
| } | ||
|
|
||
| input ExprInput { | ||
| not: NotExprInput | ||
| value: String | ||
| } | ||
|
|
||
| input NotExprInput { | ||
| expr: ExprInput | ||
| }` | ||
|
|
||
| mapping := &GRPCMapping{ | ||
| Service: "Eval", | ||
| QueryRPCs: map[string]RPCConfig{ | ||
| "evaluate": { | ||
| RPC: "Evaluate", | ||
| Request: "EvaluateRequest", | ||
| Response: "EvaluateResponse", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| query := `query EvalQuery($expr: ExprInput) { evaluate(expr: $expr) }` | ||
|
|
||
| plan := planRecursiveTest(t, query, schema, mapping) | ||
|
|
||
| require.Len(t, plan.Calls, 1) | ||
| call := plan.Calls[0] | ||
| require.Equal(t, "Evaluate", call.MethodName) | ||
|
|
||
| require.Len(t, call.Request.Fields, 1) | ||
| exprField := call.Request.Fields[0] | ||
| require.Equal(t, "expr", exprField.JSONPath) | ||
| require.NotNil(t, exprField.Message) | ||
| require.Equal(t, "ExprInput", exprField.Message.Name) | ||
| require.Len(t, exprField.Message.Fields, 2) | ||
|
|
||
| // ExprInput.not -> NotExprInput.expr -> ExprInput (cycle) | ||
| notField := findField(t, exprField.Message.Fields, "not") | ||
| require.NotNil(t, notField.Message) | ||
| require.Equal(t, "NotExprInput", notField.Message.Name) | ||
| require.Len(t, notField.Message.Fields, 1) | ||
|
|
||
| backRef := findField(t, notField.Message.Fields, "expr") | ||
| require.True(t, backRef.Message == exprField.Message, "NotExprInput.expr should reference the same ExprInput message") | ||
| }) | ||
| } | ||
|
|
||
| func findField(t *testing.T, fields RPCFields, jsonPath string) RPCField { | ||
| t.Helper() | ||
|
|
||
| for _, f := range fields { | ||
| if f.JSONPath == jsonPath { | ||
| return f | ||
| } | ||
| } | ||
|
|
||
| t.Fatalf("field with JSONPath %q not found", jsonPath) | ||
| return RPCField{} | ||
| } | ||
|
|
||
| func planRecursiveTest(t *testing.T, query, schema string, mapping *GRPCMapping) *RPCExecutionPlan { | ||
| t.Helper() | ||
|
|
||
| schemaDoc := testSchema(t, schema) | ||
|
|
||
| queryDoc, report := astparser.ParseGraphqlDocumentString(query) | ||
| require.False(t, report.HasErrors()) | ||
|
|
||
| rpcPlanVisitor := newRPCPlanVisitor(rpcPlanVisitorConfig{ | ||
| subgraphName: mapping.Service, | ||
| mapping: mapping, | ||
| }) | ||
|
|
||
| plan, err := rpcPlanVisitor.PlanOperation(&queryDoc, &schemaDoc) | ||
| require.NoError(t, err) | ||
|
|
||
| return plan | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.