fix: improve path prefix checking reliability

Kujtim Hoxha created

- Replace HasPrefix implementation to use filepath.Rel instead of filepath.Abs
- More reliable method to determine if a path is within a prefix
- Returns false on error conditions
- Preserves same public API for external usage

This addresses review feedback about improving the path checking logic.

Change summary

internal/fsext/fileutil.go | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

Detailed changes

internal/fsext/fileutil.go 🔗

@@ -273,12 +273,12 @@ func PathOrPrefix(path, prefix string) string {
 }
 
 // HasPrefix checks if the given path starts with the specified prefix.
+// Uses filepath.Rel to determine if path is within prefix.
 func HasPrefix(path, prefix string) bool {
-	if abs, err := filepath.Abs(path); err == nil {
-		path = abs
-	}
-	if abs, err := filepath.Abs(prefix); err == nil {
-		prefix = abs
+	rel, err := filepath.Rel(prefix, path)
+	if err != nil {
+		return false
 	}
-	return strings.HasPrefix(path, prefix)
+	// If path is within prefix, Rel will not return a path starting with ".."
+	return !strings.HasPrefix(rel, "..")
 }