base64decodeFunc now checks for valid UTF-8

This commit is contained in:
Kristin Laemmert 2018-05-24 09:09:22 -07:00 committed by Martin Atkins
parent f8a8f26c0d
commit 6171ba3b8a
2 changed files with 9 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"net/url" "net/url"
"unicode/utf8"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function" "github.com/zclconf/go-cty/cty/function"
@ -26,7 +27,9 @@ var Base64DecodeFunc = function.New(&function.Spec{
if err != nil { if err != nil {
return cty.UnknownVal(cty.String), fmt.Errorf("failed to decode base64 data '%s'", s) return cty.UnknownVal(cty.String), fmt.Errorf("failed to decode base64 data '%s'", s)
} }
if !utf8.Valid([]byte(s)) {
return cty.UnknownVal(cty.String), fmt.Errorf("contents of '%s' are not valid UTF-8", s)
}
return cty.StringVal(string(sDec)), nil return cty.StringVal(string(sDec)), nil
}, },
}) })

View File

@ -23,6 +23,11 @@ func TestBase64Decode(t *testing.T) {
cty.UnknownVal(cty.String), cty.UnknownVal(cty.String),
true, true,
}, },
{ // Invalid utf-8
cty.StringVal("\xc3\x28"),
cty.UnknownVal(cty.String),
true,
},
} }
for _, test := range tests { for _, test := range tests {