Skip to content

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

:lock: Isolate publish WebSocket sessions from content broadcasts https://github.com/siyuan-note/siyuan/security/advisories/GHSA-mw8r-mw84-88v2

Danielba948639d7f6
diff --git a/kernel/model/auth.go b/kernel/model/auth.go
index 66f2b0e18f..036232cbaf 100644
--- a/kernel/model/auth.go
+++ b/kernel/model/auth.go
@@ -19,6 +19,7 @@ package model
import (
"crypto/rand"
"net/http"
+ "slices"
"sync"
"github.com/golang-jwt/jwt/v5"
@@ -42,7 +43,8 @@ const (
ClaimsContextKey = "claims"
- iss = "siyuan-kernel" // token 的发行者
+ iss = "siyuan-kernel" // token 的发行者
+ publishServiceAudience = "siyuan-publish-server" // 发布服务 token 的受众
ClaimsKeyRole string = "role"
)
@@ -113,10 +115,10 @@ func InitPublishJWT() {
t := jwt.NewWithClaims(
jwt.SigningMethodHS256,
jwt.MapClaims{
- "iss": iss, // token 的发行者
- "sub": username, // token 代表的主体
- "aud": "siyuan-publish-server", // token 的受众
- "jti": uuid.New().String(), // token 的唯一标识
+ "iss": iss, // token 的发行者
+ "sub": username, // token 代表的主体
+ "aud": publishServiceAudience, // token 的受众
+ "jti": uuid.New().String(), // token 的唯一标识
ClaimsKeyRole: RoleReader, // 角色
},
@@ -191,8 +193,10 @@ func IsPublishServiceToken(token *jwt.Token) bool {
return false
}
claims := GetTokenClaims(token)
- if tokenIssuer, ok := claims["iss"].(string); ok {
- return tokenIssuer == iss
+ tokenIssuer, ok := claims["iss"].(string)
+ if !ok || tokenIssuer != iss {
+ return false
}
- return false
+ audience, err := claims.GetAudience()
+ return err == nil && slices.Contains(audience, publishServiceAudience)
}
diff --git a/kernel/model/auth_test.go b/kernel/model/auth_test.go
new file mode 100644
index 0000000000..fabb6a45e6
--- /dev/null
+++ b/kernel/model/auth_test.go
@@ -0,0 +1,77 @@
+// 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 model
+
+import (
+ "testing"
+
+ "github.com/golang-jwt/jwt/v5"
+)
+
+func TestIsPublishServiceToken(t *testing.T) {
+ tests := []struct {
+ name string
+ token *jwt.Token
+ isPublish bool
+ }{
+ {name: "nil"},
+ {
+ name: "invalid",
+ token: &jwt.Token{
+ Claims: jwt.MapClaims{"iss": iss, "aud": publishServiceAudience},
+ },
+ },
+ {
+ name: "wrong issuer",
+ token: &jwt.Token{
+ Valid: true,
+ Claims: jwt.MapClaims{"iss": "other", "aud": publishServiceAudience},
+ },
+ },
+ {
+ name: "wrong audience",
+ token: &jwt.Token{
+ Valid: true,
+ Claims: jwt.MapClaims{"iss": iss, "aud": "siyuan-kernel-plugin"},
+ },
+ },
+ {
+ name: "publish audience",
+ token: &jwt.Token{
+ Valid: true,
+ Claims: jwt.MapClaims{"iss": iss, "aud": publishServiceAudience},
+ },
+ isPublish: true,
+ },
+ {
+ name: "publish audience list",
+ token: &jwt.Token{
+ Valid: true,
+ Claims: jwt.MapClaims{"iss": iss, "aud": []string{"other", publishServiceAudience}},
+ },
+ isPublish: true,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ if actual := IsPublishServiceToken(test.token); actual != test.isPublish {
+ t.Fatalf("IsPublishServiceToken() = %v, want %v", actual, test.isPublish)
+ }
+ })
+ }
+}
diff --git a/kernel/util/websocket.go b/kernel/util/websocket.go
index 815db5d580..37a2630cbb 100644
--- a/kernel/util/websocket.go
+++ b/kernel/util/websocket.go
@@ -49,6 +49,9 @@ func BroadcastByTypeAndExcludeApp(excludeApp, typ, cmd string, code int, msg str
appSessions.Range(func(key, value any) bool {
session := value.(*melody.Session)
+ if isPublishSession(session) {
+ return true
+ }
if t, ok := session.Get("type"); ok && typ == t {
event := NewResult()
event.Cmd = cmd
@@ -71,6 +74,9 @@ func BroadcastByTypeAndApp(typ, app, cmd string, code int, msg string, data any)
appSessions.(*sync.Map).Range(func(key, value any) bool {
session := value.(*melody.Session)
+ if isPublishSession(session) {
+ return true
+ }
if t, ok := session.Get("type"); ok && typ == t {
event := NewResult()
event.Cmd = cmd
@@ -103,6 +109,9 @@ func SessionsByType(typ string) (ret []*melody.Session) {
appSessions := value.(*sync.Map)
appSessions.Range(func(key, value any) bool {
session := value.(*melody.Session)
+ if isPublishSession(session) {
+ return true
+ }
if t, ok := session.Get("type"); ok && typ == t {
ret = append(ret, session)
}
@@ -113,6 +122,11 @@ func SessionsByType(typ string) (ret []*melody.Session) {
return
}
+func isPublishSession(session *melody.Session) bool {
+ isPublish, ok := session.Get("isPublish")
+ return ok && isPublish == true
+}
+
func AddPushChan(session *melody.Session) {
appID := strings.TrimSpace(session.Request.URL.Query().Get("app"))
if "" == appID {
@@ -441,6 +455,9 @@ func Broadcast(msg []byte) {
appSessions := value.(*sync.Map)
appSessions.Range(func(key, value any) bool {
session := value.(*melody.Session)
+ if isPublishSession(session) {
+ return true
+ }
session.Write(msg)
return true
})
@@ -453,6 +470,9 @@ func broadcastOtherApps(msg []byte, excludeApp string) {
appSessions := value.(*sync.Map)
appSessions.Range(func(key, value any) bool {
session := value.(*melody.Session)
+ if isPublishSession(session) {
+ return true
+ }
if app, _ := session.Get("app"); app == excludeApp {
return true
}
@@ -468,6 +488,9 @@ func broadcastOtherAppMains(msg []byte, excludeApp string) {
appSessions := value.(*sync.Map)
appSessions.Range(func(key, value any) bool {
session := value.(*melody.Session)
+ if isPublishSession(session) {
+ return true
+ }
if app, _ := session.Get("app"); app == excludeApp {
return true
}
@@ -488,6 +511,9 @@ func broadcastApp(msg []byte, app string) {
appSessions := value.(*sync.Map)
appSessions.Range(func(key, value any) bool {
session := value.(*melody.Session)
+ if isPublishSession(session) {
+ return true
+ }
if sessionApp, _ := session.Get("app"); sessionApp != app {
return true
}
@@ -503,6 +529,9 @@ func broadcastOthers(msg []byte, excludeSID string) {
appSessions := value.(*sync.Map)
appSessions.Range(func(key, value any) bool {
session := value.(*melody.Session)
+ if isPublishSession(session) {
+ return true
+ }
if id, _ := session.Get("id"); id == excludeSID {
return true
}
@@ -537,7 +566,7 @@ func ClosePublishServiceSessions() {
appSessions := value.(*sync.Map)
appSessions.Range(func(key, value any) bool {
session := value.(*melody.Session)
- if isPublish, ok := session.Get("isPublish"); ok && isPublish == true {
+ if isPublishSession(session) {
publishSessions = append(publishSessions, session)
}
return true
diff --git a/kernel/util/websocket_test.go b/kernel/util/websocket_test.go
new file mode 100644
index 0000000000..cb103153de
--- /dev/null
+++ b/kernel/util/websocket_test.go
@@ -0,0 +1,76 @@
+// 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 util
+
+import (
+ "sync"
+ "testing"
+
+ "github.com/olahol/melody"
+)
+
+func TestIsPublishSession(t *testing.T) {
+ tests := []struct {
+ name string
+ value any
+ setValue bool
+ isPublish bool
+ }{
+ {name: "unset"},
+ {name: "false", value: false, setValue: true},
+ {name: "true", value: true, setValue: true, isPublish: true},
+ {name: "non-boolean", value: "true", setValue: true},
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ session := &melody.Session{}
+ if test.setValue {
+ session.Set("isPublish", test.value)
+ }
+ if actual := isPublishSession(session); actual != test.isPublish {
+ t.Fatalf("isPublishSession() = %v, want %v", actual, test.isPublish)
+ }
+ })
+ }
+}
+
+func TestSessionsByTypeExcludesPublishSession(t *testing.T) {
+ const (
+ appID = "test-publish-session-filter"
+ sessionType = "test-publish-session-type"
+ )
+
+ regularSession := &melody.Session{}
+ regularSession.Set("type", sessionType)
+ publishSession := &melody.Session{}
+ publishSession.Set("type", sessionType)
+ publishSession.Set("isPublish", true)
+
+ appSessions := &sync.Map{}
+ appSessions.Store("regular", regularSession)
+ appSessions.Store("publish", publishSession)
+ sessions.Store(appID, appSessions)
+ t.Cleanup(func() {
+ sessions.Delete(appID)
+ })
+
+ actual := SessionsByType(sessionType)
+ if len(actual) != 1 || actual[0] != regularSession {
+ t.Fatalf("SessionsByType() returned %d sessions, want only the regular session", len(actual))
+ }
+}