From f50d79a426c661bd2ebaa8b9517be31d4bc0ef1c Mon Sep 17 00:00:00 2001 From: AlliotTech <24980252+AlliotTech@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:58:09 +0800 Subject: [PATCH 1/4] fix(onedrive): retry token refresh only once Request() recursed unconditionally when the server kept rejecting the token with InvalidAuthenticationToken, even after a successful refresh. Split the public Request() into a wrapper and a private request() carrying a retried flag, so a refreshed token is tried exactly once and a still-invalid token returns its error instead of recursing forever. Same fix in onedrive_app, which had the identical loop. --- drivers/onedrive/util.go | 9 +++++++-- drivers/onedrive_app/util.go | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/drivers/onedrive/util.go b/drivers/onedrive/util.go index 84d244ec..4907bb53 100644 --- a/drivers/onedrive/util.go +++ b/drivers/onedrive/util.go @@ -95,6 +95,10 @@ func (d *Onedrive) _refreshToken() error { } func (d *Onedrive) Request(url string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) { + return d.request(url, method, callback, resp, false) +} + +func (d *Onedrive) request(url string, method string, callback base.ReqCallback, resp interface{}, retried bool) ([]byte, error) { req := base.RestyClient.R() req.SetHeader("Authorization", "Bearer "+d.AccessToken) if callback != nil { @@ -110,12 +114,13 @@ func (d *Onedrive) Request(url string, method string, callback base.ReqCallback, return nil, err } if e.Error.Code != "" { - if e.Error.Code == "InvalidAuthenticationToken" { + if e.Error.Code == "InvalidAuthenticationToken" && !retried { err = d.refreshToken() if err != nil { return nil, err } - return d.Request(url, method, callback, resp) + // retry once with the refreshed token, never loop on a bad token + return d.request(url, method, callback, resp, true) } return nil, errors.New(e.Error.Message) } diff --git a/drivers/onedrive_app/util.go b/drivers/onedrive_app/util.go index 64756034..361f6129 100644 --- a/drivers/onedrive_app/util.go +++ b/drivers/onedrive_app/util.go @@ -86,6 +86,10 @@ func (d *OnedriveAPP) _accessToken() error { } func (d *OnedriveAPP) Request(url string, method string, callback base.ReqCallback, resp interface{}) ([]byte, error) { + return d.request(url, method, callback, resp, false) +} + +func (d *OnedriveAPP) request(url string, method string, callback base.ReqCallback, resp interface{}, retried bool) ([]byte, error) { req := base.RestyClient.R() req.SetHeader("Authorization", "Bearer "+d.AccessToken) if callback != nil { @@ -101,12 +105,13 @@ func (d *OnedriveAPP) Request(url string, method string, callback base.ReqCallba return nil, err } if e.Error.Code != "" { - if e.Error.Code == "InvalidAuthenticationToken" { + if e.Error.Code == "InvalidAuthenticationToken" && !retried { err = d.accessToken() if err != nil { return nil, err } - return d.Request(url, method, callback, resp) + // retry once with the refreshed token, never loop on a bad token + return d.request(url, method, callback, resp, true) } return nil, errors.New(e.Error.Message) } From 45475ee85d51049acf8d79fcd6b0d306e17e6af8 Mon Sep 17 00:00:00 2001 From: AlliotTech <24980252+AlliotTech@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:58:10 +0800 Subject: [PATCH 2/4] fix(115): retry rapid upload init on decrypt failure rapidUpload() gave up on the first ECDH decrypt failure of the upload-init response. Retry the init request up to 3 times on decrypt failure. Also close the response body promptly instead of deferring it inside the retry loop. --- drivers/115/util.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/115/util.go b/drivers/115/util.go index d7673230..26cf04b7 100644 --- a/drivers/115/util.go +++ b/drivers/115/util.go @@ -197,6 +197,7 @@ func (d *Pan115) rapidUpload(fileSize int64, fileName, dirID, preID, fileID stri form.Set("sig", d.client.GenerateSignature(fileID, target)) signKey, signVal := "", "" + decryptRetryCount := 0 for retry := true; retry; { t := driver115.NowMilli() @@ -228,11 +229,16 @@ func (d *Pan115) rapidUpload(fileSize int64, fileName, dirID, preID, fileID stri return nil, err } data := resp.RawBody() - defer data.Close() if bodyBytes, err = io.ReadAll(data); err != nil { + _ = data.Close() return nil, err } + _ = data.Close() if decrypted, err = ecdhCipher.Decrypt(bodyBytes); err != nil { + decryptRetryCount++ + if decryptRetryCount < 3 { + continue + } return nil, err } if err = driver115.CheckErr(json.Unmarshal(decrypted, &result), &result, resp); err != nil { From 94aab9d924fad08ff31c6872629ad673c327fc1d Mon Sep 17 00:00:00 2001 From: AlliotTech <24980252+AlliotTech@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:58:10 +0800 Subject: [PATCH 3/4] fix(webdav): return request URL as LOCK lockroot handleLock() wrote the internal storage path into the lockroot href of the LOCK response. Map the storage path back to the URL path the client sees with lockRootHref(), like the hrefs PROPFIND writes. Add TestLockRootHref. --- server/webdav/webdav.go | 14 ++++++++++++++ server/webdav/webdav_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 server/webdav/webdav_test.go diff --git a/server/webdav/webdav.go b/server/webdav/webdav.go index e6ac51f8..2b4278d5 100644 --- a/server/webdav/webdav.go +++ b/server/webdav/webdav.go @@ -580,6 +580,10 @@ func (h *Handler) handleLock(w http.ResponseWriter, r *http.Request) (retStatus w.Header().Set("Lock-Token", "<"+token+">") } + // ld.Root is the storage path; the response's lockroot is the client's URL. + obj, err := fs.Get(ctx, ld.Root, &fs.GetArgs{NoLog: true}) + ld.Root = lockRootHref(h.Prefix, user.BasePath, ld.Root, err == nil && obj.IsDir()) + w.Header().Set("Content-Type", "application/xml; charset=utf-8") if created { // This is "w.WriteHeader(http.StatusCreated)" and not "return @@ -591,6 +595,16 @@ func (h *Handler) handleLock(w http.ResponseWriter, r *http.Request) (retStatus return 0, nil } +// lockRootHref maps the storage path a lock is kept under back to the URL path +// the client sees, like the hrefs PROPFIND writes. +func lockRootHref(prefix, basePath, root string, isDir bool) string { + href := path.Join(prefix, strings.TrimPrefix(root, basePath)) + if href != "/" && isDir { + href += "/" + } + return (&url.URL{Path: href}).EscapedPath() +} + func (h *Handler) handleUnlock(w http.ResponseWriter, r *http.Request) (status int, err error) { // http://www.webdav.org/specs/rfc4918.html#HEADER_Lock-Token says that the // Lock-Token value is a Coded-URL. We strip its angle brackets. diff --git a/server/webdav/webdav_test.go b/server/webdav/webdav_test.go new file mode 100644 index 00000000..337f84eb --- /dev/null +++ b/server/webdav/webdav_test.go @@ -0,0 +1,24 @@ +package webdav + +import "testing" + +func TestLockRootHref(t *testing.T) { + testCases := []struct { + prefix, basePath, root string + isDir bool + want string + }{ + {"/dav", "/storage", "/storage/lock-probe", true, "/dav/lock-probe/"}, + {"/dav", "/storage", "/storage/a.txt", false, "/dav/a.txt"}, + {"/dav", "/", "/storage/a.txt", false, "/dav/storage/a.txt"}, + {"/dav", "/storage", "/storage", true, "/dav/"}, + {"", "/", "/", true, "/"}, + {"/dav", "/", "/new folder/文件.txt", false, "/dav/new%20folder/%E6%96%87%E4%BB%B6.txt"}, + } + for _, tc := range testCases { + got := lockRootHref(tc.prefix, tc.basePath, tc.root, tc.isDir) + if got != tc.want { + t.Errorf("lockRootHref(%q, %q, %q, %v) = %q, want %q", tc.prefix, tc.basePath, tc.root, tc.isDir, got, tc.want) + } + } +} From a312902350e75d2068807d4e6da02b876e016da2 Mon Sep 17 00:00:00 2001 From: AlliotTech <24980252+AlliotTech@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:58:10 +0800 Subject: [PATCH 4/4] fix(local): fall back to copy when recycle-bin move crosses devices Remove() moved files to the recycle bin with a bare os.Rename(), which fails when the recycle bin lives on another device. On a cross-device error, copy the file or tree with copyForMove() (preserves symlinks, named pipes and modtimes) and remove the source, cleaning up a partial copy on failure. Add per-platform isCrossDeviceError() helpers and copyForMove tests. --- drivers/local/copy_namedpipes.go | 16 +++++ drivers/local/copy_namedpipes_x.go | 9 +++ drivers/local/driver.go | 12 ++++ drivers/local/remove_test.go | 109 +++++++++++++++++++++++++++++ drivers/local/util.go | 65 +++++++++++++++++ drivers/local/util_unix.go | 7 ++ drivers/local/util_windows.go | 7 ++ 7 files changed, 225 insertions(+) create mode 100644 drivers/local/copy_namedpipes.go create mode 100644 drivers/local/copy_namedpipes_x.go diff --git a/drivers/local/copy_namedpipes.go b/drivers/local/copy_namedpipes.go new file mode 100644 index 00000000..8217e3e0 --- /dev/null +++ b/drivers/local/copy_namedpipes.go @@ -0,0 +1,16 @@ +//go:build !windows && !plan9 && !netbsd && !aix && !illumos && !solaris && !js + +package local + +import ( + "os" + "path/filepath" + "syscall" +) + +func copyNamedPipe(dstPath string, mode os.FileMode, dirMode os.FileMode) error { + if err := os.MkdirAll(filepath.Dir(dstPath), dirMode); err != nil { + return err + } + return syscall.Mkfifo(dstPath, uint32(mode)) +} diff --git a/drivers/local/copy_namedpipes_x.go b/drivers/local/copy_namedpipes_x.go new file mode 100644 index 00000000..81f676f1 --- /dev/null +++ b/drivers/local/copy_namedpipes_x.go @@ -0,0 +1,9 @@ +//go:build windows || plan9 || netbsd || aix || illumos || solaris || js + +package local + +import "os" + +func copyNamedPipe(_ string, _, _ os.FileMode) error { + return nil +} diff --git a/drivers/local/driver.go b/drivers/local/driver.go index 1a52293c..347e0032 100644 --- a/drivers/local/driver.go +++ b/drivers/local/driver.go @@ -326,6 +326,18 @@ func (d *Local) Remove(ctx context.Context, obj model.Obj) error { dstPath = filepath.Join(d.RecycleBinPath, obj.GetName()+"_"+time.Now().Format("20060102150405")) } err = os.Rename(obj.GetPath(), dstPath) + if isCrossDeviceError(err) { + err = d.copyForMove(obj.GetPath(), dstPath) + if err != nil { + _ = os.RemoveAll(dstPath) + } else { + if obj.IsDir() { + err = os.RemoveAll(obj.GetPath()) + } else { + err = os.Remove(obj.GetPath()) + } + } + } } if err != nil { return err diff --git a/drivers/local/remove_test.go b/drivers/local/remove_test.go index 957196f3..3e9f57f7 100644 --- a/drivers/local/remove_test.go +++ b/drivers/local/remove_test.go @@ -4,7 +4,9 @@ import ( "context" "os" "path/filepath" + "runtime" "testing" + "time" "github.com/AlliotTech/openalist/internal/model" ) @@ -35,3 +37,110 @@ func TestThumbCachePathDisabled(t *testing.T) { t.Fatalf("thumbCachePath = %q, want empty", got) } } + +func TestCopyForMovePreservesRelativeSymlink(t *testing.T) { + root := t.TempDir() + srcPath := filepath.Join(root, "source") + dstPath := filepath.Join(root, "recycle", "source") + if err := os.Mkdir(srcPath, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(srcPath, "data"), []byte("data"), 0o666); err != nil { + t.Fatal(err) + } + if err := os.Symlink("data", filepath.Join(srcPath, "link")); err != nil { + t.Skipf("symlinks are unavailable: %v", err) + } + + driver := &Local{mkdirPerm: 0o755} + if err := driver.copyForMove(srcPath, dstPath); err != nil { + t.Fatal(err) + } + if err := os.RemoveAll(srcPath); err != nil { + t.Fatal(err) + } + + content, err := os.ReadFile(filepath.Join(dstPath, "link")) + if err != nil { + t.Fatal(err) + } + if string(content) != "data" { + t.Fatalf("expected data through moved relative symlink, got %q", content) + } +} + +func TestCopyForMoveHandlesReadOnlyDirectory(t *testing.T) { + root := t.TempDir() + srcPath := filepath.Join(root, "source") + dstPath := filepath.Join(root, "recycle", "source") + if err := os.Mkdir(srcPath, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(srcPath, "data"), []byte("data"), 0o666); err != nil { + t.Fatal(err) + } + if err := os.Chmod(srcPath, 0o555); err != nil { + t.Fatal(err) + } + defer os.Chmod(srcPath, 0o755) + defer os.Chmod(dstPath, 0o755) // copied dir is 0555 too, or TempDir cleanup cannot remove it + + driver := &Local{mkdirPerm: 0o755} + if err := driver.copyForMove(srcPath, dstPath); err != nil { + t.Fatal(err) + } + + content, err := os.ReadFile(filepath.Join(dstPath, "data")) + if err != nil { + t.Fatal(err) + } + if string(content) != "data" { + t.Fatalf("expected copied file content, got %q", content) + } + info, err := os.Stat(dstPath) + if err != nil { + t.Fatal(err) + } + if runtime.GOOS != "windows" && info.Mode().Perm() != 0o555 { + t.Fatalf("expected destination mode 0555, got %o", info.Mode().Perm()) + } +} + +func TestCopyForMovePreservesModTime(t *testing.T) { + root := t.TempDir() + srcPath := filepath.Join(root, "source") + dstPath := filepath.Join(root, "recycle", "source") + filePath := filepath.Join(srcPath, "data") + if err := os.Mkdir(srcPath, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filePath, []byte("data"), 0o666); err != nil { + t.Fatal(err) + } + fileTime := time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC) + dirTime := time.Date(2021, 6, 7, 8, 9, 10, 0, time.UTC) + if err := os.Chtimes(filePath, fileTime, fileTime); err != nil { + t.Fatal(err) + } + if err := os.Chtimes(srcPath, dirTime, dirTime); err != nil { + t.Fatal(err) + } + + driver := &Local{mkdirPerm: 0o755} + if err := driver.copyForMove(srcPath, dstPath); err != nil { + t.Fatal(err) + } + + for path, want := range map[string]time.Time{ + filepath.Join(dstPath, "data"): fileTime, + dstPath: dirTime, + } { + info, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + if !info.ModTime().Equal(want) { + t.Fatalf("expected %s mod time %v, got %v", path, want, info.ModTime()) + } + } +} diff --git a/drivers/local/util.go b/drivers/local/util.go index eb7962c2..0b3b877c 100644 --- a/drivers/local/util.go +++ b/drivers/local/util.go @@ -3,6 +3,7 @@ package local import ( "bytes" "encoding/json" + "errors" "fmt" "io/fs" "os" @@ -17,6 +18,7 @@ import ( "github.com/AlliotTech/openalist/internal/model" "github.com/AlliotTech/openalist/pkg/utils" "github.com/disintegration/imaging" + log "github.com/sirupsen/logrus" ffmpeg "github.com/u2takey/ffmpeg-go" ) @@ -204,3 +206,66 @@ func (d *Local) removeThumbCache(fullPath string) { _ = os.Remove(thumbPath) } } + +func (d *Local) copyForMove(srcPath, dstPath string) error { + info, err := os.Lstat(srcPath) + if err != nil { + return err + } + if info.Mode()&os.ModeDevice != 0 { + return errors.New("cannot copy a device") + } + if info.Mode()&os.ModeSymlink != 0 { + return d.copySymlinkForMove(srcPath, dstPath) + } + if info.Mode()&os.ModeNamedPipe != 0 { + if err = copyNamedPipe(dstPath, info.Mode(), os.FileMode(d.mkdirPerm)); err != nil { + return err + } + // Some platforms skip named pipes, so keep the source when nothing was created. + _, err = os.Lstat(dstPath) + return err + } + if !info.IsDir() { + if err = utils.CopyFile(srcPath, dstPath); err != nil { + return err + } + preserveModTime(dstPath, info) + return nil + } + + if err = os.MkdirAll(dstPath, os.FileMode(d.mkdirPerm)); err != nil { + return err + } + files, err := readDir(srcPath) + if err != nil { + return err + } + for _, file := range files { + if err = d.copyForMove(filepath.Join(srcPath, file.Name()), filepath.Join(dstPath, file.Name())); err != nil { + return err + } + } + if err = os.Chmod(dstPath, info.Mode()); err != nil { + return err + } + preserveModTime(dstPath, info) + return nil +} + +func preserveModTime(dstPath string, info os.FileInfo) { + if err := os.Chtimes(dstPath, info.ModTime(), info.ModTime()); err != nil { + log.Errorf("[local] failed to change time of %s: %s", dstPath, err) + } +} + +func (d *Local) copySymlinkForMove(srcPath, dstPath string) error { + linkOrig, err := os.Readlink(srcPath) + if err != nil { + return err + } + if err = os.MkdirAll(filepath.Dir(dstPath), os.FileMode(d.mkdirPerm)); err != nil { + return err + } + return os.Symlink(linkOrig, dstPath) +} diff --git a/drivers/local/util_unix.go b/drivers/local/util_unix.go index ddb4879f..3aa1f2db 100644 --- a/drivers/local/util_unix.go +++ b/drivers/local/util_unix.go @@ -3,10 +3,17 @@ package local import ( + "errors" "io/fs" "strings" + + "golang.org/x/sys/unix" ) func isHidden(f fs.FileInfo, _ string) bool { return strings.HasPrefix(f.Name(), ".") } + +func isCrossDeviceError(err error) bool { + return errors.Is(err, unix.EXDEV) +} diff --git a/drivers/local/util_windows.go b/drivers/local/util_windows.go index 76315f54..a1427242 100644 --- a/drivers/local/util_windows.go +++ b/drivers/local/util_windows.go @@ -3,9 +3,12 @@ package local import ( + "errors" "io/fs" "path/filepath" "syscall" + + "golang.org/x/sys/windows" ) func isHidden(f fs.FileInfo, fullPath string) bool { @@ -20,3 +23,7 @@ func isHidden(f fs.FileInfo, fullPath string) bool { } return attrs&syscall.FILE_ATTRIBUTE_HIDDEN != 0 } + +func isCrossDeviceError(err error) bool { + return errors.Is(err, windows.ERROR_NOT_SAME_DEVICE) +}