From 0cb5c49cf3cf3e13c08a33841095829e61416ca1 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Fri, 21 May 2021 13:04:22 +0200 Subject: [PATCH] map path resolution errors to Permission errors this way the affected paths will be ignored in WebDAV Fixes #432 --- sftpd/sftpd_test.go | 4 +--- vfs/osfs.go | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/sftpd/sftpd_test.go b/sftpd/sftpd_test.go index e6aa655a..2a341f40 100644 --- a/sftpd/sftpd_test.go +++ b/sftpd/sftpd_test.go @@ -1365,9 +1365,7 @@ func TestEscapeHomeDir(t *testing.T) { _, err := client.ReadDir(testDir) assert.Error(t, err, "reading a symbolic link outside home dir should not succeeded") err = client.Chmod(path.Join(testDir, "sub", "dir"), os.ModePerm) - if assert.Error(t, err) { - assert.Contains(t, err.Error(), "SSH_FX_FAILURE") - } + assert.ErrorIs(t, err, os.ErrPermission) assert.Error(t, err, "setstat on a file outside home dir must fail") testFilePath := filepath.Join(homeBasePath, testFileName) testFileSize := int64(65535) diff --git a/vfs/osfs.go b/vfs/osfs.go index de583009..35d89214 100644 --- a/vfs/osfs.go +++ b/vfs/osfs.go @@ -22,6 +22,14 @@ const ( osFsName = "osfs" ) +type pathResolutionError struct { + err string +} + +func (e *pathResolutionError) Error() string { + return fmt.Sprintf("Path resolution error: %s", e.err) +} + // OsFs is a Fs implementation that uses functions provided by the os package. type OsFs struct { name string @@ -180,6 +188,10 @@ func (*OsFs) IsNotExist(err error) bool { // IsPermission returns a boolean indicating whether the error is known to // report that permission is denied. func (*OsFs) IsPermission(err error) bool { + if _, ok := err.(*pathResolutionError); ok { + return true + } + return os.IsPermission(err) } @@ -367,7 +379,7 @@ func (fs *OsFs) isSubDir(sub string) error { } if len(sub) < len(parent) { err = fmt.Errorf("path %#v is not inside %#v", sub, parent) - return err + return &pathResolutionError{err: err.Error()} } separator := string(os.PathSeparator) if parent == filepath.Dir(parent) { @@ -377,7 +389,7 @@ func (fs *OsFs) isSubDir(sub string) error { } if !strings.HasPrefix(sub, parent+separator) { err = fmt.Errorf("path %#v is not inside %#v", sub, parent) - return err + return &pathResolutionError{err: err.Error()} } return nil }