mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-10-23 06:47:26 +00:00
Enhance the tests to cover the end status code as well.
This commit is contained in:
@@ -184,11 +184,20 @@ func checkSelections(c *fiber.Ctx, selections []ast.Selection) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Check nested selections even if current field is allowed
|
||||||
if sel.SelectionSet != nil {
|
if sel.SelectionSet != nil {
|
||||||
if checkSelections(c, sel.GetSelectionSet().Selections) {
|
if checkSelections(c, sel.GetSelectionSet().Selections) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case *ast.InlineFragment:
|
||||||
|
if sel.SelectionSet != nil {
|
||||||
|
if checkSelections(c, sel.GetSelectionSet().Selections) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case *ast.FragmentSpread:
|
||||||
|
// If we need to handle fragment spreads, additional logic would go here
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/goccy/go-json"
|
||||||
fiber "github.com/gofiber/fiber/v2"
|
fiber "github.com/gofiber/fiber/v2"
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
@@ -432,3 +433,60 @@ func createTestContext(body string) *fiber.Ctx {
|
|||||||
ctx.Request().SetBody([]byte(body))
|
ctx.Request().SetBody([]byte(body))
|
||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (suite *Tests) Test_DeepIntrospectionQueries() {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
query string
|
||||||
|
allowed []string
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "deeply nested single introspection",
|
||||||
|
query: "query { users { profiles { settings { preferences { __typename } } } } }",
|
||||||
|
allowed: []string{},
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multiple nested introspections",
|
||||||
|
query: "query { users { __typename profiles { __schema settings { __type } } } }",
|
||||||
|
allowed: []string{},
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "nested with selective allowlist",
|
||||||
|
query: "query { users { __typename profiles { __schema settings { __type } } } }",
|
||||||
|
allowed: []string{"__typename"},
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "deeply nested with full allowlist",
|
||||||
|
query: "query { users { __typename profiles { __schema settings { __type } } } }",
|
||||||
|
allowed: []string{"__typename", "__schema", "__type"},
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
suite.Run(tt.name, func() {
|
||||||
|
cfg.Security.BlockIntrospection = true
|
||||||
|
cfg.Security.IntrospectionAllowed = tt.allowed
|
||||||
|
introspectionAllowedQueries = make(map[string]struct{})
|
||||||
|
for _, q := range tt.allowed {
|
||||||
|
introspectionAllowedQueries[strings.ToLower(q)] = struct{}{}
|
||||||
|
}
|
||||||
|
body := map[string]interface{}{
|
||||||
|
"query": tt.query,
|
||||||
|
}
|
||||||
|
bodyBytes, _ := json.Marshal(body)
|
||||||
|
ctx := fiber.New().AcquireCtx(&fasthttp.RequestCtx{})
|
||||||
|
ctx.Request().SetBody(bodyBytes)
|
||||||
|
parseGraphQLQuery(ctx)
|
||||||
|
if tt.expected {
|
||||||
|
suite.Equal(403, ctx.Response().StatusCode())
|
||||||
|
} else {
|
||||||
|
suite.Equal(200, ctx.Response().StatusCode())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user