Skip to content

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

:lock: Exclude publish-inaccessible documents from notebook info aggregates https://github.com/siyuan-note/siyuan/security/advisories/GHSA-ccxp-q3vg-jvgj

Daniel99fa1dca8495
diff --git a/kernel/api/notebook.go b/kernel/api/notebook.go
index ff77625a7b..bb251608e7 100644
--- a/kernel/api/notebook.go
+++ b/kernel/api/notebook.go
@@ -54,7 +54,13 @@ var getNotebookInfo = contractHandler(apicontract.GetNotebookInfo, func(c *gin.C
return contractFailure[apicontract.NotebookInfoData](ret)
}
- boxInfo := box.GetInfo()
+ var boxInfo *model.BoxInfo
+ if model.IsReadOnlyRoleContext(c) {
+ // 发布读者的统计口径与可见的发布视图一致,不包含隐藏和禁止发布的文档
+ boxInfo = box.GetInfoForPublish(model.GetPublishAccess())
+ } else {
+ boxInfo = box.GetInfo()
+ }
return apicontract.Success(apicontract.NotebookInfoData{BoxInfo: notebookInfoContract(boxInfo)})
})
diff --git a/kernel/api/notebook_security_test.go b/kernel/api/notebook_security_test.go
index 80ba63b433..294df09366 100644
--- a/kernel/api/notebook_security_test.go
+++ b/kernel/api/notebook_security_test.go
@@ -25,6 +25,7 @@ import (
"path/filepath"
"strings"
"testing"
+ "time"
"github.com/gin-gonic/gin"
"github.com/siyuan-note/siyuan/kernel/conf"
@@ -189,6 +190,156 @@ func TestGetNotebookInfoHidesInvisibleNotebookFromReader(t *testing.T) {
}
}
+// installNotebookSecurityTimeLangs 为指定语言安装时间本地化标签,避免聚合信息渲染相对时间失败。
+func installNotebookSecurityTimeLangs(t *testing.T, lang string) {
+ t.Helper()
+
+ oldLang, hadLang := util.TimeLangs[lang]
+ util.TimeLangs[lang] = map[string]any{}
+ for _, key := range []string{"albl", "blbl", "now", "1s", "xs", "1m", "xm", "1h", "xh", "1d", "xd", "1w", "xw", "1M", "xM", "1y", "2y", "xy", "max"} {
+ util.TimeLangs[lang][key] = ""
+ }
+ t.Cleanup(func() {
+ if hadLang {
+ util.TimeLangs[lang] = oldLang
+ } else {
+ delete(util.TimeLangs, lang)
+ }
+ })
+}
+
+// TestGetNotebookInfoExcludesPublishExcludedDocumentsForReader 验证发布读者的笔记本聚合信息
+// 只统计发布可见的文档,隐藏和禁止发布的文档不计入文档数、体积和最后修改时间。
+func TestGetNotebookInfoExcludesPublishExcludedDocumentsForReader(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+
+ oldConf, oldDataDir := model.Conf, util.DataDir
+ oldPublishAccess := model.GetPublishAccess()
+ util.DataDir = t.TempDir()
+ model.Conf = model.NewAppConf()
+ model.Conf.FileTree = conf.NewFileTree()
+ const testLang = "notebook-info-publish-test"
+ installNotebookSecurityTimeLangs(t, testLang)
+ model.Conf.Lang = testLang
+ t.Cleanup(func() {
+ if err := model.SetPublishAccess(oldPublishAccess); err != nil {
+ t.Errorf("restore publish access failed: %v", err)
+ }
+ model.Conf, util.DataDir = oldConf, oldDataDir
+ })
+
+ const (
+ boxID = "20260726000000-abcdefg"
+ publicID = "20260726000001-publica"
+ hiddenID = "20260726000002-hiddend"
+ forbiddenID = "20260726000003-forbidd"
+ )
+ boxConf := conf.NewBoxConf()
+ boxConf.Name = "Mixed notebook"
+ boxConf.Closed = false
+ boxConfPath := filepath.Join(util.DataDir, boxID, ".siyuan", "conf.json")
+ if err := os.MkdirAll(filepath.Dir(boxConfPath), 0755); err != nil {
+ t.Fatal(err)
+ }
+ boxConfData, err := json.Marshal(boxConf)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err = os.WriteFile(boxConfPath, boxConfData, 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ // 隐藏和禁止发布的文档使用更晚的修改时间,用于验证读者看到的时间不包含它们
+ excludedModTime := time.Now().Add(time.Hour).Truncate(time.Second)
+ writeDoc := func(docID string, modTime time.Time) int64 {
+ t.Helper()
+ docPath := filepath.Join(util.DataDir, boxID, docID+".sy")
+ if writeErr := os.WriteFile(docPath, []byte(`{"Properties":{}}`), 0644); nil != writeErr {
+ t.Fatal(writeErr)
+ }
+ if writeErr := os.Chtimes(docPath, modTime, modTime); nil != writeErr {
+ t.Fatal(writeErr)
+ }
+ info, statErr := os.Stat(docPath)
+ if nil != statErr {
+ t.Fatal(statErr)
+ }
+ return info.Size()
+ }
+
+ rootSize := writeDoc(boxID, time.Now())
+ publicSize := writeDoc(publicID, time.Now())
+ hiddenSize := writeDoc(hiddenID, excludedModTime)
+ forbiddenSize := writeDoc(forbiddenID, excludedModTime)
+ wholeSize := uint64(rootSize + publicSize + hiddenSize + forbiddenSize)
+
+ if err = model.SetPublishAccess(model.PublishAccess{
+ {ID: hiddenID, Visible: false},
+ {ID: forbiddenID, Visible: false, Disable: true},
+ }); err != nil {
+ t.Fatal(err)
+ }
+
+ request := func(role model.Role) *model.BoxInfo {
+ t.Helper()
+
+ engine := gin.New()
+ engine.Use(func(c *gin.Context) {
+ c.Set(model.RoleContextKey, role)
+ c.Next()
+ })
+ engine.POST("/api/notebook/getNotebookInfo", getNotebookInfo)
+
+ recorder := httptest.NewRecorder()
+ httpRequest := httptest.NewRequest(
+ http.MethodPost,
+ "/api/notebook/getNotebookInfo",
+ strings.NewReader(`{"notebook":"`+boxID+`"}`),
+ )
+ httpRequest.Header.Set("Content-Type", "application/json")
+ engine.ServeHTTP(recorder, httpRequest)
+ requireAPIContract(t, http.MethodPost, "/api/notebook/getNotebookInfo", recorder)
+
+ response := &struct {
+ Code int `json:"code"`
+ Msg string `json:"msg"`
+ Data struct {
+ BoxInfo *model.BoxInfo `json:"boxInfo"`
+ } `json:"data"`
+ }{}
+ if unmarshalErr := json.Unmarshal(recorder.Body.Bytes(), response); nil != unmarshalErr {
+ t.Fatalf("unmarshal response failed: %v", unmarshalErr)
+ }
+ if 0 != response.Code || nil == response.Data.BoxInfo {
+ t.Fatalf("unexpected response: %s", recorder.Body.String())
+ }
+ return response.Data.BoxInfo
+ }
+
+ reader := request(model.RoleReader)
+ if 1 != reader.DocCount {
+ t.Fatalf("reader saw %d documents, want only the published one", reader.DocCount)
+ }
+ // Size 按既有语义包含笔记本根文档,DocCount 不包含
+ if uint64(rootSize+publicSize) != reader.Size {
+ t.Fatalf("reader size = %d, want %d", reader.Size, rootSize+publicSize)
+ }
+ if reader.Mtime >= excludedModTime.Unix() {
+ t.Fatalf("reader modification time %d covers publish-excluded documents", reader.Mtime)
+ }
+
+ administrator := request(model.RoleAdministrator)
+ if 3 != administrator.DocCount {
+ t.Fatalf("administrator saw %d documents, want the whole notebook", administrator.DocCount)
+ }
+ if wholeSize != administrator.Size {
+ t.Fatalf("administrator size = %d, want %d", administrator.Size, wholeSize)
+ }
+ if administrator.Mtime != excludedModTime.Unix() {
+ t.Fatalf("administrator modification time = %d, want %d", administrator.Mtime, excludedModTime.Unix())
+ }
+}
+
func TestSetNotebookIconRejectsPathTraversal(t *testing.T) {
gin.SetMode(gin.TestMode)
diff --git a/kernel/model/box.go b/kernel/model/box.go
index 81737bc21f..da5a75c13a 100644
--- a/kernel/model/box.go
+++ b/kernel/model/box.go
@@ -587,6 +587,16 @@ type BoxInfo struct {
}
func (box *Box) GetInfo() (ret *BoxInfo) {
+ return box.getInfo(nil)
+}
+
+// GetInfoForPublish 返回发布访问控制下笔记本的聚合信息,只统计发布读者可见的文档。
+func (box *Box) GetInfoForPublish(publishAccess PublishAccess) (ret *BoxInfo) {
+ return box.getInfo(PublishVisibleDocPathFilter(box.ID, publishAccess))
+}
+
+// getInfo 统计笔记本的聚合信息,include 为文档路径可见性判定,nil 表示统计全部文档。
+func (box *Box) getInfo(include func(docPath string) bool) (ret *BoxInfo) {
ret = &BoxInfo{
ID: box.ID,
Name: util.EscapeHTML(box.Name),
@@ -617,6 +627,10 @@ func (box *Box) GetInfo() (ret *BoxInfo) {
continue
}
+ if nil != include && !include(fileInfo.path) {
+ continue
+ }
+
absPath := filepath.Join(util.DataDir, box.ID, fileInfo.path)
info, err := os.Stat(absPath)
if err != nil {
diff --git a/kernel/model/box_doc.go b/kernel/model/box_doc.go
index 239c9abb1c..f8e861ba9b 100644
--- a/kernel/model/box_doc.go
+++ b/kernel/model/box_doc.go
@@ -279,12 +279,7 @@ func BoxDocSubFileCount(boxID string) int {
// BoxDocSubFileCountForPublish 返回发布访问控制下笔记本顶层文档的可见下级文档数。
func BoxDocSubFileCountForPublish(boxID string, publishAccess PublishAccess) int {
- publishInvisible := GetInvisiblePublishAccess(publishAccess)
- publishDisable := GetDisablePublishAccess(publishAccess)
- return boxDocSubFileCount(boxID, func(p string) bool {
- return CheckPathAccessableByPublishIgnore(boxID, p, publishInvisible) &&
- CheckPathAccessableByPublishIgnore(boxID, p, publishDisable)
- })
+ return boxDocSubFileCount(boxID, PublishVisibleDocPathFilter(boxID, publishAccess))
}
func boxDocSubFileCount(boxID string, include func(string) bool) int {
diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.go
index ba05c88010..9071c0f053 100644
--- a/kernel/model/publish_access.go
+++ b/kernel/model/publish_access.go
@@ -303,6 +303,17 @@ func CheckPathAccessableByPublishIgnore(box string, path string, publishIgnore P
return true
}
+// PublishVisibleDocPathFilter 返回发布读者可见文档路径的判定函数,口径与读者可见的发布视图一致:
+// 隐藏、禁止发布以及加密笔记本中的文档都不计入读者统计。
+func PublishVisibleDocPathFilter(boxID string, publishAccess PublishAccess) func(docPath string) bool {
+ publishInvisible := GetInvisiblePublishAccess(publishAccess)
+ publishDisable := GetDisablePublishAccess(publishAccess)
+ return func(docPath string) bool {
+ return CheckPathAccessableByPublishIgnore(boxID, docPath, publishInvisible) &&
+ CheckPathAccessableByPublishIgnore(boxID, docPath, publishDisable)
+ }
+}
+
// IsEncryptedPublishRuntimeTarget 判断发布读取目标是否属于当前可解析的加密笔记本。
func IsEncryptedPublishRuntimeTarget(id string) bool {
boxIDs, denyAll := encryptedBoxIDsForPublishAccess()