diff --git a/filesystemserver/handler/handler.go b/filesystemserver/handler/handler.go index 8c23909..2dfff04 100644 --- a/filesystemserver/handler/handler.go +++ b/filesystemserver/handler/handler.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "strings" ) type FilesystemHandler struct { @@ -33,7 +34,11 @@ func NewFilesystemHandler(allowedDirs []string) (*FilesystemHandler, error) { // Ensure the path ends with a separator to prevent prefix matching issues // For example, /tmp/foo should not match /tmp/foobar - normalized = append(normalized, filepath.Clean(abs)+string(filepath.Separator)) + cleanPath := filepath.Clean(abs) + if !strings.HasSuffix(cleanPath, string(filepath.Separator)) { + cleanPath = cleanPath + string(filepath.Separator) + } + normalized = append(normalized, cleanPath) } return &FilesystemHandler{ allowedDirs: normalized, diff --git a/filesystemserver/handler/helper_test.go b/filesystemserver/handler/helper_test.go new file mode 100644 index 0000000..4865e14 --- /dev/null +++ b/filesystemserver/handler/helper_test.go @@ -0,0 +1,13 @@ +package handler + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRoot(t *testing.T) { + handler, err := NewFilesystemHandler([]string{"/"}) + assert.NoError(t, err) + assert.True(t, handler.isPathInAllowedDirs("/etc/hostname")) +}