Skip to content

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

:lock: Enforce publish access for database keys and block IDs

Danielf36331956ae9
diff --git a/kernel/api/block.go b/kernel/api/block.go
index 10d231a1ff..48724c618a 100644
--- a/kernel/api/block.go
+++ b/kernel/api/block.go
@@ -689,6 +689,7 @@ func getBlockDefIDsByRefText(c *gin.Context) {
anchor := arg["anchor"].(string)
ids := model.GetBlockDefIDsByRefText(anchor)
+ ids = filterBlockIDsByPublishAccess(c, ids, "")
var retRefDefs []model.RefDefs
for _, id := range ids {
retRefDefs = append(retRefDefs, model.RefDefs{RefID: id, DefIDs: []string{}})
diff --git a/kernel/api/block_test.go b/kernel/api/block_test.go
new file mode 100644
index 0000000000..b28c3aa722
--- /dev/null
+++ b/kernel/api/block_test.go
@@ -0,0 +1,92 @@
+// 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 api
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "path/filepath"
+ "slices"
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/88250/lute/parse"
+ "github.com/gin-gonic/gin"
+ "github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestFilterBlockIDsByPublishAccess(t *testing.T) {
+ const (
+ boxID = "20260724000000-boxid01"
+ publicID = "20260724000001-public1"
+ hiddenID = "20260724000002-hidden1"
+ protectedID = "20260724000003-protect"
+ missingID = "20260724000004-missing"
+ 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: hiddenID, 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, hiddenID, protectedID} {
+ treenode.IndexBlockTree(&parse.Tree{
+ ID: id,
+ Box: boxID,
+ Path: "/" + id + ".sy",
+ Root: &ast.Node{ID: id, Type: ast.NodeDocument},
+ })
+ }
+
+ ids := []string{publicID, hiddenID, protectedID, missingID}
+ c, _ := gin.CreateTestContext(httptest.NewRecorder())
+ c.Request = httptest.NewRequest(http.MethodPost, "/", nil)
+ c.Set(model.RoleContextKey, model.RoleReader)
+ if filtered := filterBlockIDsByPublishAccess(c, ids, ""); !slices.Equal(filtered, []string{publicID}) {
+ t.Fatalf("unexpected unauthenticated reader block IDs: %v", filtered)
+ }
+
+ c.Request.AddCookie(&http.Cookie{
+ Name: "publish-auth-" + protectedID,
+ Value: util.SHA256Hash([]byte(protectedID + protectedPassword)),
+ })
+ if filtered := filterBlockIDsByPublishAccess(c, ids, ""); !slices.Equal(filtered, []string{publicID, protectedID}) {
+ t.Fatalf("unexpected authenticated reader block IDs: %v", filtered)
+ }
+
+ c.Set(model.RoleContextKey, model.RoleAdministrator)
+ if filtered := filterBlockIDsByPublishAccess(c, ids, ""); !slices.Equal(filtered, ids) {
+ t.Fatalf("administrator block IDs should remain unchanged: %v", filtered)
+ }
+}
diff --git a/kernel/api/router.go b/kernel/api/router.go
index add6edf46c..07d04c3e4d 100644
--- a/kernel/api/router.go
+++ b/kernel/api/router.go
@@ -550,7 +550,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/av/setDatabaseBlockView", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, setDatabaseBlockView)
ginServer.Handle("POST", "/api/av/getMirrorDatabaseBlocks", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, getMirrorDatabaseBlocks)
ginServer.Handle("POST", "/api/av/getAttributeViewKeysByAvID", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, getAttributeViewKeysByAvID)
- ginServer.Handle("POST", "/api/av/getAttributeViewKeysByID", model.CheckAuth, getAttributeViewKeysByID)
+ ginServer.Handle("POST", "/api/av/getAttributeViewKeysByID", model.CheckAuth, model.CheckReadonly, getAttributeViewKeysByID)
ginServer.Handle("POST", "/api/av/duplicateAttributeViewBlock", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, duplicateAttributeViewBlock)
ginServer.Handle("POST", "/api/av/appendAttributeViewDetachedBlocksWithValues", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, appendAttributeViewDetachedBlocksWithValues)
ginServer.Handle("POST", "/api/av/getCurrentAttrViewImages", model.CheckAuth, getCurrentAttrViewImages)