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: Prevent exposing administrator UI layout in publish mode https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hgfg-j9pg-43xw
Daniel63c7d5474a0c
diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.tsindex 6d0d4c270f..4a98d58306 100644--- a/app/src/boot/onGetConfig.ts+++ b/app/src/boot/onGetConfig.ts@@ -33,6 +33,7 @@ import {recordBeforeResizeTop} from "../protyle/util/resize";import {processSiYuanUri} from "../util/uri";import {getAllEditor} from "../layout/getAll";import {openDesktopOnboarding} from "../onboarding";+import {ensureUILayout} from "../util/ensureUILayout";export const onGetConfig = (isStart: boolean, app: App) => {correctHotkey(app);@@ -54,9 +55,7 @@ export const onGetConfig = (isStart: boolean, app: App) => {},});/// #endif- if (!window.siyuan.config.uiLayout || (window.siyuan.config.uiLayout && !window.siyuan.config.uiLayout.left)) {- window.siyuan.config.uiLayout = Constants.SIYUAN_EMPTY_LAYOUT;- }+ ensureUILayout();initWindowEvent(app);fetchPost("/api/system/getEmojiConf", {}, response => {window.siyuan.emojis = response.data as IEmoji[];diff --git a/app/src/index.ts b/app/src/index.tsindex 3a2f0623b3..566974ea56 100644--- a/app/src/index.ts+++ b/app/src/index.ts@@ -51,6 +51,7 @@ import {renderSnippet} from "./config/util/snippets";import {setBodyHighlight} from "./util/assets";import {reloadSync} from "./util/reloadSync";import {setTitle} from "./util/processTitle";+import {ensureUILayout} from "./util/ensureUILayout";export class App {public plugins: import("./plugin").Plugin[] = [];@@ -272,6 +273,7 @@ export class App {addScriptSync(`${Constants.PROTYLE_CDN}/js/lute/lute.min.js?v=${Constants.SIYUAN_VERSION}`, "protyleLuteScript");addScript(`${Constants.PROTYLE_CDN}/js/protyle-html.js?v=${Constants.SIYUAN_VERSION}`, "protyleWcHtmlScript");window.siyuan.config = response.data.conf;+ ensureUILayout();window.siyuan.isPublish = response.data.isPublish;setBodyHighlight();await loadPlugins(this);diff --git a/app/src/util/ensureUILayout.ts b/app/src/util/ensureUILayout.tsnew file mode 100644index 0000000000..709b1630fd--- /dev/null+++ b/app/src/util/ensureUILayout.ts@@ -0,0 +1,7 @@+import {Constants} from "../constants";++export const ensureUILayout = () => {+ if (!window.siyuan.config.uiLayout?.left) {+ window.siyuan.config.uiLayout = JSON.parse(JSON.stringify(Constants.SIYUAN_EMPTY_LAYOUT));+ }+};diff --git a/app/src/window/index.ts b/app/src/window/index.tsindex 428040efe1..c7405e34ff 100644--- a/app/src/window/index.ts+++ b/app/src/window/index.ts@@ -30,6 +30,7 @@ import {renderSnippet} from "../config/util/snippets";import {setBodyHighlight} from "../util/assets";import {reloadSync} from "../util/reloadSync";import {setTitle} from "../util/processTitle";+import {ensureUILayout} from "../util/ensureUILayout";class App {public plugins: import("../plugin").Plugin[] = [];@@ -191,6 +192,7 @@ class App {addScriptSync(`${Constants.PROTYLE_CDN}/js/lute/lute.min.js?v=${Constants.SIYUAN_VERSION}`, "protyleLuteScript");addScript(`${Constants.PROTYLE_CDN}/js/protyle-html.js?v=${Constants.SIYUAN_VERSION}`, "protyleWcHtmlScript");window.siyuan.config = response.data.conf;+ ensureUILayout();setBodyHighlight();window.siyuan.isPublish = response.data.isPublish;await loadPlugins(this);diff --git a/kernel/api/system.go b/kernel/api/system.goindex 048f203576..fefe97be89 100644--- a/kernel/api/system.go+++ b/kernel/api/system.go@@ -619,9 +619,7 @@ func getConf(c *gin.Context) {}if model.IsReadOnlyRoleContext(c) {- publishAccess := model.GetPublishAccess()- publishIgnore := model.GetInvisiblePublishAccess(publishAccess)- maskedConf = model.FilterConfByPublishIgnore(publishIgnore, maskedConf)+ maskedConf.UILayout = &conf.UILayout{}}// 浏览器环境下不返回工作空间绝对路径,避免泄露用户名等敏感信息diff --git a/kernel/api/system_test.go b/kernel/api/system_test.goindex 4b6f5aff8c..eac19b55e4 100644--- a/kernel/api/system_test.go+++ b/kernel/api/system_test.go@@ -17,11 +17,113 @@package apiimport (+ "encoding/json"+ "net/http"+ "net/http/httptest"+ "strings""testing"+ "github.com/gin-gonic/gin""github.com/siyuan-note/siyuan/kernel/conf"+ "github.com/siyuan-note/siyuan/kernel/model")+func TestGetConfUILayoutByRole(t *testing.T) {+ gin.SetMode(gin.TestMode)++ oldConf := model.Conf+ model.Conf = model.NewAppConf()+ model.Conf.Sync = conf.NewSync()+ model.Conf.Sync.Enabled = true+ model.Conf.Sync.Provider = 1+ model.Conf.System = &conf.System{}+ uiLayout := conf.UILayout{+ "layout": map[string]any{+ "instance": "Layout",+ "children": []any{+ map[string]any{+ "instance": "Tab",+ "title": "private-document-title",+ "children": map[string]any{+ "instance": "Search",+ "config": map[string]any{"k": "private-search-term"},+ "assetPath": "assets/private-file.pdf",+ "pluginData": "private-plugin-state",+ },+ },+ },+ },+ "left": map[string]any{+ "data": []any{map[string]any{"title": "private-dock-title"}},+ },+ }+ model.Conf.UILayout = &uiLayout+ t.Cleanup(func() {+ model.Conf = oldConf+ })++ sentinels := []string{+ "private-document-title",+ "private-search-term",+ "assets/private-file.pdf",+ "private-plugin-state",+ "private-dock-title",+ }+ tests := []struct {+ name string+ role model.Role+ expectEmpty bool+ }{+ {name: "administrator", role: model.RoleAdministrator},+ {name: "reader", role: model.RoleReader, expectEmpty: true},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ engine := gin.New()+ engine.Use(func(c *gin.Context) {+ c.Set(model.RoleContextKey, test.role)+ c.Next()+ })+ engine.POST("/api/system/getConf", getConf)++ recorder := httptest.NewRecorder()+ request := httptest.NewRequest(http.MethodPost, "/api/system/getConf", nil)+ engine.ServeHTTP(recorder, request)++ response := &struct {+ Code int `json:"code"`+ Data struct {+ Conf struct {+ UILayout map[string]any `json:"uiLayout"`+ } `json:"conf"`+ } `json:"data"`+ }{}+ if err := json.Unmarshal(recorder.Body.Bytes(), response); err != nil {+ t.Fatalf("unmarshal getConf response failed: %v", err)+ }+ if response.Code != 0 {+ t.Fatalf("getConf failed: %s", recorder.Body.String())+ }++ for _, sentinel := range sentinels {+ contains := strings.Contains(recorder.Body.String(), sentinel)+ if test.expectEmpty && contains {+ t.Fatalf("reader received administrator layout data %q: %s", sentinel, recorder.Body.String())+ }+ if !test.expectEmpty && !contains {+ t.Fatalf("administrator layout data %q was removed: %s", sentinel, recorder.Body.String())+ }+ }+ if test.expectEmpty && len(response.Data.Conf.UILayout) != 0 {+ t.Fatalf("reader received a non-empty UI layout: %#v", response.Data.Conf.UILayout)+ }+ if !test.expectEmpty && len(response.Data.Conf.UILayout) == 0 {+ t.Fatal("administrator received an empty UI layout")+ }+ })+ }+}+func TestPreserveImportedAISecrets(t *testing.T) {currentMCP := &conf.MCP{Servers: []conf.MCPServer{{ID: "server", Headers: map[string]string{"Authorization": "secret"}}}}current := &conf.AI{diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.goindex 54595c61a4..a40c9f153a 100644--- a/kernel/model/publish_access.go+++ b/kernel/model/publish_access.go@@ -26,7 +26,6 @@ import ("path""path/filepath""slices"- "strconv""strings""sync""time"@@ -37,7 +36,6 @@ import ("github.com/siyuan-note/filelock""github.com/siyuan-note/logging""github.com/siyuan-note/siyuan/kernel/av"- "github.com/siyuan-note/siyuan/kernel/conf""github.com/siyuan-note/siyuan/kernel/sql""github.com/siyuan-note/siyuan/kernel/treenode""github.com/siyuan-note/siyuan/kernel/util"@@ -1356,123 +1354,6 @@ func FilterRefDefsByPublishIgnore(publishIgnore PublishAccess, refDefs []*RefDefreturn}-func FilterConfByPublishIgnore(publishIgnore PublishAccess, appConf *AppConf) (ret *AppConf) {- ret = appConf- if appConf == nil {- return- }-- appConf.UILayout = FilterUILayoutByPublishIgnore(publishIgnore, appConf.UILayout)- return-}--func FilterUILayoutByPublishIgnore(publishIgnore PublishAccess, uiLayout *conf.UILayout) (ret *conf.UILayout) {- ret = uiLayout- if uiLayout == nil {- return- }-- layout, ok := (*uiLayout)["layout"].(map[string]any)- if !ok {- return- }- layout = filterLayoutItemByPublishIgnore(publishIgnore, layout)- (*ret)["layout"] = layout- return-}--func filterLayoutItemByPublishIgnore(publishIgnore PublishAccess, item map[string]any) (ret map[string]any) {- ret = item- if item == nil {- return- }-- instanceItem, exists := item["instance"]- if !exists {- return- }- instance := instanceItem.(string)- if instance == "Tab" {- childrenItem, exists := item["children"]- if !exists {- return- }- children := childrenItem.(map[string]any)- if children == nil {- return- }- rootIdItem, exists := children["rootId"]- if rootIdItem == nil {- return- }- rootId := children["rootId"].(string)- bt := treenode.GetBlockTree(rootId)- if bt == nil {- return- }- if !CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishIgnore) {- ret = nil- }- } else {- childrenItem, exists := item["children"]- if !exists {- return- }- children := childrenItem.([]any)- if children == nil {- return- }- newChildren := []any{}- updateTabs := false- for _, childItem := range children {- child := childItem.(map[string]any)- if child == nil {- return- }- child = filterLayoutItemByPublishIgnore(publishIgnore, child)- if child != nil {- newChildren = append(newChildren, child)- } else {- updateTabs = true- }- }- if updateTabs {- hasActive := false- activeTimes := []int64{}- for _, childItem := range newChildren {- child := childItem.(map[string]any)- activeTimeStr := child["activeTime"].(string)- var activeTime int64- if len(activeTimeStr) > 0 {- activeTime, _ = strconv.ParseInt(activeTimeStr, 10, 64)- }- activeTimes = append(activeTimes, activeTime)- if active, exists := child["active"]; exists && active.(bool) {- hasActive = true- }- }- if !hasActive && len(activeTimes) > 0 {- // 如果原本激活的tab刚好被去掉了,就选择日期最新的一个tab激活- maxIndex := 0- for i, activeTime := range activeTimes {- if activeTime > activeTimes[maxIndex] {- maxIndex = i- }- }- newChildren[maxIndex].(map[string]any)["active"] = true- }- if len(newChildren) == 0 {- child := make(map[string]any)- child["instance"] = "Tab"- child["children"] = make(map[string]any)- newChildren = append(newChildren, child)- }- }- ret["children"] = newChildren- }- return-}-func FilterGraphByPublishAccess(c *gin.Context, publishAccess PublishAccess, nodes []*GraphNode, links []*GraphLink) (retNodes []*GraphNode, retLinks []*GraphLink) {retNodes = []*GraphNode{}retLinks = []*GraphLink{}