on main help sign in
fix(fsext): stop traversing if the directory owner changes
8f45a84Andrey Nering
internal/fsext/parent.go+15
@@ -8,6 +8,8 @@ import (
8
8
9// SearchParent searches for a target file or directory starting from dir
9// SearchParent searches for a target file or directory starting from dir
10// and walking up the directory tree until found or root or home is reached.
10// and walking up the directory tree until found or root or home is reached.
11+// It also checks the ownership of directories to ensure that the search does
12+// not cross ownership boundaries.
11// Returns the full path to the target if found, empty string and false otherwise.
13// Returns the full path to the target if found, empty string and false otherwise.
12// The search includes the starting directory itself.
14// The search includes the starting directory itself.
13func SearchParent(dir, target string) (string, bool) {
15func SearchParent(dir, target string) (string, bool) {
@@ -24,6 +26,10 @@ func SearchParent(dir, target string) (string, bool) {
24 }
26 }
25
27
26 previousParent := absDir
28 previousParent := absDir
29+ previousOwner, err := Owner(previousParent)
30+ if err != nil {
31+ return "", false
32+ }
27
33
28 for {
34 for {
29 parent := filepath.Dir(previousParent)
35 parent := filepath.Dir(previousParent)
@@ -31,6 +37,14 @@ func SearchParent(dir, target string) (string, bool) {
31 return "", false
37 return "", false
32 }
38 }
33
39
40+ parentOwner, err := Owner(parent)
41+ if err != nil {
42+ return "", false
43+ }
44+ if parentOwner != previousOwner {
45+ return "", false
46+ }
47+
34 path := filepath.Join(parent, target)
48 path := filepath.Join(parent, target)
35 if _, err := os.Stat(path); err == nil {
49 if _, err := os.Stat(path); err == nil {
36 return path, true
50 return path, true
@@ -39,5 +53,6 @@ func SearchParent(dir, target string) (string, bool) {
39 }
53 }
40
54
41 previousParent = parent
55 previousParent = parent
56+ previousOwner = parentOwner
42 }
57 }
43}
58}