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: Validate session-cookie Origin and set SameSite on the session cookie https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hhm2-g993-p656
Daniel292965e0389f
diff --git a/kernel/model/session.go b/kernel/model/session.goindex 4eb38c7fd1..fc8537468a 100644--- a/kernel/model/session.go+++ b/kernel/model/session.go@@ -309,6 +309,13 @@ func CheckAuth(c *gin.Context) {session := util.GetSession(c)workspaceSession := util.GetWorkspaceSession(session)if workspaceSession.AccessAuthCode == Conf.AccessAuthCode {+ // 校验 Origin 防止跨站请求伪造 https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hhm2-g993-p656+ if !util.IsSessionOriginAllowed(c.GetHeader("Origin"), c.GetHeader("Host")) {+ logging.LogWarnf("invalid Origin [%s] for session auth [ip=%s]", c.GetHeader("Origin"), c.ClientIP())+ c.JSON(http.StatusUnauthorized, map[string]any{"code": -1, "msg": "Auth failed: invalid Origin"})+ c.Abort()+ return+ }c.Set(RoleContextKey, RoleAdministrator)c.Next()returndiff --git a/kernel/server/serve.go b/kernel/server/serve.goindex 2b4c8b81c3..1f521fc0f3 100644--- a/kernel/server/serve.go+++ b/kernel/server/serve.go@@ -160,6 +160,7 @@ func Serve(fastMode bool, cookieKey string) {Secure: util.SSL,//MaxAge: 60 * 60 * 24 * 7, // 默认是 SessionHttpOnly: true,+ SameSite: http.SameSiteLaxMode, // 防止跨站请求伪造 https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hhm2-g993-p656})ginServer.Use(sessions.Sessions("siyuan", sessionStore))diff --git a/kernel/util/net.go b/kernel/util/net.goindex 3ea4fec662..3cc6ac2e72 100644--- a/kernel/util/net.go+++ b/kernel/util/net.go@@ -123,6 +123,30 @@ func IsLocalOrigin(origin string) bool {return false}+// IsSessionOriginAllowed 校验会话 Cookie 认证请求的 Origin,防止跨站请求伪造+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hhm2-g993-p656+func IsSessionOriginAllowed(origin, host string) bool {+ if "" == origin {+ // 浏览器发起的跨站请求都会携带 Origin,未携带时视为非浏览器客户端,按原行为放行+ return true+ }+ if IsLocalOrigin(origin) {+ return true+ }+ // 已设置锁屏密码的远程访问场景:Origin 必须与 Host 一致,否则视为跨站请求+ return originHostEquals(origin, host)+}++func originHostEquals(origin, host string) bool {+ u, err := url.Parse(origin)+ if nil != err {+ return false+ }+ originHost := strings.ToLower(strings.TrimSuffix(strings.TrimSuffix(u.Host, ":80"), ":443"))+ host = strings.ToLower(strings.TrimSuffix(strings.TrimSuffix(strings.TrimSpace(host), ":80"), ":443"))+ return "" != originHost && originHost == host+}+// SSRFSafeDialer returns a net.Dialer whose Control hook blocks private, loopback, link-local and unspecified IPs.func SSRFSafeDialer(timeout time.Duration) *net.Dialer {return &net.Dialer{diff --git a/kernel/util/net_test.go b/kernel/util/net_test.gonew file mode 100644index 0000000000..10093acf49--- /dev/null+++ b/kernel/util/net_test.go@@ -0,0 +1,50 @@+// 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 "testing"++// TestIsSessionOriginAllowed 验证会话 Cookie 认证的 Origin 校验逻辑+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hhm2-g993-p656+func TestIsSessionOriginAllowed(t *testing.T) {+ tests := []struct {+ name string+ origin string+ host string+ want bool+ }{+ {name: "no origin header", origin: "", host: "127.0.0.1:6806", want: true},+ {name: "local loopback origin", origin: "http://127.0.0.1:6806", host: "127.0.0.1:6806", want: true},+ {name: "localhost origin", origin: "http://localhost:6806", host: "localhost:6806", want: true},+ {name: "cross-site origin", origin: "https://evil.example", host: "127.0.0.1:6806", want: false},+ {name: "remote access with matching host", origin: "http://192.168.1.5:6806", host: "192.168.1.5:6806", want: true},+ {name: "remote access with same host different port", origin: "http://192.168.1.5:8080", host: "192.168.1.5:6806", want: false},+ {name: "default port normalized", origin: "http://192.168.1.5:80", host: "192.168.1.5", want: true},+ {name: "https default port normalized", origin: "https://192.168.1.5:443", host: "192.168.1.5", want: true},+ {name: "null origin", origin: "null", host: "127.0.0.1:6806", want: false},+ {name: "malformed origin", origin: "://bad", host: "127.0.0.1:6806", want: false},+ {name: "origin without host header", origin: "http://192.168.1.5:6806", host: "", want: false},+ }++ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ if got := IsSessionOriginAllowed(test.origin, test.host); got != test.want {+ t.Fatalf("IsSessionOriginAllowed(%q, %q) = %v, want %v", test.origin, test.host, got, test.want)+ }+ })+ }+}