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 block metadata
Daniel931ba693375e
diff --git a/kernel/api/block.go b/kernel/api/block.goindex bdafc1b9a6..10d231a1ff 100644--- a/kernel/api/block.go+++ b/kernel/api/block.go@@ -68,7 +68,9 @@ func getBlockTreeInfos(c *gin.Context) {ids = append(ids, id.(string))}- ret.Data = model.GetBlockTreeInfosInBox(ids, encryptedNotebookFromArg(arg))+ boxID := encryptedNotebookFromArg(arg)+ ids = filterBlockIDsByPublishAccess(c, ids, boxID)+ ret.Data = model.GetBlockTreeInfosInBox(ids, boxID)}func getBlockSiblingID(c *gin.Context) {@@ -81,7 +83,17 @@ func getBlockSiblingID(c *gin.Context) {}id := arg["id"].(string)- parent, previous, next := model.GetBlockSiblingIDInBox(id, encryptedNotebookFromArg(arg))+ boxID := encryptedNotebookFromArg(arg)+ if !isBlockPublishAccessible(c, id, boxID) {+ ret.Data = map[string]string{+ "parent": "",+ "next": "",+ "previous": "",+ }+ return+ }++ parent, previous, next := model.GetBlockSiblingIDInBox(id, boxID)ret.Data = map[string]string{"parent": parent,"next": next,@@ -99,7 +111,17 @@ func getBlockRelevantIDs(c *gin.Context) {}id := arg["id"].(string)- parentID, previousID, nextID, err := model.GetBlockRelevantIDsInBox(id, encryptedNotebookFromArg(arg))+ boxID := encryptedNotebookFromArg(arg)+ if !isBlockPublishAccessible(c, id, boxID) {+ ret.Data = map[string]string{+ "parentID": "",+ "previousID": "",+ "nextID": "",+ }+ return+ }++ parentID, previousID, nextID, err := model.GetBlockRelevantIDsInBox(id, boxID)if nil != err {ret.Code = -1ret.Msg = err.Error()@@ -415,14 +437,15 @@ func getDocInfo(c *gin.Context) {}id := arg["id"].(string)- if !checkBlockPublishAccess(c, id, ret) {+ boxID := encryptedNotebookFromArg(arg)+ if !checkBlockPublishAccessInBox(c, id, boxID, ret) {return}var info *model.BlockInfovar err error- if notebook, ok := arg["notebook"].(string); ok && notebook != "" && model.IsEncryptedBox(notebook) {- info, err = model.GetDocInfoInBox(id, notebook)+ if boxID != "" {+ info, err = model.GetDocInfoInBox(id, boxID)} else {info, err = model.GetDocInfo(id)}@@ -575,10 +598,16 @@ func getRefText(c *gin.Context) {return}+ boxID := encryptedNotebookFromArg(arg)+ if !isBlockPublishAccessible(c, id, boxID) {+ ret.Data = model.ErrBlockNotFound.Error()+ return+ }+// 加密笔记本的块引解析走 InBox 版(查加密 blocktree + content db)var refText string- if notebook, ok := arg["notebook"].(string); ok && notebook != "" && model.IsEncryptedBox(notebook) {- refText = model.GetBlockRefTextInBox(id, notebook)+ if boxID != "" {+ refText = model.GetBlockRefTextInBox(id, boxID)} else {refText = model.GetBlockRefText(id)}@@ -691,10 +720,16 @@ func getBlockBreadcrumb(c *gin.Context) {}}+ boxID := encryptedNotebookFromArg(arg)+ if !isBlockPublishAccessible(c, id, boxID) {+ ret.Data = []*model.BlockPath{}+ return+ }+var blockPath []*model.BlockPathvar err error- if notebook, ok := arg["notebook"].(string); ok && notebook != "" && model.IsEncryptedBox(notebook) {- blockPath, err = model.BuildBlockBreadcrumbInBox(id, excludeTypes, notebook)+ if boxID != "" {+ blockPath, err = model.BuildBlockBreadcrumbInBox(id, excludeTypes, boxID)} else {blockPath, err = model.BuildBlockBreadcrumb(id, excludeTypes)}@@ -755,15 +790,16 @@ func getBlockInfo(c *gin.Context) {if util.InvalidIDPattern(id, ret) {return}- if !checkBlockPublishAccess(c, id, ret) {+ boxID := encryptedNotebookFromArg(arg)+ if !checkBlockPublishAccessInBox(c, id, boxID, ret) {return}// 仅在此处使用带重建索引的加载函数,其他地方不要使用var tree *parse.Treevar err error- if notebook, ok := arg["notebook"].(string); ok && notebook != "" && model.IsEncryptedBox(notebook) {- tree, err = model.LoadTreeByBlockIDWithReindexInBox(id, notebook)+ if boxID != "" {+ tree, err = model.LoadTreeByBlockIDWithReindexInBox(id, boxID)} else {tree, err = model.LoadTreeByBlockIDWithReindex(id)}@@ -835,12 +871,11 @@ func getBlockInfo(c *gin.Context) {}func checkBlockPublishAccess(c *gin.Context, id string, ret *gulu.Result) bool {- if !model.IsReadOnlyRoleContext(c) {- return true- }+ return checkBlockPublishAccessInBox(c, id, "", ret)+}- publishAccess := model.GetPublishAccess()- if model.CheckBlockIdAccessableByPublishAccess(c, publishAccess, id) {+func checkBlockPublishAccessInBox(c *gin.Context, id, boxID string, ret *gulu.Result) bool {+ if isBlockPublishAccessible(c, id, boxID) {return true}@@ -849,6 +884,29 @@ func checkBlockPublishAccess(c *gin.Context, id string, ret *gulu.Result) bool {return false}+func isBlockPublishAccessible(c *gin.Context, id, boxID string) bool {+ if !model.IsReadOnlyRoleContext(c) {+ return true+ }++ return model.CheckBlockIdAccessableByPublishAccessInBox(c, model.GetPublishAccess(), id, boxID)+}++func filterBlockIDsByPublishAccess(c *gin.Context, ids []string, boxID string) []string {+ if !model.IsReadOnlyRoleContext(c) {+ return ids+ }++ publishAccess := model.GetPublishAccess()+ ret := make([]string, 0, len(ids))+ for _, id := range ids {+ if model.CheckBlockIdAccessableByPublishAccessInBox(c, publishAccess, id, boxID) {+ ret = append(ret, id)+ }+ }+ return ret+}+func getBlockDOM(c *gin.Context) {ret := gulu.Ret.NewResult()defer c.JSON(http.StatusOK, ret)diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.goindex 0b07ce815d..2afe2f3081 100644--- a/kernel/model/publish_access.go+++ b/kernel/model/publish_access.go@@ -217,7 +217,11 @@ func GetPathPasswordByPublishAccess(box string, blockPath string, publishAccess}func CheckBlockIdAccessableByPublishAccess(c *gin.Context, publishAccess PublishAccess, blockID string) bool {- bt := treenode.GetBlockTree(blockID)+ return CheckBlockIdAccessableByPublishAccessInBox(c, publishAccess, blockID, "")+}++func CheckBlockIdAccessableByPublishAccessInBox(c *gin.Context, publishAccess PublishAccess, blockID, boxID string) bool {+ bt := treenode.GetBlockTreeInBox(blockID, boxID)return checkBlockTreeAccessableByPublishAccess(c, publishAccess, bt)}diff --git a/kernel/model/publish_access_sqlcipher_test.go b/kernel/model/publish_access_sqlcipher_test.gonew file mode 100644index 0000000000..49a9690c99--- /dev/null+++ b/kernel/model/publish_access_sqlcipher_test.go@@ -0,0 +1,81 @@+// SiYuan - Refactor your thinking+// Copyright (c) 2020-present, 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/>.++//go:build sqlcipher || libsqlcipher++package model++import (+ "net/http"+ "net/http/httptest"+ "testing"++ "github.com/gin-gonic/gin"+ "github.com/siyuan-note/siyuan/kernel/treenode"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestCheckBlockIdAccessableByPublishAccessInBox(t *testing.T) {+ const (+ boxID = "20260724000000-boxid01"+ otherBoxID = "20260724000001-boxid02"+ docID = "20260724000002-docid01"+ docPassword = "password"+ )++ originalTempDir := util.TempDir+ util.TempDir = t.TempDir()+ t.Cleanup(func() {+ treenode.CloseEncryptedBlockTreeDB(boxID)+ treenode.CloseEncryptedBlockTreeDB(otherBoxID)+ util.TempDir = originalTempDir+ })++ dek := make([]byte, 32)+ if err := treenode.OpenEncryptedBlockTreeDB(boxID, dek); err != nil {+ t.Fatal(err)+ }+ if err := treenode.OpenEncryptedBlockTreeDB(otherBoxID, dek); err != nil {+ t.Fatal(err)+ }++ tree := treenode.NewTree(boxID, "/"+docID+".sy", "/Document", "Document")+ treenode.UpsertBlockTree(tree)++ c, _ := gin.CreateTestContext(httptest.NewRecorder())+ c.Request = httptest.NewRequest(http.MethodGet, "/", nil)+ if !CheckBlockIdAccessableByPublishAccessInBox(c, PublishAccess{}, docID, boxID) {+ t.Fatal("public document in an encrypted notebook should be accessible")+ }+ if CheckBlockIdAccessableByPublishAccessInBox(c, PublishAccess{}, docID, otherBoxID) {+ t.Fatal("block lookup should not cross the requested notebook boundary")+ }+ if CheckBlockIdAccessableByPublishAccessInBox(c, PublishAccess{{ID: docID, Disable: true}}, docID, boxID) {+ t.Fatal("publish-disabled document in an encrypted notebook should not be accessible")+ }++ protectedAccess := PublishAccess{{ID: docID, Visible: true, Password: docPassword}}+ if CheckBlockIdAccessableByPublishAccessInBox(c, protectedAccess, docID, boxID) {+ t.Fatal("password-protected document in an encrypted notebook should require authorization")+ }+ c.Request.AddCookie(&http.Cookie{+ Name: "publish-auth-" + docID,+ Value: util.SHA256Hash([]byte(docID + docPassword)),+ })+ if !CheckBlockIdAccessableByPublishAccessInBox(c, protectedAccess, docID, boxID) {+ t.Fatal("password-protected document in an encrypted notebook should be accessible after authorization")+ }+}