An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Allow same-origin remote browser session authentication
Daniel34a525f69b19
diff --git a/kernel/model/session.go b/kernel/model/session.goindex f9d6fd2660..d0e14a765d 100644--- a/kernel/model/session.go+++ b/kernel/model/session.go@@ -255,7 +255,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/9180clientIP := c.ClientIP()- host := c.GetHeader("Host")+ host := c.Request.Hostorigin := c.GetHeader("Origin")forwardedHost := c.GetHeader("X-Forwarded-Host")if !localhost ||@@ -312,7 +312,7 @@ func CheckAuth(c *gin.Context) {workspaceSession := util.GetWorkspaceSession(session)if IsWorkspaceSessionAuthenticated(workspaceSession) {// 校验 Origin 防止跨站请求伪造 https://github.com/siyuan-note/siyuan/security/advisories/GHSA-hhm2-g993-p656- if !util.IsSessionOriginAllowed(c.GetHeader("Origin"), c.GetHeader("Host")) {+ if !util.IsSessionOriginAllowed(c.GetHeader("Origin"), c.Request.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()diff --git a/kernel/model/session_test.go b/kernel/model/session_test.goindex b13653e6d7..d43226bc33 100644--- a/kernel/model/session_test.go+++ b/kernel/model/session_test.go@@ -21,6 +21,8 @@ import ("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/util"@@ -136,3 +138,60 @@ func TestIsLocalRequest(t *testing.T) {})}}++// TestCheckAuthRemoteSessionOrigin 验证局域网浏览器登录后,同源 POST 请求可通过会话鉴权。+func TestCheckAuthRemoteSessionOrigin(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()+ store := cookie.NewStore([]byte("test-session-cookie-key"))+ engine.Use(ginSessions.Sessions("siyuan", store))+ engine.GET("/login", func(c *gin.Context) {+ session := util.GetSession(c)+ workspaceSession := util.GetWorkspaceSession(session)+ workspaceSession.AccessAuthCode = Conf.AccessAuthCode+ if err := session.Save(c); err != nil {+ c.Status(http.StatusInternalServerError)+ return+ }+ c.Status(http.StatusNoContent)+ })+ engine.POST("/api/notebook/lsNotebooks", CheckAuth, func(c *gin.Context) {+ c.Status(http.StatusNoContent)+ })++ loginRequest := httptest.NewRequest(http.MethodGet, "http://192.0.2.1:6806/login", nil)+ loginRequest.RemoteAddr = "192.0.2.2:1234"+ loginRecorder := httptest.NewRecorder()+ engine.ServeHTTP(loginRecorder, loginRequest)+ if loginRecorder.Code != http.StatusNoContent {+ t.Fatalf("login status = %d, want %d", loginRecorder.Code, http.StatusNoContent)+ }++ request := func(origin string) *httptest.ResponseRecorder {+ request := httptest.NewRequest(http.MethodPost, "http://192.0.2.1:6806/api/notebook/lsNotebooks", nil)+ request.RemoteAddr = "192.0.2.2:1234"+ request.Header.Set("Origin", origin)+ for _, responseCookie := range loginRecorder.Result().Cookies() {+ request.AddCookie(responseCookie)+ }+ recorder := httptest.NewRecorder()+ engine.ServeHTTP(recorder, request)+ return recorder+ }++ if recorder := request("http://192.0.2.1:6806"); recorder.Code != http.StatusNoContent {+ t.Fatalf("same-origin status = %d, want %d, body = %s", recorder.Code, http.StatusNoContent, recorder.Body.String())+ }+ if recorder := request("https://evil.example"); recorder.Code != http.StatusUnauthorized {+ t.Fatalf("cross-origin status = %d, want %d", recorder.Code, http.StatusUnauthorized)+ }+}