Skip to content

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

:bug: Allow authenticated PWA entry navigation https://github.com/siyuan-note/siyuan/issues/20058

Daniel69027a2e5e36
diff --git a/kernel/model/session_test.go b/kernel/model/session_test.go
index 135aa3451d..8199e8e953 100644
--- a/kernel/model/session_test.go
+++ b/kernel/model/session_test.go
@@ -408,7 +408,7 @@ func TestCheckAuthLoopbackProxy(t *testing.T) {
}
c.Status(http.StatusNoContent)
})
- for _, path := range []string{"/stage/build/desktop/", "/check-auth", "/assets/icon.png", "/api/system/exit", "/api/test"} {
+ for _, path := range []string{"/", "/stage/build/mobile/", "/stage/build/desktop/", "/check-auth", "/assets/icon.png", "/api/system/exit", "/api/test"} {
engine.Any(path, CheckAuth, func(c *gin.Context) {
if c.Request.URL.Path != "/check-auth" && !IsAdminRoleContext(c) {
t.Error("authenticated request has no administrator role")
@@ -441,7 +441,11 @@ func TestCheckAuthLoopbackProxy(t *testing.T) {
{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},
+ {name: "PWA navigation still requires login", method: http.MethodGet, path: "/", site: "cross-site", mode: "navigate", dest: "document", want: http.StatusFound},
+ {name: "authenticated PWA root navigation", method: http.MethodGet, path: "/", site: "cross-site", mode: "navigate", dest: "document", authenticated: true, want: http.StatusNoContent},
+ {name: "authenticated PWA mobile navigation", method: http.MethodGet, path: "/stage/build/mobile/", site: "cross-site", mode: "navigate", dest: "document", authenticated: true, want: http.StatusNoContent},
+ {name: "authenticated cross-site app navigation", method: http.MethodGet, path: "/stage/build/desktop/", site: "cross-site", mode: "navigate", dest: "document", authenticated: true, want: http.StatusNoContent},
+ {name: "authenticated cross-site API navigation denied", method: http.MethodGet, path: "/api/test", 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)
diff --git a/kernel/util/net.go b/kernel/util/net.go
index 603296a9b8..6e5d40c700 100644
--- a/kernel/util/net.go
+++ b/kernel/util/net.go
@@ -147,13 +147,14 @@ func IsCrossSiteFetchSite(site string) bool {
return "" != site && "same-origin" != site && "none" != site
}
-// IsSessionOriginAllowedRequest 校验会话认证请求:同站子域的应用页面导航可放行,
+// IsSessionOriginAllowedRequest 校验会话认证请求:应用入口的顶层 GET 页面导航可放行,
// 其他跨源请求拒绝,其余请求继续校验 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")) {
- // 已登录用户可从同站子域进入应用页面;跨站导航和同站 API 请求仍须拒绝。
- return r.Header.Get("Sec-Fetch-Site") == "same-site" &&
+ // 已登录用户可从子域、外部链接或 PWA 图标进入应用;API、资源和嵌入页面请求仍须拒绝。
+ site := r.Header.Get("Sec-Fetch-Site")
+ return (site == "same-site" || site == "cross-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)
diff --git a/kernel/util/net_test.go b/kernel/util/net_test.go
index fd77511665..35cc2d5559 100644
--- a/kernel/util/net_test.go
+++ b/kernel/util/net_test.go
@@ -81,7 +81,7 @@ func TestIsCrossSiteFetchSite(t *testing.T) {
}
}
-// TestIsSessionOriginAllowedRequest 验证会话认证请求校验:浏览器标记的跨站请求直接拒绝,
+// TestIsSessionOriginAllowedRequest 验证会话认证请求校验:仅允许跨站应用入口导航,
// 其余请求回退到 Origin 校验
// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-2w6q-wgc8-q743
func TestIsSessionOriginAllowedRequest(t *testing.T) {
@@ -103,7 +103,7 @@ func TestIsSessionOriginAllowedRequest(t *testing.T) {
{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: "cross-site app navigation", site: "cross-site", host: "note.example.com", path: "/stage/build/desktop/", mode: "navigate", dest: "document", want: true},
{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},
@@ -129,6 +129,35 @@ func TestIsSessionOriginAllowedRequest(t *testing.T) {
}
}
+func TestIsSessionOriginAllowedPWANavigation(t *testing.T) {
+ for _, path := range []string{"/", "/stage/build/app/", "/stage/build/desktop/", "/stage/build/mobile/",
+ "/stage/build/app/index.html", "/stage/build/desktop/index.html", "/stage/build/mobile/index.html"} {
+ request := httptest.NewRequest(http.MethodGet, "https://notes.example"+path+"?r=start", nil)
+ request.Header.Set("Sec-Fetch-Site", "cross-site")
+ request.Header.Set("Sec-Fetch-Mode", "navigate")
+ request.Header.Set("Sec-Fetch-Dest", "document")
+ if !IsSessionOriginAllowedRequest(request) {
+ t.Errorf("PWA entry rejected: %s", path)
+ }
+ for _, change := range []func(*http.Request){
+ func(r *http.Request) { r.Method = http.MethodPost },
+ func(r *http.Request) { r.Header.Set("Origin", "https://external.example") },
+ func(r *http.Request) { r.Header.Set("Sec-Fetch-Mode", "cors") },
+ func(r *http.Request) { r.Header.Set("Sec-Fetch-Dest", "iframe") },
+ func(r *http.Request) { r.Header.Set("Sec-Fetch-Site", "unknown") },
+ func(r *http.Request) { r.URL.Path = "/api/network/proxy" },
+ func(r *http.Request) { r.URL.Path = "/assets/image.png" },
+ func(r *http.Request) { r.URL.Path = path + "extra" },
+ } {
+ blocked := request.Clone(request.Context())
+ change(blocked)
+ if IsSessionOriginAllowedRequest(blocked) {
+ t.Errorf("unexpected navigation exception: %s %s %v", blocked.Method, blocked.URL, blocked.Header)
+ }
+ }
+ }
+}
+
// TestIsPrivateIP 验证 isPrivateIP 对私网地址及 IPv6 过渡地址的判断
// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-qq8m-8p8v-x4xg
// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-rg26-cg95-gq6p