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: Fix loading assets from junctioned Windows workspaces https://github.com/siyuan-note/siyuan/issues/18691
Danielc5b177505fd9
diff --git a/kernel/model/asset_path_resolver_other.go b/kernel/model/asset_path_resolver_other.gonew file mode 100644index 0000000000..2e530a3d57--- /dev/null+++ b/kernel/model/asset_path_resolver_other.go@@ -0,0 +1,25 @@+// 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/>.++//go:build !windows++package model++import "path/filepath"++func resolveAssetRealPath(path string) (string, error) {+ return filepath.EvalSymlinks(path)+}diff --git a/kernel/model/asset_path_resolver_windows.go b/kernel/model/asset_path_resolver_windows.gonew file mode 100644index 0000000000..abc3db48c2--- /dev/null+++ b/kernel/model/asset_path_resolver_windows.go@@ -0,0 +1,72 @@+// 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/>.++//go:build windows++package model++import (+ "path/filepath"+ "strings"++ "golang.org/x/sys/windows"+)++// resolveAssetRealPath 使用文件句柄解析符号链接和目录联接,返回现有资源的最终绝对路径。+func resolveAssetRealPath(path string) (string, error) {+ absPath, err := filepath.Abs(path)+ if err != nil {+ return "", err+ }+ pathUTF16, err := windows.UTF16PtrFromString(absPath)+ if err != nil {+ return "", err+ }+ handle, err := windows.CreateFile(+ pathUTF16,+ 0,+ windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE,+ nil,+ windows.OPEN_EXISTING,+ windows.FILE_FLAG_BACKUP_SEMANTICS,+ 0,+ )+ if err != nil {+ return "", err+ }+ defer func() {+ _ = windows.CloseHandle(handle)+ }()++ buffer := make([]uint16, 512)+ for {+ length, callErr := windows.GetFinalPathNameByHandle(handle, &buffer[0], uint32(len(buffer)), 0)+ if callErr != nil {+ return "", callErr+ }+ if length < uint32(len(buffer)) {+ resolved := windows.UTF16ToString(buffer[:length])+ switch {+ case strings.HasPrefix(resolved, `\\?\UNC\`):+ resolved = `\\` + strings.TrimPrefix(resolved, `\\?\UNC\`)+ case len(resolved) > 5 && strings.HasPrefix(resolved, `\\?\`) && resolved[5] == ':':+ resolved = strings.TrimPrefix(resolved, `\\?\`)+ }+ return filepath.Clean(resolved), nil+ }+ buffer = make([]uint16, length+1)+ }+}diff --git a/kernel/model/assets.go b/kernel/model/assets.goindex e0630e350c..f0f3be4e1b 100644--- a/kernel/model/assets.go+++ b/kernel/model/assets.go@@ -946,15 +946,15 @@ func ResolveDataAssetPath(assetPath string) (relativePath, absPath string, err ereturn}- resolvedRoot, evalErr := filepath.EvalSymlinks(assetRoot)+ resolvedRoot, evalErr := resolveAssetRealPath(assetRoot)if evalErr != nil {err = fmt.Errorf("resolve assets directory [%s] failed: %w", assetRoot, evalErr)return}if assetDirIndex > 0 {notebookRoot := filepath.Join(util.DataDir, parts[0])- resolvedDataDir, dataEvalErr := filepath.EvalSymlinks(util.DataDir)- resolvedNotebookRoot, notebookEvalErr := filepath.EvalSymlinks(notebookRoot)+ resolvedDataDir, dataEvalErr := resolveAssetRealPath(util.DataDir)+ resolvedNotebookRoot, notebookEvalErr := resolveAssetRealPath(notebookRoot)if dataEvalErr != nil || notebookEvalErr != nil ||!gulu.File.IsSubPath(resolvedDataDir, resolvedNotebookRoot) ||!gulu.File.IsSubPath(resolvedNotebookRoot, resolvedRoot) {@@ -962,7 +962,7 @@ func ResolveDataAssetPath(assetPath string) (relativePath, absPath string, err ereturn}}- resolvedPath, evalErr := filepath.EvalSymlinks(absPath)+ resolvedPath, evalErr := resolveAssetRealPath(absPath)if evalErr != nil {err = fmt.Errorf("resolve asset [%s] failed: %w", absPath, evalErr)return@@ -991,10 +991,10 @@ func ResolveUnusedDataAssetPath(assetPath string) (relativePath, absPath string,}func unusedAssetsContainPath(relativePath, absPath string, items []*UnusedItem) bool {- resolvedPath, _ := filepath.EvalSymlinks(absPath)+ resolvedPath, _ := resolveAssetRealPath(absPath)for _, item := range items {if item.AbsPath != "" {- resolvedItemPath, evalErr := filepath.EvalSymlinks(item.AbsPath)+ resolvedItemPath, evalErr := resolveAssetRealPath(item.AbsPath)samePath, relErr := filepath.Rel(resolvedPath, resolvedItemPath)if resolvedPath != "" && evalErr == nil && relErr == nil && samePath == "." {return truediff --git a/kernel/server/serve_assets_windows_test.go b/kernel/server/serve_assets_windows_test.gonew file mode 100644index 0000000000..284e156976--- /dev/null+++ b/kernel/server/serve_assets_windows_test.go@@ -0,0 +1,124 @@+// 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/>.++//go:build windows++package server++import (+ "os"+ "os/exec"+ "path/filepath"+ "testing"++ "github.com/siyuan-note/siyuan/kernel/model"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func createAssetTestJunction(t *testing.T, junction, target string) {+ t.Helper()+ output, err := exec.Command("cmd", "/d", "/c", "mklink", "/J", junction, target).CombinedOutput()+ if err != nil {+ t.Fatalf("create directory junction [%s] to [%s] failed: %s: %s", junction, target, err, output)+ }+ t.Cleanup(func() {+ if removeErr := os.Remove(junction); removeErr != nil && !os.IsNotExist(removeErr) {+ t.Errorf("remove directory junction [%s] failed: %s", junction, removeErr)+ }+ })+}++func TestIsValidResolvedAssetPathWithJunctionedWorkspace(t *testing.T) {+ originalDataDir, originalWorkspaceDir := util.DataDir, util.WorkspaceDir+ t.Cleanup(func() {+ util.DataDir, util.WorkspaceDir = originalDataDir, originalWorkspaceDir+ })++ realWorkspaceDir := t.TempDir()+ aliasWorkspaceDir := filepath.Join(t.TempDir(), "workspace")+ createAssetTestJunction(t, aliasWorkspaceDir, realWorkspaceDir)+ util.WorkspaceDir = aliasWorkspaceDir+ util.DataDir = filepath.Join(aliasWorkspaceDir, "data")++ assetPath := filepath.Join(util.DataDir, "assets", "image.png")+ if err := os.MkdirAll(filepath.Dir(assetPath), 0755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(assetPath, []byte("image"), 0644); err != nil {+ t.Fatal(err)+ }++ resolvedPath, err := model.GetAssetAbsPath("assets/image.png")+ if err != nil {+ t.Fatal(err)+ }+ if !isValidResolvedAssetPath(resolvedPath, "") {+ t.Fatalf("asset [%s] under junctioned workspace should be valid", resolvedPath)+ }++ const (+ boxID = "20260813000000-box0001"+ docID = "20260813000001-doc0001"+ )+ boxConfPath := filepath.Join(util.DataDir, boxID, ".siyuan", "conf.json")+ if err = os.MkdirAll(filepath.Dir(boxConfPath), 0755); err != nil {+ t.Fatal(err)+ }+ if err = os.WriteFile(boxConfPath, []byte(`{"name":"Notebook"}`), 0644); err != nil {+ t.Fatal(err)+ }+ documentAssetPath := filepath.Join(util.DataDir, boxID, docID, "assets", "document.png")+ if err = os.MkdirAll(filepath.Dir(documentAssetPath), 0755); err != nil {+ t.Fatal(err)+ }+ if err = os.WriteFile(documentAssetPath, []byte("document image"), 0644); err != nil {+ t.Fatal(err)+ }++ if !isValidResolvedAssetPath(documentAssetPath, "") {+ t.Fatalf("document asset [%s] under junctioned workspace should be valid", documentAssetPath)+ }+}++func TestIsValidResolvedAssetPathRejectsEscapingJunction(t *testing.T) {+ originalDataDir, originalWorkspaceDir := util.DataDir, util.WorkspaceDir+ t.Cleanup(func() {+ util.DataDir, util.WorkspaceDir = originalDataDir, originalWorkspaceDir+ })++ util.WorkspaceDir = t.TempDir()+ util.DataDir = filepath.Join(util.WorkspaceDir, "data")+ assetsDir := filepath.Join(util.DataDir, "assets")+ if err := os.MkdirAll(assetsDir, 0755); err != nil {+ t.Fatal(err)+ }++ outsideDir := t.TempDir()+ outsidePath := filepath.Join(outsideDir, "outside.png")+ if err := os.WriteFile(outsidePath, []byte("outside"), 0644); err != nil {+ t.Fatal(err)+ }+ linkedDir := filepath.Join(assetsDir, "linked")+ createAssetTestJunction(t, linkedDir, outsideDir)++ resolvedPath, err := model.GetAssetAbsPath("assets/linked/outside.png")+ if err != nil {+ t.Fatal(err)+ }+ if isValidResolvedAssetPath(resolvedPath, "") {+ t.Fatalf("asset junction escaping assets directory should be rejected: %s", resolvedPath)+ }+}