Skip to content

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

:bug: Fix nil pointer crash on boot when Publish.Auth is null https://github.com/siyuan-note/siyuan/security/advisories/GHSA-rp9f-c2fj-h648

Daniele42497dfac00
diff --git a/kernel/api/setting.go b/kernel/api/setting.go
index b524bd1f24..915a514de2 100644
--- a/kernel/api/setting.go
+++ b/kernel/api/setting.go
@@ -858,6 +858,16 @@ func setPublish(c *gin.Context) {
return
}
+ if nil == publish.Auth {
+ // 请求体缺省 auth(如 null)时保留现有认证配置,避免把 null 写入 conf.json 导致下次启动崩溃
+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-rp9f-c2fj-h648
+ if nil != model.Conf.Publish.Auth {
+ publish.Auth = model.Conf.Publish.Auth
+ } else {
+ publish.Auth = conf.NewPublish().Auth
+ }
+ }
+
// 认证启用时校验发布服务账户:用户名非空且不重复、密码至少 8 位,
// 防止弱密码或无密码账户被暴力破解 https://github.com/siyuan-note/siyuan/security/advisories/GHSA-phg7-xcr4-q5wg
if publish.Auth.Enable {
diff --git a/kernel/model/auth.go b/kernel/model/auth.go
index 036232cbaf..d5a1bf4b3a 100644
--- a/kernel/model/auth.go
+++ b/kernel/model/auth.go
@@ -25,6 +25,7 @@ import (
"github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
"github.com/siyuan-note/logging"
+ "github.com/siyuan-note/siyuan/kernel/conf"
)
type Account struct {
@@ -91,6 +92,14 @@ func DeleteSession(sessionID string) {
}
func InitPublishAccounts() {
+ if nil == Conf.Publish {
+ Conf.Publish = conf.NewPublish()
+ }
+ if nil == Conf.Publish.Auth {
+ // 防御 conf.json 中 auth 为 null 的历史坏配置,避免启动时解引用空指针崩溃
+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-rp9f-c2fj-h648
+ Conf.Publish.Auth = conf.NewPublish().Auth
+ }
accountsMap = AccountsMap{
"": &Account{}, // 匿名用户
}
diff --git a/kernel/model/auth_test.go b/kernel/model/auth_test.go
index fabb6a45e6..43fdedf7ea 100644
--- a/kernel/model/auth_test.go
+++ b/kernel/model/auth_test.go
@@ -20,6 +20,7 @@ import (
"testing"
"github.com/golang-jwt/jwt/v5"
+ "github.com/siyuan-note/siyuan/kernel/conf"
)
func TestIsPublishServiceToken(t *testing.T) {
@@ -75,3 +76,26 @@ func TestIsPublishServiceToken(t *testing.T) {
})
}
}
+
+func TestInitPublishAccountsWithNilAuth(t *testing.T) {
+ originalConf := Conf
+ originalAccounts := accountsMap
+ defer func() {
+ Conf = originalConf
+ accountsMap = originalAccounts
+ }()
+
+ Conf = NewAppConf()
+ Conf.Publish = &conf.Publish{Enable: true, Port: 6808}
+
+ defer func() {
+ if e := recover(); nil != e {
+ t.Fatalf("InitPublishAccounts panicked on nil Publish.Auth: %v", e)
+ }
+ }()
+ InitPublishAccounts()
+
+ if nil == Conf.Publish.Auth {
+ t.Fatal("InitPublishAccounts should default Publish.Auth when it is nil")
+ }
+}
diff --git a/kernel/server/serve.go b/kernel/server/serve.go
index 64c5c2a59b..1b3d3d9a07 100644
--- a/kernel/server/serve.go
+++ b/kernel/server/serve.go
@@ -256,9 +256,23 @@ func Serve(fastMode bool, cookieKey string) {
go func() {
time.Sleep(1 * time.Second)
- go proxy.InitFixedPortService(host, certPath, keyPath)
- go proxy.InitPublishService()
// 反代服务器启动失败不影响核心服务器启动
+ go func() {
+ defer func() {
+ if e := recover(); nil != e {
+ logging.LogWarnf("boot fixed port service failed: %v", e)
+ }
+ }()
+ proxy.InitFixedPortService(host, certPath, keyPath)
+ }()
+ go func() {
+ defer func() {
+ if e := recover(); nil != e {
+ logging.LogWarnf("boot publish service failed: %v", e)
+ }
+ }()
+ proxy.InitPublishService()
+ }()
}()
httpHandler := ginServer.Handler()