Skip to content

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

:bug: Allow authenticated same-site app navigation https://github.com/siyuan-note/siyuan/issues/19810

Danielae092ff92789
diff --git a/kernel/model/session.go b/kernel/model/session.go
index d30ebf861a..4f1413e293 100644
--- a/kernel/model/session.go
+++ b/kernel/model/session.go
@@ -313,8 +313,9 @@ func CheckAuth(c *gin.Context) {
// 同时拒绝浏览器标记的跨站请求,防止跨站 GET 导航不带 Origin 时绕过校验
// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-2w6q-wgc8-q743
if !util.IsSessionOriginAllowedRequest(c.Request) {
- 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"})
+ logging.LogWarnf("invalid session request origin [origin=%s, fetch-site=%s, ip=%s]",
+ c.GetHeader("Origin"), c.GetHeader("Sec-Fetch-Site"), c.ClientIP())
+ c.JSON(http.StatusUnauthorized, map[string]any{"code": -1, "msg": "Auth failed: invalid request origin"})
c.Abort()
return
}
diff --git a/kernel/model/session_test.go b/kernel/model/session_test.go
index e2ad4026ec..09b730ac4e 100644
--- a/kernel/model/session_test.go
+++ b/kernel/model/session_test.go
@@ -423,9 +423,9 @@ func TestCheckAuthLoopbackProxy(t *testing.T) {
}
for _, test := range []struct {
- name, method, path, origin, site string
- authenticated bool
- want int
+ name, method, path, origin, site, mode, dest string
+ authenticated bool
+ want int
}{
{name: "page redirects to login", method: http.MethodGet, path: "/stage/build/desktop/", site: "none", want: http.StatusFound},
{name: "login page accessible", method: http.MethodGet, path: "/check-auth", site: "same-origin", want: http.StatusNoContent},
@@ -438,6 +438,9 @@ func TestCheckAuthLoopbackProxy(t *testing.T) {
{name: "authenticated cross-origin API denied", method: http.MethodPost, path: "/api/test", origin: "https://evil.example", authenticated: true, want: http.StatusUnauthorized},
{name: "authenticated cross-site navigation denied", method: http.MethodGet, path: "/assets/icon.png", site: "cross-site", authenticated: true, want: http.StatusUnauthorized},
{name: "authenticated same-site request denied", method: http.MethodPost, path: "/api/system/exit", site: "same-site", authenticated: true, want: http.StatusUnauthorized},
+ {name: "authenticated same-site app navigation", method: http.MethodGet, path: "/stage/build/desktop/", site: "same-site", mode: "navigate", dest: "document", authenticated: true, want: http.StatusNoContent},
+ {name: "authenticated same-site API navigation denied", method: http.MethodGet, path: "/api/test", site: "same-site", mode: "navigate", dest: "document", authenticated: true, want: http.StatusUnauthorized},
+ {name: "authenticated cross-site app navigation denied", method: http.MethodGet, path: "/stage/build/desktop/", site: "cross-site", mode: "navigate", dest: "document", authenticated: true, want: http.StatusUnauthorized},
} {
t.Run(test.name, func(t *testing.T) {
request := httptest.NewRequest(test.method, test.path, nil)
@@ -447,6 +450,8 @@ func TestCheckAuthLoopbackProxy(t *testing.T) {
request.Header.Set("X-Forwarded-Host", request.Host)
request.Header.Set("Origin", test.origin)
request.Header.Set("Sec-Fetch-Site", test.site)
+ request.Header.Set("Sec-Fetch-Mode", test.mode)
+ request.Header.Set("Sec-Fetch-Dest", test.dest)
if test.authenticated {
for _, responseCookie := range loginRecorder.Result().Cookies() {
request.AddCookie(responseCookie)
diff --git a/kernel/util/net.go b/kernel/util/net.go
index 8feeedba85..603296a9b8 100644
--- a/kernel/util/net.go
+++ b/kernel/util/net.go
@@ -147,16 +147,29 @@ func IsCrossSiteFetchSite(site string) bool {
return "" != site && "same-origin" != site && "none" != site
}
-// IsSessionOriginAllowedRequest 校验会话认证请求是否允许放行:浏览器标记的跨站请求直接拒绝,
-// 其余请求继续校验 Origin。
+// IsSessionOriginAllowedRequest 校验会话认证请求:同站子域的应用页面导航可放行,
+// 其他跨源请求拒绝,其余请求继续校验 Origin。
// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-2w6q-wgc8-q743
func IsSessionOriginAllowedRequest(r *http.Request) bool {
if IsCrossSiteFetchSite(r.Header.Get("Sec-Fetch-Site")) {
- return false
+ // 已登录用户可从同站子域进入应用页面;跨站导航和同站 API 请求仍须拒绝。
+ return r.Header.Get("Sec-Fetch-Site") == "same-site" &&
+ r.Method == http.MethodGet && r.Header.Get("Origin") == "" &&
+ r.Header.Get("Sec-Fetch-Mode") == "navigate" &&
+ r.Header.Get("Sec-Fetch-Dest") == "document" && isAppEntryPath(r.URL.Path)
}
return IsSessionOriginAllowed(r.Header.Get("Origin"), r.Host)
}
+func isAppEntryPath(path string) bool {
+ switch path {
+ case "/", "/stage/build/app/", "/stage/build/desktop/", "/stage/build/mobile/",
+ "/stage/build/app/index.html", "/stage/build/desktop/index.html", "/stage/build/mobile/index.html":
+ return true
+ }
+ return false
+}
+
func originHostEquals(origin, host string) bool {
u, err := url.Parse(origin)
if nil != err {
diff --git a/kernel/util/net_test.go b/kernel/util/net_test.go
index 05094b6e6a..fd77511665 100644
--- a/kernel/util/net_test.go
+++ b/kernel/util/net_test.go
@@ -90,10 +90,20 @@ func TestIsSessionOriginAllowedRequest(t *testing.T) {
site string
origin string
host string
+ path string
+ mode string
+ dest string
want bool
}{
{name: "cross-site navigation without Origin", site: "cross-site", origin: "", host: "127.0.0.1:6806", want: false},
{name: "same-site without Origin", site: "same-site", origin: "", host: "127.0.0.1:6806", want: false},
+ {name: "same-site app navigation", site: "same-site", host: "note.example.com", path: "/stage/build/desktop/", mode: "navigate", dest: "document", want: true},
+ {name: "same-site root navigation", site: "same-site", host: "note.example.com", path: "/", mode: "navigate", dest: "document", want: true},
+ {name: "same-site API navigation denied", site: "same-site", host: "note.example.com", path: "/api/system/exit", mode: "navigate", dest: "document", want: false},
+ {name: "same-site asset navigation denied", site: "same-site", host: "note.example.com", path: "/assets/file.png", mode: "navigate", dest: "document", want: false},
+ {name: "same-site app fetch denied", site: "same-site", host: "note.example.com", path: "/stage/build/desktop/", mode: "cors", dest: "empty", want: false},
+ {name: "same-site app navigation with Origin denied", site: "same-site", origin: "https://home.example.com", host: "note.example.com", path: "/stage/build/desktop/", mode: "navigate", dest: "document", want: false},
+ {name: "cross-site app navigation denied", site: "cross-site", host: "note.example.com", path: "/stage/build/desktop/", mode: "navigate", dest: "document", want: false},
{name: "same-origin without Origin", site: "same-origin", origin: "", host: "127.0.0.1:6806", want: true},
{name: "none without Origin", site: "none", origin: "", host: "127.0.0.1:6806", want: true},
{name: "absent fetch site with local origin", site: "", origin: "http://127.0.0.1:6806", host: "127.0.0.1:6806", want: true},
@@ -103,9 +113,15 @@ func TestIsSessionOriginAllowedRequest(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
- request := httptest.NewRequest(http.MethodGet, "http://"+test.host+"/", nil)
+ path := test.path
+ if path == "" {
+ path = "/"
+ }
+ request := httptest.NewRequest(http.MethodGet, "http://"+test.host+path, nil)
request.Header.Set("Sec-Fetch-Site", test.site)
request.Header.Set("Origin", test.origin)
+ request.Header.Set("Sec-Fetch-Mode", test.mode)
+ request.Header.Set("Sec-Fetch-Dest", test.dest)
if got := IsSessionOriginAllowedRequest(request); got != test.want {
t.Fatalf("IsSessionOriginAllowedRequest(site=%q, origin=%q, host=%q) = %v, want %v", test.site, test.origin, test.host, got, test.want)
}