diff options
Diffstat (limited to 'modules/storage/local.go')
-rw-r--r-- | modules/storage/local.go | 34 |
1 files changed, 8 insertions, 26 deletions
diff --git a/modules/storage/local.go b/modules/storage/local.go index 8d9aa603d0..701b0b1a9f 100644 --- a/modules/storage/local.go +++ b/modules/storage/local.go @@ -6,7 +6,6 @@ package storage import ( "context" - "errors" "io" "net/url" "os" @@ -18,8 +17,6 @@ import ( "code.gitea.io/gitea/modules/util" ) -// ErrLocalPathNotSupported represents an error that path is not supported -var ErrLocalPathNotSupported = errors.New("local path is not supported") var _ ObjectStorage = &LocalStorage{} // LocalStorageType is the type descriptor for local storage @@ -62,21 +59,18 @@ func NewLocalStorage(ctx context.Context, cfg interface{}) (ObjectStorage, error }, nil } +func (l *LocalStorage) buildLocalPath(p string) string { + return filepath.Join(l.dir, path.Clean("/" + strings.ReplaceAll(p, "\\", "/"))[1:]) +} + // Open a file func (l *LocalStorage) Open(path string) (Object, error) { - if !isLocalPathValid(path) { - return nil, ErrLocalPathNotSupported - } - return os.Open(filepath.Join(l.dir, path)) + return os.Open(l.buildLocalPath(path)) } // Save a file func (l *LocalStorage) Save(path string, r io.Reader, size int64) (int64, error) { - if !isLocalPathValid(path) { - return 0, ErrLocalPathNotSupported - } - - p := filepath.Join(l.dir, path) + p := l.buildLocalPath(path) if err := os.MkdirAll(filepath.Dir(p), os.ModePerm); err != nil { return 0, err } @@ -116,24 +110,12 @@ func (l *LocalStorage) Save(path string, r io.Reader, size int64) (int64, error) // Stat returns the info of the file func (l *LocalStorage) Stat(path string) (os.FileInfo, error) { - return os.Stat(filepath.Join(l.dir, path)) -} - -func isLocalPathValid(p string) bool { - a := path.Clean(p) - if strings.HasPrefix(a, "../") || strings.HasPrefix(a, "..\\") { - return false - } - return a == p + return os.Stat(l.buildLocalPath(path)) } // Delete delete a file func (l *LocalStorage) Delete(path string) error { - if !isLocalPathValid(path) { - return ErrLocalPathNotSupported - } - p := filepath.Join(l.dir, path) - return util.Remove(p) + return util.Remove(l.buildLocalPath(path)) } // URL gets the redirect URL to a file |