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: Quarantine keepalive WebSocket sessions by connect-time flag https://github.com/siyuan-note/siyuan/security/advisories/GHSA-c8w8-3pqp-wr83
Daniel67bc300b6ea6
diff --git a/kernel/server/serve.go b/kernel/server/serve.goindex 2ef00b376a..2b4c8b81c3 100644--- a/kernel/server/serve.go+++ b/kernel/server/serve.go@@ -1234,6 +1234,15 @@ func serveWebSocket(ginServer *gin.Engine) {util.WebSocketServer.HandleConnect(func(s *melody.Session) {//logging.LogInfof("ws check auth for [%s]", s.Request.RequestURI)++ // 拒绝携带重复查询键的请求,避免同一 URI 表达多个会话身份+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-c8w8-3pqp-wr83+ if util.HasDuplicateQueryValues(s.Request) {+ s.CloseWithMsg([]byte(" invalid query"))+ logging.LogWarnf("closed a session with duplicated query values [%s]", util.GetRemoteAddr(s.Request))+ return+ }+authOk := trueif "" != model.Conf.AccessAuthCode {@@ -1272,7 +1281,10 @@ func serveWebSocket(ginServer *gin.Engine) {if !authOk {// 用于授权页保持连接,避免非常驻内存内核自动退出 https://github.com/siyuan-note/insider/issues/1099- authOk = strings.Contains(s.Request.RequestURI, "/ws?app=siyuan") && strings.Contains(s.Request.RequestURI, "&id=auth&type=auth")+ if util.IsAuthPageKeepaliveRequest(s.Request) {+ authOk = true+ s.Set("authSession", true)+ }}if !authOk {diff --git a/kernel/util/websocket.go b/kernel/util/websocket.goindex 37a2630cbb..9139799a7a 100644--- a/kernel/util/websocket.go+++ b/kernel/util/websocket.go@@ -17,6 +17,7 @@package utilimport (+ "net/http""strings""sync""sync/atomic"@@ -168,13 +169,36 @@ func AddPushChan(session *melody.Session) {}}+// IsAuthPageKeepaliveRequest 判断是否为授权页保持连接请求,避免非常驻内存内核自动退出。+// 该请求无需认证,但连接必须被隔离在广播池之外。+// https://github.com/siyuan-note/insider/issues/1099+func IsAuthPageKeepaliveRequest(r *http.Request) bool {+ if "/ws" != r.URL.Path {+ return false+ }++ query := r.URL.Query()+ return strings.HasPrefix(query.Get("app"), "siyuan") && "auth" == query.Get("id") && "auth" == query.Get("type")+}++// HasDuplicateQueryValues 判断请求查询参数中是否存在重复键。+func HasDuplicateQueryValues(r *http.Request) bool {+ query := r.URL.Query()+ for _, values := range query {+ if 1 < len(values) {+ return true+ }+ }+ return false+}+func IsAuthSession(session *melody.Session) bool {- id, _ := session.Get("id")- if "auth" == id {- return true+ // 授权页保持连接会话在接入时打上标记,禁止重解析请求查询参数判定会话身份+ if isAuth, ok := session.Get("authSession"); ok {+ return isAuth.(bool)}- id = session.Request.URL.Query().Get("id")+ id, _ := session.Get("id")return "auth" == id}