Skip to content

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

:lock: Redact peekMutatedRootIDs for read-only roles in undoState https://github.com/siyuan-note/siyuan/security/advisories/GHSA-6gf8-q9ch-w732

Daniel3bc89e0dbe2c
diff --git a/kernel/api/transaction.go b/kernel/api/transaction.go
index 7723c986f6..e083e81ad5 100644
--- a/kernel/api/transaction.go
+++ b/kernel/api/transaction.go
@@ -232,6 +232,11 @@ func undoState(c *gin.Context) {
}
canUndo, canRedo, peekMutatedRootIDs := model.GlobalUndoLog.State(rootID)
+ if model.IsReadOnlyRole(model.GetGinContextRole(c)) {
+ // 只读角色(读者、访问者)没有撤销权限,跨文档联动文档 ID 对其无用,
+ // 且可能泄露同一事务中私有文档的 rootID,裁剪为空列表。
+ peekMutatedRootIDs = []string{}
+ }
ret.Data = map[string]any{
"canUndo": canUndo,
"canRedo": canRedo,
diff --git a/kernel/api/transaction_test.go b/kernel/api/transaction_test.go
index 8d8278285c..26187c1595 100644
--- a/kernel/api/transaction_test.go
+++ b/kernel/api/transaction_test.go
@@ -9,8 +9,16 @@
package api
import (
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "reflect"
+ "strings"
"testing"
+ "unsafe"
+ "github.com/88250/lute/parse"
+ "github.com/gin-gonic/gin"
"github.com/siyuan-note/siyuan/kernel/model"
)
@@ -28,3 +36,97 @@ func TestShouldBroadcastAttrViewTransactions(t *testing.T) {
t.Fatal("attribute view name updates should keep using optimistic local updates")
}
}
+
+// recordCrossDocUndoEntry 在全局撤销日志中记录一笔跨文档事务,使公开文档的栈顶
+// 关联私有文档 rootID,用于复现 undoState 只读角色泄露场景。
+func recordCrossDocUndoEntry(publicRootID, privateRootID string) {
+ tx := &model.Transaction{
+ UndoOperations: []*model.Operation{{Action: "delete", ID: publicRootID}},
+ }
+ tx.MarkFromAPI()
+ treesField := reflect.ValueOf(tx).Elem().FieldByName("trees")
+ trees := map[string]*parse.Tree{publicRootID: {}, privateRootID: {}}
+ reflect.NewAt(treesField.Type(), unsafe.Pointer(treesField.UnsafeAddr())).Elem().
+ Set(reflect.ValueOf(trees))
+ model.GlobalUndoLog.Record(tx)
+}
+
+func TestUndoStateRedactsMutatedRootIDsForReadOnlyRoles(t *testing.T) {
+ const (
+ publicRootID = "20260821000000-public1"
+ privateRootID = "20260821000001-privat1"
+ )
+ model.GlobalUndoLog.Clear("")
+ defer model.GlobalUndoLog.Clear("")
+ recordCrossDocUndoEntry(publicRootID, privateRootID)
+
+ gin.SetMode(gin.TestMode)
+ engine := gin.New()
+ engine.POST("/api/transactions/undoState", func(c *gin.Context) {
+ c.Set(model.RoleContextKey, model.RoleReader)
+ undoState(c)
+ })
+
+ recorder := httptest.NewRecorder()
+ request := httptest.NewRequest(http.MethodPost, "/api/transactions/undoState",
+ strings.NewReader(`{"rootID":"`+publicRootID+`"}`))
+ request.Header.Set("Content-Type", "application/json")
+ engine.ServeHTTP(recorder, request)
+
+ var response struct {
+ Data struct {
+ CanUndo bool `json:"canUndo"`
+ CanRedo bool `json:"canRedo"`
+ PeekMutatedRootIDs []string `json:"peekMutatedRootIDs"`
+ } `json:"data"`
+ }
+ if err := json.Unmarshal(recorder.Body.Bytes(), &response); nil != err {
+ t.Fatalf("unmarshal response failed: %v", err)
+ }
+ if !response.Data.CanUndo {
+ t.Fatal("reader should still see the canUndo state")
+ }
+ if 0 != len(response.Data.PeekMutatedRootIDs) {
+ t.Fatalf("reader should not see mutated root IDs, got %v", response.Data.PeekMutatedRootIDs)
+ }
+}
+
+func TestUndoStateReturnsMutatedRootIDsForEditors(t *testing.T) {
+ const (
+ publicRootID = "20260821000000-public2"
+ privateRootID = "20260821000001-privat2"
+ )
+ model.GlobalUndoLog.Clear("")
+ defer model.GlobalUndoLog.Clear("")
+ recordCrossDocUndoEntry(publicRootID, privateRootID)
+
+ gin.SetMode(gin.TestMode)
+ engine := gin.New()
+ engine.POST("/api/transactions/undoState", func(c *gin.Context) {
+ c.Set(model.RoleContextKey, model.RoleEditor)
+ undoState(c)
+ })
+
+ recorder := httptest.NewRecorder()
+ request := httptest.NewRequest(http.MethodPost, "/api/transactions/undoState",
+ strings.NewReader(`{"rootID":"`+publicRootID+`"}`))
+ request.Header.Set("Content-Type", "application/json")
+ engine.ServeHTTP(recorder, request)
+
+ var response struct {
+ Data struct {
+ CanUndo bool `json:"canUndo"`
+ CanRedo bool `json:"canRedo"`
+ PeekMutatedRootIDs []string `json:"peekMutatedRootIDs"`
+ } `json:"data"`
+ }
+ if err := json.Unmarshal(recorder.Body.Bytes(), &response); nil != err {
+ t.Fatalf("unmarshal response failed: %v", err)
+ }
+ if !response.Data.CanUndo {
+ t.Fatal("editor should see the canUndo state")
+ }
+ if 2 != len(response.Data.PeekMutatedRootIDs) {
+ t.Fatalf("editor should see both mutated root IDs, got %v", response.Data.PeekMutatedRootIDs)
+ }
+}