From cab16dcacb39f335f8396d29758cc01f70654bd5 Mon Sep 17 00:00:00 2001 From: YG52052051 <52052051@qq.com> Date: Mon, 21 Sep 2026 14:21:25 +0800 Subject: [PATCH] fix(storage): strip storage-backend scope in local provider path normalization (#2979) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit KB deletion runs on the process-wide default file service, which is not wrapped by BackendScopedFileService. Storage-scoped physical paths ("storage:///local:///...") therefore reach the raw local provider, fall through to the relative-path branch, and get joined under baseDir — producing a nonexistent path so every os.Remove fails with ENOENT and the physical files silently leak on every KB/document delete. normalizePathForBase now strips the scope wrapper up front (both the canonical "storage://" form and the "storage:/" form produced when a path has been through filepath.Clean), and tolerates the cleaned "local:/" provider scheme. Unscoped paths are unaffected. Co-authored-by: YG52052051 --- internal/application/service/file/local.go | 42 ++++++++++++++++-- .../application/service/file/local_test.go | 44 +++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/internal/application/service/file/local.go b/internal/application/service/file/local.go index 80f6fa80..3707f25a 100644 --- a/internal/application/service/file/local.go +++ b/internal/application/service/file/local.go @@ -282,15 +282,51 @@ func (s *localFileService) GetFileURL(ctx context.Context, filePath string) (str return normalized, nil } +// storageScopePrefixes matches the storage:/// scope wrapper that +// backend-scoped file services prepend to every persisted path +// (types.BuildStorageBackendPath). On chains assembled without the scoped +// wrapper (e.g. the process-wide default file service used by KB deletion), +// these scoped paths reach the raw local provider, where they used to fall +// through to the relative-path branch, get joined under baseDir, and produce a +// nonexistent path — silently leaking the physical file on delete. Path +// normalization (filepath.Clean) may also collapse "storage://" into +// "storage:/", so both forms are accepted. +const storageScheme = "storage:" + +// trimStorageScope strips a leading "storage:///" (or the cleaned +// "storage://") scope wrapper and returns the inner provider path. +// Paths without the wrapper are returned unchanged. +func trimStorageScope(path string) string { + if !strings.HasPrefix(path, storageScheme) { + return path + } + rest := strings.TrimPrefix(path, storageScheme) + // Tolerate "storage://", "storage:/" and even a bare "storage:" prefix — + // filepath.Clean collapses "//" to "/", so cleaned paths arrive single-slashed. + rest = strings.TrimLeft(rest, "/") + // Skip the backend-id segment; the remainder is the provider path. + if i := strings.Index(rest, "/"); i >= 0 { + return rest[i+1:] + } + return "" +} + // normalizePathForBase keeps backward compatibility for legacy file paths: +// - storage-backend scope: "storage:///local://tenant/.." → baseDir/tenant/.. // - provider scheme: "local://tenant/.." → baseDir/tenant/.. // - absolute path: "/data/files/tenant/.." // - path under base dir: "tenant/.." // - legacy relative with base prefix: "data/files/tenant/.." func (s *localFileService) normalizePathForBase(filePath string) string { - // Handle provider:// format: local://{relPath} - if strings.HasPrefix(filePath, localScheme) { - relPath := strings.TrimPrefix(filePath, localScheme) + // Strip a storage-backend scope wrapper first: some chains (KB deletion + // uses the process-wide default file service) hand scoped paths straight + // to this provider, and the scoped prefix is not a filesystem component. + filePath = trimStorageScope(strings.TrimSpace(filePath)) + + // Handle provider scheme: local://{relPath}. Also tolerate the cleaned + // "local:/" single-slash form for the same reason as trimStorageScope. + if strings.HasPrefix(filePath, "local:") { + relPath := strings.TrimLeft(strings.TrimPrefix(filePath, "local:"), "/") return filepath.Join(s.baseDir, filepath.FromSlash(relPath)) } diff --git a/internal/application/service/file/local_test.go b/internal/application/service/file/local_test.go index 7492a4f7..750b43c6 100644 --- a/internal/application/service/file/local_test.go +++ b/internal/application/service/file/local_test.go @@ -3,6 +3,7 @@ package file import ( "context" "net/url" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -41,3 +42,46 @@ func TestLocalGetFileURL_NoExternalURL(t *testing.T) { require.NoError(t, err) assert.Equal(t, "local://1/abc/img.png", got) } + +// TestNormalizePathForBase_StripsStorageBackendScope verifies that paths +// carrying the storage:/// scope wrapper (written by +// backend-scoped chains, e.g. "storage://uuid/local://10000/doc/file.md") +// resolve to the same file as their unscoped provider path. Scoped paths +// reach the raw provider on chains without the scoped wrapper — notably KB +// deletion — and previously fell into the relative-path branch, producing a +// nonexistent path under baseDir and silently leaking physical files. +func TestNormalizePathForBase_StripsStorageBackendScope(t *testing.T) { + svc := NewLocalFileService("/data/files", "").(*localFileService) + + cases := []struct { + name string + in string + want string + }{ + { + "scoped provider path", + "storage://3ac51020-0fe9-4eb6-a58e-e3f8eaee9d04/local://10000/doc1/file.md", + "/data/files/10000/doc1/file.md", + }, + { + "scoped path after filepath.Clean collapsed double slashes", + "storage:/3ac51020-0fe9-4eb6-a58e-e3f8eaee9d04/local:/10000/doc1/file.md", + "/data/files/10000/doc1/file.md", + }, + { + "plain provider path unaffected", + "local://10000/doc1/file.md", + "/data/files/10000/doc1/file.md", + }, + { + "plain relative path unaffected", + "10000/doc1/file.md", + "/data/files/10000/doc1/file.md", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, filepath.ToSlash(svc.normalizePathForBase(tc.in))) + }) + } +}