Skip to content

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

:lock: Prevent exposing local storage in publish mode https://github.com/siyuan-note/siyuan/security/advisories/GHSA-xp3q-r38w-vgqm

Daniel4daee87d4152
diff --git a/kernel/api/storage.go b/kernel/api/storage.go
index 07ea83fc28..aa243131c5 100644
--- a/kernel/api/storage.go
+++ b/kernel/api/storage.go
@@ -31,8 +31,7 @@ func getLocalStorage(c *gin.Context) {
data := model.GetLocalStorage()
if model.IsReadOnlyRoleContext(c) {
- publishAccess := model.GetPublishAccess()
- data = model.FilterLocalStorageByPublishAccess(publishAccess, data)
+ data = model.FilterLocalStorageByPublishAccess(data)
}
ret.Data = data
}
@@ -53,8 +52,7 @@ func getLocalStorageVal(c *gin.Context) {
data := model.GetLocalStorage()
if model.IsReadOnlyRoleContext(c) {
- publishAccess := model.GetPublishAccess()
- data = model.FilterLocalStorageByPublishAccess(publishAccess, data)
+ data = model.FilterLocalStorageByPublishAccess(data)
}
ret.Data = data[key]
}
@@ -91,8 +89,7 @@ func getLocalStorageVals(c *gin.Context) {
data := model.GetLocalStorage()
if model.IsReadOnlyRoleContext(c) {
- publishAccess := model.GetPublishAccess()
- data = model.FilterLocalStorageByPublishAccess(publishAccess, data)
+ data = model.FilterLocalStorageByPublishAccess(data)
}
out := map[string]any{}
for _, k := range keys {
diff --git a/kernel/api/storage_test.go b/kernel/api/storage_test.go
new file mode 100644
index 0000000000..cbbb8d530a
--- /dev/null
+++ b/kernel/api/storage_test.go
@@ -0,0 +1,147 @@
+// 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 (
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/gin-gonic/gin"
+ "github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestGetLocalStorageByRole(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+
+ oldDataDir := util.DataDir
+ util.DataDir = t.TempDir()
+ t.Cleanup(func() {
+ util.DataDir = oldDataDir
+ })
+
+ if err := model.SetLocalStorage(map[string]any{
+ "local-closed-tabs": []any{
+ map[string]any{
+ "title": "private-document-title",
+ "children": map[string]any{
+ "rootId": "20260729000000-private",
+ },
+ },
+ },
+ "future-private-key": "future-private-value",
+ }); err != nil {
+ t.Fatal(err)
+ }
+
+ tests := []struct {
+ name string
+ path string
+ body string
+ }{
+ {
+ name: "all",
+ path: "/api/storage/getLocalStorage",
+ },
+ {
+ name: "single value",
+ path: "/api/storage/getLocalStorageVal",
+ body: `{"key":"local-closed-tabs"}`,
+ },
+ {
+ name: "multiple values",
+ path: "/api/storage/getLocalStorageVals",
+ body: `{"keys":["local-closed-tabs","future-private-key"]}`,
+ },
+ }
+ roles := []struct {
+ name string
+ role model.Role
+ }{
+ {name: "administrator", role: model.RoleAdministrator},
+ {name: "reader", role: model.RoleReader},
+ }
+ for _, role := range roles {
+ for _, test := range tests {
+ t.Run(role.name+"/"+test.name, func(t *testing.T) {
+ engine := gin.New()
+ engine.Use(func(c *gin.Context) {
+ c.Set(model.RoleContextKey, role.role)
+ c.Next()
+ })
+ engine.POST("/api/storage/getLocalStorage", getLocalStorage)
+ engine.POST("/api/storage/getLocalStorageVal", getLocalStorageVal)
+ engine.POST("/api/storage/getLocalStorageVals", getLocalStorageVals)
+
+ recorder := httptest.NewRecorder()
+ request := httptest.NewRequest(http.MethodPost, test.path, strings.NewReader(test.body))
+ if test.body != "" {
+ request.Header.Set("Content-Type", "application/json")
+ }
+ engine.ServeHTTP(recorder, request)
+
+ response := &struct {
+ Code int `json:"code"`
+ Data json.RawMessage `json:"data"`
+ }{}
+ if err := json.Unmarshal(recorder.Body.Bytes(), response); err != nil {
+ t.Fatalf("unmarshal local storage response failed: %v", err)
+ }
+ if response.Code != 0 {
+ t.Fatalf("get local storage failed: %s", recorder.Body.String())
+ }
+
+ if role.role == model.RoleAdministrator {
+ if !strings.Contains(string(response.Data), "private-document-title") {
+ t.Fatalf("administrator local storage data was removed: %s", recorder.Body.String())
+ }
+ return
+ }
+
+ if strings.Contains(string(response.Data), "private-document-title") ||
+ strings.Contains(string(response.Data), "future-private-value") {
+ t.Fatalf("reader received administrator local storage data: %s", recorder.Body.String())
+ }
+ switch test.name {
+ case "all":
+ data := map[string]any{}
+ if err := json.Unmarshal(response.Data, &data); err != nil {
+ t.Fatal(err)
+ }
+ if len(data) != 0 {
+ t.Fatalf("reader received non-empty local storage: %#v", data)
+ }
+ case "single value":
+ if string(response.Data) != "null" {
+ t.Fatalf("reader received a local storage value: %s", response.Data)
+ }
+ case "multiple values":
+ data := map[string]any{}
+ if err := json.Unmarshal(response.Data, &data); err != nil {
+ t.Fatal(err)
+ }
+ if len(data) != 2 || data["local-closed-tabs"] != nil || data["future-private-key"] != nil {
+ t.Fatalf("reader received local storage values: %#v", data)
+ }
+ }
+ })
+ }
+ }
+}
diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.go
index d3adb0d37e..7ea8eb5e28 100644
--- a/kernel/model/publish_access.go
+++ b/kernel/model/publish_access.go
@@ -1478,39 +1478,14 @@ func reassignTagCounts(tag *Tag, counts map[string]int) (ret *Tag) {
return tag
}
-func FilterLocalStorageByPublishAccess(publishAccess PublishAccess, localStorage map[string]any) (ret map[string]any) {
- ret = localStorage
- publishIgnore := GetInvisiblePublishAccess(publishAccess)
- // 清空搜索历史记录
- searchKeysItem := ret["local-searchkeys"]
- if searchKeysItem != nil {
- searchKeys := searchKeysItem.(map[string]any)
- if searchKeys != nil {
- searchKeys["keys"] = []string{}
- }
- }
- searchAssetItem := ret["local-searchasset"]
- if searchAssetItem != nil {
- searchAsset := searchAssetItem.(map[string]any)
- if searchAsset != nil {
- searchAsset["k"] = ""
- searchAsset["keys"] = []string{}
- }
- }
- docInfoItem := ret["local-docinfo"]
- if docInfoItem != nil {
- docInfo := docInfoItem.(map[string]any)
- if docInfo != nil {
- idItem := docInfo["id"]
- if idItem != nil {
- id := idItem.(string)
- bt := treenode.GetBlockTree(id)
- if bt != nil {
- if !CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishIgnore) {
- docInfo["id"] = ""
- }
- }
- }
+// 发布模式下默认不返回管理员的本地工作状态,新增键只有在确认不含敏感信息后才能加入白名单。
+var publishLocalStorageAllowedKeys = []string{}
+
+func FilterLocalStorageByPublishAccess(localStorage map[string]any) (ret map[string]any) {
+ ret = map[string]any{}
+ for _, key := range publishLocalStorageAllowedKeys {
+ if value, ok := localStorage[key]; ok {
+ ret[key] = value
}
}
return
diff --git a/kernel/model/publish_access_test.go b/kernel/model/publish_access_test.go
index 9a78c06863..127f5cc98b 100644
--- a/kernel/model/publish_access_test.go
+++ b/kernel/model/publish_access_test.go
@@ -32,6 +32,36 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
+func TestFilterLocalStorageByPublishAccess(t *testing.T) {
+ localStorage := map[string]any{
+ "local-closed-tabs": []any{
+ map[string]any{
+ "title": "private-document-title",
+ "children": map[string]any{
+ "rootId": "20260729000000-private",
+ },
+ },
+ },
+ "local-searchkeys": map[string]any{
+ "keys": []any{"private-search-term"},
+ "replaceKeys": []any{"private-replace-term"},
+ },
+ "future-private-key": "future-private-value",
+ }
+
+ filtered := FilterLocalStorageByPublishAccess(localStorage)
+ if len(filtered) != 0 {
+ t.Fatalf("publish reader received local storage data: %#v", filtered)
+ }
+ if len(localStorage) != 3 {
+ t.Fatalf("filter modified the input local storage: %#v", localStorage)
+ }
+ searchKeys := localStorage["local-searchkeys"].(map[string]any)
+ if len(searchKeys["keys"].([]any)) != 1 || len(searchKeys["replaceKeys"].([]any)) != 1 {
+ t.Fatalf("filter modified nested input local storage: %#v", searchKeys)
+ }
+}
+
func TestCheckBlockTreeAccessableByPublishAccess(t *testing.T) {
const (
boxID = "20260721000000-boxid01"