An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:lock: Enforce publish access for embedded block content https://github.com/siyuan-note/siyuan/security/advisories/GHSA-h6w7-xxcf-w2mq
Daniel1ca1c3c9d94b
diff --git a/kernel/api/block.go b/kernel/api/block.goindex 48724c618a..4f5b9ae231 100644--- a/kernel/api/block.go+++ b/kernel/api/block.go@@ -979,10 +979,18 @@ func getBlockDOMWithEmbed(c *gin.Context) {id := arg["id"].(string)boxID := encryptedNotebookFromArg(arg)- dom := model.GetBlockDOMWithEmbedInBox(id, boxID)+ isReadOnlyRole := model.IsReadOnlyRoleContext(c)+ var publishAccess model.PublishAccess+ var accessChecker model.EmbedBlockAccessChecker+ if isReadOnlyRole {+ publishAccess = model.GetPublishAccess()+ accessChecker = func(blockID string) bool {+ return model.CheckBlockIdAccessableByPublishAccessInBox(c, publishAccess, blockID, boxID)+ }+ }+ dom := model.GetBlockDOMWithEmbedInBoxWithAccessChecker(id, boxID, accessChecker)- if model.IsReadOnlyRoleContext(c) {- publishAccess := model.GetPublishAccess()+ if isReadOnlyRole {publishIgnore := model.GetDisablePublishAccess(publishAccess)bt := treenode.GetBlockTreeInBox(id, boxID)if nil == bt {@@ -1017,10 +1025,18 @@ func getBlockDOMsWithEmbed(c *gin.Context) {}boxID := encryptedNotebookFromArg(arg)- doms := model.GetBlockDOMsWithEmbedInBox(ids, boxID)+ isReadOnlyRole := model.IsReadOnlyRoleContext(c)+ var publishAccess model.PublishAccess+ var accessChecker model.EmbedBlockAccessChecker+ if isReadOnlyRole {+ publishAccess = model.GetPublishAccess()+ accessChecker = func(blockID string) bool {+ return model.CheckBlockIdAccessableByPublishAccessInBox(c, publishAccess, blockID, boxID)+ }+ }+ doms := model.GetBlockDOMsWithEmbedInBoxWithAccessChecker(ids, boxID, accessChecker)- if model.IsReadOnlyRoleContext(c) {- publishAccess := model.GetPublishAccess()+ if isReadOnlyRole {publishIgnore := model.GetDisablePublishAccess(publishAccess)filterBlockDOMsByPublishAccess(c, doms, ids, boxID, publishAccess, publishIgnore)}diff --git a/kernel/model/block.go b/kernel/model/block.goindex d20e82de35..06ad5a4773 100644--- a/kernel/model/block.go+++ b/kernel/model/block.go@@ -904,11 +904,19 @@ func GetBlockDOMWithEmbed(id string) (ret string) {// GetBlockDOMWithEmbedInBox 渲染指定笔记本内包含嵌入块的 DOM。func GetBlockDOMWithEmbedInBox(id, boxID string) (ret string) {+ return GetBlockDOMWithEmbedInBoxWithAccessChecker(id, boxID, nil)+}++// EmbedBlockAccessChecker 判断嵌入查询结果块是否允许返回。+type EmbedBlockAccessChecker func(blockID string) bool++// GetBlockDOMWithEmbedInBoxWithAccessChecker 按访问权限渲染指定笔记本内包含嵌入块的 DOM。+func GetBlockDOMWithEmbedInBoxWithAccessChecker(id, boxID string, accessChecker EmbedBlockAccessChecker) (ret string) {if "" == id {return}- doms := GetBlockDOMsWithEmbedInBox([]string{id}, boxID)+ doms := GetBlockDOMsWithEmbedInBoxWithAccessChecker([]string{id}, boxID, accessChecker)ret = doms[id]return}@@ -919,6 +927,11 @@ func GetBlockDOMsWithEmbed(ids []string) (ret map[string]string) {// GetBlockDOMsWithEmbedInBox 渲染指定笔记本内包含嵌入块的 DOM。boxID 为空时仅查询普通全局库。func GetBlockDOMsWithEmbedInBox(ids []string, boxID string) (ret map[string]string) {+ return GetBlockDOMsWithEmbedInBoxWithAccessChecker(ids, boxID, nil)+}++// GetBlockDOMsWithEmbedInBoxWithAccessChecker 按访问权限渲染指定笔记本内包含嵌入块的 DOM。+func GetBlockDOMsWithEmbedInBoxWithAccessChecker(ids []string, boxID string, accessChecker EmbedBlockAccessChecker) (ret map[string]string) {ret = map[string]string{}if 0 == len(ids) {return@@ -935,7 +948,7 @@ func GetBlockDOMsWithEmbedInBox(ids []string, boxID string) (ret map[string]stricontinue}- resolveEmbedContentInBox(node, luteEngine, boxID)+ resolveEmbedContentInBox(node, luteEngine, boxID, accessChecker)// 处理折叠标题ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {@@ -959,7 +972,7 @@ func GetBlockDOMsWithEmbedInBox(ids []string, boxID string) (ret map[string]stri}func resolveEmbedContent(n *ast.Node, luteEngine *lute.Lute) {- resolveEmbedContentInBox(n, luteEngine, "")+ resolveEmbedContentInBox(n, luteEngine, "", nil)}// loadTreeForBlockDOM 按指定 box 加载树,空 box 不回退搜索已打开的加密笔记本。@@ -975,7 +988,7 @@ func loadTreeForBlockDOM(id, boxID string) *parse.Tree {return tree}-func resolveEmbedContentInBox(n *ast.Node, luteEngine *lute.Lute, boxID string) {+func resolveEmbedContentInBox(n *ast.Node, luteEngine *lute.Lute, boxID string, accessChecker EmbedBlockAccessChecker) {ast.Walk(n, func(node *ast.Node, entering bool) ast.WalkStatus {if !entering || ast.NodeBlockQueryEmbed != node.Type {return ast.WalkContinue@@ -992,6 +1005,7 @@ func resolveEmbedContentInBox(n *ast.Node, luteEngine *lute.Lute, boxID string)// 执行查询获取嵌入的块sqlBlocks := sql.SelectBlocksRawStmtInBox(stmt, 1, Conf.Search.Limit, boxID)+ sqlBlocks = filterEmbedBlocksByAccess(sqlBlocks, accessChecker)// 收集所有嵌入块的内容 HTMLvar embedContents []string@@ -1049,6 +1063,20 @@ func resolveEmbedContentInBox(n *ast.Node, luteEngine *lute.Lute, boxID string)})}+func filterEmbedBlocksByAccess(blocks []*sql.Block, accessChecker EmbedBlockAccessChecker) (ret []*sql.Block) {+ if nil == accessChecker {+ return blocks+ }++ ret = make([]*sql.Block, 0, len(blocks))+ for _, block := range blocks {+ if nil != block && accessChecker(block.ID) {+ ret = append(ret, block)+ }+ }+ return+}+func processEmbedHTML(htmlStr string) string {// 使用正则表达式查找所有带有 embed-content 属性的嵌入块embedPattern := `<div[^>]*data-type="NodeBlockQueryEmbed"[^>]*embed-content="[^"]*"[^>]*>`diff --git a/kernel/model/block_embed_test.go b/kernel/model/block_embed_test.gonew file mode 100644index 0000000000..167591ed0a--- /dev/null+++ b/kernel/model/block_embed_test.go@@ -0,0 +1,79 @@+// Copyright (c) 2026, peterq.cn (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 model++import (+ "net/http"+ "net/http/httptest"+ "testing"++ "github.com/gin-gonic/gin"+ "github.com/siyuan-note/siyuan/kernel/sql"+ "github.com/siyuan-note/siyuan/kernel/treenode"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestFilterEmbedBlocksByAccess(t *testing.T) {+ const (+ boxID = "20260725000000-boxid01"+ publicDocID = "20260725000001-public1"+ hiddenDocID = "20260725000002-hidden1"+ forbiddenDocID = "20260725000003-forbid"+ protectedDocID = "20260725000004-protect"+ protectedPassword = "password"+ )+ blocks := []*sql.Block{+ {ID: publicDocID},+ {ID: hiddenDocID},+ {ID: forbiddenDocID},+ {ID: protectedDocID},+ }++ if filtered := filterEmbedBlocksByAccess(blocks, nil); len(filtered) != len(blocks) {+ t.Fatalf("administrator results should remain unchanged: %+v", filtered)+ }++ blockTrees := map[string]*treenode.BlockTree{+ publicDocID: {ID: publicDocID, BoxID: boxID, Path: "/" + publicDocID + ".sy"},+ hiddenDocID: {ID: hiddenDocID, BoxID: boxID, Path: "/" + hiddenDocID + ".sy"},+ forbiddenDocID: {ID: forbiddenDocID, BoxID: boxID, Path: "/" + forbiddenDocID + ".sy"},+ protectedDocID: {ID: protectedDocID, BoxID: boxID, Path: "/" + protectedDocID + ".sy"},+ }+ publishAccess := PublishAccess{+ {ID: hiddenDocID, Visible: false},+ {ID: forbiddenDocID, Disable: true},+ {ID: protectedDocID, Visible: true, Password: protectedPassword},+ }+ c, _ := gin.CreateTestContext(httptest.NewRecorder())+ c.Request = httptest.NewRequest(http.MethodGet, "/", nil)+ accessChecker := func(blockID string) bool {+ return checkBlockTreeAccessableByPublishAccess(c, publishAccess, blockTrees[blockID])+ }++ filtered := filterEmbedBlocksByAccess(blocks, accessChecker)+ if 2 != len(filtered) || publicDocID != filtered[0].ID || hiddenDocID != filtered[1].ID {+ t.Fatalf("inaccessible embed results should be removed: %+v", filtered)+ }++ c.Request.AddCookie(&http.Cookie{+ Name: "publish-auth-" + protectedDocID,+ Value: util.SHA256Hash([]byte(protectedDocID + protectedPassword)),+ })+ filtered = filterEmbedBlocksByAccess(blocks, accessChecker)+ if 3 != len(filtered) || protectedDocID != filtered[2].ID {+ t.Fatalf("authorized protected embed result should be returned: %+v", filtered)+ }+}