Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Allow workspace resources in platform app data directories https://github.com/siyuan-note/siyuan/issues/20569

Daniel773b2963265e
diff --git a/kernel/util/path.go b/kernel/util/path.go
index 763d4248ea..c662554ee5 100644
--- a/kernel/util/path.go
+++ b/kernel/util/path.go
@@ -746,6 +746,10 @@ func isSensitivePath(p string, htmlAsset bool) bool {
}
}
}
+ // 仅豁免工作空间所在的平台应用数据目录,私有目录和凭据规则仍然生效。
+ if inWorkspace {
+ continue
+ }
for _, entry := range platformSensitiveHomeEntries[runtime.GOOS] {
prefix := strings.ToLower(filepath.Join(homeDir, entry.name))
for _, checkPath := range homeCheckPaths {
diff --git a/kernel/util/path_platform_workspace_test.go b/kernel/util/path_platform_workspace_test.go
new file mode 100644
index 0000000000..3f10794d97
--- /dev/null
+++ b/kernel/util/path_platform_workspace_test.go
@@ -0,0 +1,119 @@
+// SiYuan - From thought to insight, with agents
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+package util
+
+import (
+ "os"
+ "path/filepath"
+ "runtime"
+ "testing"
+)
+
+// TestIsSensitivePathPlatformWorkspace 覆盖平台应用数据目录中的工作空间及其访问边界。
+func TestIsSensitivePathPlatformWorkspace(t *testing.T) {
+ originalHome, originalWorkspace := HomeDir, WorkspaceDir
+ originalEntries, hadEntries := platformSensitiveHomeEntries[runtime.GOOS]
+ t.Cleanup(func() {
+ HomeDir, WorkspaceDir = originalHome, originalWorkspace
+ if hadEntries {
+ platformSensitiveHomeEntries[runtime.GOOS] = originalEntries
+ } else {
+ delete(platformSensitiveHomeEntries, runtime.GOOS)
+ }
+ })
+
+ // 注入各平台的目录规则,使所有运行平台都能验证 macOS 和 Windows 的工作空间布局。
+ for _, layout := range []struct {
+ name string
+ platform string
+ workspace string
+ external string
+ }{
+ {"macOS default", "darwin", "Library/Application Support/SiYuan", "Library/Keychains/login.keychain-db"},
+ {"macOS iCloud", "darwin", "Library/Mobile Documents/SiYuan", "Library/Keychains/login.keychain-db"},
+ {"Windows roaming", "windows", "AppData/Roaming/SiYuan", "AppData/Roaming/gcloud/token.db"},
+ {"Windows local", "windows", "AppData/Local/SiYuan", "AppData/Local/Google/Chrome/profile.db"},
+ } {
+ t.Run(layout.name, func(t *testing.T) {
+ entries := originalEntries
+ if layout.platform != runtime.GOOS {
+ entries = platformSensitiveHomeEntries[layout.platform]
+ }
+ // 测试目录位于系统临时目录内,关闭临时目录例外以独立验证工作空间例外。
+ entries = append([]sensitiveHomeEntry(nil), entries...)
+ for i := range entries {
+ entries[i].exemptSystemTemp = false
+ }
+ platformSensitiveHomeEntries[runtime.GOOS] = entries
+ home, err := filepath.EvalSymlinks(t.TempDir())
+ if err != nil {
+ t.Fatal(err)
+ }
+ HomeDir = home
+ realWorkspace := filepath.Join(home, filepath.FromSlash(layout.workspace))
+ WorkspaceDir = realWorkspace
+ if err := os.MkdirAll(realWorkspace, 0755); err != nil {
+ t.Fatal(err)
+ }
+
+ checkPaths := func(t *testing.T, root string) {
+ t.Helper()
+ for _, checker := range []struct {
+ name string
+ check func(string) bool
+ }{
+ {"file", IsSensitivePath},
+ {"HTML asset", IsSensitiveHTMLAssetPath},
+ } {
+ for _, test := range []struct {
+ rel string
+ sensitive bool
+ }{
+ {"data/assets/image.png", false},
+ {"data/assets/attachment.pdf", false},
+ {"data/20260806000000-abcdefg/assets/image.png", false},
+ {"data/plugins/example/icon.png", false},
+ {"data/widgets/example/index.html", false},
+ {"data/emojis/example.svg", false},
+ {"temp/export/new/document.html", false},
+ {"conf/conf.json", true},
+ {"conf/new/config.json", true},
+ {"temp/private.txt", true},
+ {"data/assets/credentials.json", true},
+ {"data/assets/id_rsa", true},
+ } {
+ p := filepath.Join(root, filepath.FromSlash(test.rel))
+ if got := checker.check(p); got != test.sensitive {
+ t.Errorf("%s check(%q) = %v, want %v", checker.name, p, got, test.sensitive)
+ }
+ }
+ for _, p := range []string{
+ realWorkspace + "-outside" + string(filepath.Separator) + "image.png",
+ filepath.Join(home, filepath.FromSlash(layout.external)),
+ filepath.Join(home, ".ssh", "config"),
+ filepath.Join(home, ".env.local"),
+ } {
+ if !checker.check(p) {
+ t.Errorf("%s check allowed external sensitive path %q", checker.name, p)
+ }
+ }
+ }
+ }
+ checkPaths(t, realWorkspace)
+ t.Run("workspace alias", func(t *testing.T) {
+ WorkspaceDir = filepath.Join(t.TempDir(), "workspace")
+ if err := os.Symlink(realWorkspace, WorkspaceDir); err != nil {
+ t.Skipf("create workspace symlink: %v", err)
+ }
+ checkPaths(t, WorkspaceDir)
+ checkPaths(t, realWorkspace)
+ })
+ })
+ }
+}