Skip to content

Commit 07477d1

Browse files
committed
Merge pull request #70 from aaronkavlie-wf/nil_nested
Guard agains null array member input causing panic
2 parents da3d5fa + 83a30e5 commit 07477d1

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

rest/rule.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package rest
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"log"
2223
"reflect"
@@ -341,9 +342,13 @@ func applyNestedInboundRules(
341342
s := reflect.ValueOf(value)
342343
nestedValues := make([]interface{}, s.Len())
343344
for i := 0; i < s.Len(); i++ {
345+
val := s.Index(i).Interface()
346+
if val == nil {
347+
return nil, errors.New("nested value is nil")
348+
}
344349
// Check to see if the nested type is a slice or map.
345350
// If not, it should be coerced to its rule type.
346-
iKind := reflect.TypeOf(s.Index(i).Interface()).Kind()
351+
iKind := reflect.TypeOf(val).Kind()
347352
ruleType := rules.Contents()[0].Type
348353
if ruleKind := typeToKind[ruleType]; iKind == reflect.Slice && ruleKind != reflect.Slice {
349354
return nil, fmt.Errorf("Value does not match rule type, expecting: %v, got: %v", ruleKind, iKind)

rest/rule_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,30 @@ func TestApplyInboundRulesNestedRulesDifferentVersion(t *testing.T) {
10731073
assert.Nil(err, "Error should be nil")
10741074
}
10751075

1076+
// Ensures that nested rules throw an error if a member is nil.
1077+
func TestApplyInboundRulesNestedRulesNilMember(t *testing.T) {
1078+
assert := assert.New(t)
1079+
payload := Payload{"foo": []interface{}{nil}}
1080+
rules := NewRules((*TestResourceSlice)(nil),
1081+
&Rule{
1082+
Field: "Foo",
1083+
FieldAlias: "foo",
1084+
Type: Slice,
1085+
Versions: []string{"1"},
1086+
Rules: NewRules((*string)(nil),
1087+
&Rule{
1088+
Type: String,
1089+
},
1090+
),
1091+
},
1092+
)
1093+
1094+
actual, err := applyInboundRules(payload, rules, "1")
1095+
1096+
assert.Nil(actual, "Payload should be nil")
1097+
assert.NotNil(err, "Error should not be nil")
1098+
}
1099+
10761100
// Ensures that non-resource Rules are applied correctly.
10771101
func TestApplyInboundRulesNonResourceRule(t *testing.T) {
10781102
assert := assert.New(t)

0 commit comments

Comments
 (0)