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: Block sensitive files in history and repo diff routes https://github.com/siyuan-note/siyuan/security/advisories/GHSA-3cm4-ccvw-6xr6
Daniel035bf9a8c311
diff --git a/kernel/go.mod b/kernel/go.modindex 400e558de5..37a5fb4f15 100644--- a/kernel/go.mod+++ b/kernel/go.mod@@ -71,7 +71,7 @@ require (github.com/sashabaranov/go-openai v1.42.0github.com/shirou/gopsutil/v4 v4.26.7github.com/siyuan-note/dataparser v0.0.0-20260115084335-b57cb8bc7c17- github.com/siyuan-note/dejavu v0.0.0-20260813091452-352ea5f76eb3+ github.com/siyuan-note/dejavu v0.0.0-20260816033657-68735293f4c1github.com/siyuan-note/encryption v0.0.0-20260715062728-9cb8e9548044github.com/siyuan-note/eventbus v0.0.0-20260530125927-d77c74260dcegithub.com/siyuan-note/filelock v0.0.0-20260411141728-bf44452627c0diff --git a/kernel/go.sum b/kernel/go.sumindex ed50f97b6d..c9a42bda8d 100644--- a/kernel/go.sum+++ b/kernel/go.sum@@ -418,6 +418,8 @@ github.com/siyuan-note/dataparser v0.0.0-20260115084335-b57cb8bc7c17 h1:cZbH1QcWgithub.com/siyuan-note/dataparser v0.0.0-20260115084335-b57cb8bc7c17/go.mod h1:zE2SJ4t+2lFAeLgmhi27YtfzdyOWh3l9DXXUr9m/xU0=github.com/siyuan-note/dejavu v0.0.0-20260813091452-352ea5f76eb3 h1:8Qi5+/HFiJZi4e0qQ99cnCpgIUO3s/uVzhjvZ+lPo4o=github.com/siyuan-note/dejavu v0.0.0-20260813091452-352ea5f76eb3/go.mod h1:ixj6rxm+wkjoV8pBDGbuCyfMiPQh3MlxBwHG5OvtEQU=+github.com/siyuan-note/dejavu v0.0.0-20260816033657-68735293f4c1 h1:Q5gv0Bxsblq4kT3AbeWvaBDu+oFdpQCS4tp9hA4cumI=+github.com/siyuan-note/dejavu v0.0.0-20260816033657-68735293f4c1/go.mod h1:ixj6rxm+wkjoV8pBDGbuCyfMiPQh3MlxBwHG5OvtEQU=github.com/siyuan-note/encryption v0.0.0-20260715062728-9cb8e9548044 h1:b1/Q4DSREDvGh1/NbzR9873EKrjakdoUtsNlghWxUOI=github.com/siyuan-note/encryption v0.0.0-20260715062728-9cb8e9548044/go.mod h1:t6jXpJdrXzRsdQF+XGaw35f16JVZcDyIkk5/6//ywnY=github.com/siyuan-note/eventbus v0.0.0-20260530125927-d77c74260dce h1:356bfC3pucxXuYpZIwaQ3viVCbq5RbRuvBhb4xtoBR0=diff --git a/kernel/server/serve.go b/kernel/server/serve.goindex b67272c1fa..257648fe67 100644--- a/kernel/server/serve.go+++ b/kernel/server/serve.go@@ -993,7 +993,25 @@ func serveAssets(ginServer *gin.Engine) {})ginServer.GET("/history/*path", model.CheckAuth, model.CheckAdminRole, func(context *gin.Context) {- p := filepath.Join(util.HistoryDir, context.Param("path"))+ requestPath := context.Param("path")+ // 与 /repo/diff 一致,先拒绝路径穿越+ if strings.Contains(requestPath, "..") {+ context.Status(http.StatusUnauthorized)+ return+ }+ p := filepath.Join(util.HistoryDir, requestPath)+ // 历史快照可能包含敏感文件副本(如 data/.siyuan/publishAccess.json、data/templates 下的文件),+ // 去掉快照目录前缀后按数据相对路径拒绝访问(见 kernel/util/path_guard.go 的 IsForbiddenDataRelPath)+ rel := strings.TrimPrefix(requestPath, "/")+ if idx := strings.Index(rel, "/"); 0 <= idx {+ rel = rel[idx+1:]+ } else {+ rel = ""+ }+ if util.IsForbiddenDataRelPath(rel) {+ context.Status(http.StatusForbidden)+ return+ }// 加密笔记本的历史是密文(.sy/assets/AV),需先解密再输出if serveEncryptedHistory(context, p) {return@@ -1245,6 +1263,11 @@ func serveRepoDiff(ginServer *gin.Engine) {context.Status(http.StatusUnauthorized)return}+ // 与 /history 一致,按数据相对路径拒绝敏感文件(纵深防御,正常仅存放资源文件)+ if util.IsForbiddenDataRelPath(requestPath) {+ context.Status(http.StatusForbidden)+ return+ }// 从路径提取 boxID,加密笔记本已锁定时拒绝访问(锁定后 repo 预览解密文件仍存在磁盘上)parts := strings.SplitN(strings.TrimPrefix(requestPath, "/"), "/", 2)if len(parts) >= 1 && model.IsEncryptedBox(parts[0]) {diff --git a/kernel/server/serve_history_guard_test.go b/kernel/server/serve_history_guard_test.gonew file mode 100644index 0000000000..a3c19ef117--- /dev/null+++ b/kernel/server/serve_history_guard_test.go@@ -0,0 +1,117 @@+// SiYuan - From thought to insight, with agents+// 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.++package server++import (+ "net/http"+ "net/http/httptest"+ "os"+ "path/filepath"+ "testing"++ "github.com/gin-gonic/gin"+ "github.com/siyuan-note/siyuan/kernel/model"+ "github.com/siyuan-note/siyuan/kernel/util"+)++// TestHistoryRouteBlocksSensitiveSnapshots 验证 /history 路由拒绝历史快照中的敏感文件副本+// (data/.siyuan/publishAccess.json、data/templates、data/snippets/conf.json),+// 且不影响普通历史文件访问。+func TestHistoryRouteBlocksSensitiveSnapshots(t *testing.T) {+ gin.SetMode(gin.TestMode)+ originalHistoryDir := util.HistoryDir+ util.HistoryDir = t.TempDir()+ t.Cleanup(func() {+ util.HistoryDir = originalHistoryDir+ })++ snapshot := filepath.Join(util.HistoryDir, "2026-08-15-120000-sync")+ writeFile := func(rel string, content string) string {+ p := filepath.Join(snapshot, rel)+ if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(p, []byte(content), 0644); err != nil {+ t.Fatal(err)+ }+ return p+ }+ writeFile(filepath.Join(".siyuan", "publishAccess.json"), `{"id":"","password":"secret"}`)+ writeFile(filepath.Join("templates", "private.md"), "template")+ writeFile(filepath.Join("snippets", "conf.json"), `{"enabled":true}`)+ writeFile(filepath.Join("assets", "image.png"), "asset")++ engine := gin.New()+ engine.Use(func(c *gin.Context) {+ c.Set(model.RoleContextKey, model.RoleAdministrator)+ c.Next()+ })+ serveAssets(engine)++ for _, test := range []struct {+ requestPath string+ wantStatus int+ }{+ {"/history/2026-08-15-120000-sync/.siyuan/publishAccess.json", http.StatusForbidden},+ {"/history/2026-08-15-120000-sync/templates/private.md", http.StatusForbidden},+ {"/history/2026-08-15-120000-sync/snippets/conf.json", http.StatusForbidden},+ {"/history/2026-08-15-120000-sync/assets/image.png", http.StatusOK},+ } {+ recorder := httptest.NewRecorder()+ engine.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, test.requestPath, nil))+ if recorder.Code != test.wantStatus {+ t.Fatalf("GET %s returned %d, want %d", test.requestPath, recorder.Code, test.wantStatus)+ }+ }+}++// TestRepoDiffRouteBlocksSensitivePaths 验证 /repo/diff 路由按数据相对路径拒绝敏感文件(纵深防御),+// 且不影响正常资源文件访问。+func TestRepoDiffRouteBlocksSensitivePaths(t *testing.T) {+ gin.SetMode(gin.TestMode)+ originalTempDir, originalConf := util.TempDir, model.Conf+ util.TempDir = t.TempDir()+ model.Conf = model.NewAppConf()+ t.Cleanup(func() {+ util.TempDir = originalTempDir+ model.Conf = originalConf+ })++ diffDir := filepath.Join(util.TempDir, "repo", "diff")+ allowedPath := filepath.Join(diffDir, "assets", "image.png")+ if err := os.MkdirAll(filepath.Dir(allowedPath), 0755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(allowedPath, []byte("asset"), 0644); err != nil {+ t.Fatal(err)+ }++ engine := gin.New()+ engine.Use(func(c *gin.Context) {+ c.Set(model.RoleContextKey, model.RoleAdministrator)+ c.Next()+ })+ serveRepoDiff(engine)++ for _, test := range []struct {+ requestPath string+ wantStatus int+ }{+ {"/repo/diff/.siyuan/publishAccess.json", http.StatusForbidden},+ {"/repo/diff/templates/private.md", http.StatusForbidden},+ {"/repo/diff/snippets/conf.json", http.StatusForbidden},+ {"/repo/diff/assets/image.png", http.StatusOK},+ } {+ recorder := httptest.NewRecorder()+ engine.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, test.requestPath, nil))+ if recorder.Code != test.wantStatus {+ t.Fatalf("GET %s returned %d, want %d", test.requestPath, recorder.Code, test.wantStatus)+ }+ }+}diff --git a/kernel/util/path_guard.go b/kernel/util/path_guard.goindex ee354bec58..5b345c199f 100644--- a/kernel/util/path_guard.go+++ b/kernel/util/path_guard.go@@ -17,11 +17,10 @@package utilimport (+ "path""path/filepath""runtime""strings"-- "github.com/88250/gulu")// IsForbiddenAbsPath 判断绝对路径是否为敏感路径,HTTP 文件 API(kernel/api/file.go 的 refuseToAccess)@@ -36,21 +35,40 @@ func IsForbiddenAbsPath(abs string) bool {return true}+ // 数据目录内的敏感位置(snippets/conf.json、templates、.siyuan/publishAccess.json),+ // 与历史快照、仓库 diff 路由(IsForbiddenDataRelPath)共用同一判断,避免黑名单分散维护。+ dataNorm := NormalizeAndResolve(DataDir)+ if rel, relErr := filepath.Rel(dataNorm, fileNorm); nil == relErr &&+ !strings.HasPrefix(rel, "..") && IsForbiddenDataRelPath(rel) {+ return true+ }+ return false+}++// IsForbiddenDataRelPath 判断数据目录下的相对路径是否指向敏感位置(data/snippets/conf.json、+// data/templates 目录以及 data/.siyuan/publishAccess.json)。历史快照与仓库 diff 检出中的文件副本+// 位于其他绝对路径下,无法用 IsForbiddenAbsPath 的精确匹配拦截,因此 /history 与 /repo/diff 路由+// (kernel/server/serve.go)在去掉快照目录前缀后按数据相对路径调用本函数进行片段匹配。+func IsForbiddenDataRelPath(rel string) bool {+ // 统一为斜杠并清理(path.Clean 使用斜杠语义,避免 Windows 上分隔符差异)+ rel = path.Clean("/" + filepath.ToSlash(rel))+ // 在 Windows 和 macOS 上文件系统通常为不区分大小写,使用小写统一比较+ if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {+ rel = strings.ToLower(rel)+ }+// 禁止访问 data/snippets/conf.json- snippetPath := NormalizeAndResolve(filepath.Join(DataDir, "snippets", "conf.json"))- if fileNorm == snippetPath {+ if rel == "/snippets/conf.json" {return true}// 禁止访问 data/templates 目录(含目录本身及其全部子路径)- templatesBase := NormalizeAndResolve(filepath.Join(DataDir, "templates"))- if fileNorm == templatesBase || gulu.File.IsSubPath(templatesBase, fileNorm) {+ if rel == "/templates" || strings.HasPrefix(rel, "/templates/") {return true}// 禁止访问 data/.siyuan/publishAccess.json(含发布模式明文访问密码)- publishAccessPath := NormalizeAndResolve(filepath.Join(DataDir, ".siyuan", "publishAccess.json"))- if fileNorm == publishAccessPath {+ if rel == "/.siyuan/publishaccess.json" {return true}return falsediff --git a/kernel/util/path_guard_test.go b/kernel/util/path_guard_test.goindex 01dbb4e1a2..b6bb5179c9 100644--- a/kernel/util/path_guard_test.go+++ b/kernel/util/path_guard_test.go@@ -122,3 +122,59 @@ func TestIsForbiddenAbsPathSymlinkBypass(t *testing.T) {t.Errorf("IsForbiddenAbsPath(symlink -> publishAccess.json) = false, want true")}}++// TestIsForbiddenDataRelPath 覆盖 /history 与 /repo/diff 路由共用的数据相对路径片段匹配黑名单。+// 历史快照副本位于 HistoryDir 等绝对路径下,无法用 IsForbiddenAbsPath 精确匹配,因此按数据目录下的+// 相对位置拦截:data/snippets/conf.json、data/templates 目录以及 data/.siyuan/publishAccess.json。+func TestIsForbiddenDataRelPath(t *testing.T) {+ forbidden := []string{+ ".siyuan/publishAccess.json",+ "/.siyuan/publishAccess.json",+ filepath.Join(".siyuan", "publishAccess.json"),+ "templates",+ "/templates",+ filepath.Join("templates", "a.md"),+ "snippets/conf.json",+ filepath.Join("snippets", "conf.json"),+ }+ for _, p := range forbidden {+ if got := IsForbiddenDataRelPath(p); !got {+ t.Errorf("IsForbiddenDataRelPath(%q) = false, want true", p)+ }+ }++ // 快照内合法文件不应被误判,尤其笔记本内名为 templates 的文档目录。+ allowed := []string{+ "",+ "/",+ "assets/image.png",+ filepath.Join("20210808180117-6v0mkxr", "templates", "a.sy"),+ filepath.Join("20210808180117-6v0mkxr", "20240101.sy"),+ "plugins/example/main.js",+ ".siyuan/publishAccess.json.bak",+ filepath.Join("20210808180117-6v0mkxr", ".siyuan", "publishAccess.json"),+ }+ for _, p := range allowed {+ if got := IsForbiddenDataRelPath(p); got {+ t.Errorf("IsForbiddenDataRelPath(%q) = true, want false", p)+ }+ }+}++// TestIsForbiddenDataRelPathCaseInsensitive 在大小写不敏感的文件系统上验证大小写变体无法绕过片段匹配。+func TestIsForbiddenDataRelPathCaseInsensitive(t *testing.T) {+ if runtime.GOOS != "windows" && runtime.GOOS != "darwin" {+ t.Skip("case-insensitive comparison only on windows/darwin")+ }++ cases := []string{+ filepath.Join(".SIYUAN", "PUBLISHACCESS.JSON"),+ strings.ToUpper(filepath.Join("templates", "A.MD")),+ filepath.Join("SNIPPETS", "CONF.JSON"),+ }+ for _, p := range cases {+ if got := IsForbiddenDataRelPath(p); !got {+ t.Errorf("IsForbiddenDataRelPath(%q) = false, want true (case variant)", p)+ }+ }+}