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 reference IDs https://github.com/siyuan-note/siyuan/security/advisories/GHSA-vg99-7gj7-2fr5
Daniel74dbf857cf78
diff --git a/kernel/api/block.go b/kernel/api/block.goindex 8b68978cac..ac7be60915 100644--- a/kernel/api/block.go+++ b/kernel/api/block.go@@ -653,8 +653,7 @@ func getRefIDs(c *gin.Context) {refDefs, originalRefBlockIDs := model.GetBlockRefsInBox(id, encryptedNotebookFromArg(arg))if model.IsReadOnlyRoleContext(c) {publishAccess := model.GetPublishAccess()- publishIgnore := model.GetInvisiblePublishAccess(publishAccess)- refDefs, originalRefBlockIDs = model.FilterRefDefsByPublishIgnore(publishIgnore, refDefs)+ refDefs, originalRefBlockIDs = model.FilterRefDefsByPublishAccess(c, publishAccess, refDefs)}ret.Data = map[string]any{"refDefs": refDefs,diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.goindex f32fda7a14..50bb4258ed 100644--- a/kernel/model/publish_access.go+++ b/kernel/model/publish_access.go@@ -1311,17 +1311,24 @@ func FilterSearchDocsByPublishAccess(c *gin.Context, publishAccess PublishAccessreturn}-func FilterBlockTreesByPublishIgnore(publishIgnore PublishAccess, bts map[string]*treenode.BlockTree) (ret map[string]*treenode.BlockTree) {+func filterBlockTreesByPublishAccess(c *gin.Context, publishAccess PublishAccess, bts map[string]*treenode.BlockTree) (ret map[string]*treenode.BlockTree) {ret = map[string]*treenode.BlockTree{}for id, bt := range bts {- if CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishIgnore) {- ret[id] = bt+ if !CheckBlockTreeDiscoverableByPublishAccess(publishAccess, bt) {+ continue}++ passwordID, password := GetPathPasswordByPublishAccess(bt.BoxID, bt.Path, publishAccess)+ if password != "" && !CheckPublishAuthCookie(c, passwordID, password) {+ continue+ }++ ret[id] = bt}return}-func FilterRefDefsByPublishIgnore(publishIgnore PublishAccess, refDefs []*RefDefs) (retRefDefs []*RefDefs, originalRefBlockIDs map[string]string) {+func FilterRefDefsByPublishAccess(c *gin.Context, publishAccess PublishAccess, refDefs []*RefDefs) (retRefDefs []*RefDefs, originalRefBlockIDs map[string]string) {retRefDefs = []*RefDefs{}IDs := []string{}for _, refDef := range refDefs {@@ -1330,7 +1337,7 @@ func FilterRefDefsByPublishIgnore(publishIgnore PublishAccess, refDefs []*RefDef}IDs = gulu.Str.RemoveDuplicatedElem(IDs)bts := treenode.GetBlockTrees(IDs)- bts = FilterBlockTreesByPublishIgnore(publishIgnore, bts)+ bts = filterBlockTreesByPublishAccess(c, publishAccess, bts)visibles := make(map[string]bool)for _, ID := range IDs {visibles[ID] = falsediff --git a/kernel/model/publish_access_test.go b/kernel/model/publish_access_test.goindex 127f5cc98b..d60434fc8e 100644--- a/kernel/model/publish_access_test.go+++ b/kernel/model/publish_access_test.go@@ -98,6 +98,81 @@ func TestCheckBlockTreeAccessableByPublishAccess(t *testing.T) {}}+func TestFilterBlockTreesByPublishAccess(t *testing.T) {+ const (+ boxID = "20260729000000-boxid01"+ publicID = "20260729000001-public1"+ protectedID = "20260729000002-protect"+ hiddenID = "20260729000003-hidden1"+ privateID = "20260729000004-private"+ forbiddenID = "20260729000005-forbid"+ inconsistentID = "20260729000006-invalid"+ protectedParentID = "20260729000007-parent1"+ protectedChildID = "20260729000008-child01"+ protectedPassword = "protected-password"+ privatePassword = "private-password"+ )++ oldDataDir := util.DataDir+ util.DataDir = t.TempDir()+ invalidateEncryptedPublishAccessCache()+ t.Cleanup(func() {+ util.DataDir = oldDataDir+ invalidateEncryptedPublishAccessCache()+ })++ newBlockTree := func(id, blockPath string) *treenode.BlockTree {+ return &treenode.BlockTree{+ ID: id,+ BoxID: boxID,+ Path: blockPath,+ }+ }+ blockTrees := map[string]*treenode.BlockTree{+ publicID: newBlockTree(publicID, "/"+publicID+".sy"),+ protectedID: newBlockTree(protectedID, "/"+protectedID+".sy"),+ hiddenID: newBlockTree(hiddenID, "/"+hiddenID+".sy"),+ privateID: newBlockTree(privateID, "/"+privateID+".sy"),+ forbiddenID: newBlockTree(forbiddenID, "/"+forbiddenID+".sy"),+ inconsistentID: newBlockTree(inconsistentID, "/"+inconsistentID+".sy"),+ protectedChildID: newBlockTree(protectedChildID, "/"+protectedParentID+"/"+protectedChildID+".sy"),+ "missing-block-id": nil,+ }+ publishAccess := PublishAccess{+ {ID: protectedID, Visible: true, Password: protectedPassword},+ {ID: hiddenID, Visible: false},+ {ID: privateID, Visible: false, Password: privatePassword},+ {ID: forbiddenID, Visible: false, Disable: true},+ {ID: inconsistentID, Visible: true, Disable: true},+ {ID: protectedParentID, Visible: true, Password: protectedPassword},+ }+ c, _ := gin.CreateTestContext(httptest.NewRecorder())+ c.Request = httptest.NewRequest(http.MethodGet, "/", nil)++ filtered := filterBlockTreesByPublishAccess(c, publishAccess, blockTrees)+ if len(filtered) != 1 || filtered[publicID] == nil {+ t.Fatalf("unauthorized publish reader received unexpected block trees: %+v", filtered)+ }++ c.Request.AddCookie(&http.Cookie{+ Name: "publish-auth-" + protectedID,+ Value: util.SHA256Hash([]byte(protectedID + protectedPassword)),+ })+ c.Request.AddCookie(&http.Cookie{+ Name: "publish-auth-" + protectedParentID,+ Value: util.SHA256Hash([]byte(protectedParentID + protectedPassword)),+ })+ c.Request.AddCookie(&http.Cookie{+ Name: "publish-auth-" + privateID,+ Value: util.SHA256Hash([]byte(privateID + privatePassword)),+ })++ filtered = filterBlockTreesByPublishAccess(c, publishAccess, blockTrees)+ if len(filtered) != 3 || filtered[publicID] == nil || filtered[protectedID] == nil || filtered[protectedChildID] == nil {+ t.Fatalf("authorized publish reader received unexpected block trees: %+v", filtered)+ }+}+func TestEncryptedNotebookDeniedByPublishAccess(t *testing.T) {const (boxID = "20260726000000-encrypt"