Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 8d1120f

Browse files
committed
internal/fs: Use filepath.Clean() on string parameters in HasFilepathPrefix
Previously strings were being cleaned with strings.TrimSuffix() to remove trailing separators. However, filepath.Clean() additionally removes directory navigation symbols (e.g. './', '../') and repeated separators. TestHasFilepathPrefix has additional test cases added to ensure trailing, and repeated separators are handled correctly. Fixes #1946
1 parent 48dd697 commit 8d1120f

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

internal/fs/fs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func HasFilepathPrefix(path, prefix string) (bool, error) {
6161
dn = filepath.Dir(path)
6262
}
6363

64-
dn = strings.TrimSuffix(dn, string(os.PathSeparator))
65-
prefix = strings.TrimSuffix(prefix, string(os.PathSeparator))
64+
dn = filepath.Clean(dn)
65+
prefix = filepath.Clean(prefix)
6666

6767
// [1:] in the lines below eliminates empty string on *nix and volume name on Windows
6868
dirs := strings.Split(dn, string(os.PathSeparator))[1:]

internal/fs/fs_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,19 @@ func TestHasFilepathPrefix(t *testing.T) {
4747
dir2 = dir
4848
}
4949

50+
// For testing trailing and repeated separators
51+
sep := string(os.PathSeparator)
52+
5053
cases := []struct {
5154
path string
5255
prefix string
5356
want bool
5457
}{
5558
{filepath.Join(dir, "a", "b"), filepath.Join(dir2), true},
59+
{filepath.Join(dir, "a", "b"), dir2 + sep + sep + "a", true},
60+
{filepath.Join(dir, "a", "b"), filepath.Join(dir2, "a") + sep, true},
61+
{filepath.Join(dir, "a", "b") + sep, filepath.Join(dir2), true},
62+
{dir + sep + sep + filepath.Join("a", "b"), filepath.Join(dir2, "a"), true},
5663
{filepath.Join(dir, "a", "b"), filepath.Join(dir2, "a"), true},
5764
{filepath.Join(dir, "a", "b"), filepath.Join(dir2, "a", "b"), true},
5865
{filepath.Join(dir, "a", "b"), filepath.Join(dir2, "c"), false},

0 commit comments

Comments
 (0)