Skip to content

Commit

Permalink
Merge pull request #223 from graphql-go/titanous/fix-nil-crashes
Browse files Browse the repository at this point in the history
go-fuzz nil panic fixes
  • Loading branch information
chris-ramon authored Aug 5, 2017
2 parents d114382 + 24b3245 commit 36ee564
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 28 deletions.
55 changes: 33 additions & 22 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,30 +214,38 @@ func FieldsOnCorrectTypeRule(context *ValidationContext) *ValidationRuleInstance
var result interface{}
if node, ok := p.Node.(*ast.Field); ok {
ttype := context.ParentType()
if ttype == nil {
return action, result
}
if t, ok := ttype.(*Object); ok && t == nil {
return action, result
}
if t, ok := ttype.(*Interface); ok && t == nil {
return action, result
}
if t, ok := ttype.(*Union); ok && t == nil {
return action, result
}
fieldDef := context.FieldDef()
if fieldDef == nil {
// This field doesn't exist, lets look for suggestions.
nodeName := ""
if node.Name != nil {
nodeName = node.Name.Value
}
// First determine if there are any suggested types to condition on.
suggestedTypeNames := getSuggestedTypeNames(context.Schema(), ttype, nodeName)

if ttype != nil {
fieldDef := context.FieldDef()
if fieldDef == nil {
// This field doesn't exist, lets look for suggestions.
nodeName := ""
if node.Name != nil {
nodeName = node.Name.Value
}
// First determine if there are any suggested types to condition on.
suggestedTypeNames := getSuggestedTypeNames(context.Schema(), ttype, nodeName)

// If there are no suggested types, then perhaps this was a typo?
suggestedFieldNames := []string{}
if len(suggestedTypeNames) == 0 {
suggestedFieldNames = getSuggestedFieldNames(context.Schema(), ttype, nodeName)
}

reportError(
context,
UndefinedFieldMessage(nodeName, ttype.Name(), suggestedTypeNames, suggestedFieldNames),
[]ast.Node{node},
)
// If there are no suggested types, then perhaps this was a typo?
suggestedFieldNames := []string{}
if len(suggestedTypeNames) == 0 {
suggestedFieldNames = getSuggestedFieldNames(context.Schema(), ttype, nodeName)
}
reportError(
context,
UndefinedFieldMessage(nodeName, ttype.Name(), suggestedTypeNames, suggestedFieldNames),
[]ast.Node{node},
)
}
}
return action, result
Expand Down Expand Up @@ -1717,6 +1725,9 @@ func VariablesInAllowedPositionRule(context *ValidationContext) *ValidationRuleI
func isValidLiteralValue(ttype Input, valueAST ast.Value) (bool, []string) {
// A value must be provided if the type is non-null.
if ttype, ok := ttype.(*NonNull); ok {
if e := ttype.Error(); e != nil {
return false, []string{e.Error()}
}
if valueAST == nil {
if ttype.OfType.Name() != "" {
return false, []string{fmt.Sprintf(`Expected "%v!", found null.`, ttype.OfType.Name())}
Expand Down
4 changes: 4 additions & 0 deletions rules_default_values_of_correct_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,7 @@ func TestValidate_VariableDefaultValuesOfCorrectType_ListVariablesWithInvalidIte
2, 40),
})
}

func TestValidate_VariableDefaultValuesOfCorrectType_InvalidNonNull(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.DefaultValuesOfCorrectTypeRule, `query($g:e!){a}`)
}
4 changes: 4 additions & 0 deletions rules_fields_on_correct_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,7 @@ func TestValidate_FieldsOnCorrectTypeErrorMessage_LimitLotsOfFieldSuggestions(t
t.Fatalf("Unexpected message, expected: %v, got %v", expected, message)
}
}

func TestValidate_FieldsOnCorrectType_NilCrash(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.FieldsOnCorrectTypeRule, `mutation{o}`)
}
4 changes: 2 additions & 2 deletions rules_overlapping_fields_can_be_merged.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,10 @@ func (rule *overlappingFieldsCanBeMergedRule) getFieldsAndFragmentNames(parentTy
fieldName = selection.Name.Value
}
var fieldDef *FieldDefinition
if parentType, ok := parentType.(*Object); ok {
if parentType, ok := parentType.(*Object); ok && parentType != nil {
fieldDef, _ = parentType.Fields()[fieldName]
}
if parentType, ok := parentType.(*Interface); ok {
if parentType, ok := parentType.(*Interface); ok && parentType != nil {
fieldDef, _ = parentType.Fields()[fieldName]
}

Expand Down
4 changes: 4 additions & 0 deletions rules_overlapping_fields_can_be_merged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -888,3 +888,7 @@ func TestValidate_OverlappingFieldsCanBeMerged_ReturnTypesMustBeUnambiguous_Igno
}
`)
}

func TestValidate_OverlappingFieldsCanBeMerged_NilCrash(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.OverlappingFieldsCanBeMergedRule, `subscription {e}`)
}
8 changes: 4 additions & 4 deletions type_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,14 @@ func DefaultTypeInfoFieldDef(schema *Schema, parentType Type, fieldAST *ast.Fiel
schema.QueryType() == parentType {
return TypeMetaFieldDef
}
if name == TypeNameMetaFieldDef.Name {
if _, ok := parentType.(*Object); ok && parentType != nil {
if name == TypeNameMetaFieldDef.Name && parentType != nil {
if t, ok := parentType.(*Object); ok && t != nil {
return TypeNameMetaFieldDef
}
if _, ok := parentType.(*Interface); ok && parentType != nil {
if t, ok := parentType.(*Interface); ok && t != nil {
return TypeNameMetaFieldDef
}
if _, ok := parentType.(*Union); ok && parentType != nil {
if t, ok := parentType.(*Union); ok && t != nil {
return TypeNameMetaFieldDef
}
}
Expand Down

0 comments on commit 36ee564

Please sign in to comment.