From 74f2d58b8bcdcb5c1671ee9cf17768a1ad25d882 Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Thu, 24 May 2018 11:48:44 -0700 Subject: [PATCH] base64decode: check that the decoded (not encoded) string is valid UTF-8 --- lang/funcs/encoding.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lang/funcs/encoding.go b/lang/funcs/encoding.go index 856e9198d..af93f08dc 100644 --- a/lang/funcs/encoding.go +++ b/lang/funcs/encoding.go @@ -5,6 +5,7 @@ import ( "compress/gzip" "encoding/base64" "fmt" + "log" "net/url" "unicode/utf8" @@ -27,8 +28,9 @@ var Base64DecodeFunc = function.New(&function.Spec{ if err != nil { 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) + if !utf8.Valid([]byte(sDec)) { + log.Printf("[DEBUG] the result of decoding the the provided string is not valid UTF-8: %s", sDec) + return cty.UnknownVal(cty.String), fmt.Errorf("the result of decoding the the provided string is not valid UTF-8") } return cty.StringVal(string(sDec)), nil },