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: Enforce the networkServeTLS setting over plaintext HTTP https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hpj5-f7cj-vvwr
Danielf8c53c97e00c
diff --git a/kernel/model/auth_session.go b/kernel/model/auth_session.goindex e37b5efe0c..ea9f8ce966 100644--- a/kernel/model/auth_session.go+++ b/kernel/model/auth_session.go@@ -49,8 +49,10 @@ func applyAuthenticatedSession(c *gin.Context, workspaceSession *util.WorkspaceSmaxAge = 60 * 60 * 24 * 30}ginSessions.Default(c).Options(ginSessions.Options{- Path: "/",- Secure: util.SSL,+ Path: "/",+ // 与登录会话保持一致:只有客户端连接确实使用 TLS 时才标记 Secure+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hpj5-f7cj-vvwr+ Secure: util.SSL || util.IsSecureRequest(c.Request),MaxAge: maxAge,HttpOnly: true,SameSite: http.SameSiteLaxMode,diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.goindex 45e5ec15ae..efb8408b0e 100644--- a/kernel/model/publish_access.go+++ b/kernel/model/publish_access.go@@ -470,11 +470,13 @@ func checkBlockTreeAccessableByPublishAccess(c *gin.Context, publishAccess Publifunc SetPublishAuthCookie(c *gin.Context, ID string, password string) {authCookie := util.SHA256Hash([]byte(ID + password))http.SetCookie(c.Writer, &http.Cookie{- Name: "publish-auth-" + ID,- Value: authCookie,- MaxAge: 24 * 60 * 60,- Path: "/",- Secure: util.SSL,+ Name: "publish-auth-" + ID,+ Value: authCookie,+ MaxAge: 24 * 60 * 60,+ Path: "/",+ // 只有客户端连接确实使用 TLS 时才标记 Secure,与发布会话 Cookie 保持一致+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hpj5-f7cj-vvwr+ Secure: util.SSL || util.IsSecureRequest(c.Request),HttpOnly: true,})}diff --git a/kernel/model/session.go b/kernel/model/session.goindex c520a74834..a3c72d28e0 100644--- a/kernel/model/session.go+++ b/kernel/model/session.go@@ -134,8 +134,11 @@ func LoginAuth(c *gin.Context, request apicontract.SystemLoginAuthRequest) (retmaxAge = 60 * 60 * 24 * 30 // 30 days}ginSessions.Default(c).Options(ginSessions.Options{- Path: "/",- Secure: util.SSL,+ Path: "/",+ // 按当前连接是否为 TLS 标记 Secure;固定端口反代的明文连接不得标记,+ // 否则浏览器不会在明文的 6806 端口上回传该 Cookie。+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hpj5-f7cj-vvwr+ Secure: util.SSL || util.IsSecureRequest(c.Request),MaxAge: maxAge,HttpOnly: true,SameSite: http.SameSiteLaxMode,diff --git a/kernel/server/proxy/fixedport.go b/kernel/server/proxy/fixedport.goindex b614ac218d..66ec9eb8ce 100644--- a/kernel/server/proxy/fixedport.go+++ b/kernel/server/proxy/fixedport.go@@ -49,7 +49,10 @@ func InitFixedPortService(host string, certPath, keyPath string) {return}- if _, _, serveErr := util.ServeMultiplexed(ln, proxy, certPath, keyPath, nil, nil); serveErr != nil {+ // 双栈监听下仍需按开关强制加密,否则明文的 6806 端口会继续放行全部 API 请求+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hpj5-f7cj-vvwr+ handler := util.TLSRedirectHandler(proxy)+ if _, _, serveErr := util.ServeMultiplexed(ln, handler, certPath, keyPath, nil, nil); serveErr != nil {if !errors.Is(serveErr, cmux.ErrListenerClosed) && !errors.Is(serveErr, http.ErrServerClosed) {logging.LogWarnf("fixed port cmux serve error: %s", serveErr)}@@ -70,6 +73,14 @@ func newFixedPortReverseProxy(target *url.URL) *httputil.ReverseProxy {request.SetURL(target)request.Out.Host = request.In.Hostrequest.SetXForwarded()++ // 反代以 HTTPS 回源内核,内核据此会把明文客户端的连接误判为 TLS,+ // 因此显式标记客户端侧的真实连接方式;调用方伪造的同名头部一律清除。+ // 这里必须判断原始请求(request.In)而不是改写后的请求:改写后的请求是 HTTPS 回源。+ request.Out.Header.Del(util.ClientTLSHeader)+ if request.In.TLS != nil {+ request.Out.Header.Set(util.ClientTLSHeader, util.ClientTLSHeaderValue)+ }},Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true},diff --git a/kernel/server/proxy/fixedport_tls_test.go b/kernel/server/proxy/fixedport_tls_test.gonew file mode 100644index 0000000000..0f01b24614--- /dev/null+++ b/kernel/server/proxy/fixedport_tls_test.go@@ -0,0 +1,66 @@+// SiYuan - From thought to insight, with agents+// 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 proxy++import (+ "net/http"+ "net/http/httptest"+ "testing"++ "github.com/siyuan-note/siyuan/kernel/util"+)++// TestFixedPortReverseProxyMarksClientTLSState 校验固定端口反代把客户端侧的真实连接方式+// 传给以 HTTPS 回源的内核,并清除调用方伪造的同名头部。+func TestFixedPortReverseProxyMarksClientTLSState(t *testing.T) {+ requestInfo := make(chan http.Header, 2)+ backend := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {+ requestInfo <- request.Header.Clone()+ writer.WriteHeader(http.StatusNoContent)+ }))+ defer backend.Close()++ proxy := httptest.NewServer(newFixedPortReverseProxy(mustParseURL(t, backend.URL)))+ defer proxy.Close()++ // 明文客户端连接:不得标记,且调用方伪造的头部要被清除+ request, err := http.NewRequest(http.MethodGet, proxy.URL+"/api/system/version", nil)+ if err != nil {+ t.Fatal(err)+ }+ request.Header.Set(util.ClientTLSHeader, util.ClientTLSHeaderValue)+ response, err := http.DefaultClient.Do(request)+ if err != nil {+ t.Fatal(err)+ }+ response.Body.Close()+ if header := <-requestInfo; "" != header.Get(util.ClientTLSHeader) {+ t.Fatalf("%s = %q, want empty for plaintext client", util.ClientTLSHeader, header.Get(util.ClientTLSHeader))+ }++ // TLS 客户端连接:回源请求必须带上标记+ tlsProxy := httptest.NewTLSServer(newFixedPortReverseProxy(mustParseURL(t, backend.URL)))+ defer tlsProxy.Close()+ response, err = tlsProxy.Client().Get(tlsProxy.URL + "/api/system/version")+ if err != nil {+ t.Fatal(err)+ }+ response.Body.Close()+ if header := <-requestInfo; util.ClientTLSHeaderValue != header.Get(util.ClientTLSHeader) {+ t.Fatalf("%s = %q, want %q for TLS client", util.ClientTLSHeader, header.Get(util.ClientTLSHeader), util.ClientTLSHeaderValue)+ }+}diff --git a/kernel/server/proxy/publish.go b/kernel/server/proxy/publish.goindex 4b549e2402..767877b8af 100644--- a/kernel/server/proxy/publish.go+++ b/kernel/server/proxy/publish.go@@ -133,10 +133,15 @@ func closePublishListener() {func startPublishReverseProxyService() {logging.LogInfof("publish service [%s:%s] is running", Host, Port)- handler := newPublishReverseProxy(util.ServerURL, transport)+ reverseProxy := newPublishReverseProxy(util.ServerURL, transport)+ handler := http.Handler(reverseProxy)certPath, keyPath, certErr := util.GetOrCreateTLSCert()if certErr == nil && "" != certPath {+ // 双栈监听下仍需按开关强制加密,否则发布页面的明文入口会继续可用+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hpj5-f7cj-vvwr+ handler = util.TLSRedirectHandler(handler)+// 提前创建 HTTP/HTTPS 各自的 *http.Server 并传入,这样在服务运行期间就能持有它们的引用,// closePublishListener 调用其 Shutdown/Close 时才能关闭已建立的活跃连接(含 HTTP/2 长连接),// 避免切换工作空间后旧连接仍被旧内核接管。@@ -240,9 +245,9 @@ func (PublishServiceTransport) RoundTrip(request *http.Request) (response *http.Value: sessionID,Path: "/",HttpOnly: true,- // 只有直连 TLS 的连接才标记 Secure,不信任调用方可伪造的 X-Forwarded-Proto,+ // 只有客户端连接确实使用 TLS 时才标记 Secure,不信任调用方可伪造的 X-Forwarded-Proto,// 避免明文连接上的会话标识被浏览器带到其他连接上 https://github.com/siyuan-note/siyuan/security/advisories/GHSA-7j37-4gq6-wm7m- Secure: nil != request.TLS,+ Secure: util.IsTLSRequest(request),// 阻止跨站请求携带会话 Cookie,与内核其他会话 Cookie 保持一致SameSite: http.SameSiteLaxMode,}diff --git a/kernel/server/serve.go b/kernel/server/serve.goindex bae56ea3dc..aaa6add609 100644--- a/kernel/server/serve.go+++ b/kernel/server/serve.go@@ -161,12 +161,16 @@ func Serve(fastMode bool, cookieKey string) {sessionStore = cookie.NewStore([]byte(cookieKey))sessionStore.Options(sessions.Options{- Path: "/",- Secure: util.SSL,- //MaxAge: 60 * 60 * 24 * 7, // 默认是 Session+ Path: "/",+ // Secure 属性逐请求按连接修正,见 tlsEnforceMiddleware+ Secure: util.SSL,HttpOnly: true,SameSite: http.SameSiteLaxMode, // 防止跨站请求伪造 https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hhm2-g993-p656})+ // TLS 强制中间件必须在会话中间件之前:跳转要发生在会话保存之前,+ // 否则明文响应仍会携带未标记 Secure 的会话 Cookie+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hpj5-f7cj-vvwr+ ginServer.Use(tlsEnforceMiddleware())ginServer.Use(sessions.Sessions("siyuan", sessionStore))ginServer.Use(mapHostMiddleware())@@ -229,6 +233,17 @@ func Serve(fastMode bool, cookieKey string) {certPath = ""}+ // 用户开启「启用 HTTPS」后网络服务必须真正强制加密传输,因此这里先把策略定下来:+ // 证书不可用时不能静默退回明文,否则等于开关没生效却仍然宣称已加密。+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hpj5-f7cj-vvwr+ tlsRequired := model.Conf.System.NetworkServeTLS && model.Conf.System.NetworkServe+ util.SetTLSRequired(tlsRequired)+ if tlsRequired && "" == certPath {+ logging.LogErrorf("network serve TLS is enabled but no TLS certificate is available, "++ "plaintext HTTP requests from other hosts will be rejected [err=%s]", certErr)+ go notifyTLSCertUnavailable()+ }+if "" != certPath {util.ServerURL, err = url.Parse("https://127.0.0.1:" + port)} else {@@ -243,9 +258,10 @@ func Serve(fastMode bool, cookieKey string) {rewritePortJSON(pid, port)}- useTLS := model.Conf.System.NetworkServeTLS && model.Conf.System.NetworkServe+ useTLS := tlsRequired && "" != certPathif useTLS {- logging.LogInfof("kernel [pid=%s] http server [%s] is booting (TLS will be enabled on fixed port proxy)", pid, host+":"+port)+ logging.LogInfof("kernel [pid=%s] http server [%s] is booting (TLS enforced, "++ "plaintext requests from other hosts are redirected)", pid, host+":"+port)} else if "" != certPath {logging.LogInfof("kernel [pid=%s] http server [%s] is booting (local HTTPS + HTTP/2 enabled)", pid, host+":"+port)} else {diff --git a/kernel/server/serve_tls.go b/kernel/server/serve_tls.gonew file mode 100644index 0000000000..d56b26f70c--- /dev/null+++ b/kernel/server/serve_tls.go@@ -0,0 +1,102 @@+// SiYuan - From thought to insight, with agents+// 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 server++import (+ "net/http"+ "time"++ "github.com/gin-gonic/gin"+ "github.com/siyuan-note/siyuan/kernel/util"+)++// tlsEnforceMiddleware 在用户开启「启用 HTTPS」后真正强制加密传输。+//+// 内核通过 cmux 在同一端口上同时承载 HTTP 与 HTTPS,仅凭开关无法阻止明文请求,+// 因此这里在应用层把明文请求重定向到 HTTPS,避免登录流程与会话 Cookie 在网络上明文暴露。+// 本机明文请求(桌面端、固定端口反代、浏览器扩展)与 CORS 预检放行,否则本机客户端无法连接。+//+// 注册在会话中间件之前:跳转先于会话保存发生,明文响应就不会带出会话 Cookie;+// 本机明文连接上写出的 Cookie 由 secureSessionCookies 兜底补齐 Secure 属性。+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hpj5-f7cj-vvwr+func tlsEnforceMiddleware() gin.HandlerFunc {+ return func(c *gin.Context) {+ if util.ShouldRedirectToTLS(c.Request) && util.TLSRequired() && util.TLSEnforced() {+ host := c.Request.Host+ if "" == host || !util.IsValidRedirectHost(host) {+ // Host 异常时不能据此拼出跳转地址,直接拒绝明文请求+ c.AbortWithStatus(http.StatusBadRequest)+ return+ }++ // 跳转目标取决于 Host,声明 Vary 避免中间缓存串用响应+ c.Header("Vary", "Host")+ c.Redirect(http.StatusTemporaryRedirect, "https://"+host+c.Request.URL.RequestURI())+ // 必须显式终止链:否则会话中间件仍会在明文响应上保存会话 Cookie+ c.Abort()+ return+ }++ if util.IsTLSRequest(c.Request) {+ c.Header("Strict-Transport-Security", util.StrictTransportSecurityValue)+ }++ if util.IsSecureRequest(c.Request) {+ c.Next()+ return+ }++ // 明文连接(只可能是本机客户端)上不能下发可被回传的会话 Cookie,+ // 因此在该响应写出前统一补齐会话 Cookie 的 Secure 属性。+ c.Next()+ secureSessionCookies(c)+ }+}++// secureSessionCookies 为响应中所有未标记的 Cookie 补齐 Secure 属性。+//+// 会话 Cookie 由会话中间件在响应阶段保存,其 Secure 属性来自会话选项,+// 无法在保存前按请求改写,因此在响应写出前统一处理,避免明文连接下发未标记的会话 Cookie。+func secureSessionCookies(c *gin.Context) {+ headers := c.Writer.Header()+ cookies := headers.Values("Set-Cookie")+ if 0 == len(cookies) {+ return+ }++ headers.Del("Set-Cookie")+ for _, cookie := range cookies {+ parsed, err := http.ParseSetCookie(cookie)+ if nil != err || !parsed.Secure {+ // 解析失败时按最保守的方式处理,直接补上 Secure+ cookie += "; Secure"+ }+ headers.Add("Set-Cookie", cookie)+ }+}++// notifyTLSCertUnavailable 提示用户「启用 HTTPS」因证书不可用而未能生效,避免用户误以为流量已加密。+// 内核日志已记录原始错误,这里只提示结论。+func notifyTLSCertUnavailable() {+ if util.AttachUI {+ util.WaitForUILoaded()+ time.Sleep(3 * time.Second)+ }++ util.PushErrMsg("HTTPS is enabled but the TLS certificate is unavailable, "++ "network requests from other hosts are rejected. Restart the kernel to retry certificate generation", 15000)+}diff --git a/kernel/server/serve_tls_test.go b/kernel/server/serve_tls_test.gonew file mode 100644index 0000000000..b0954d8dbf--- /dev/null+++ b/kernel/server/serve_tls_test.go@@ -0,0 +1,306 @@+// SiYuan - From thought to insight, with agents+// 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 server++import (+ "crypto/tls"+ "net/http"+ "net/http/httptest"+ "testing"++ ginSessions "github.com/gin-contrib/sessions"+ "github.com/gin-contrib/sessions/cookie"+ "github.com/gin-gonic/gin"+ "github.com/siyuan-note/siyuan/kernel/conf"+ "github.com/siyuan-note/siyuan/kernel/model"+ "github.com/siyuan-note/siyuan/kernel/util"+)++// prepareTLSEnforceTest 打开「启用 HTTPS」并标记证书可用,避免依赖真实配置文件。+func prepareTLSEnforceTest(t *testing.T, required, enforced bool) {+ t.Helper()++ previousConf := model.Conf+ previousRequired := util.TLSRequired()+ model.Conf = &model.AppConf{System: &conf.System{NetworkServe: required, NetworkServeTLS: required}}+ util.SetTLSRequired(required)+ if enforced {+ util.MarkTLSEnforced()+ }+ t.Cleanup(func() {+ model.Conf = previousConf+ util.SetTLSRequired(previousRequired)+ })+}++func newTLSEnforceTestEngine() *gin.Engine {+ gin.SetMode(gin.TestMode)+ engine := gin.New()+ // 与 Serve 中的注册顺序一致:TLS 强制中间件在会话中间件之前+ engine.Use(tlsEnforceMiddleware())+ engine.Use(ginSessions.Sessions("siyuan", cookie.NewStore([]byte("test-cookie-key"))))+ engine.Any("/*path", func(c *gin.Context) {+ c.String(http.StatusOK, "ok")+ })+ return engine+}++func TestTLSEnforceRedirectsPlaintextFromOtherHosts(t *testing.T) {+ prepareTLSEnforceTest(t, true, true)+ engine := newTLSEnforceTestEngine()++ request := httptest.NewRequest(http.MethodPost, "/api/system/loginAuth?x=1", nil)+ request.RemoteAddr = "192.0.2.10:54321"+ request.Host = "notes.example.com:6806"+ recorder := httptest.NewRecorder()+ engine.ServeHTTP(recorder, request)++ if location := recorder.Header().Get("Location"); location != "https://notes.example.com:6806/api/system/loginAuth?x=1" {+ t.Fatalf("Location = %q", location)+ }+ if vary := recorder.Header().Get("Vary"); vary != "Host" {+ t.Fatalf("Vary = %q, want Host", vary)+ }+ // gin 对 3xx 惰性写头,响应体中不会包含处理器写入的内容,这里以 Location 是否存在作为跳转判据+}++// TestTLSEnforcePassesThroughOverRealConnection 通过真实 HTTP 往返确认放行路径可用。+// httptest 服务监听环回地址,因此这里覆盖的是「本机明文连接不被跳转」,+// 也顺带验证 gin 对 3xx 的惰性写头不会影响放行分支。+func TestTLSEnforcePassesThroughOverRealConnection(t *testing.T) {+ prepareTLSEnforceTest(t, true, true)++ server := httptest.NewServer(newTLSEnforceTestEngine())+ defer server.Close()++ client := &http.Client{+ CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },+ }+ request, err := http.NewRequest(http.MethodPost, server.URL+"/api/system/loginAuth?x=1", nil)+ if err != nil {+ t.Fatal(err)+ }+ request.Host = "notes.example.com:6806"++ response, err := client.Do(request)+ if err != nil {+ t.Fatal(err)+ }+ defer response.Body.Close()++ // httptest 服务监听环回地址,本机明文请求按设计放行+ if response.StatusCode != http.StatusOK {+ t.Fatalf("status = %d, want %d", response.StatusCode, http.StatusOK)+ }+}++func TestTLSEnforceKeepsPlaintextAllowedForLocalClients(t *testing.T) {+ tests := []struct {+ name string+ remoteAddr string+ wantStatus int+ }{+ {name: "loopback desktop client", remoteAddr: "127.0.0.1:54321", wantStatus: http.StatusOK},+ {name: "loopback ipv6 client", remoteAddr: "[::1]:54321", wantStatus: http.StatusOK},+ {name: "other host", remoteAddr: "192.0.2.10:54321", wantStatus: http.StatusTemporaryRedirect},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ prepareTLSEnforceTest(t, true, true)+ engine := newTLSEnforceTestEngine()++ request := httptest.NewRequest(http.MethodGet, "/api/system/version", nil)+ request.RemoteAddr = test.remoteAddr+ request.Host = "notes.example.com:6806"+ recorder := httptest.NewRecorder()+ engine.ServeHTTP(recorder, request)++ if status := recorder.Result().StatusCode; status != test.wantStatus {+ t.Fatalf("status = %d, want %d", status, test.wantStatus)+ }+ })+ }+}++func TestTLSEnforceKeepsCorsPreflightAvailable(t *testing.T) {+ prepareTLSEnforceTest(t, true, true)+ engine := newTLSEnforceTestEngine()++ // 预检不携带凭据,且浏览器不会跟随跳转,因此必须继续以明文应答+ request := httptest.NewRequest(http.MethodOptions, "/api/system/getConf", nil)+ request.RemoteAddr = "192.0.2.10:54321"+ request.Host = "notes.example.com:6806"+ recorder := httptest.NewRecorder()+ engine.ServeHTTP(recorder, request)++ if status := recorder.Result().StatusCode; status != http.StatusOK {+ t.Fatalf("status = %d, want %d", status, http.StatusOK)+ }+}++func TestTLSEnforceInactiveWhenSettingDisabled(t *testing.T) {+ prepareTLSEnforceTest(t, false, true)+ engine := newTLSEnforceTestEngine()++ request := httptest.NewRequest(http.MethodGet, "/api/system/version", nil)+ request.RemoteAddr = "192.0.2.10:54321"+ request.Host = "notes.example.com:6806"+ recorder := httptest.NewRecorder()+ engine.ServeHTTP(recorder, request)++ if status := recorder.Result().StatusCode; status != http.StatusOK {+ t.Fatalf("status = %d, want %d", status, http.StatusOK)+ }+ if location := recorder.Header().Get("Location"); "" != location {+ t.Fatalf("Location = %q, want empty", location)+ }+}++func TestTLSEnforceRejectsPlaintextWithInvalidHost(t *testing.T) {+ prepareTLSEnforceTest(t, true, true)+ engine := newTLSEnforceTestEngine()++ // 被污染的 Host 不能被用来拼跳转地址+ request := httptest.NewRequest(http.MethodGet, "/api/system/version", nil)+ request.RemoteAddr = "192.0.2.10:54321"+ request.Host = "notes.example.com/evil"+ recorder := httptest.NewRecorder()+ engine.ServeHTTP(recorder, request)++ if status := recorder.Result().StatusCode; status != http.StatusBadRequest {+ t.Fatalf("status = %d, want %d", status, http.StatusBadRequest)+ }+}++func TestTLSEnforceAddsHSTSOnlyForTLSRequests(t *testing.T) {+ tests := []struct {+ name string+ serveTLS bool+ wantHSTS string+ }{+ {name: "https request", serveTLS: true, wantHSTS: util.StrictTransportSecurityValue},+ {name: "plaintext loopback request", serveTLS: false, wantHSTS: ""},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ prepareTLSEnforceTest(t, true, true)+ engine := newTLSEnforceTestEngine()++ request := httptest.NewRequest(http.MethodGet, "/api/system/version", nil)+ request.RemoteAddr = "127.0.0.1:54321"+ request.Host = "127.0.0.1:6806"+ recorder := httptest.NewRecorder()+ if test.serveTLS {+ request.TLS = &tls.ConnectionState{}+ }+ engine.ServeHTTP(recorder, request)++ if hsts := recorder.Header().Get("Strict-Transport-Security"); hsts != test.wantHSTS {+ t.Fatalf("Strict-Transport-Security = %q, want %q", hsts, test.wantHSTS)+ }+ })+ }+}++// newSessionCookieSecureTestEngine 按 Serve 的顺序注册 TLS 强制中间件、会话中间件与登录路由,+// 用于验证会话 Cookie 的 Secure 属性是否跟随实际连接。+func newSessionCookieSecureTestEngine() *gin.Engine {+ gin.SetMode(gin.TestMode)+ engine := gin.New()+ store := cookie.NewStore([]byte("test-cookie-key"))+ store.Options(ginSessions.Options{Path: "/", HttpOnly: true, SameSite: http.SameSiteLaxMode})+ engine.Use(tlsEnforceMiddleware())+ engine.Use(ginSessions.Sessions("siyuan", store))+ engine.POST("/api/system/loginAuth", func(c *gin.Context) {+ // 与 model.LoginAuth 中的写法保持一致+ ginSessions.Default(c).Options(ginSessions.Options{+ Path: "/",+ Secure: util.SSL || util.IsSecureRequest(c.Request),+ MaxAge: 60 * 60 * 24 * 30,+ HttpOnly: true,+ SameSite: http.SameSiteLaxMode,+ })+ if err := ginSessions.Default(c).Save(); err != nil {+ c.String(http.StatusInternalServerError, "save failed: %v", err)+ return+ }+ c.String(http.StatusOK, "ok")+ })+ return engine+}++// TestSessionCookieSecureFollowsConnection 覆盖 GHSA-hpj5-f7cj-vvwr 中「会话 Cookie 缺少 Secure」的要点:+// 登录会话的 Secure 属性必须跟随实际连接,而不是只跟随 --ssl 命令行参数。+func TestSessionCookieSecureFollowsConnection(t *testing.T) {+ previousSSL := util.SSL+ t.Cleanup(func() { util.SSL = previousSSL })+ util.SSL = false++ tests := []struct {+ name string+ remoteAddr string+ serveTLS bool+ host string+ wantSecure bool+ wantRedirect bool+ }{+ {name: "https connection", remoteAddr: "192.0.2.10:54321", serveTLS: true, host: "notes.example.com:6806", wantSecure: true},+ {name: "plaintext connection", remoteAddr: "192.0.2.10:54321", host: "notes.example.com:6806", wantRedirect: true},+ // 本机明文连接放行,且本机与内核自身的 HTTPS 回源等价,可以标记 Secure+ {name: "plaintext loopback connection", remoteAddr: "127.0.0.1:54321", host: "127.0.0.1:6806", wantSecure: true},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ prepareTLSEnforceTest(t, true, true)++ request := httptest.NewRequest(http.MethodPost, "/api/system/loginAuth", nil)+ request.RemoteAddr = test.remoteAddr+ request.Host = test.host+ if test.serveTLS {+ request.TLS = &tls.ConnectionState{}+ }++ recorder := httptest.NewRecorder()+ newSessionCookieSecureTestEngine().ServeHTTP(recorder, request)++ if test.wantRedirect {+ if location := recorder.Header().Get("Location"); "" == location {+ t.Fatal("plaintext request from another host was not redirected to HTTPS")+ }+ // gin 的会话中间件在响应写头时保存会话,因此跳转响应也可能带上会话 Cookie;+ // 这条明文连接上不能有任何可被回传的 Cookie,必须全部标记 Secure。+ for _, cookie := range recorder.Result().Cookies() {+ if !cookie.Secure {+ t.Fatalf("cookie %q over plaintext from another host is missing Secure", cookie.Name)+ }+ }+ return+ }++ cookies := recorder.Result().Cookies()+ if 1 != len(cookies) {+ t.Fatalf("Set-Cookie count = %d, want 1", len(cookies))+ }+ if cookies[0].Secure != test.wantSecure {+ t.Fatalf("cookie Secure = %v, want %v", cookies[0].Secure, test.wantSecure)+ }+ if !cookies[0].HttpOnly {+ t.Fatal("cookie HttpOnly = false, want true")+ }+ })+ }+}diff --git a/kernel/util/cert.go b/kernel/util/cert.goindex 2406393435..19cdfd3bfc 100644--- a/kernel/util/cert.go+++ b/kernel/util/cert.go@@ -57,6 +57,7 @@ func GetOrCreateTLSCert() (certPath, keyPath string, err error) {if hasTLSCertManager(certPath, keyPath) {logging.LogInfof("using active TLS certificate manager for [%s]", ConfDir)+ MarkTLSEnforced()return certPath, keyPath, nil}@@ -71,6 +72,7 @@ func GetOrCreateTLSCert() (certPath, keyPath string, err error) {if gulu.File.IsExist(certPath) && gulu.File.IsExist(keyPath) {if validateCert(certPath) {logging.LogInfof("using existing TLS certificates from [%s]", ConfDir)+ MarkTLSEnforced()return certPath, keyPath, nil}logging.LogInfof("existing TLS certificates are invalid or expired, regenerating...")@@ -89,6 +91,7 @@ func GetOrCreateTLSCert() (certPath, keyPath string, err error) {}logging.LogInfof("generated TLS certificates at [%s]", ConfDir)+ MarkTLSEnforced()return certPath, keyPath, nil}diff --git a/kernel/util/net.go b/kernel/util/net.goindex 6f9f8621bf..2666f7bb65 100644--- a/kernel/util/net.go+++ b/kernel/util/net.go@@ -117,6 +117,50 @@ func IsLocalHost(host string) bool {}}+// IsTLSRequest 判断这条客户端连接是否使用 TLS。+//+// 内核在同一端口上通过 cmux 同时承载 HTTP 与 HTTPS,入站方向由 request.TLS 区分;+// 固定端口反代以 HTTPS 回源内核,由它写入的 ClientTLSHeader 标记客户端侧的真实连接方式。+// 该头部仅在直连对端为本机时采信,避免调用方伪造 https 让 Cookie 被错误标记为 Secure。+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hpj5-f7cj-vvwr+func IsTLSRequest(request *http.Request) bool {+ if nil == request {+ return false+ }+ if nil != request.TLS {+ return true+ }+ return ClientTLSHeaderValue == request.Header.Get(ClientTLSHeader) && IsLocalHost(request.RemoteAddr)+}++// IsValidRedirectHost 校验 Host 是否为可以直接拼接跳转地址的合法 authority。+//+// 仅接受 host 或 host:port 形式,拒绝凭据、路径、查询、片段与空白字符,+// 避免被污染的 Host 让 HTTPS 跳转指向站外地址。+func IsValidRedirectHost(host string) bool {+ if "" == host || host != strings.TrimSpace(host) || strings.ContainsAny(host, "/\\?#@,; \t\r\n") {+ return false+ }++ hostname, port, err := net.SplitHostPort(host)+ if err != nil {+ // 不含端口时按纯主机名处理,但要排除 "host:" 这类残留冒号的写法+ if strings.Contains(host, ":") {+ return false+ }+ hostname, port = host, ""+ }+ if "" == hostname || (strings.Contains(host, ":") && "" == port) {+ return false+ }+ if "" != port {+ if portNum, portErr := strconv.Atoi(port); nil != portErr || 1 > portNum || 65535 < portNum {+ return false+ }+ }+ return true+}+func IsLocalOrigin(origin string) bool {if u, err := url.Parse(origin); err == nil {return IsLocalHostname(u.Hostname())diff --git a/kernel/util/transport.go b/kernel/util/transport.gonew file mode 100644index 0000000000..aee5e8c1c4--- /dev/null+++ b/kernel/util/transport.go@@ -0,0 +1,136 @@+// SiYuan - From thought to insight, with agents+// 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 (+ "net/http"+ "strings"+ "sync"++ "github.com/siyuan-note/logging"+)++const (+ // ClientTLSHeader 由本机的固定端口反向代理写入,标记客户端到反代这一跳是否使用 TLS。+ // 反代以 HTTPS 回源内核,若不额外标记,内核会把明文客户端的连接误判为 TLS。+ ClientTLSHeader = "X-SiYuan-Client-TLS"++ // ClientTLSHeaderValue 为标记 TLS 的头部取值。+ ClientTLSHeaderValue = "1"++ // StrictTransportSecurityValue 为 HTTPS 响应附加的 HSTS 策略,有效期一年。+ StrictTransportSecurityValue = "max-age=31536000"+)++// tlsEnforced 记录当前进程是否已经在提供 HTTPS 服务。+// 内核、固定端口反代与发布服务共用同一份工作空间证书,任一环节准备好证书即可开启强制跳转。+var (+ tlsEnforcedMu sync.Mutex+ tlsEnforced bool++ // tlsRequired 记录用户是否开启了「启用 HTTPS」。证书可用且该值为真时才强制跳转。+ tlsRequired bool+)++// MarkTLSEnforced 记录进程已经开始提供 HTTPS 服务。+func MarkTLSEnforced() {+ tlsEnforcedMu.Lock()+ defer tlsEnforcedMu.Unlock()+ tlsEnforced = true+}++// TLSEnforced 返回当前进程是否已经在提供 HTTPS 服务。+func TLSEnforced() bool {+ tlsEnforcedMu.Lock()+ defer tlsEnforcedMu.Unlock()+ return tlsEnforced+}++// SetTLSRequired 记录用户是否开启了「启用 HTTPS」。+func SetTLSRequired(required bool) {+ tlsEnforcedMu.Lock()+ defer tlsEnforcedMu.Unlock()+ tlsRequired = required+}++// TLSRequired 返回用户是否开启了「启用 HTTPS」。+func TLSRequired() bool {+ tlsEnforcedMu.Lock()+ defer tlsEnforcedMu.Unlock()+ return tlsRequired+}++// TLSRedirectHandler 包装明文 HTTP 处理器:用户开启「启用 HTTPS」且证书可用时,+// 把来自其他主机的明文请求重定向到 HTTPS,本机明文请求与 CORS 预检放行。+//+// 内核通过 cmux 在同一端口同时承载 HTTP 与 HTTPS,固定端口反代与发布服务也是双栈,+// 仅在监听层无法阻止明文访问,因此三条入口都要经过这里。+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hpj5-f7cj-vvwr+func TLSRedirectHandler(handler http.Handler) http.Handler {+ return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {+ if ShouldRedirectToTLS(request) && TLSRequired() && TLSEnforced() {+ host := request.Host+ if "" == host || !IsValidRedirectHost(host) {+ logging.LogWarnf("reject plaintext request with invalid host [host=%s, remote=%s]",+ host, GetRemoteAddr(request))+ writer.WriteHeader(http.StatusBadRequest)+ return+ }++ // 跳转目标取决于 Host,声明 Vary 避免中间缓存串用响应+ writer.Header().Set("Vary", "Host")+ writer.Header().Set("Location", "https://"+host+request.URL.RequestURI())+ writer.WriteHeader(http.StatusTemporaryRedirect)+ return+ }++ if IsTLSRequest(request) {+ writer.Header().Set("Strict-Transport-Security", StrictTransportSecurityValue)+ }++ handler.ServeHTTP(writer, request)+ })+}++// IsSecureRequest 判断请求是否可以安全地承载会话凭据:+// 客户端连接使用 TLS,或来自本机(本机明文与内核自身 HTTPS 回源等价,且桌面端与浏览器扩展依赖本机明文)。+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hpj5-f7cj-vvwr+func IsSecureRequest(request *http.Request) bool {+ if IsTLSRequest(request) {+ return true+ }+ return IsLocalHost(request.RemoteAddr)+}++// ShouldRedirectToTLS 判断当前请求是否需要被重定向到 HTTPS。+//+// 仅在用户开启「启用 HTTPS」且证书可用时生效;本机明文请求放行,否则桌面端、+// 固定端口反代与浏览器扩展无法连接。CORS 预检不携带凭据且浏览器不跟随跳转,故同样放行。+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hpj5-f7cj-vvwr+func ShouldRedirectToTLS(request *http.Request) bool {+ if nil == request {+ return false+ }+ if IsTLSRequest(request) || IsLocalHost(request.RemoteAddr) {+ return false+ }+ if http.MethodOptions == request.Method {+ return false+ }+ // HTTPS 端口上不接受绝对形式或带协议相对前缀的请求目标,避免拼出站外跳转地址+ return !strings.HasPrefix(request.RequestURI, "//") && !strings.Contains(request.RequestURI, "://")+}diff --git a/kernel/util/transport_test.go b/kernel/util/transport_test.gonew file mode 100644index 0000000000..bd042be7ba--- /dev/null+++ b/kernel/util/transport_test.go@@ -0,0 +1,113 @@+// SiYuan - From thought to insight, with agents+// 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 (+ "crypto/tls"+ "net/http"+ "net/http/httptest"+ "testing"+)++func TestShouldRedirectToTLS(t *testing.T) {+ tests := []struct {+ name string+ method string+ remoteAddr string+ requestURI string+ tls bool+ want bool+ }{+ {name: "plaintext from other host", method: http.MethodPost, remoteAddr: "192.0.2.10:54321", requestURI: "/api/system/loginAuth", want: true},+ {name: "plaintext loopback", method: http.MethodPost, remoteAddr: "127.0.0.1:54321", requestURI: "/api/system/loginAuth", want: false},+ {name: "plaintext ipv6 loopback", method: http.MethodGet, remoteAddr: "[::1]:54321", requestURI: "/api/system/version", want: false},+ {name: "cors preflight from other host", method: http.MethodOptions, remoteAddr: "192.0.2.10:54321", requestURI: "/api/system/getConf", want: false},+ {name: "already tls", method: http.MethodGet, remoteAddr: "192.0.2.10:54321", requestURI: "/api/system/version", tls: true, want: false},+ {name: "absolute request target", method: http.MethodGet, remoteAddr: "192.0.2.10:54321", requestURI: "https://evil.example/", want: false},+ {name: "protocol relative request target", method: http.MethodGet, remoteAddr: "192.0.2.10:54321", requestURI: "//evil.example/", want: false},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ request := httptest.NewRequest(test.method, "/", nil)+ request.RemoteAddr = test.remoteAddr+ request.RequestURI = test.requestURI+ if test.tls {+ request.TLS = &tls.ConnectionState{}+ }+ if actual := ShouldRedirectToTLS(request); actual != test.want {+ t.Fatalf("ShouldRedirectToTLS = %v, want %v", actual, test.want)+ }+ })+ }+}++func TestIsTLSRequestHonorsClientTLSHeaderOnlyForLocalPeers(t *testing.T) {+ request := httptest.NewRequest(http.MethodGet, "/", nil)+ request.RemoteAddr = "127.0.0.1:54321"+ request.Header.Set(ClientTLSHeader, ClientTLSHeaderValue)+ if !IsTLSRequest(request) {+ t.Fatal("local proxy marked connection should be treated as TLS")+ }++ // 远端调用方伪造同名头部不得被采信+ request.RemoteAddr = "192.0.2.10:54321"+ if IsTLSRequest(request) {+ t.Fatal("forged client TLS header from a remote peer must be ignored")+ }+}++func TestIsSecureRequest(t *testing.T) {+ tests := []struct {+ name string+ remoteAddr string+ tls bool+ want bool+ }{+ {name: "tls connection", remoteAddr: "192.0.2.10:54321", tls: true, want: true},+ {name: "plaintext loopback", remoteAddr: "127.0.0.1:54321", want: true},+ {name: "plaintext from other host", remoteAddr: "192.0.2.10:54321", want: false},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ request := httptest.NewRequest(http.MethodGet, "/", nil)+ request.RemoteAddr = test.remoteAddr+ if test.tls {+ request.TLS = &tls.ConnectionState{}+ }+ if actual := IsSecureRequest(request); actual != test.want {+ t.Fatalf("IsSecureRequest = %v, want %v", actual, test.want)+ }+ })+ }+}++func TestIsValidRedirectHost(t *testing.T) {+ valid := []string{"notes.example.com", "notes.example.com:6806", "127.0.0.1:6806", "[::1]:6806", "localhost"}+ for _, host := range valid {+ if !IsValidRedirectHost(host) {+ t.Fatalf("IsValidRedirectHost(%q) = false, want true", host)+ }+ }++ invalid := []string{"", " notes.example.com", "notes.example.com/evil", "notes.example.com:evil",+ "notes.example.com:0", "notes.example.com:70000", "[email protected]", "notes.example.com#x", "notes.example.com:"}+ for _, host := range invalid {+ if IsValidRedirectHost(host) {+ t.Fatalf("IsValidRedirectHost(%q) = true, want false", host)+ }+ }+}