Skip to content

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

:lock: Require auth and bound UI-process registration entries https://github.com/siyuan-note/siyuan/security/advisories/GHSA-wv96-wmf5-xvj2

Daniel6e23081cf1cf
diff --git a/kernel/api/router.go b/kernel/api/router.go
index 7f61e9de92..c2c02184d3 100644
--- a/kernel/api/router.go
+++ b/kernel/api/router.go
@@ -37,7 +37,6 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("GET", "/api/system/version", version)
ginServer.Handle("POST", "/api/system/version", version)
ginServer.Handle("POST", "/api/system/currentTime", currentTime)
- ginServer.Handle("POST", "/api/system/uiproc", addUIProcess)
ginServer.Handle("POST", "/api/system/loginAuth", model.LoginAuth)
ginServer.Handle("POST", "/api/system/logoutAuth", model.LogoutAuth)
ginServer.Handle("GET", "/api/system/getCaptcha", model.GetCaptcha)
@@ -83,6 +82,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/system/importCustomFont", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, importCustomFont)
ginServer.Handle("POST", "/api/system/removeCustomFont", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, removeCustomFont)
ginServer.Handle("POST", "/api/system/exit", model.CheckAuth, model.CheckAdminRole, exit)
+ ginServer.Handle("POST", "/api/system/uiproc", model.CheckAuth, addUIProcess)
ginServer.Handle("POST", "/api/system/getConf", model.CheckAuth, getConf)
ginServer.Handle("POST", "/api/system/ensureOnboarding", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, ensureOnboarding)
ginServer.Handle("POST", "/api/system/dismissOnboarding", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, dismissOnboarding)
diff --git a/kernel/api/system.go b/kernel/api/system.go
index 714fefa232..cf49561641 100644
--- a/kernel/api/system.go
+++ b/kernel/api/system.go
@@ -28,6 +28,7 @@ import (
"os"
"path/filepath"
"reflect"
+ "strconv"
"strings"
"time"
@@ -1471,7 +1472,16 @@ func setNetworkProxy(c *gin.Context) {
func addUIProcess(c *gin.Context) {
pid := c.Query("pid")
- util.UIProcessIDs.Store(pid, true)
+ pidInt, err := strconv.Atoi(pid)
+ if err != nil || 0 >= pidInt {
+ return
+ }
+
+ // 限制注册表中的 UI 进程数,防止无界增长导致内存耗尽
+ if util.UIProcessCount() >= util.MaxUIProcessCount {
+ return
+ }
+ util.UIProcessIDs.Store(strconv.Itoa(pidInt), true)
}
func exit(c *gin.Context) {
diff --git a/kernel/model/process.go b/kernel/model/process.go
index 54a7a4f080..b9fb9eaa41 100644
--- a/kernel/model/process.go
+++ b/kernel/model/process.go
@@ -88,6 +88,8 @@ func getAttachedUIProcCount() (ret int) {
util.UIProcessIDs.Range(func(uiProcIDArg, _ any) bool {
uiProcID, err := strconv.Atoi(uiProcIDArg.(string))
if err != nil {
+ // 清理无效的 UI 进程 ID,防止注册表被污染
+ util.UIProcessIDs.Delete(uiProcIDArg)
logging.LogErrorf("invalid UI proc ID [%s]: %s", uiProcIDArg, err)
return true
}
@@ -99,6 +101,8 @@ func getAttachedUIProcCount() (ret int) {
}
if nil == proc {
+ // 清理已经不存在的 UI 进程 ID
+ util.UIProcessIDs.Delete(uiProcIDArg)
return true
}
diff --git a/kernel/model/session.go b/kernel/model/session.go
index bbc97fe724..ee78a18bc4 100644
--- a/kernel/model/session.go
+++ b/kernel/model/session.go
@@ -288,7 +288,8 @@ func CheckAuth(c *gin.Context) {
c.Next()
return
}
- if strings.HasPrefix(c.Request.RequestURI, "/api/system/exit") {
+ if strings.HasPrefix(c.Request.RequestURI, "/api/system/exit") ||
+ strings.HasPrefix(c.Request.RequestURI, "/api/system/uiproc") {
c.Set(RoleContextKey, RoleAdministrator)
c.Next()
return
diff --git a/kernel/util/working.go b/kernel/util/working.go
index ae4b614edb..1d0c8b6aff 100644
--- a/kernel/util/working.go
+++ b/kernel/util/working.go
@@ -276,6 +276,18 @@ var (
UIProcessIDs = sync.Map{} // UI 进程 ID
)
+// MaxUIProcessCount UI 进程注册表条目数上限。
+const MaxUIProcessCount = 64
+
+// UIProcessCount 获取 UI 进程注册表条目数。
+func UIProcessCount() (ret int) {
+ UIProcessIDs.Range(func(_, _ any) bool {
+ ret++
+ return true
+ })
+ return
+}
+
func initWorkspaceDir(workspaceArg string) {
userHomeConfDir := filepath.Join(HomeDir, ".config", "siyuan")
workspaceConf := filepath.Join(userHomeConfDir, "workspace.json")