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 bookmark labels https://github.com/siyuan-note/siyuan/security/advisories/GHSA-j4ph-9xwf-wcj4
Daniel82a1bc89e658
diff --git a/kernel/api/attr.go b/kernel/api/attr.goindex 8499dcb56c..b14e34ae11 100644--- a/kernel/api/attr.go+++ b/kernel/api/attr.go@@ -32,7 +32,11 @@ func getBookmarkLabels(c *gin.Context) {ret := gulu.Ret.NewResult()defer c.JSON(http.StatusOK, ret)- ret.Data = model.BookmarkLabels()+ if model.IsReadOnlyRoleContext(c) {+ ret.Data = model.BookmarkLabelsByPublishAccess(c, model.GetPublishAccess())+ } else {+ ret.Data = model.BookmarkLabels()+ }}func batchGetBlockAttrs(c *gin.Context) {diff --git a/kernel/model/bookmark.go b/kernel/model/bookmark.goindex 04987b5f4e..fbb716f342 100644--- a/kernel/model/bookmark.go+++ b/kernel/model/bookmark.go@@ -25,6 +25,7 @@ import ("github.com/88250/gulu""github.com/88250/lute/parse"+ "github.com/gin-gonic/gin""github.com/siyuan-note/logging""github.com/siyuan-note/siyuan/kernel/av""github.com/siyuan-note/siyuan/kernel/cache"@@ -186,6 +187,35 @@ func BookmarkLabels() (ret []string) {return}+func BookmarkLabelsByPublishAccess(c *gin.Context, publishAccess PublishAccess) (ret []string) {+ return filterBookmarkLabelsByPublishAccess(c, publishAccess, sql.QueryBookmarkLabelBlocks())+}++func filterBookmarkLabelsByPublishAccess(c *gin.Context, publishAccess PublishAccess, blocks []*sql.BookmarkLabelBlock) (ret []string) {+ ret = []string{}+ publishInvisible := GetInvisiblePublishAccess(publishAccess)+ publishDisable := GetDisablePublishAccess(publishAccess)+ labels := map[string]bool{}+ for _, block := range blocks {+ if block == nil || block.Label == "" ||+ !CheckPathAccessableByPublishIgnore(block.Box, block.Path, publishInvisible) ||+ !CheckPathAccessableByPublishIgnore(block.Box, block.Path, publishDisable) {+ continue+ }+ passwordID, password := GetPathPasswordByPublishAccess(block.Box, block.Path, publishAccess)+ if password != "" && !CheckPublishAuthCookie(c, passwordID, password) {+ continue+ }+ labels[block.Label] = true+ }++ for label := range labels {+ ret = append(ret, label)+ }+ sort.Strings(ret)+ return+}+func BuildBookmark() (ret *Bookmarks) {FlushTxQueue()sql.FlushQueue()diff --git a/kernel/model/bookmark_test.go b/kernel/model/bookmark_test.gonew file mode 100644index 0000000000..610418d7f4--- /dev/null+++ b/kernel/model/bookmark_test.go@@ -0,0 +1,91 @@+// 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/>.++package model++import (+ "net/http"+ "net/http/httptest"+ "slices"+ "testing"++ "github.com/gin-gonic/gin"+ "github.com/siyuan-note/siyuan/kernel/sql"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestFilterBookmarkLabelsByPublishAccess(t *testing.T) {+ const (+ boxID = "20260729000000-boxid01"+ publicDocID = "20260729000001-public1"+ protectedDocID = "20260729000002-protect"+ hiddenDocID = "20260729000003-hidden1"+ disabledDocID = "20260729000004-disable"+ protectedPassword = "password"+ )+ publishAccess := PublishAccess{+ {ID: protectedDocID, Visible: true, Password: protectedPassword},+ {ID: hiddenDocID, Visible: false},+ {ID: disabledDocID, Visible: true, Disable: true},+ }+ blocks := []*sql.BookmarkLabelBlock{+ {Label: "shared", Box: boxID, Path: "/" + publicDocID + ".sy"},+ {Label: "shared", Box: boxID, Path: "/" + protectedDocID + ".sy"},+ {Label: "protected", Box: boxID, Path: "/" + protectedDocID + ".sy"},+ {Label: "hidden", Box: boxID, Path: "/" + hiddenDocID + ".sy"},+ {Label: "disabled", Box: boxID, Path: "/" + disabledDocID + ".sy"},+ {Label: "", Box: boxID, Path: "/" + publicDocID + ".sy"},+ nil,+ }++ c, _ := gin.CreateTestContext(httptest.NewRecorder())+ c.Request = httptest.NewRequest(http.MethodGet, "/", nil)+ filtered := filterBookmarkLabelsByPublishAccess(c, publishAccess, blocks)+ if !slices.Equal(filtered, []string{"shared"}) {+ t.Fatalf("unexpected unauthenticated bookmark labels: %v", filtered)+ }++ c.Request.AddCookie(&http.Cookie{+ Name: "publish-auth-" + protectedDocID,+ Value: util.SHA256Hash([]byte(protectedDocID + protectedPassword)),+ })+ filtered = filterBookmarkLabelsByPublishAccess(c, publishAccess, blocks)+ if !slices.Equal(filtered, []string{"protected", "shared"}) {+ t.Fatalf("unexpected authenticated bookmark labels: %v", filtered)+ }+}++func TestFilterBlocksByPublishAccessRejectsDisabledBlocks(t *testing.T) {+ const (+ boxID = "20260729000100-boxid01"+ publicDocID = "20260729000101-public1"+ disabledDocID = "20260729000102-disable"+ )+ publishAccess := PublishAccess{+ {ID: disabledDocID, Visible: true, Disable: true},+ }+ blocks := []*Block{+ {ID: publicDocID, Box: boxID, Path: "/" + publicDocID + ".sy"},+ {ID: disabledDocID, Box: boxID, Path: "/" + disabledDocID + ".sy"},+ }++ c, _ := gin.CreateTestContext(httptest.NewRecorder())+ c.Request = httptest.NewRequest(http.MethodGet, "/", nil)+ filtered := FilterBlocksByPublishAccess(c, publishAccess, blocks)+ if len(filtered) != 1 || filtered[0].ID != publicDocID {+ t.Fatalf("unexpected filtered blocks: %+v", filtered)+ }+}diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.goindex 7ea8eb5e28..f32fda7a14 100644--- a/kernel/model/publish_access.go+++ b/kernel/model/publish_access.go@@ -1278,11 +1278,14 @@ func FilterPathsByPublishAccess(c *gin.Context, publishAccess PublishAccess, patfunc FilterBlocksByPublishAccess(c *gin.Context, publishAccess PublishAccess, blocks []*Block) (ret []*Block) {ret = []*Block{}- publishIgnore := GetInvisiblePublishAccess(publishAccess)+ publishInvisible := GetInvisiblePublishAccess(publishAccess)+ publishDisable := GetDisablePublishAccess(publishAccess)for _, block := range blocks {passwordID, password := GetPathPasswordByPublishAccess(block.Box, block.Path, publishAccess)- if CheckPathAccessableByPublishIgnore(block.Box, block.Path, publishIgnore) && (c == nil || password == "" || CheckPublishAuthCookie(c, passwordID, password)) {+ if CheckPathAccessableByPublishIgnore(block.Box, block.Path, publishInvisible) &&+ CheckPathAccessableByPublishIgnore(block.Box, block.Path, publishDisable) &&+ (c == nil || password == "" || CheckPublishAuthCookie(c, passwordID, password)) {ret = append(ret, block)}}diff --git a/kernel/sql/block_query.go b/kernel/sql/block_query.goindex 2f7f95e0d2..072fada2ca 100644--- a/kernel/sql/block_query.go+++ b/kernel/sql/block_query.go@@ -312,22 +312,39 @@ func QueryBookmarkBlocks() (ret []*Block) {return}-func QueryBookmarkLabels() (ret []string) {- ret = []string{}- sqlStmt := "SELECT * FROM blocks WHERE ial LIKE ?"+type BookmarkLabelBlock struct {+ Label string+ Box string+ Path string+}++func QueryBookmarkLabelBlocks() (ret []*BookmarkLabelBlock) {+ ret = []*BookmarkLabelBlock{}+ sqlStmt := "SELECT ial, box, path FROM blocks WHERE ial LIKE ?"rows, err := query(sqlStmt, "%bookmark=%")if err != nil {logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)return}defer rows.Close()- labels := map[string]bool{}for rows.Next() {- if block := scanBlockRows(rows); nil != block {- if v := ialAttr(block.IAL, "bookmark"); "" != v {- labels[v] = true- }+ var ial, box, blockPath string+ if err = rows.Scan(&ial, &box, &blockPath); err != nil {+ logging.LogErrorf("scan query rows failed: %s", err)+ continue}+ if label := ialAttr(ial, "bookmark"); label != "" {+ ret = append(ret, &BookmarkLabelBlock{Label: label, Box: box, Path: blockPath})+ }+ }+ return+}++func QueryBookmarkLabels() (ret []string) {+ ret = []string{}+ labels := map[string]bool{}+ for _, block := range QueryBookmarkLabelBlocks() {+ labels[block.Label] = true}for label := range labels {