Skip to content

Commit 99eabdf

Browse files
kotakanbeknqyf263
andauthored
refactor(deps): replace archived go-homedir with os.UserHomeDir (aquasecurity#10484)
Co-authored-by: knqyf263 <knqyf263@gmail.com>
1 parent 28ed214 commit 99eabdf

3 files changed

Lines changed: 105 additions & 5 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ require (
7777
github.com/masahiro331/go-vmdk-parser v0.0.0-20260423020818-08305fa668d2
7878
github.com/masahiro331/go-xfs-filesystem v0.0.0-20260422061116-d21e5e4481bb
7979
github.com/mattn/go-shellwords v1.0.12
80-
github.com/mitchellh/go-homedir v1.1.0
80+
github.com/mitchellh/go-homedir v1.1.0 // indirect
8181
github.com/mitchellh/hashstructure/v2 v2.0.2
8282
github.com/moby/buildkit v0.29.0
8383
github.com/moby/docker-image-spec v1.3.1

pkg/iac/scanners/terraform/parser/funcs/filesystem.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414
"github.com/bmatcuk/doublestar/v4"
1515
"github.com/hashicorp/hcl/v2"
1616
"github.com/hashicorp/hcl/v2/hclsyntax"
17-
"github.com/mitchellh/go-homedir"
1817
"github.com/zclconf/go-cty/cty"
1918
"github.com/zclconf/go-cty/cty/function"
19+
"golang.org/x/xerrors"
2020
)
2121

2222
// MakeFileFunc constructs a function that takes a file path and returns the
@@ -196,7 +196,7 @@ func MakeFileExistsFunc(target fs.FS, baseDir string) function.Function {
196196
Type: function.StaticReturnType(cty.Bool),
197197
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
198198
path := args[0].AsString()
199-
path, err := homedir.Expand(path)
199+
path, err := expandHome(path)
200200
if err != nil {
201201
return cty.UnknownVal(cty.Bool), fmt.Errorf("failed to expand ~: %s", err)
202202
}
@@ -350,13 +350,13 @@ var PathExpandFunc = function.New(&function.Spec{
350350
Type: function.StaticReturnType(cty.String),
351351
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
352352

353-
homePath, err := homedir.Expand(args[0].AsString())
353+
homePath, err := expandHome(args[0].AsString())
354354
return cty.StringVal(homePath), err
355355
},
356356
})
357357

358358
func openFile(target fs.FS, baseDir, path string) (fs.File, error) {
359-
path, err := homedir.Expand(path)
359+
path, err := expandHome(path)
360360
if err != nil {
361361
return nil, fmt.Errorf("failed to expand ~: %s", err)
362362
}
@@ -404,3 +404,23 @@ func File(target fs.FS, baseDir string, path cty.Value) (cty.Value, error) {
404404
fn := MakeFileFunc(target, baseDir, false)
405405
return fn.Call([]cty.Value{path})
406406
}
407+
408+
// expandHome expands a leading ~ in the path to the current user's home directory.
409+
// User-specific forms like ~user/foo are not supported and return an error,
410+
// matching the behavior of the archived github.com/mitchellh/go-homedir Expand.
411+
func expandHome(path string) (string, error) {
412+
if len(path) == 0 {
413+
return path, nil
414+
}
415+
if path[0] != '~' {
416+
return path, nil
417+
}
418+
if len(path) > 1 && path[1] != '/' && path[1] != '\\' {
419+
return "", xerrors.New("cannot expand user-specific home dir")
420+
}
421+
home, err := os.UserHomeDir()
422+
if err != nil {
423+
return "", err
424+
}
425+
return filepath.Join(home, path[1:]), nil
426+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package funcs
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestExpandHome(t *testing.T) {
13+
home, err := os.UserHomeDir()
14+
require.NoError(t, err)
15+
16+
tests := []struct {
17+
name string
18+
input string
19+
want string
20+
wantErr bool
21+
}{
22+
{
23+
name: "tilde only",
24+
input: "~",
25+
want: home,
26+
},
27+
{
28+
name: "tilde with forward slash path",
29+
input: "~/Documents/test.tf",
30+
want: filepath.Join(home, "Documents/test.tf"),
31+
},
32+
{
33+
name: "tilde with nested path",
34+
input: "~/a/b/c",
35+
want: filepath.Join(home, "a/b/c"),
36+
},
37+
{
38+
name: "absolute path unchanged",
39+
input: "/etc/passwd",
40+
want: "/etc/passwd",
41+
},
42+
{
43+
name: "relative path unchanged",
44+
input: "relative/path",
45+
want: "relative/path",
46+
},
47+
{
48+
name: "empty string",
49+
input: "",
50+
want: "",
51+
},
52+
{
53+
name: "tilde in middle unchanged",
54+
input: "/foo/~/bar",
55+
want: "/foo/~/bar",
56+
},
57+
{
58+
name: "user-specific home dir is not supported",
59+
input: "~username",
60+
wantErr: true,
61+
},
62+
{
63+
name: "user-specific home dir with path is not supported",
64+
input: "~foo/foo",
65+
wantErr: true,
66+
},
67+
}
68+
69+
for _, tt := range tests {
70+
t.Run(tt.name, func(t *testing.T) {
71+
got, err := expandHome(tt.input)
72+
if tt.wantErr {
73+
require.Error(t, err)
74+
return
75+
}
76+
require.NoError(t, err)
77+
assert.Equal(t, tt.want, got)
78+
})
79+
}
80+
}

0 commit comments

Comments
 (0)