base64decodeFunc now checks for valid UTF-8
This commit is contained in:
parent
f8a8f26c0d
commit
6171ba3b8a
|
@ -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
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue