From b971b13362d306ab69ee3f8f64b4aafefc8d631d Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Thu, 23 Jun 2022 16:09:22 +0800 Subject: [PATCH] feat: dir and file check --- internal/errs/errors.go | 14 +++----------- internal/errs/object.go | 16 ++++++++++++++++ internal/operations/fs.go | 3 +++ internal/operations/path.go | 4 ++++ 4 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 internal/errs/object.go diff --git a/internal/errs/errors.go b/internal/errs/errors.go index 80a202cb..fc6051d6 100644 --- a/internal/errs/errors.go +++ b/internal/errs/errors.go @@ -2,23 +2,15 @@ package errs import ( "errors" - pkgerr "github.com/pkg/errors" ) var ( - ObjectNotFound = errors.New("object not found") - NotImplement = errors.New("not implement") - NotSupport = errors.New("not support") - RelativePath = errors.New("access using relative path is not allowed") + NotImplement = errors.New("not implement") + NotSupport = errors.New("not support") + RelativePath = errors.New("access using relative path is not allowed") MoveBetweenTwoAccounts = errors.New("can't move files between two account, try to copy") UploadNotSupported = errors.New("upload not supported") - NotFolder = errors.New("not a folder") - NotFile = errors.New("not a file") MetaNotFound = errors.New("meta not found") ) - -func IsObjectNotFound(err error) bool { - return pkgerr.Cause(err) == ObjectNotFound -} diff --git a/internal/errs/object.go b/internal/errs/object.go new file mode 100644 index 00000000..5c0b339a --- /dev/null +++ b/internal/errs/object.go @@ -0,0 +1,16 @@ +package errs + +import ( + "errors" + pkgerr "github.com/pkg/errors" +) + +var ( + ObjectNotFound = errors.New("object not found") + NotFolder = errors.New("not a folder") + NotFile = errors.New("not a file") +) + +func IsObjectNotFound(err error) bool { + return pkgerr.Cause(err) == ObjectNotFound +} diff --git a/internal/operations/fs.go b/internal/operations/fs.go index c78a9702..377af206 100644 --- a/internal/operations/fs.go +++ b/internal/operations/fs.go @@ -26,6 +26,9 @@ func List(ctx context.Context, account driver.Driver, path string, refresh ...bo if err != nil { return nil, errors.WithMessage(err, "failed get dir") } + if !dir.IsDir() { + return nil, errors.WithStack(errs.NotFolder) + } if account.Config().NoCache { return account.List(ctx, dir) } diff --git a/internal/operations/path.go b/internal/operations/path.go index 984a48d5..6b10c024 100644 --- a/internal/operations/path.go +++ b/internal/operations/path.go @@ -1,6 +1,7 @@ package operations import ( + "github.com/alist-org/alist/v3/internal/errs" stdpath "path" "strings" @@ -21,6 +22,9 @@ func ActualPath(account driver.Additional, rawPath string) string { // for path: remove the virtual path prefix and join the actual root folder if exists func GetAccountAndActualPath(rawPath string) (driver.Driver, string, error) { rawPath = utils.StandardizationPath(rawPath) + if strings.Contains(rawPath, "..") { + return nil, "", errors.WithStack(errs.RelativePath) + } account := GetBalancedAccount(rawPath) if account == nil { return nil, "", errors.Errorf("can't find account with rawPath: %s", rawPath)