An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Protect the mobile workspace base directory
Daniel914e65077055
diff --git a/kernel/api/workspace.go b/kernel/api/workspace.goindex 942edb0023..8c2ed59949 100644--- a/kernel/api/workspace.go+++ b/kernel/api/workspace.go@@ -44,6 +44,9 @@ func checkWorkspaceDir(c *gin.Context) {}path := arg["path"].(string)+ if rejectMobileWorkspaceBaseDir(ret, path) {+ return+ }// 检查路径是否是分区根路径if util.IsPartitionRootPath(path) {ret.Code = -1@@ -97,6 +100,9 @@ func createWorkspaceDir(c *gin.Context) {absPath := arg["path"].(string)absPath = util.RemoveInvalid(absPath)absPath = strings.TrimSpace(absPath)+ if rejectMobileWorkspaceBaseDir(ret, absPath) {+ return+ }if isInvalidWorkspacePath(absPath) {ret.Code = -1ret.Msg = "This workspace name is not allowed, please use another name"@@ -180,6 +186,9 @@ func removeWorkspaceDirPhysically(c *gin.Context) {ret.Msg = absErr.Error()return}+ if rejectMobileWorkspaceBaseDir(ret, cleanPath) {+ return+ }if cleanPath == util.WorkspaceDir {ret.Code = -1ret.Msg = "cannot remove current workspace"@@ -300,6 +309,9 @@ func setWorkspaceDir(c *gin.Context) {}path := arg["path"].(string)+ if rejectMobileWorkspaceBaseDir(ret, path) {+ return+ }if util.WorkspaceDir == path {ret.Code = -1ret.Msg = model.Conf.Language(78)@@ -367,6 +379,9 @@ func isInvalidWorkspacePath(absPath string) bool {if "" == absPath {return true}+ if util.IsMobileWorkspaceBaseDir(absPath) {+ return true+ }name := filepath.Base(absPath)if "" == name {return true@@ -384,3 +399,13 @@ func isInvalidWorkspacePath(absPath string) bool {toLower := strings.ToLower(name)return gulu.Str.Contains(toLower, []string{"conf", "home", "data", "temp"})}++func rejectMobileWorkspaceBaseDir(ret *gulu.Result, path string) bool {+ if !util.IsMobileWorkspaceBaseDir(path) {+ return false+ }+ ret.Code = -1+ ret.Msg = model.Conf.Language(274)+ ret.Data = map[string]any{"closeTimeout": 7000}+ return true+}diff --git a/kernel/api/workspace_test.go b/kernel/api/workspace_test.gonew file mode 100644index 0000000000..b01e18201c--- /dev/null+++ b/kernel/api/workspace_test.go@@ -0,0 +1,141 @@+// 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.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package api++import (+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "os"+ "path/filepath"+ "slices"+ "strings"+ "testing"++ "github.com/88250/gulu"+ "github.com/gin-gonic/gin"+ "github.com/siyuan-note/siyuan/kernel/model"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestRejectMobileWorkspaceBaseDir(t *testing.T) {+ _, workspaceBaseDir := prepareMobileWorkspaceBaseDirTest(t)+ ret := gulu.Ret.NewResult()++ if !rejectMobileWorkspaceBaseDir(ret, workspaceBaseDir) {+ t.Fatal("mobile workspace base dir should be rejected")+ }+ if -1 != ret.Code {+ t.Fatalf("unexpected result code: got %d, want -1", ret.Code)+ }+ if !isInvalidWorkspacePath(workspaceBaseDir) {+ t.Fatal("mobile workspace base dir should be excluded from workspace discovery")+ }+}++func TestGetMobileWorkspacesExcludesBaseDir(t *testing.T) {+ packageDir, workspaceBaseDir := prepareMobileWorkspaceBaseDirTest(t)+ otherWorkspaceDir := filepath.Join(packageDir, "Todo")+ for _, dir := range []string{util.WorkspaceDir, otherWorkspaceDir} {+ if err := os.MkdirAll(dir, 0755); err != nil {+ t.Fatal(err)+ }+ }++ gin.SetMode(gin.TestMode)+ engine := gin.New()+ engine.GET("/api/system/getMobileWorkspaces", getMobileWorkspaces)+ recorder := httptest.NewRecorder()+ request := httptest.NewRequest(http.MethodGet, "http://127.0.0.1/api/system/getMobileWorkspaces", nil)+ engine.ServeHTTP(recorder, request)++ response := &struct {+ Code int `json:"code"`+ Data []string `json:"data"`+ }{}+ if err := json.Unmarshal(recorder.Body.Bytes(), response); err != nil {+ t.Fatalf("unmarshal getMobileWorkspaces response failed: %s", err)+ }+ if 0 != response.Code {+ t.Fatalf("getMobileWorkspaces failed: %s", recorder.Body.String())+ }+ if slices.Contains(response.Data, workspaceBaseDir) {+ t.Fatalf("mobile workspace base dir must not be discoverable: %v", response.Data)+ }+ if !slices.Contains(response.Data, otherWorkspaceDir) {+ t.Fatalf("regular sibling workspace should remain discoverable: %v", response.Data)+ }+}++func TestRemoveWorkspaceDirPhysicallyProtectsBaseDir(t *testing.T) {+ _, workspaceBaseDir := prepareMobileWorkspaceBaseDirTest(t)+ marker := filepath.Join(workspaceBaseDir, "marker")+ if err := os.WriteFile(marker, []byte("test"), 0644); err != nil {+ t.Fatal(err)+ }++ gin.SetMode(gin.TestMode)+ engine := gin.New()+ engine.POST("/api/system/removeWorkspaceDirPhysically", removeWorkspaceDirPhysically)+ recorder := httptest.NewRecorder()+ request := httptest.NewRequest(http.MethodPost, "http://127.0.0.1/api/system/removeWorkspaceDirPhysically",+ strings.NewReader(`{"path":"`+filepath.ToSlash(workspaceBaseDir)+`"}`))+ request.Header.Set("Content-Type", "application/json")+ engine.ServeHTTP(recorder, request)++ response := &struct {+ Code int `json:"code"`+ }{}+ if err := json.Unmarshal(recorder.Body.Bytes(), response); err != nil {+ t.Fatalf("unmarshal removeWorkspaceDirPhysically response failed: %s", err)+ }+ if -1 != response.Code {+ t.Fatalf("mobile workspace base dir removal was accepted: %s", recorder.Body.String())+ }+ if _, err := os.Stat(marker); err != nil {+ t.Fatalf("mobile workspace base dir content must remain unchanged: %s", err)+ }+}++func prepareMobileWorkspaceBaseDirTest(t *testing.T) (packageDir, workspaceBaseDir string) {+ t.Helper()+ packageDir = t.TempDir()+ workspaceBaseDir = filepath.Join(packageDir, "files")+ if err := os.MkdirAll(workspaceBaseDir, 0755); err != nil {+ t.Fatal(err)+ }++ originalContainer, originalHomeDir, originalWorkspaceDir := util.Container, util.HomeDir, util.WorkspaceDir+ originalConf := model.Conf+ originalEnglishLang, hadEnglishLang := util.Langs["en"]+ util.Container = util.ContainerAndroid+ util.HomeDir = filepath.Join(workspaceBaseDir, "home")+ util.WorkspaceDir = filepath.Join(packageDir, "1")+ model.Conf = model.NewAppConf()+ model.Conf.Lang = "en"+ util.Langs["en"] = map[int]string{274: "Please create a new folder as the workspace"}+ t.Cleanup(func() {+ util.Container, util.HomeDir, util.WorkspaceDir = originalContainer, originalHomeDir, originalWorkspaceDir+ model.Conf = originalConf+ if hadEnglishLang {+ util.Langs["en"] = originalEnglishLang+ } else {+ delete(util.Langs, "en")+ }+ })+ return packageDir, workspaceBaseDir+}diff --git a/kernel/util/working_mobile.go b/kernel/util/working_mobile.goindex e4e903de29..ad469ab09f 100644--- a/kernel/util/working_mobile.go+++ b/kernel/util/working_mobile.go@@ -220,3 +220,11 @@ func replaceLegacyIOSWorkspacePath(workspacePaths []string, workspaceBaseDir, de}return DeduplicateWorkspacePaths(workspacePaths)}++// IsMobileWorkspaceBaseDir 判断路径是否为移动端保留的工作空间基目录。+func IsMobileWorkspaceBaseDir(path string) bool {+ if !IsMobileContainer() || "" == path || "" == HomeDir {+ return false+ }+ return filepath.Clean(path) == filepath.Clean(filepath.Dir(HomeDir))+}diff --git a/kernel/util/working_mobile_test.go b/kernel/util/working_mobile_test.goindex 450196655d..c7bbb54607 100644--- a/kernel/util/working_mobile_test.go+++ b/kernel/util/working_mobile_test.go@@ -122,6 +122,33 @@ func TestReplaceLegacyIOSWorkspacePath(t *testing.T) {}}+func TestIsMobileWorkspaceBaseDir(t *testing.T) {+ originalContainer, originalHomeDir := Container, HomeDir+ workspaceBaseDir := filepath.Join(t.TempDir(), "files")+ Container = ContainerAndroid+ HomeDir = filepath.Join(workspaceBaseDir, "home")+ t.Cleanup(func() {+ Container, HomeDir = originalContainer, originalHomeDir+ })++ for _, path := range []string{workspaceBaseDir, workspaceBaseDir + string(os.PathSeparator),+ filepath.Join(workspaceBaseDir, ".")} {+ if !IsMobileWorkspaceBaseDir(path) {+ t.Fatalf("mobile workspace base dir [%s] should be reserved", path)+ }+ }+ for _, path := range []string{filepath.Dir(workspaceBaseDir), filepath.Join(workspaceBaseDir, "siyuan")} {+ if IsMobileWorkspaceBaseDir(path) {+ t.Fatalf("path [%s] should not be treated as the mobile workspace base dir", path)+ }+ }++ Container = ContainerStd+ if IsMobileWorkspaceBaseDir(workspaceBaseDir) {+ t.Fatal("desktop paths must not be treated as the mobile workspace base dir")+ }+}+func prepareLegacyMobileWorkspace(t *testing.T) string {t.Helper()workspaceBaseDir := t.TempDir()