Skip to content

Commit ae79d6d

Browse files
authored
feat: prevent path expansion using host context (#158)
Any references to `~` should return an error
1 parent 47802e4 commit ae79d6d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

init.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package preview
2+
3+
import (
4+
"github.com/aquasecurity/trivy/pkg/iac/scanners/terraform/parser/funcs"
5+
"github.com/zclconf/go-cty/cty"
6+
"github.com/zclconf/go-cty/cty/function"
7+
"golang.org/x/xerrors"
8+
)
9+
10+
// init intends to override some of the default functions afforded by terraform.
11+
// Specifically, any functions that require the context of the host.
12+
//
13+
// This is really unfortunate, but all the functions are globals, and this
14+
// is the only way to override them.
15+
func init() {
16+
// PathExpandFunc looks for references to a home directory on the host. The
17+
// preview rendering should not have access to the host's home directory path,
18+
// and will return an error if it is used.
19+
funcs.PathExpandFunc = function.New(&function.Spec{
20+
Params: []function.Parameter{
21+
{
22+
Name: "path",
23+
Type: cty.String,
24+
},
25+
},
26+
Type: function.StaticReturnType(cty.String),
27+
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
28+
path := args[0].AsString()
29+
if len(path) == 0 {
30+
return cty.StringVal(path), nil
31+
}
32+
33+
if path[0] != '~' {
34+
return cty.StringVal(path), nil
35+
}
36+
37+
return cty.NilVal, xerrors.Errorf("not allowed to expand paths starting with '~' in this context")
38+
},
39+
})
40+
}

0 commit comments

Comments
 (0)