Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:lock: Hide inaccessible query embed results https://github.com/siyuan-note/siyuan/security/advisories/GHSA-vh22-h7hf-www7

Daniel0015cbafbf68
diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.go
index 789b0cebc0..979a96e2b1 100644
--- a/kernel/model/publish_access.go
+++ b/kernel/model/publish_access.go
@@ -602,23 +602,18 @@ func FilterEmbedBlocksByPublishAccess(c *gin.Context, publishAccess PublishAcces
passwordID, password := GetPathPasswordByPublishAccess(block.Box, block.Path, publishAccess)
accessible := CheckPathAccessableByPublishIgnore(block.Box, block.Path, publishIgnore) &&
(password == "" || CheckPublishAuthCookie(c, passwordID, password))
- if accessible {
- ret = append(ret, &EmbedBlock{
- Block: &Block{
- ID: block.ID,
- Content: block.Content,
- },
- BlockPaths: embedBlock.BlockPaths,
- AllowChildOperation: embedBlock.AllowChildOperation,
- })
+ if !accessible {
+ // 不返回不可访问的查询结果,避免泄漏结果数量、顺序和访问控制边界。
continue
}
ret = append(ret, &EmbedBlock{
Block: &Block{
- Content: FilterContentByPublishAccess(c, publishAccess, block.Box, block.Path, block.Content, false),
+ ID: block.ID,
+ Content: block.Content,
},
- BlockPaths: []*BlockPath{},
+ BlockPaths: embedBlock.BlockPaths,
+ AllowChildOperation: embedBlock.AllowChildOperation,
})
}
return
diff --git a/kernel/model/publish_access_test.go b/kernel/model/publish_access_test.go
index 538ec3dd34..19b9ad3bd2 100644
--- a/kernel/model/publish_access_test.go
+++ b/kernel/model/publish_access_test.go
@@ -95,3 +95,35 @@ func TestFilterEmbedBlocksByPublishAccessRemovesInternalFields(t *testing.T) {
t.Fatalf("embed rendering metadata was not preserved: %+v", filtered[0])
}
}
+
+func TestFilterEmbedBlocksByPublishAccessDropsInaccessibleResults(t *testing.T) {
+ const (
+ boxID = "20260720000000-boxid01"
+ hiddenDocID = "20260720000002-hiddend"
+ protectedDocID = "20260720000003-protect"
+ protectedPassword = "password"
+ )
+ publishAccess := PublishAccess{
+ {ID: hiddenDocID, Disable: true},
+ {ID: protectedDocID, Visible: true, Password: protectedPassword},
+ }
+ embedBlocks := []*EmbedBlock{
+ {Block: &Block{ID: "20260720000004-hidden1", Box: boxID, Path: "/" + hiddenDocID + ".sy", Content: "hidden"}},
+ {Block: &Block{ID: "20260720000005-protect", Box: boxID, Path: "/" + protectedDocID + ".sy", Content: "protected"}},
+ }
+
+ c, _ := gin.CreateTestContext(httptest.NewRecorder())
+ c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
+ if filtered := FilterEmbedBlocksByPublishAccess(c, publishAccess, embedBlocks); 0 != len(filtered) {
+ t.Fatalf("不可访问的嵌入块结果不应返回:%+v", filtered)
+ }
+
+ c.Request.AddCookie(&http.Cookie{
+ Name: "publish-auth-" + protectedDocID,
+ Value: util.SHA256Hash([]byte(protectedDocID + protectedPassword)),
+ })
+ filtered := FilterEmbedBlocksByPublishAccess(c, publishAccess, embedBlocks)
+ if 1 != len(filtered) || "20260720000005-protect" != filtered[0].Block.ID {
+ t.Fatalf("密码验证后应仅返回已授权结果:%+v", filtered)
+ }
+}
diff --git a/kernel/model/search.go b/kernel/model/search.go
index 405a813d52..b787685cc0 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -226,15 +226,7 @@ func GetEmbedBlockForPublish(embedBlockID string, includeIDs []string, headingMo
}
func getEmbedBlock(embedBlockID string, includeIDs []string, headingMode int, breadcrumb, updateIndex bool) (ret []*EmbedBlock) {
- var validIDs []string
- for _, id := range includeIDs {
- if ast.IsNodeIDPattern(id) {
- validIDs = append(validIDs, id)
- }
- if 1024 <= len(validIDs) {
- break
- }
- }
+ validIDs := validEmbedBlockIDs(includeIDs, 1024)
sqlBlocks := sql.GetBlocks(validIDs)
var existingBlocks []*sql.Block
for _, block := range sqlBlocks {
@@ -257,6 +249,27 @@ func getEmbedBlock(embedBlockID string, includeIDs []string, headingMode int, br
return
}
+func validEmbedBlockIDs(includeIDs []string, limit int) (ret []string) {
+ if 1 > limit {
+ return
+ }
+ seen := map[string]struct{}{}
+ for _, id := range includeIDs {
+ if !ast.IsNodeIDPattern(id) {
+ continue
+ }
+ if _, ok := seen[id]; ok {
+ continue
+ }
+ seen[id] = struct{}{}
+ ret = append(ret, id)
+ if limit <= len(ret) {
+ break
+ }
+ }
+ return
+}
+
func GetQueryEmbedStatement(embedBlockID string) (stmt, boxID string, err error) {
bt := treenode.GetBlockTree(embedBlockID)
if nil == bt {
diff --git a/kernel/model/search_test.go b/kernel/model/search_test.go
index 6293ce44d2..920d8e5dd4 100644
--- a/kernel/model/search_test.go
+++ b/kernel/model/search_test.go
@@ -17,10 +17,21 @@
package model
import (
+ "slices"
"strings"
"testing"
)
+func TestValidEmbedBlockIDs(t *testing.T) {
+ firstID := "20260721120000-block01"
+ secondID := "20260721120001-block02"
+ thirdID := "20260721120002-block03"
+ ids := validEmbedBlockIDs([]string{firstID, "invalid", firstID, secondID, thirdID}, 2)
+ if !slices.Equal(ids, []string{firstID, secondID}) {
+ t.Fatalf("嵌入块 ID 应保持顺序、去重并限制数量:%v", ids)
+ }
+}
+
// TestIsValidSearchBoxPath 覆盖搜索入参的笔记本 ID 与文档路径校验,阻止 SQL 元字符进入语句拼接。
// 回归用例参考 /api/search/fullTextSearchBlock 的 SQL 注入报告(paths[] 投毒)。
func TestIsValidSearchBoxPath(t *testing.T) {