Skip to content

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

:lock: Validate web origin for lock screen local host pass-through requests https://github.com/siyuan-note/siyuan/security/advisories/GHSA-9gpj-3rm3-x42m

Danielb2e7ef77feea
diff --git a/kernel/model/session.go b/kernel/model/session.go
index d9cb6e8bd6..0d8895351b 100644
--- a/kernel/model/session.go
+++ b/kernel/model/session.go
@@ -262,15 +262,7 @@ func CheckAuth(c *gin.Context) {
}
// Authenticate requests with the Origin header other than 127.0.0.1 https://github.com/siyuan-note/siyuan/issues/9180
- clientIP := c.ClientIP()
- host := c.Request.Host
- origin := c.GetHeader("Origin")
- forwardedHost := c.GetHeader("X-Forwarded-Host")
- if !localhost ||
- ("" != clientIP && !util.IsLocalHostname(clientIP)) ||
- ("" != host && !util.IsLocalHost(host)) ||
- ("" != origin && !util.IsLocalOrigin(origin)) ||
- ("" != forwardedHost && !util.IsLocalHost(forwardedHost)) {
+ if !localhost || !isLocalHostRequestAllowed(c) {
c.JSON(http.StatusUnauthorized, map[string]any{"code": -1, "msg": "Auth failed: for security reasons, please set [Lock screen password] when using non-127.0.0.1 access\n\n为安全起见,使用非 127.0.0.1 访问时请设置 [锁屏密码]"})
c.Abort()
return
@@ -291,6 +283,15 @@ func CheckAuth(c *gin.Context) {
// 放过来自本机的某些请求
if localhost {
+ // 校验浏览器来源,防止恶意网页借助受害者浏览器作为环回客户端绕过锁屏鉴权
+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-9gpj-3rm3-x42m
+ if util.IsCrossSiteFetchSite(c.GetHeader("Sec-Fetch-Site")) || !isLocalHostRequestAllowed(c) {
+ logging.LogWarnf("invalid local host pass-through request [ip=%s, origin=%s, host=%s, uri=%s]",
+ c.ClientIP(), c.GetHeader("Origin"), c.Request.Host, c.Request.RequestURI)
+ c.JSON(http.StatusUnauthorized, map[string]any{"code": -1, "msg": "Auth failed: invalid request origin"})
+ c.Abort()
+ return
+ }
if strings.HasPrefix(c.Request.RequestURI, "/assets/") || strings.HasPrefix(c.Request.RequestURI, "/export/") {
c.Set(RoleContextKey, RoleAdministrator)
c.Next()
@@ -437,6 +438,21 @@ func IsLocalRequest(c *gin.Context) bool {
return util.IsLocalHost(c.Request.RemoteAddr) && util.IsLocalHostname(c.ClientIP())
}
+// isLocalHostRequestAllowed 判断本机放行请求是否可信任:
+// 要求 clientIP/host/origin/forwardedHost 均为本机地址,防止恶意网页借助受害者浏览器
+// 作为环回客户端绕过鉴权(DNS 重绑定场景通过 Host 校验兜底)
+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-9gpj-3rm3-x42m
+func isLocalHostRequestAllowed(c *gin.Context) bool {
+ clientIP := c.ClientIP()
+ host := c.Request.Host
+ origin := c.GetHeader("Origin")
+ forwardedHost := c.GetHeader("X-Forwarded-Host")
+ return ("" == clientIP || util.IsLocalHostname(clientIP)) &&
+ ("" == host || util.IsLocalHost(host)) &&
+ ("" == origin || util.IsLocalOrigin(origin)) &&
+ ("" == forwardedHost || util.IsLocalHost(forwardedHost))
+}
+
func CheckAdminRole(c *gin.Context) {
if IsAdminRoleContext(c) {
c.Next()
diff --git a/kernel/model/session_test.go b/kernel/model/session_test.go
index 9640c0b204..e9e2755bb9 100644
--- a/kernel/model/session_test.go
+++ b/kernel/model/session_test.go
@@ -299,3 +299,87 @@ func TestCheckAuthRemoteSessionOrigin(t *testing.T) {
t.Fatalf("cross-origin status = %d, want %d", recorder.Code, http.StatusUnauthorized)
}
}
+
+// TestCheckAuthLockScreenLocalHostPassThrough 验证设置锁屏密码时本机放行分支拒绝
+// 浏览器标记的跨站请求与非本机 Origin/Host 请求,无浏览器头的本机客户端及同源请求放行
+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-9gpj-3rm3-x42m
+func TestCheckAuthLockScreenLocalHostPassThrough(t *testing.T) {
+ originalConf := Conf
+ originalWorkspaceDir := util.WorkspaceDir
+ Conf = NewAppConf()
+ Conf.AccessAuthCode = "test-access-auth-code"
+ util.WorkspaceDir = "test-workspace"
+ t.Cleanup(func() {
+ Conf = originalConf
+ util.WorkspaceDir = originalWorkspaceDir
+ })
+
+ engine := gin.New()
+ engine.GET("/assets/icon.png", CheckAuth, func(c *gin.Context) {
+ c.Status(http.StatusNoContent)
+ })
+ engine.POST("/api/system/exit", CheckAuth, func(c *gin.Context) {
+ c.Status(http.StatusNoContent)
+ })
+
+ newRequest := func(method, target, host string, headers map[string]string) *httptest.ResponseRecorder {
+ request := httptest.NewRequest(method, target, nil)
+ request.RemoteAddr = "127.0.0.1:1234"
+ request.Host = "127.0.0.1:6806"
+ if "" != host {
+ request.Host = host
+ }
+ for key, value := range headers {
+ request.Header.Set(key, value)
+ }
+ recorder := httptest.NewRecorder()
+ engine.ServeHTTP(recorder, request)
+ return recorder
+ }
+
+ t.Run("headless local client allowed", func(t *testing.T) {
+ if recorder := newRequest(http.MethodGet, "/assets/icon.png", "", nil); recorder.Code != http.StatusNoContent {
+ t.Fatalf("assets status = %d, want %d", recorder.Code, http.StatusNoContent)
+ }
+ if recorder := newRequest(http.MethodPost, "/api/system/exit", "", nil); recorder.Code != http.StatusNoContent {
+ t.Fatalf("exit status = %d, want %d", recorder.Code, http.StatusNoContent)
+ }
+ })
+
+ t.Run("same-origin browser request allowed", func(t *testing.T) {
+ headers := map[string]string{
+ "Sec-Fetch-Site": "same-origin",
+ "Origin": "http://127.0.0.1:6806",
+ }
+ if recorder := newRequest(http.MethodPost, "/api/system/exit", "", headers); recorder.Code != http.StatusNoContent {
+ t.Fatalf("exit status = %d, want %d", recorder.Code, http.StatusNoContent)
+ }
+ })
+
+ t.Run("cross-site browser request denied", func(t *testing.T) {
+ headers := map[string]string{
+ "Sec-Fetch-Site": "cross-site",
+ }
+ if recorder := newRequest(http.MethodGet, "/assets/icon.png", "", headers); recorder.Code != http.StatusUnauthorized {
+ t.Fatalf("assets status = %d, want %d", recorder.Code, http.StatusUnauthorized)
+ }
+ if recorder := newRequest(http.MethodPost, "/api/system/exit", "", headers); recorder.Code != http.StatusUnauthorized {
+ t.Fatalf("exit status = %d, want %d", recorder.Code, http.StatusUnauthorized)
+ }
+ })
+
+ t.Run("non-local origin denied", func(t *testing.T) {
+ headers := map[string]string{
+ "Origin": "https://evil.example",
+ }
+ if recorder := newRequest(http.MethodPost, "/api/system/exit", "", headers); recorder.Code != http.StatusUnauthorized {
+ t.Fatalf("exit status = %d, want %d", recorder.Code, http.StatusUnauthorized)
+ }
+ })
+
+ t.Run("non-local host denied", func(t *testing.T) {
+ if recorder := newRequest(http.MethodGet, "/assets/icon.png", "evil.example", nil); recorder.Code != http.StatusUnauthorized {
+ t.Fatalf("assets status = %d, want %d", recorder.Code, http.StatusUnauthorized)
+ }
+ })
+}