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: Handle cross-platform .sy path separators https://github.com/siyuan-note/siyuan/issues/18378
Daniel7a89117e072a
diff --git a/kernel/filesys/crypto_hook.go b/kernel/filesys/crypto_hook.goindex 25a1537e71..d2ab93c96e 100644--- a/kernel/filesys/crypto_hook.go+++ b/kernel/filesys/crypto_hook.go@@ -41,11 +41,9 @@ var DEKLockRelease func(boxID string)// 非法扩展名或非节点 ID 模式返回错误,避免把任意路径当 AAD 绑定物产生不可解密的数据。// 由 filesys、model 历史查看/回滚、import 等所有 .sy 加解密路径共同使用,保证 AAD 一致。func SyObjectBase(relativePath string) (string, error) {- p := filepath.ToSlash(relativePath)- p = strings.TrimPrefix(p, "/")- base := p- if idx := strings.LastIndex(p, "/"); idx >= 0 {- base = p[idx+1:]+ base := relativePath+ if idx := strings.LastIndexAny(relativePath, "/\\"); idx >= 0 {+ base = relativePath[idx+1:]}if !strings.HasSuffix(base, ".sy") {return "", fmt.Errorf("invalid .sy base name [%s]: must end with .sy", base)diff --git a/kernel/filesys/crypto_hook_test.go b/kernel/filesys/crypto_hook_test.goindex c11e7bf254..9f58df7eb5 100644--- a/kernel/filesys/crypto_hook_test.go+++ b/kernel/filesys/crypto_hook_test.go@@ -37,6 +37,7 @@ func TestSyObjectBase(t *testing.T) {{"with parent dir", "/20240101120000-aaa/20240101120000-1a2b3c4.sy", validBase, false},{"deep parent dir", "20240101120000-aaa/20240101120000-bbb/20240101120000-1a2b3c4.sy", validBase, false},{"windows backslash", "\\20240101120000-aaa\\20240101120000-1a2b3c4.sy", validBase, false},+ {"mixed separators", "20240101120000-aaa\\20240101120000-bbb/20240101120000-1a2b3c4.sy", validBase, false},{"non-sy extension", "/20240101120000-1a2b3c4.json", "", true},{"no extension", "/20240101120000-1a2b3c4", "", true},{"bad stem not node id", "/notanid.sy", "", true},@@ -81,12 +82,22 @@ func TestSyAADIndependentOfParentDir(t *testing.T) {if err != nil {t.Fatal(err)}+ aadWithBackslash, err := SyAAD(boxID, "\\20240101120000-parent\\"+base)+ if err != nil {+ t.Fatal(err)+ }+ aadWithMixedSeparators, err := SyAAD(boxID, "20240101120000-a\\20240101120000-b/"+base)+ if err != nil {+ t.Fatal(err)+ }- if aadBare != aadWithDir || aadBare != aadDeepDir {- t.Fatalf("AAD must be independent of parent dir: bare=%q dir=%q deep=%q", aadBare, aadWithDir, aadDeepDir)+ if aadBare != aadWithDir || aadBare != aadDeepDir || aadBare != aadWithBackslash ||+ aadBare != aadWithMixedSeparators {+ t.Fatalf("AAD must be independent of parent dir and path separator: bare=%q dir=%q deep=%q backslash=%q mixed=%q",+ aadBare, aadWithDir, aadDeepDir, aadWithBackslash, aadWithMixedSeparators)}// AAD 不应包含目录分隔符,只含基名- if strings.Contains(strings.TrimPrefix(aadBare, "siyuan:v1:file:"+boxID+":"), "/") {+ if strings.ContainsAny(strings.TrimPrefix(aadBare, "siyuan:v1:file:"+boxID+":"), "/\\") {t.Fatalf("AAD must not contain path separator: %q", aadBare)}}diff --git a/kernel/util/path.go b/kernel/util/path.goindex 14154652f2..fff1bde6b1 100644--- a/kernel/util/path.go+++ b/kernel/util/path.go@@ -54,10 +54,8 @@ func TrimSpaceInPath(p string) string {}func GetTreeID(treePath string) string {- if strings.Contains(treePath, "\\") {- return strings.TrimSuffix(filepath.Base(treePath), ".sy")- }- return strings.TrimSuffix(path.Base(treePath), ".sy")+ base := path.Base(strings.ReplaceAll(treePath, "\\", "/"))+ return strings.TrimSuffix(base, ".sy")}func ShortPathForBootingDisplay(p string) string {diff --git a/kernel/util/path_test.go b/kernel/util/path_test.goindex 0c02d0f07e..05fa96c0f4 100644--- a/kernel/util/path_test.go+++ b/kernel/util/path_test.go@@ -22,6 +22,28 @@ import ("testing")+// TestGetTreeID 校验使用不同路径分隔符时均能提取相同的文档 ID。+func TestGetTreeID(t *testing.T) {+ id := "20240101120000-1a2b3c4"+ cases := []struct {+ name string+ path string+ }{+ {"bare ID", id},+ {"bare file", id + ".sy"},+ {"slash", "/20240101120000-parent/" + id + ".sy"},+ {"backslash", "\\20240101120000-parent\\" + id + ".sy"},+ {"mixed separators", "20240101120000-a\\20240101120000-b/" + id + ".sy"},+ }+ for _, c := range cases {+ t.Run(c.name, func(t *testing.T) {+ if got := GetTreeID(c.path); got != id {+ t.Fatalf("GetTreeID(%q) = %q, want %q", c.path, got, id)+ }+ })+ }+}+// TestIsSensitivePathCredentialDotfiles 覆盖 GHSA 报告中遗漏的家目录凭据 dotfile,// 确保它们在 globalCopyFiles 等接受工作空间外绝对路径的接口处被拒绝。func TestIsSensitivePathCredentialDotfiles(t *testing.T) {