From a6c54a4787b00343e50004b196e68a5b08fa6f7e Mon Sep 17 00:00:00 2001 From: Yuxin Zhao <32972764+YuxinZhaozyx@users.noreply.github.com> Date: Sat, 14 Mar 2026 11:05:46 +0800 Subject: [PATCH 1/2] :lock: Improve the security of publish service (#17210) * :art: fix https://github.com/siyuan-note/siyuan/issues/17201 * :art: fix /api/block/getBlockDOM, /api/block/getBlockDOMs, /api/block/getBlockDOMWithEmbed, /api/block/getBlockDOMsWithEmbed * :art: fix publish access for /api/file/getFile --- kernel/api/block.go | 96 +++++++++++++++++++++++++++++++++++++++++++++ kernel/api/file.go | 19 +++++++++ 2 files changed, 115 insertions(+) diff --git a/kernel/api/block.go b/kernel/api/block.go index 5b30e0678..8f9d3f403 100644 --- a/kernel/api/block.go +++ b/kernel/api/block.go @@ -741,6 +741,22 @@ func getBlockDOM(c *gin.Context) { id := arg["id"].(string) dom := model.GetBlockDOM(id) + + if model.IsReadOnlyRoleContext(c) { + publishAccess := model.GetPublishAccess() + publishIgnore := model.GetDisablePublishAccess(publishAccess) + bt := treenode.GetBlockTree(id) + if bt != nil { + passwordID, password := model.GetPathPasswordByPublishAccess(bt.BoxID, bt.Path, publishAccess) + if password != "" && !model.CheckPublishAuthCookie(c, passwordID, password) { + dom = "" + } + if !model.CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishIgnore) { + dom = "" + } + } + } + ret.Data = map[string]string{ "id": id, "dom": dom, @@ -763,6 +779,22 @@ func getBlockDOMs(c *gin.Context) { } doms := model.GetBlockDOMs(ids) + + if model.IsReadOnlyRoleContext(c) { + publishAccess := model.GetPublishAccess() + publishIgnore := model.GetDisablePublishAccess(publishAccess) + bts := treenode.GetBlockTrees(ids) + for id, bt := range bts { + _, ok := doms[id] + if ok { + passwordID, password := model.GetPathPasswordByPublishAccess(bt.BoxID, bt.Path, publishAccess) + if (password != "" && !model.CheckPublishAuthCookie(c, passwordID, password)) || !model.CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishIgnore) { + doms[id] = "" + } + } + } + } + ret.Data = doms } @@ -777,6 +809,22 @@ func getBlockDOMWithEmbed(c *gin.Context) { id := arg["id"].(string) dom := model.GetBlockDOMWithEmbed(id) + + if model.IsReadOnlyRoleContext(c) { + publishAccess := model.GetPublishAccess() + publishIgnore := model.GetDisablePublishAccess(publishAccess) + bt := treenode.GetBlockTree(id) + if bt != nil { + passwordID, password := model.GetPathPasswordByPublishAccess(bt.BoxID, bt.Path, publishAccess) + if password != "" && !model.CheckPublishAuthCookie(c, passwordID, password) { + dom = "" + } + if !model.CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishIgnore) { + dom = "" + } + } + } + ret.Data = map[string]string{ "id": id, "dom": dom, @@ -799,6 +847,22 @@ func getBlockDOMsWithEmbed(c *gin.Context) { } doms := model.GetBlockDOMsWithEmbed(ids) + + if model.IsReadOnlyRoleContext(c) { + publishAccess := model.GetPublishAccess() + publishIgnore := model.GetDisablePublishAccess(publishAccess) + bts := treenode.GetBlockTrees(ids) + for id, bt := range bts { + _, ok := doms[id] + if ok { + passwordID, password := model.GetPathPasswordByPublishAccess(bt.BoxID, bt.Path, publishAccess) + if (password != "" && !model.CheckPublishAuthCookie(c, passwordID, password)) || !model.CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishIgnore) { + doms[id] = "" + } + } + } + } + ret.Data = doms } @@ -830,6 +894,22 @@ func getBlockKramdown(c *gin.Context) { } kramdown := model.GetBlockKramdown(id, mode) + + if model.IsReadOnlyRoleContext(c) { + publishAccess := model.GetPublishAccess() + publishIgnore := model.GetDisablePublishAccess(publishAccess) + bt := treenode.GetBlockTree(id) + if bt != nil { + passwordID, password := model.GetPathPasswordByPublishAccess(bt.BoxID, bt.Path, publishAccess) + if password != "" && !model.CheckPublishAuthCookie(c, passwordID, password) { + kramdown = "" + } + if !model.CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishIgnore) { + kramdown = "" + } + } + } + ret.Data = map[string]string{ "id": id, "kramdown": kramdown, @@ -869,6 +949,22 @@ func getBlockKramdowns(c *gin.Context) { } kramdowns := model.GetBlockKramdowns(ids, mode) + + if model.IsReadOnlyRoleContext(c) { + publishAccess := model.GetPublishAccess() + publishIgnore := model.GetDisablePublishAccess(publishAccess) + bts := treenode.GetBlockTrees(ids) + for id, bt := range bts { + _, ok := kramdowns[id] + if ok { + passwordID, password := model.GetPathPasswordByPublishAccess(bt.BoxID, bt.Path, publishAccess) + if (password != "" && !model.CheckPublishAuthCookie(c, passwordID, password)) || !model.CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishIgnore) { + kramdowns[id] = "" + } + } + } + } + ret.Data = kramdowns } diff --git a/kernel/api/file.go b/kernel/api/file.go index 444bf010e..a54233411 100644 --- a/kernel/api/file.go +++ b/kernel/api/file.go @@ -274,6 +274,25 @@ func refuseToAccess(c *gin.Context, fileAbsPath string, ret *gulu.Result) bool { c.JSON(http.StatusAccepted, ret) return true } + + // 禁止访问 data/.siyuan/publishAccess.json + publishAccessPath := normalizeAndResolve(filepath.Join(util.DataDir, ".siyuan", "publishAccess.json")) + if fileNorm == publishAccessPath { + ret.Code = http.StatusForbidden + ret.Msg = http.StatusText(http.StatusForbidden) + c.JSON(http.StatusAccepted, ret) + return true + } + + // 禁止访问 无发布访问权限的文件 + publishAccess := model.GetPublishAccess() + if !model.CheckAbsPathAccessableByPublishAccess(c, fileAbsPath, publishAccess) { + ret.Code = http.StatusForbidden + ret.Msg = http.StatusText(http.StatusForbidden) + c.JSON(http.StatusAccepted, ret) + return true + } + return false } From 9914fd1d39e5f0a8dcc9fb587e1c0b46f31490a1 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 14 Mar 2026 11:14:25 +0800 Subject: [PATCH 2/2] :lock: https://github.com/siyuan-note/siyuan/security/advisories/GHSA-h5vh-m7fg-w5h6 Signed-off-by: Daniel <845765@qq.com> --- kernel/util/path.go | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/kernel/util/path.go b/kernel/util/path.go index 6e45255bf..0dee81ee3 100644 --- a/kernel/util/path.go +++ b/kernel/util/path.go @@ -353,18 +353,28 @@ func IsSensitivePath(p string) bool { if p == "" { return false } - pp := filepath.Clean(strings.ToLower(p)) + toCheckPathLower := filepath.Clean(strings.ToLower(p)) + toCheckNameLower := filepath.Base(toCheckPathLower) // 敏感目录前缀(UNIX 风格) prefixes := []string{ - "/etc/ssh", + "/.", + "/etc", "/root", "/etc", - "/var/lib/", - "/.", + "/var", + "/proc", + "/sys", + "/run", + "/bin", + "/boot", + "/dev", + "/lib", + "/srv", + "/tmp", } for _, pre := range prefixes { - if strings.HasPrefix(pp, pre) { + if strings.HasPrefix(toCheckPathLower, pre) { return true } } @@ -375,7 +385,7 @@ func IsSensitivePath(p string) bool { `c:\windows\system`, } for _, wp := range winPrefixes { - if strings.HasPrefix(pp, strings.ToLower(wp)) { + if strings.HasPrefix(toCheckPathLower, strings.ToLower(wp)) { return true } } @@ -386,17 +396,18 @@ func IsSensitivePath(p string) bool { strings.ToLower(filepath.Join(os.Getenv("ProgramData"), "Microsoft", "Windows", "Start Menu")), } for _, sp := range startMenuPrefixes { - if strings.HasPrefix(pp, sp) { + if strings.HasPrefix(toCheckPathLower, sp) { return true } } // 工作空间/conf 目录(小写比较) workspaceConfPrefix := strings.ToLower(filepath.Join(WorkspaceDir, "conf")) - if strings.HasPrefix(pp, workspaceConfPrefix) { + if strings.HasPrefix(toCheckPathLower, workspaceConfPrefix) { return true } + // 用户家目录下的敏感目录(小写比较) homePrefixes := []string{ strings.ToLower(filepath.Join(HomeDir, ".ssh")), strings.ToLower(filepath.Join(HomeDir, ".config")), @@ -405,7 +416,18 @@ func IsSensitivePath(p string) bool { strings.ToLower(filepath.Join(HomeDir, ".profile")), } for _, hp := range homePrefixes { - if strings.HasPrefix(pp, hp) { + if strings.HasPrefix(toCheckPathLower, hp) { + return true + } + } + + // 特定的文件名(小写比较) + namePrefixes := []string{ + strings.ToLower("credentials"), + strings.ToLower("id_"), + } + for _, np := range namePrefixes { + if strings.HasPrefix(toCheckNameLower, np) { return true } }