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: Filter publish-inaccessible backlink block IDs on reader endpoints https://github.com/siyuan-note/siyuan/security/advisories/GHSA-v758-8w88-pfr2
Danielb3706cba222b
diff --git a/kernel/api/block_doc_info_publish_test.go b/kernel/api/block_doc_info_publish_test.gonew file mode 100644index 0000000000..d6500f32b4--- /dev/null+++ b/kernel/api/block_doc_info_publish_test.go@@ -0,0 +1,227 @@+// SiYuan - From thought to insight, with agents+// 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/>.++package api++import (+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "os"+ "path/filepath"+ "strings"+ "testing"++ "github.com/88250/lute/ast"+ "github.com/gin-gonic/gin"+ "github.com/siyuan-note/siyuan/kernel/conf"+ "github.com/siyuan-note/siyuan/kernel/filesys"+ "github.com/siyuan-note/siyuan/kernel/model"+ "github.com/siyuan-note/siyuan/kernel/sql"+ "github.com/siyuan-note/siyuan/kernel/treenode"+ "github.com/siyuan-note/siyuan/kernel/util"+)++// refIDsDocInfo 只保留断言需要的字段,getDocInfo 下发单个对象,getDocsInfo 下发数组。+type refIDsDocInfo struct {+ ID string `json:"id"`+ RefCount int `json:"refCount"`+ RefIDs []string `json:"refIDs"`+}++func parseRefIDsDocInfos(t *testing.T, name, body string) []refIDsDocInfo {+ t.Helper()++ response := &struct {+ Code int `json:"code"`+ Data json.RawMessage `json:"data"`+ }{}+ if err := json.Unmarshal([]byte(body), response); nil != err {+ t.Fatalf("unmarshal %s response failed: %v, body: %s", name, err, body)+ }+ if 0 != response.Code {+ t.Fatalf("unexpected %s response code [%d]: %s", name, response.Code, body)+ }++ var single refIDsDocInfo+ if err := json.Unmarshal(response.Data, &single); nil == err {+ return []refIDsDocInfo{single}+ }+ var batch []refIDsDocInfo+ if err := json.Unmarshal(response.Data, &batch); nil != err {+ t.Fatalf("unmarshal %s response data failed: %v, body: %s", name, err, body)+ }+ return batch+}++// TestGetDocInfoRefIDsPublishAccess 验证读者请求发布可见文档时,反链块 ID 与引用数+// 不包含禁止发布、密码保护文档中的引用块,同时保留读者可见的反链计数。+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-v758-8w88-pfr2+func TestGetDocInfoRefIDsPublishAccess(t *testing.T) {+ previousConf := model.Conf+ previousWorkspaceDir, previousConfDir, previousDataDir := util.WorkspaceDir, util.ConfDir, util.DataDir+ previousTempDir, previousDBPath, previousBlockTreeDBPath := util.TempDir, util.DBPath, util.BlockTreeDBPath+ previousHistoryDBPath, previousAssetContentDBPath := util.HistoryDBPath, util.AssetContentDBPath++ root := t.TempDir()+ t.Cleanup(func() {+ sql.CloseDatabase()+ treenode.CloseDatabase()+ model.Conf = previousConf+ util.WorkspaceDir, util.ConfDir, util.DataDir = previousWorkspaceDir, previousConfDir, previousDataDir+ util.TempDir, util.DBPath, util.BlockTreeDBPath = previousTempDir, previousDBPath, previousBlockTreeDBPath+ util.HistoryDBPath, util.AssetContentDBPath = previousHistoryDBPath, previousAssetContentDBPath+ })++ util.WorkspaceDir = root+ util.ConfDir = filepath.Join(root, "conf")+ util.DataDir = filepath.Join(root, "data")+ util.TempDir = filepath.Join(root, "temp")+ util.DBPath = filepath.Join(util.TempDir, "siyuan.db")+ util.HistoryDBPath = filepath.Join(util.TempDir, "history.db")+ util.AssetContentDBPath = filepath.Join(util.TempDir, "asset_content.db")+ util.BlockTreeDBPath = filepath.Join(util.TempDir, "blocktree.db")+ for _, dir := range []string{util.ConfDir, util.DataDir, util.TempDir} {+ if err := os.MkdirAll(dir, 0755); err != nil {+ t.Fatal(err)+ }+ }++ model.Conf = model.NewAppConf()+ model.Conf.Lang = "en"+ model.Conf.FileTree = conf.NewFileTree()+ model.Conf.Editor = conf.NewEditor()+ model.Conf.Export = conf.NewExport()+ model.Conf.Search = conf.NewSearch()+ model.Conf.NotebookCrypto = conf.NewNotebookCrypto()+ model.Conf.Sync = conf.NewSync()++ const (+ boxID = "20260919000000-boxid01"+ publicDocID = "20260919000001-public1"+ publicBlockID = "20260919000002-publblk1"+ disabledDocID = "20260919000003-disable"+ disabledBlockID = "20260919000004-disblk1"+ protectedDocID = "20260919000005-protect"+ protectedBlockID = "20260919000006-protblk"+ visibleDocID = "20260919000007-visible"+ visibleBlockID = "20260919000008-visblk1"+ protectedPassword = "protected-password"+ )++ box := &model.Box{ID: boxID}+ boxConf := conf.NewBoxConf()+ boxConf.Name = "Publish ref IDs"+ boxConf.Closed = false+ if err := box.SaveConf(boxConf); err != nil {+ t.Fatal(err)+ }++ sql.InitDatabase(true)+ sql.InitHistoryDatabase(true)+ sql.InitAssetContentDatabase(true)+ treenode.InitBlockTree(true)++ previousPublishAccess := model.GetPublishAccess()+ if err := model.SetPublishAccess(model.PublishAccess{+ {ID: disabledDocID, Disable: true},+ {ID: protectedDocID, Password: protectedPassword},+ }); err != nil {+ t.Fatalf("set publish access failed: %v", err)+ }+ t.Cleanup(func() { _ = model.SetPublishAccess(previousPublishAccess) })++ // 建立文档树:禁止发布与密码保护文档中的段落反链公开文档中的段落,+ // 另有发布可见文档中的段落同样反链公开文档,用于确认可见反链未被一并过滤+ addDoc := func(docID, title, blockID, refDefID, content string) {+ tree := treenode.NewTree(boxID, "/"+docID+".sy", "/"+title, title)+ tree.Root.FirstChild.Unlink()+ paragraph := &ast.Node{Type: ast.NodeParagraph, ID: blockID}+ paragraph.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(content)})+ if "" != refDefID {+ paragraph.AppendChild(&ast.Node{+ Type: ast.NodeTextMark,+ TextMarkType: "block-ref",+ TextMarkBlockRefID: refDefID,+ TextMarkBlockRefSubtype: "s",+ TextMarkTextContent: refDefID,+ })+ }+ tree.Root.AppendChild(paragraph)+ treenode.IndexBlockTree(tree)+ if _, err := filesys.WriteTree(tree); err != nil {+ t.Fatal(err)+ }+ sql.IndexTreeQueue(tree)+ }++ addDoc(publicDocID, "Public", publicBlockID, "", "public content")+ addDoc(disabledDocID, "Disabled", disabledBlockID, publicBlockID, "disabled secret ")+ addDoc(protectedDocID, "Password", protectedBlockID, publicBlockID, "password secret ")+ addDoc(visibleDocID, "Visible", visibleBlockID, publicBlockID, "visible content ")+ sql.FlushQueue()++ gin.SetMode(gin.TestMode)+ engine := gin.New()+ engine.Use(boxLeaseMiddleware)+ engine.Use(func(c *gin.Context) { c.Set(model.RoleContextKey, model.RoleReader); c.Next() })+ engine.POST("/api/block/getDocInfo", getDocInfo)+ engine.POST("/api/block/getDocsInfo", getDocsInfo)++ post := func(path, body string) string {+ recorder := httptest.NewRecorder()+ request := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))+ request.Header.Set("Content-Type", "application/json")+ engine.ServeHTTP(recorder, request)+ return recorder.Body.String()+ }++ // 两个端点下发给读者的反链都必须剔除不可访问文档中的块,只保留发布可见的反链+ for _, testCase := range []struct {+ name string+ endpoint string+ body string+ }{+ {"getDocInfo", "/api/block/getDocInfo", `{"id":"` + publicDocID + `"}`},+ {"getDocsInfo", "/api/block/getDocsInfo", `{"ids":["` + publicDocID + `"],"refCount":true,"av":true}`},+ } {+ body := post(testCase.endpoint, testCase.body)+ docInfos := parseRefIDsDocInfos(t, testCase.name, body)+ if 1 != len(docInfos) {+ t.Fatalf("unexpected %s response: %s", testCase.name, body)+ }++ docInfo := docInfos[0]+ for _, hiddenRefID := range []string{disabledBlockID, protectedBlockID} {+ if strings.Contains(body, hiddenRefID) {+ t.Fatalf("%s disclosed block ID [%s] of an inaccessible document: %s", testCase.name, hiddenRefID, body)+ }+ }+ if 1 != len(docInfo.RefIDs) || visibleBlockID != docInfo.RefIDs[0] {+ t.Fatalf("%s ref IDs = %v, want only the published backlink [%s]: %s",+ testCase.name, docInfo.RefIDs, visibleBlockID, body)+ }+ if 1 != docInfo.RefCount {+ t.Fatalf("%s ref count = %d, want only the published backlink counted: %s", testCase.name, docInfo.RefCount, body)+ }+ }++ // 读者请求禁止发布文档本身仍应被拒绝+ body := post("/api/block/getDocInfo", `{"id":"`+disabledDocID+`"}`)+ if !strings.Contains(body, `"code":-1`) {+ t.Fatalf("reader should not read a publish-disabled document: %s", body)+ }+}diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.goindex b69a101d09..4eff696e0e 100644--- a/kernel/model/publish_access.go+++ b/kernel/model/publish_access.go@@ -1276,6 +1276,10 @@ func FilterBlockInfoByPublishAccess(c *gin.Context, publishAccess PublishAccess,return}+ // 反链块 ID 逐个按发布访问过滤,避免读者据此获知禁止发布、密码保护文档中的引用块+ ret.RefIDs = FilterRefIDsByPublishAccess(c, publishAccess, ret.RefIDs)+ ret.RefCount = len(ret.RefIDs)+publishIgnore := GetDisablePublishAccess(publishAccess)filteredAttrViews := []*AttrView{}avIDs := []string{}diff --git a/kernel/model/publish_access_test.go b/kernel/model/publish_access_test.goindex ab7d17743d..6ab2bec6bb 100644--- a/kernel/model/publish_access_test.go+++ b/kernel/model/publish_access_test.go@@ -1933,3 +1933,124 @@ func TestFilterBlockInfoByPublishAccessFiltersSubFileCount(t *testing.T) {t.Fatalf("missing document subfile count = %d, want 0", missing.SubFileCount)}}++// TestFilterBlockInfoByPublishAccessFiltersRefIDs 验证读者拿到的反链块 ID 与引用数+// 不包含禁止发布、密码保护文档中的引用块,同时保留发布可见的反链。+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-v758-8w88-pfr2+func TestFilterBlockInfoByPublishAccessFiltersRefIDs(t *testing.T) {+ const (+ boxID = "20260919000000-boxid01"+ publicID = "20260919000001-public1"+ visibleRefID = "20260919000002-visref1"+ hiddenRefID = "20260919000003-hidref1"+ disabledRefID = "20260919000004-disref1"+ protectedRefID = "20260919000005-protref1"+ disabledDocID = "20260919000006-disable"+ protectedDocID = "20260919000007-protect"+ protectedPassword = "protected-password"+ )++ previousDataDir, previousBlockTreeDBPath, previousConf := util.DataDir, util.BlockTreeDBPath, Conf+ util.DataDir = t.TempDir()+ util.BlockTreeDBPath = filepath.Join(util.DataDir, "blocktree.db")+ Conf = NewAppConf()+ Conf.FileTree = conf.NewFileTree()+ treenode.InitBlockTree(true)+ t.Cleanup(func() {+ treenode.CloseDatabase()+ util.DataDir, util.BlockTreeDBPath, Conf = previousDataDir, previousBlockTreeDBPath, previousConf+ })++ boxDir := filepath.Join(util.DataDir, boxID)+ if err := os.MkdirAll(boxDir, 0755); err != nil {+ t.Fatal(err)+ }+ for _, id := range []string{publicID, visibleRefID, hiddenRefID, disabledRefID, protectedRefID, disabledDocID, protectedDocID} {+ if err := os.WriteFile(filepath.Join(boxDir, id+".sy"), []byte(`{"Properties":{}}`), 0644); err != nil {+ t.Fatal(err)+ }+ }++ // 禁止发布、密码保护文档中的引用块位于这两个文档路径下+ for _, target := range []struct{ id, path string }{+ {publicID, "/" + publicID + ".sy"},+ {visibleRefID, "/" + visibleRefID + ".sy"},+ {hiddenRefID, "/" + hiddenRefID + ".sy"},+ {disabledRefID, "/" + disabledDocID + "/" + disabledRefID + ".sy"},+ {protectedRefID, "/" + protectedDocID + "/" + protectedRefID + ".sy"},+ {disabledDocID, "/" + disabledDocID + ".sy"},+ {protectedDocID, "/" + protectedDocID + ".sy"},+ } {+ treenode.IndexBlockTree(&parse.Tree{+ ID: target.id,+ Box: boxID,+ Path: target.path,+ HPath: "/" + target.id,+ Root: &ast.Node{ID: target.id, Type: ast.NodeDocument},+ })+ }++ publishAccess := PublishAccess{+ {ID: disabledDocID, Visible: true, Disable: true},+ {ID: protectedDocID, Visible: true, Password: protectedPassword},+ }+ c, _ := gin.CreateTestContext(httptest.NewRecorder())+ c.Request = httptest.NewRequest(http.MethodPost, "/", nil)+ c.Set(RoleContextKey, RoleReader)++ // RefIDs 模拟 blockinfo 已按未过滤口径查出的反链块 ID+ info := &BlockInfo{+ ID: publicID,+ RootID: publicID,+ RefIDs: []string{visibleRefID, hiddenRefID, disabledRefID, protectedRefID},+ RefCount: 4,+ IAL: map[string]string{},+ }+ filtered := FilterBlockInfoByPublishAccess(c, publishAccess, info)+ if 2 != len(filtered.RefIDs) || visibleRefID != filtered.RefIDs[0] || hiddenRefID != filtered.RefIDs[1] {+ t.Fatalf("reader reference IDs = %v, want the published and invisible backlinks", filtered.RefIDs)+ }+ if 2 != filtered.RefCount {+ t.Fatalf("reader reference count = %d, want the filtered count", filtered.RefCount)+ }++ // 授权后密码保护文档的反链可见,禁止发布文档的反链仍不可见+ c.Request.AddCookie(&http.Cookie{+ Name: "publish-auth-" + protectedDocID,+ Value: util.SHA256Hash([]byte(protectedDocID + protectedPassword)),+ })+ filtered = FilterBlockInfoByPublishAccess(c, publishAccess, &BlockInfo{+ ID: publicID,+ RootID: publicID,+ RefIDs: []string{visibleRefID, hiddenRefID, disabledRefID, protectedRefID},+ RefCount: 4,+ IAL: map[string]string{},+ })+ if 3 != len(filtered.RefIDs) || 3 != filtered.RefCount {+ t.Fatalf("authorized reader reference IDs = %v, count = %d, want the password protected backlink added",+ filtered.RefIDs, filtered.RefCount)+ }++ // 文档路径无法定位时不下发下级文档数,反链块 ID 仍按逐元素过滤结果给出+ missing := FilterBlockInfoByPublishAccess(c, publishAccess, &BlockInfo{+ ID: "20260919000008-missing",+ RootID: "20260919000008-missing",+ SubFileCount: 5,+ RefIDs: []string{visibleRefID, disabledRefID},+ RefCount: 2,+ IAL: map[string]string{},+ })+ if 0 != missing.SubFileCount {+ t.Fatalf("missing document subfile count = %d, want 0", missing.SubFileCount)+ }+ if 1 != len(missing.RefIDs) || visibleRefID != missing.RefIDs[0] || 1 != missing.RefCount {+ t.Fatalf("missing document reference IDs = %v, count = %d, want only the published backlink",+ missing.RefIDs, missing.RefCount)+ }++ // 原始未过滤反链在 API 层无 RefIDs 时仍给出空数组而不是 null+ empty := FilterBlockInfoByPublishAccess(c, publishAccess, &BlockInfo{ID: publicID, RootID: publicID, IAL: map[string]string{}})+ if nil == empty.RefIDs || 0 != len(empty.RefIDs) || 0 != empty.RefCount {+ t.Fatalf("document without references reference IDs = %v, count = %d, want empty slice", empty.RefIDs, empty.RefCount)+ }+}