Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion hclsyntax/expression_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,16 @@ func (e *TemplateExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics)
ret = cty.UnknownVal(cty.String)
if !diags.HasErrors() { // Invalid input means our partial result buffer is suspect
if knownPrefix := buf.String(); knownPrefix != "" {
ret = ret.Refine().StringPrefix(knownPrefix).NewValue()
byteLen := len(knownPrefix)
// Impose a reasonable upper limit to avoid producing too long a prefix.
// The 128 B is about 10% of the safety limits in cty's msgpack decoder.
// @see https://github.com/zclconf/go-cty/blob/v1.13.2/cty/msgpack/unknown.go#L170-L175
//
// This operation is safe because StringPrefix removes incomplete trailing grapheme clusters.
if byteLen > 128 { // arbitrarily-decided threshold
byteLen = 128
}
ret = ret.Refine().StringPrefix(knownPrefix[:byteLen]).NewValue()
}
}
} else {
Expand Down
11 changes: 11 additions & 0 deletions hclsyntax/expression_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package hclsyntax

import (
"strings"
"testing"

"github.com/hashicorp/hcl/v2"
Expand Down Expand Up @@ -307,6 +308,16 @@ trim`,
cty.UnknownVal(cty.String).Refine().NotNull().StringPrefixFull("test_known_").NewValue(),
0,
},
{ // can preserve a static prefix as a refinement, but the length is limited to 128 B
strings.Repeat("_", 130) + `${unknown}`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"unknown": cty.UnknownVal(cty.String),
},
},
cty.UnknownVal(cty.String).Refine().NotNull().StringPrefixFull(strings.Repeat("_", 128)).NewValue(),
0,
},
{ // marks from uninterpolated values are ignored
`hello%{ if false } ${target}%{ endif }`,
&hcl.EvalContext{
Expand Down