Skip to content

Commit

Permalink
Completely escape non-expr strings
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalo-rodrigues committed Apr 9, 2022
1 parent cb91e76 commit ae61226
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion _tests/escaped-strings.hcl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
EscapedString = "\n\t\r\\\""
TemplateString = "\n\t\r\\\" ${"\\ \""}"
TemplateString = "test-A${"\\ \""}"
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ require (
github.com/hashicorp/hcl v1.0.0
github.com/hashicorp/hcl/v2 v2.11.1
github.com/stretchr/testify v1.6.1
github.com/zclconf/go-cty v1.8.0 // indirect
github.com/zclconf/go-cty v1.8.0
)
4 changes: 2 additions & 2 deletions hclencoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ func TestEncoder(t *testing.T) {
ID: "escaped strings",
Input: struct {
EscapedString string
TemplateString string
TemplateString string `hcl:",expr"`
}{
"\n\t\r\\\"",
"\n\t\r\\\" ${\"\\\\ \\\"\"}",
"\"test-\u0041${\"\\\\ \\\"\"}\"",
},
Output: "escaped-strings",
},
Expand Down
2 changes: 1 addition & 1 deletion tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func tokenize(in reflect.Value, meta fieldMeta) (tkns hclwrite.Tokens, err error
case reflect.String:
val := in.String()
if !meta.expression {
val = fmt.Sprintf(`"%s"`, EscapeString(val))
return hclwrite.TokensForValue(cty.StringVal(val)), nil
}
// Unfortunately hcl escapes template expressions (${...}) when using hclwrite.TokensForValue. So we escape
// everything but template expressions and then parse the expression into tokens.
Expand Down

0 comments on commit ae61226

Please sign in to comment.