From 8780dcc04d13e4329f12387facbc34ec2bdff962 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 28 Apr 2023 16:52:03 +0200 Subject: [PATCH 1/2] Fixed IsInsideDir for paths on different filesystems --- paths.go | 5 ++++- paths_test.go | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/paths.go b/paths.go index b969606..52c036c 100644 --- a/paths.go +++ b/paths.go @@ -191,7 +191,10 @@ func (p *Path) Clean() *Path { func (p *Path) IsInsideDir(dir *Path) (bool, error) { rel, err := filepath.Rel(dir.path, p.path) if err != nil { - return false, err + // If the dir cannot be made relative to this path it means + // that it belong to a different filesystems, so it cannot be + // inside this path. + return false, nil } return !strings.Contains(rel, ".."+string(os.PathSeparator)) && rel != ".." && rel != ".", nil } diff --git a/paths_test.go b/paths_test.go index 4e7817a..6b5f236 100644 --- a/paths_test.go +++ b/paths_test.go @@ -154,16 +154,17 @@ func TestResetStatCacheWhenFollowingSymlink(t *testing.T) { } func TestIsInsideDir(t *testing.T) { - inside := func(a, b *Path) { + notInside := func(a, b *Path) { in, err := a.IsInsideDir(b) require.NoError(t, err) - require.True(t, in, "%s is inside %s", a, b) + require.False(t, in, "%s is inside %s", a, b) } - notInside := func(a, b *Path) { + inside := func(a, b *Path) { in, err := a.IsInsideDir(b) require.NoError(t, err) - require.False(t, in, "%s is inside %s", a, b) + require.True(t, in, "%s is inside %s", a, b) + notInside(b, a) } f1 := New("/a/b/c") @@ -196,6 +197,14 @@ func TestIsInsideDir(t *testing.T) { f5 := New("/home/megabug/a15/packages") notInside(f5, f4) notInside(f4, f5) + + if runtime.GOOS == "windows" { + f6 := New("C:\\", "A") + f7 := New("C:\\", "A", "B", "C") + f8 := New("E:\\", "A", "B", "C") + inside(f7, f6) + notInside(f8, f6) + } } func TestReadFileAsLines(t *testing.T) { From 657985f4e8bb0767df6dfab71d5069b4f7ce88b2 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 28 Apr 2023 16:54:55 +0200 Subject: [PATCH 2/2] Removed now useless error from IsInsideDir --- paths.go | 8 +++++--- paths_test.go | 12 +++--------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/paths.go b/paths.go index 52c036c..a9e752b 100644 --- a/paths.go +++ b/paths.go @@ -188,15 +188,17 @@ func (p *Path) Clean() *Path { // IsInsideDir returns true if the current path is inside the provided // dir -func (p *Path) IsInsideDir(dir *Path) (bool, error) { +func (p *Path) IsInsideDir(dir *Path) bool { rel, err := filepath.Rel(dir.path, p.path) if err != nil { // If the dir cannot be made relative to this path it means // that it belong to a different filesystems, so it cannot be // inside this path. - return false, nil + return false } - return !strings.Contains(rel, ".."+string(os.PathSeparator)) && rel != ".." && rel != ".", nil + return !strings.Contains(rel, ".."+string(os.PathSeparator)) && + rel != ".." && + rel != "." } // Parent returns all but the last element of path, typically the path's diff --git a/paths_test.go b/paths_test.go index 6b5f236..fba0481 100644 --- a/paths_test.go +++ b/paths_test.go @@ -155,15 +155,11 @@ func TestResetStatCacheWhenFollowingSymlink(t *testing.T) { func TestIsInsideDir(t *testing.T) { notInside := func(a, b *Path) { - in, err := a.IsInsideDir(b) - require.NoError(t, err) - require.False(t, in, "%s is inside %s", a, b) + require.False(t, a.IsInsideDir(b), "%s is inside %s", a, b) } inside := func(a, b *Path) { - in, err := a.IsInsideDir(b) - require.NoError(t, err) - require.True(t, in, "%s is inside %s", a, b) + require.True(t, a.IsInsideDir(b), "%s is inside %s", a, b) notInside(b, a) } @@ -381,9 +377,7 @@ func TestWriteToTempFile(t *testing.T) { defer tmp.Remove() require.NoError(t, err) require.True(t, strings.HasPrefix(tmp.Base(), "prefix")) - inside, err := tmp.IsInsideDir(tmpDir) - require.NoError(t, err) - require.True(t, inside) + require.True(t, tmp.IsInsideDir(tmpDir)) data, err := tmp.ReadFile() require.NoError(t, err) require.Equal(t, tmpData, data)