Skip to content

Commit 9dd28fc

Browse files
funcs: Don't panic if templatefile path is sensitive
Previously we were partially propagating any marks from the path, but not going all the way so we still ran into trouble when trying to use the string containing the file contents. Now we'll have loadTmpl also return the marks it had to read through to actually parse the template, and then we'll use those (instead of the original path marks) to mark the result. In practice the pathMarks and the tmplMarks should always match today, but this is intentionally structured to make the data flow clearer -- the marks always travel along with whatever they related to -- so we're less likely to break this accidentally under future maintenence.
1 parent ee5cda7 commit 9dd28fc

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

internal/lang/funcs/filesystem.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,21 @@ func MakeFileFunc(baseDir string, encBase64 bool) function.Function {
7272
// the templatefile function, since that would risk the same file being
7373
// included into itself indefinitely.
7474
func MakeTemplateFileFunc(baseDir string, funcsCb func() (funcs map[string]function.Function, fsFuncs collections.Set[string], templateFuncs collections.Set[string])) function.Function {
75-
loadTmpl := func(fn string, marks cty.ValueMarks) (hcl.Expression, error) {
75+
loadTmpl := func(fn string, marks cty.ValueMarks) (hcl.Expression, cty.ValueMarks, error) {
7676
// We re-use File here to ensure the same filename interpretation
7777
// as it does, along with its other safety checks.
7878
tmplVal, err := File(baseDir, cty.StringVal(fn).WithMarks(marks))
7979
if err != nil {
80-
return nil, err
80+
return nil, nil, err
8181
}
8282

83+
tmplVal, marks = tmplVal.Unmark()
8384
expr, diags := hclsyntax.ParseTemplate([]byte(tmplVal.AsString()), fn, hcl.Pos{Line: 1, Column: 1})
8485
if diags.HasErrors() {
85-
return nil, diags
86+
return nil, nil, diags
8687
}
8788

88-
return expr, nil
89+
return expr, marks, nil
8990
}
9091

9192
renderTmpl := makeRenderTemplateFunc(funcsCb, true)
@@ -112,7 +113,7 @@ func MakeTemplateFileFunc(baseDir string, funcsCb func() (funcs map[string]funct
112113
// return any type.
113114

114115
pathArg, pathMarks := args[0].Unmark()
115-
expr, err := loadTmpl(pathArg.AsString(), pathMarks)
116+
expr, _, err := loadTmpl(pathArg.AsString(), pathMarks)
116117
if err != nil {
117118
return cty.DynamicPseudoType, err
118119
}
@@ -124,12 +125,12 @@ func MakeTemplateFileFunc(baseDir string, funcsCb func() (funcs map[string]funct
124125
},
125126
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
126127
pathArg, pathMarks := args[0].Unmark()
127-
expr, err := loadTmpl(pathArg.AsString(), pathMarks)
128+
expr, tmplMarks, err := loadTmpl(pathArg.AsString(), pathMarks)
128129
if err != nil {
129130
return cty.DynamicVal, err
130131
}
131132
result, err := renderTmpl(expr, args[1])
132-
return result.WithMarks(pathMarks), err
133+
return result.WithMarks(tmplMarks), err
133134
},
134135
})
135136

internal/lang/funcs/filesystem_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@ func TestTemplateFile(t *testing.T) {
187187
cty.True, // since this template contains only an interpolation, its true value shines through
188188
``,
189189
},
190+
{
191+
// If the template filename is sensitive then we also treat the
192+
// rendered result as sensitive, because the rendered result
193+
// is likely to imply which filename was used.
194+
// (Sensitive filenames seem pretty unlikely, but if they do
195+
// crop up then we should handle them consistently with our
196+
// usual sensitivity rules.)
197+
cty.StringVal("testdata/hello.txt").Mark(marks.Sensitive),
198+
cty.EmptyObjectVal,
199+
cty.StringVal("Hello World").Mark(marks.Sensitive),
200+
``,
201+
},
190202
}
191203

192204
funcs := map[string]function.Function{

0 commit comments

Comments
 (0)