Skip to content
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

tftypes: Allow null DynamicPseudoType in newValue and prevent potential msgpack panic #99

Merged
merged 5 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/99.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
tftypes: Prevent potential panic unmarshaling null DynamicPseudoType in msgpack
```
9 changes: 0 additions & 9 deletions tftypes/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,6 @@ func (o Object) supportedGoTypes() []string {
return []string{"map[string]tftypes.Value"}
}

func valueCanBeObject(val interface{}) bool {
switch val.(type) {
case map[string]Value:
return true
default:
return false
}
}

func valueFromObject(types map[string]Type, optionalAttrs map[string]struct{}, in interface{}) (Value, error) {
switch value := in.(type) {
case map[string]Value:
Expand Down
93 changes: 1 addition & 92 deletions tftypes/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,30 +102,11 @@ func (p primitive) supportedGoTypes() []string {
case Bool.name:
return []string{"bool", "*bool"}
case DynamicPseudoType.name:
possibleTypes := []Type{
String, Bool, Number,
Tuple{}, Object{},
}
results := []string{}
for _, t := range possibleTypes {
results = append(results, t.supportedGoTypes()...)
}
return results
return []string{"nil"}
bflad marked this conversation as resolved.
Show resolved Hide resolved
}
panic(fmt.Sprintf("unknown primitive type %q", p.name))
}

func valueCanBeString(val interface{}) bool {
switch val.(type) {
case string:
return true
case *string:
return true
default:
return false
}
}

func valueFromString(in interface{}) (Value, error) {
switch value := in.(type) {
case *string:
Expand All @@ -149,17 +130,6 @@ func valueFromString(in interface{}) (Value, error) {
}
}

func valueCanBeBool(val interface{}) bool {
switch val.(type) {
case bool:
return true
case *bool:
return true
default:
return false
}
}

func valueFromBool(in interface{}) (Value, error) {
switch value := in.(type) {
case *bool:
Expand All @@ -183,25 +153,6 @@ func valueFromBool(in interface{}) (Value, error) {
}
}

func valueCanBeNumber(val interface{}) bool {
switch val.(type) {
case uint, uint8, uint16, uint32, uint64:
return true
case *uint, *uint8, *uint16, *uint32, *uint64:
return true
case int, int8, int16, int32, int64:
return true
case *int, *int8, *int16, *int32, *int64:
return true
case float64, *float64:
return true
case *big.Float:
return true
default:
return false
}
}

func valueFromNumber(in interface{}) (Value, error) {
switch value := in.(type) {
case *big.Float:
Expand Down Expand Up @@ -389,45 +340,3 @@ func valueFromNumber(in interface{}) (Value, error) {
return Value{}, fmt.Errorf("tftypes.NewValue can't use %T as a tftypes.Number; expected types are: %s", in, formattedSupportedGoTypes(Number))
}
}

func valueFromDynamicPseudoType(val interface{}) (Value, error) {
switch {
case valueCanBeString(val):
v, err := valueFromString(val)
if err != nil {
return Value{}, err
}
v.typ = DynamicPseudoType
return v, nil
case valueCanBeNumber(val):
v, err := valueFromNumber(val)
if err != nil {
return Value{}, err
}
v.typ = DynamicPseudoType
return v, nil
case valueCanBeBool(val):
v, err := valueFromBool(val)
if err != nil {
return Value{}, err
}
v.typ = DynamicPseudoType
return v, nil
case valueCanBeObject(val):
v, err := valueFromObject(nil, nil, val)
if err != nil {
return Value{}, err
}
v.typ = DynamicPseudoType
return v, nil
case valueCanBeTuple(val):
v, err := valueFromTuple(nil, val)
if err != nil {
return Value{}, err
}
v.typ = DynamicPseudoType
return v, nil
default:
return Value{}, fmt.Errorf("tftypes.NewValue can't use %T as a tftypes.DynamicPseudoType; expected types are: %s", val, formattedSupportedGoTypes(DynamicPseudoType))
}
}
9 changes: 0 additions & 9 deletions tftypes/tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,6 @@ func (tu Tuple) supportedGoTypes() []string {
return []string{"[]tftypes.Value"}
}

func valueCanBeTuple(val interface{}) bool {
switch val.(type) {
case []Value:
return true
default:
return false
}
}

func valueFromTuple(types []Type, in interface{}) (Value, error) {
switch value := in.(type) {
case []Value:
Expand Down
14 changes: 5 additions & 9 deletions tftypes/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,17 @@ func ValidateValue(t Type, val interface{}) error {
}

func newValue(t Type, val interface{}) (Value, error) {
if t.Is(DynamicPseudoType) && val != UnknownValue {
return Value{}, errors.New("cannot have DynamicPseudoType with known value, DynamicPseudoType can only contain unknown values")
}
if val == nil || val == UnknownValue {
return Value{
typ: t,
value: val,
}, nil
}

if t.Is(DynamicPseudoType) {
return Value{}, errors.New("cannot have DynamicPseudoType with known value, DynamicPseudoType can only contain null or unknown values")
}

if creator, ok := val.(ValueCreator); ok {
var err error
val, err = creator.ToTerraform5Value()
Expand All @@ -307,12 +309,6 @@ func newValue(t Type, val interface{}) (Value, error) {
}

switch {
case t.Is(DynamicPseudoType):
v, err := valueFromDynamicPseudoType(val)
if err != nil {
return Value{}, err
}
return v, nil
case t.Is(String):
v, err := valueFromString(val)
if err != nil {
Expand Down
20 changes: 14 additions & 6 deletions tftypes/value_dpt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,20 @@ func Test_newValue_dpt(t *testing.T) {
expected Value
}
tests := map[string]testCase{
"normal": {
"known": {
typ: DynamicPseudoType,
val: "hello",
err: regexp.MustCompile(`cannot have DynamicPseudoType with known value, DynamicPseudoType can only contain null or unknown values`),
},
"null": {
typ: DynamicPseudoType,
val: nil,
expected: Value{
typ: DynamicPseudoType,
value: nil,
},
},
"unknown": {
typ: DynamicPseudoType,
val: UnknownValue,
expected: Value{
Expand All @@ -23,11 +36,6 @@ func Test_newValue_dpt(t *testing.T) {
},
err: nil,
},
"dynamic-known": {
typ: DynamicPseudoType,
val: "hello",
err: regexp.MustCompile(`cannot have DynamicPseudoType with known value, DynamicPseudoType can only contain unknown values`),
},
}
for name, test := range tests {
name, test := name, test
Expand Down
2 changes: 1 addition & 1 deletion tftypes/value_msgpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func msgpackUnmarshalDynamic(dec *msgpack.Decoder, path *AttributePath) (Value,

switch {
case length == -1:
return NewValue(DynamicPseudoType, nil), nil
return newValue(DynamicPseudoType, nil)
case length != 2:
return Value{}, path.NewErrorf("expected %d elements in DynamicPseudoType array, got %d", 2, length)
}
Expand Down
5 changes: 5 additions & 0 deletions tftypes/value_msgpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ func TestValueFromMsgPack(t *testing.T) {
value: NewValue(String, nil),
typ: DynamicPseudoType,
},
"dynamic-null": {
hex: "c0",
value: NewValue(DynamicPseudoType, nil),
typ: DynamicPseudoType,
},
"dynamic-unknown": {
hex: "d40000",
value: NewValue(DynamicPseudoType, UnknownValue),
Expand Down
14 changes: 14 additions & 0 deletions tftypes/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ func TestValueIsKnown(t *testing.T) {
known: false,
fullyKnown: false,
},
"dynamic-unknown": {
value: NewValue(DynamicPseudoType, UnknownValue),
known: false,
fullyKnown: false,
},
"list-string-known": {
value: NewValue(List{ElementType: String}, []Value{NewValue(String, "hello")}),
known: true,
Expand Down Expand Up @@ -655,6 +660,11 @@ func TestValueIsKnown(t *testing.T) {
known: true,
fullyKnown: false,
},
"dynamic-null": {
value: NewValue(DynamicPseudoType, nil),
known: true,
fullyKnown: true,
},
"object-null": {
value: NewValue(Object{AttributeTypes: map[string]Type{
"foo": String,
Expand Down Expand Up @@ -1606,6 +1616,10 @@ func TestValueString(t *testing.T) {
in: NewValue(Bool, nil),
expected: "tftypes.Bool<null>",
},
"dynamic-null": {
in: NewValue(DynamicPseudoType, nil),
expected: "tftypes.DynamicPseudoType<null>",
},
"map": {
in: NewValue(Map{AttributeType: String}, map[string]Value{
"hello": NewValue(String, "world"),
Expand Down