Skip to content

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

:lock: Enforce publish access for block metadata and existence endpoints https://github.com/siyuan-note/siyuan/security/advisories/GHSA-4vpg-gwqq-w44c

Danielbd067a4fe9b2
diff --git a/kernel/api/block.go b/kernel/api/block.go
index e685113d3a..2048bb945a 100644
--- a/kernel/api/block.go
+++ b/kernel/api/block.go
@@ -95,6 +95,11 @@ func checkBlockRef(c *gin.Context) {
if "" != notebook && util.InvalidIDPattern(notebook, ret) {
return
}
+ if "" != notebook && !holdBlockRequest(c, ret, notebook) {
+ return
+ }
+ ids = filterBlockIDsByPublishAccess(c, ids, notebook)
+ exactIDs = filterBlockIDsByPublishAccess(c, exactIDs, notebook)
var err error
ret.Data, err = model.CheckBlockRefInBox(ids, exactIDs, notebook)
if err != nil {
@@ -106,6 +111,20 @@ func checkBlockRef(c *gin.Context) {
if !parsed {
return
}
+ if model.IsReadOnlyRoleContext(c) {
+ publishAccess := model.GetPublishAccess()
+ var accessiblePaths []string
+ for _, p := range paths {
+ if model.CheckBlockIdAccessableByPublishAccess(c, publishAccess, util.GetTreeID(p)) {
+ accessiblePaths = append(accessiblePaths, p)
+ }
+ }
+ paths = accessiblePaths
+ }
+ if 0 == len(paths) {
+ ret.Data = false
+ return
+ }
var err error
ret.Data, err = model.CheckDocsRef(paths)
if err != nil {
@@ -120,6 +139,23 @@ func checkBlockRef(c *gin.Context) {
if util.InvalidIDPattern(notebook, ret) {
return
}
+ if !holdBlockRequest(c, ret, notebook) {
+ return
+ }
+ if model.IsReadOnlyRoleContext(c) {
+ publishAccess := model.GetPublishAccess()
+ accessible := false
+ for _, rootID := range treenode.GetRootBlockIDsByBoxID(notebook) {
+ if model.CheckBlockIdAccessableByPublishAccessInBox(c, publishAccess, rootID, notebook) {
+ accessible = true
+ break
+ }
+ }
+ if !accessible {
+ ret.Data = false
+ return
+ }
+ }
var err error
ret.Data, err = model.CheckNotebookRef(notebook)
if err != nil {
@@ -500,6 +536,16 @@ func getUnfoldedParentID(c *gin.Context) {
}
id := arg["id"].(string)
+ boxID := encryptedNotebookFromArg(arg)
+ if !holdBlockRequest(c, ret, boxID) {
+ return
+ }
+ if !isBlockPublishAccessible(c, id, boxID) {
+ ret.Data = map[string]any{
+ "parentID": "",
+ }
+ return
+ }
parentID := model.GetUnfoldedParentID(id)
ret.Data = map[string]any{
"parentID": parentID,
@@ -516,6 +562,17 @@ func checkBlockFold(c *gin.Context) {
}
id := arg["id"].(string)
+ boxID := encryptedNotebookFromArg(arg)
+ if !holdBlockRequest(c, ret, boxID) {
+ return
+ }
+ if !isBlockPublishAccessible(c, id, boxID) {
+ ret.Data = map[string]any{
+ "isFolded": false,
+ "isRoot": false,
+ }
+ return
+ }
isFolded, isRoot := model.IsBlockFolded(id)
ret.Data = map[string]any{
"isFolded": isFolded,
@@ -533,6 +590,14 @@ func checkBlockExist(c *gin.Context) {
}
id := arg["id"].(string)
+ boxID := encryptedNotebookFromArg(arg)
+ if !holdBlockRequest(c, ret, boxID) {
+ return
+ }
+ if !isBlockPublishAccessible(c, id, boxID) {
+ ret.Data = false
+ return
+ }
ret.Data = treenode.ExistBlockTree(id)
}
@@ -552,7 +617,12 @@ func checkBlocksExist(c *gin.Context) {
ids = append(ids, id)
}
}
- ret.Data = treenode.ExistBlockTrees(ids)
+ boxID := encryptedNotebookFromArg(arg)
+ if !holdBlockRequest(c, ret, boxID) {
+ return
+ }
+ ids = filterBlockIDsByPublishAccess(c, ids, boxID)
+ ret.Data = treenode.ExistBlockTreesInBox(ids, boxID)
}
func getDocInfo(c *gin.Context) {
@@ -680,6 +750,11 @@ func getBlocksWordCount(c *gin.Context) {
for _, id := range idsArg {
ids = append(ids, id.(string))
}
+ boxID := encryptedNotebookFromArg(arg)
+ if !holdBlockRequest(c, ret, boxID) {
+ return
+ }
+ ids = filterBlockIDsByPublishAccess(c, ids, boxID)
ret.Data = map[string]any{
"reqId": arg["reqId"],
"stat": filesys.BlocksWordCount(ids),
@@ -696,6 +771,17 @@ func getTreeStat(c *gin.Context) {
}
id := arg["id"].(string)
+ boxID := encryptedNotebookFromArg(arg)
+ if !holdBlockRequest(c, ret, boxID) {
+ return
+ }
+ if !isBlockPublishAccessible(c, id, boxID) {
+ ret.Data = map[string]any{
+ "reqId": arg["reqId"],
+ "stat": nil,
+ }
+ return
+ }
ret.Data = map[string]any{
"reqId": arg["reqId"],
"stat": filesys.StatTree(id),
@@ -953,6 +1039,14 @@ func getBlockIndex(c *gin.Context) {
}
id := arg["id"].(string)
+ boxID := encryptedNotebookFromArg(arg)
+ if !holdBlockRequest(c, ret, boxID) {
+ return
+ }
+ if !isBlockPublishAccessible(c, id, boxID) {
+ ret.Data = 0
+ return
+ }
index := model.GetBlockIndex(id)
ret.Data = index
}
@@ -971,6 +1065,11 @@ func getBlocksIndexes(c *gin.Context) {
for _, id := range idsArg {
ids = append(ids, id.(string))
}
+ boxID := encryptedNotebookFromArg(arg)
+ if !holdBlockRequest(c, ret, boxID) {
+ return
+ }
+ ids = filterBlockIDsByPublishAccess(c, ids, boxID)
index := model.GetBlocksIndexes(ids)
ret.Data = index
}
diff --git a/kernel/api/block_test.go b/kernel/api/block_test.go
index 673a000fbd..14376c6ef0 100644
--- a/kernel/api/block_test.go
+++ b/kernel/api/block_test.go
@@ -146,6 +146,109 @@ type docBlocksOrdersResponse struct {
Data json.RawMessage `json:"data"`
}
+func TestBlockPublishAccessGuards(t *testing.T) {
+ const (
+ boxID = "20260724000000-boxid03"
+ publicID = "20260724000020-public3"
+ disabledID = "20260724000021-disable"
+ protectedID = "20260724000022-protect"
+ protectedPassword = "password"
+ )
+
+ previousBlockTreeDBPath := util.BlockTreeDBPath
+ previousDataDir := util.DataDir
+ util.DataDir = t.TempDir()
+ util.BlockTreeDBPath = filepath.Join(util.DataDir, "blocktree.db")
+ treenode.InitBlockTree(true)
+ previousPublishAccess := model.GetPublishAccess()
+ if err := model.SetPublishAccess(model.PublishAccess{
+ {ID: disabledID, Visible: true, Disable: true},
+ {ID: protectedID, Visible: true, Password: protectedPassword},
+ }); err != nil {
+ t.Fatalf("set publish access failed: %v", err)
+ }
+ t.Cleanup(func() {
+ _ = model.SetPublishAccess(previousPublishAccess)
+ treenode.CloseDatabase()
+ util.BlockTreeDBPath = previousBlockTreeDBPath
+ util.DataDir = previousDataDir
+ })
+
+ for _, id := range []string{publicID, disabledID, protectedID} {
+ treenode.IndexBlockTree(&parse.Tree{
+ ID: id,
+ Box: boxID,
+ Path: "/" + id + ".sy",
+ Root: &ast.Node{ID: id, Type: ast.NodeDocument},
+ })
+ }
+
+ blockGuardRequest := func(role any, body string, handler gin.HandlerFunc) map[string]any {
+ gin.SetMode(gin.TestMode)
+ engine := gin.New()
+ engine.Use(func(c *gin.Context) {
+ c.Set(model.RoleContextKey, role)
+ c.Next()
+ })
+ engine.POST("/api/block/block", handler)
+
+ recorder := httptest.NewRecorder()
+ request := httptest.NewRequest(http.MethodPost, "/api/block/block", strings.NewReader(body))
+ request.Header.Set("Content-Type", "application/json")
+ engine.ServeHTTP(recorder, request)
+
+ var response map[string]any
+ if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
+ t.Fatalf("unmarshal response failed: %v", err)
+ }
+ return response
+ }
+
+ t.Run("reader", func(t *testing.T) {
+ if response := blockGuardRequest(model.RoleReader, `{"id":"`+disabledID+`"}`, checkBlockExist); response["data"].(bool) {
+ t.Fatalf("disabled block should be hidden from the reader, got %v", response["data"])
+ }
+ if response := blockGuardRequest(model.RoleReader, `{"id":"`+publicID+`"}`, checkBlockExist); !response["data"].(bool) {
+ t.Fatalf("public block should be visible to the reader, got %v", response["data"])
+ }
+ response := blockGuardRequest(model.RoleReader, `{"ids":["`+publicID+`","`+disabledID+`"]}`, checkBlocksExist)
+ if !response["data"].(map[string]any)[publicID].(bool) {
+ t.Fatalf("unexpected block existence for the reader: %v", response["data"])
+ }
+ if _, exists := response["data"].(map[string]any)[disabledID]; exists {
+ t.Fatalf("disabled block existence should be hidden from the reader: %v", response["data"])
+ }
+ if response := blockGuardRequest(model.RoleReader, `{"id":"`+disabledID+`"}`, checkBlockFold); response["data"].(map[string]any)["isFolded"].(bool) ||
+ response["data"].(map[string]any)["isRoot"].(bool) {
+ t.Fatalf("fold state of a disabled block should be hidden from the reader: %v", response["data"])
+ }
+ if response := blockGuardRequest(model.RoleReader, `{"id":"`+disabledID+`"}`, getUnfoldedParentID); "" != response["data"].(map[string]any)["parentID"].(string) {
+ t.Fatalf("unfolded parent of a disabled block should be hidden from the reader: %v", response["data"])
+ }
+ if response := blockGuardRequest(model.RoleReader, `{"id":"`+disabledID+`"}`, getBlockIndex); 0 != int(response["data"].(float64)) {
+ t.Fatalf("index of a disabled block should be hidden from the reader: %v", response["data"])
+ }
+ if response := blockGuardRequest(model.RoleReader, `{"id":"`+disabledID+`"}`, getTreeStat); nil != response["data"].(map[string]any)["stat"] {
+ t.Fatalf("tree stat of a disabled block should be hidden from the reader: %v", response["data"])
+ }
+ if response := blockGuardRequest(model.RoleReader, `{"ids":["`+publicID+`","`+disabledID+`"]}`, getBlocksWordCount); 0 != int(response["code"].(float64)) {
+ t.Fatalf("word count of disabled blocks should not error, got %v", response)
+ }
+ if response := blockGuardRequest(model.RoleReader, `{"ids":["`+publicID+`","`+disabledID+`"]}`, getBlocksIndexes); nil != response["data"].(map[string]any)[disabledID] {
+ t.Fatalf("indexes of disabled blocks should be hidden from the reader: %v", response["data"])
+ }
+ if response := blockGuardRequest(model.RoleReader, `{"ids":["`+disabledID+`"]}`, checkBlockRef); response["data"].(bool) {
+ t.Fatalf("ref check of a disabled block should be hidden from the reader: %v", response["data"])
+ }
+ })
+
+ t.Run("administrator", func(t *testing.T) {
+ if response := blockGuardRequest(model.RoleAdministrator, `{"id":"`+disabledID+`"}`, checkBlockExist); !response["data"].(bool) {
+ t.Fatalf("disabled block should be visible to the administrator, got %v", response["data"])
+ }
+ })
+}
+
func postDocBlocksOrders(t *testing.T, body string) *docBlocksOrdersResponse {
t.Helper()