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 publish files accessed through Windows short names https://github.com/siyuan-note/siyuan/security/advisories/GHSA-3pm7-cc76-7j2p
Daniel7e873910b3ec
diff --git a/kernel/api/plugin_publish_test.go b/kernel/api/plugin_publish_test.goindex 2c3fda5f9a..9fec2385e9 100644--- a/kernel/api/plugin_publish_test.go+++ b/kernel/api/plugin_publish_test.go@@ -12,6 +12,7 @@ import ("github.com/gin-gonic/gin""github.com/siyuan-note/siyuan/kernel/conf"+ "github.com/siyuan-note/siyuan/kernel/internal/testutil""github.com/siyuan-note/siyuan/kernel/model""github.com/siyuan-note/siyuan/kernel/util")@@ -27,19 +28,20 @@ func TestAPIContractPluginPublish(t *testing.T) {util.WorkspaceDir, util.DataDir, util.ConfDir, model.Conf = oldWorkspace, oldData, oldConfDir, oldConf})files := map[string]string{- "data/plugins/example/plugin.json": `{"name":"example","version":"1.0.0","minAppVersion":"0.0.1","publish":{"resources":["logo.txt","credentials.json"],"data":["theme"]}}`,- "data/plugins/example/index.js": "frontend",- "data/plugins/example/kernel.js": "kernel secret",- "data/plugins/example/logo.txt": "logo",- "data/plugins/example/credentials.json": "API_TOKEN=secret",- "data/plugins/example/private.json": "secret",- "data/storage/petal/example/private.json": "private token",- "data/public/file.txt": "public",- "data/private.json": "private data root",- "conf/private.json": "conf secret",- "temp/private.json": "temp secret",- "repo/private.json": "repo secret",- "history/private.json": "history secret",+ "data/plugins/example/plugin.json": `{"name":"example","version":"1.0.0","minAppVersion":"0.0.1","publish":{"resources":["logo.txt","credentials.json","resources/"],"data":["theme"]}}`,+ "data/plugins/example/index.js": "frontend",+ "data/plugins/example/kernel.js": "kernel secret",+ "data/plugins/example/logo.txt": "logo",+ "data/plugins/example/credentials.json": "API_TOKEN=secret",+ "data/plugins/example/resources/credentials.json": "API_TOKEN=nested-secret",+ "data/plugins/example/private.json": "secret",+ "data/storage/petal/example/private.json": "private token",+ "data/public/file.txt": "public",+ "data/private.json": "private data root",+ "conf/private.json": "conf secret",+ "temp/private.json": "temp secret",+ "repo/private.json": "repo secret",+ "history/private.json": "history secret",}for path, content := range files {path = filepath.Join(util.WorkspaceDir, filepath.FromSlash(path))@@ -117,6 +119,15 @@ func TestAPIContractPluginPublish(t *testing.T) {t.Fatalf("denied %s: %d %s", path, response.Code, response.Body)}}+ t.Run("short filename in declared directory", func(t *testing.T) {+ path := filepath.Join(util.DataDir, "plugins", "example", "resources", "credentials.json")+ short := filepath.Base(testutil.ShortPath(t, path))+ body, _ := json.Marshal(map[string]string{"path": "data/plugins/example/resources/" + short})+ response := request(model.RoleReader, "/api/file/getFile", string(body))+ if response.Code != http.StatusAccepted || code(response) != http.StatusForbidden || strings.Contains(response.Body.String(), "nested-secret") {+ t.Fatalf("sensitive plugin alias %s: %d %s", short, response.Code, response.Body)+ }+ })response := request(model.RoleReader, "/api/petal/loadPetals", `{"frontend":"browser-desktop"}`)if bytes.Contains(response.Body.Bytes(), []byte("kernel secret")) || !bytes.Contains(response.Body.Bytes(), []byte("frontend")) {t.Fatal(response.Body)diff --git a/kernel/internal/testutil/short_path_other.go b/kernel/internal/testutil/short_path_other.gonew file mode 100644index 0000000000..ba27ee75d1--- /dev/null+++ b/kernel/internal/testutil/short_path_other.go@@ -0,0 +1,12 @@+//go:build !windows++package testutil++import "testing"++// ShortPath 仅在 Windows 上提供短文件名夹具。+func ShortPath(t testing.TB, path string) string {+ t.Helper()+ t.Skip("short filenames require Windows")+ return ""+}diff --git a/kernel/internal/testutil/short_path_windows.go b/kernel/internal/testutil/short_path_windows.gonew file mode 100644index 0000000000..3470d6d070--- /dev/null+++ b/kernel/internal/testutil/short_path_windows.go@@ -0,0 +1,33 @@+package testutil++import (+ "path/filepath"+ "strings"+ "testing"++ "golang.org/x/sys/windows"+)++// ShortPath 返回现有文件的短路径;卷未生成短文件名时跳过对应子测试。+func ShortPath(t testing.TB, path string) string {+ t.Helper()+ name, err := windows.UTF16PtrFromString(path)+ if err != nil {+ t.Fatal(err)+ }+ buffer := make([]uint16, 512)+ for {+ length, err := windows.GetShortPathName(name, &buffer[0], uint32(len(buffer)))+ if err != nil {+ t.Fatal(err)+ }+ if length < uint32(len(buffer)) {+ short := windows.UTF16ToString(buffer[:length])+ if strings.EqualFold(filepath.Base(short), filepath.Base(path)) {+ t.Skip("volume does not generate a short filename for this fixture")+ }+ return short+ }+ buffer = make([]uint16, length+1)+ }+}diff --git a/kernel/model/publish_package_file_test.go b/kernel/model/publish_package_file_test.goindex 051db8454a..7120c4b00b 100644--- a/kernel/model/publish_package_file_test.go+++ b/kernel/model/publish_package_file_test.go@@ -14,6 +14,7 @@ import ("github.com/88250/lute/parse""github.com/gin-gonic/gin""github.com/siyuan-note/siyuan/kernel/conf"+ "github.com/siyuan-note/siyuan/kernel/internal/testutil""github.com/siyuan-note/siyuan/kernel/treenode""github.com/siyuan-note/siyuan/kernel/util")@@ -77,6 +78,16 @@ func TestOpenPublishPackageFileRejectsSensitiveWidgetResource(t *testing.T) {t.Fatalf("sensitive %s: handled = %v, err = %v, want permission denied", requestPath, handled, err)}}+ t.Run("short filename", func(t *testing.T) {+ short := filepath.Base(testutil.ShortPath(t, filepath.Join(widgetDir, "credentials.json")))+ file, handled, err := OpenPublishPackageFile(c, "/data/widgets/example/"+short)+ if file != nil {+ file.Close()+ }+ if !handled || !errors.Is(err, fs.ErrPermission) {+ t.Fatalf("sensitive widget alias %s: handled = %v, err = %v", short, handled, err)+ }+ })file, handled, err := OpenPublishPackageFile(c, "/data/widgets/example/index.html")if err != nil || !handled {@@ -91,23 +102,25 @@ func TestOpenPublishPackageFileRejectsSensitiveWidgetResource(t *testing.T) {// TestOpenPublishPackageFileRejectsSensitivePluginResource 验证发布读者经文件接口读取插件包时先拒绝敏感文件名。func TestOpenPublishPackageFileRejectsSensitivePluginResource(t *testing.T) {- originalDataDir, originalConf := util.DataDir, Conf- util.DataDir = t.TempDir()+ originalWorkspace, originalDataDir, originalConf := util.WorkspaceDir, util.DataDir, Conf+ util.WorkspaceDir = t.TempDir()+ util.DataDir = filepath.Join(util.WorkspaceDir, "data")Conf = NewAppConf()Conf.Sync.Store(conf.NewSync())Conf.Bazaar.Store(&conf.Bazaar{Trust: true})Conf.Lang = "en"- t.Cleanup(func() { util.DataDir, Conf = originalDataDir, originalConf })+ t.Cleanup(func() { util.WorkspaceDir, util.DataDir, Conf = originalWorkspace, originalDataDir, originalConf })pluginDir := filepath.Join(util.DataDir, "plugins", "example")- if err := os.MkdirAll(pluginDir, 0755); err != nil {+ if err := os.MkdirAll(filepath.Join(pluginDir, "resources"), 0755); err != nil {t.Fatal(err)}files := map[string]string{- "plugin.json": `{"name":"example","version":"1.0.0","minAppVersion":"0.0.1","publish":{"resources":["logo.txt","credentials.json"]}}`,- "index.js": "frontend",- "logo.txt": "logo",- "credentials.json": "API_TOKEN=secret",+ "plugin.json": `{"name":"example","version":"1.0.0","minAppVersion":"0.0.1","publish":{"resources":["logo.txt","credentials.json","resources/"]}}`,+ "index.js": "frontend",+ "logo.txt": "logo",+ "credentials.json": "API_TOKEN=secret",+ "resources/credentials.json": "API_TOKEN=nested-secret",}for name, content := range files {if err := os.WriteFile(filepath.Join(pluginDir, name), []byte(content), 0644); err != nil {@@ -127,6 +140,16 @@ func TestOpenPublishPackageFileRejectsSensitivePluginResource(t *testing.T) {if !handled || !errors.Is(err, fs.ErrPermission) {t.Fatalf("sensitive plugin resource: handled = %v, err = %v, want permission denied", handled, err)}+ t.Run("short filename in declared directory", func(t *testing.T) {+ short := filepath.Base(testutil.ShortPath(t, filepath.Join(pluginDir, "resources", "credentials.json")))+ file, handled, err := OpenPublishPackageFile(c, "/data/plugins/example/resources/"+short)+ if file != nil {+ file.Close()+ }+ if !handled || !errors.Is(err, fs.ErrPermission) {+ t.Fatalf("sensitive plugin alias %s: handled = %v, err = %v", short, handled, err)+ }+ })file, handled, err = OpenPublishPackageFile(c, "/data/plugins/example/logo.txt")if err != nil || !handled {diff --git a/kernel/server/serve_publish_package_test.go b/kernel/server/serve_publish_package_test.goindex d38419da72..3fec7bce20 100644--- a/kernel/server/serve_publish_package_test.go+++ b/kernel/server/serve_publish_package_test.go@@ -11,6 +11,7 @@ import ("github.com/gin-gonic/gin""github.com/siyuan-note/siyuan/kernel/conf""github.com/siyuan-note/siyuan/kernel/filesys"+ "github.com/siyuan-note/siyuan/kernel/internal/testutil""github.com/siyuan-note/siyuan/kernel/model""github.com/siyuan-note/siyuan/kernel/treenode""github.com/siyuan-note/siyuan/kernel/util"@@ -82,6 +83,13 @@ func TestWidgetPublishReaderRefusesSensitiveFile(t *testing.T) {return recorder}+ t.Run("reader short filename", func(t *testing.T) {+ short := filepath.Base(testutil.ShortPath(t, filepath.Join(widgetDir, "credentials.json")))+ recorder := request(model.RoleReader, "/widgets/example/"+short)+ if recorder.Code != http.StatusForbidden || recorder.Body.Len() != 0 {+ t.Fatalf("sensitive widget alias %s: status = %d, body = %q", short, recorder.Code, recorder.Body.String())+ }+ })for _, role := range []model.Role{model.RoleReader, model.RoleAdministrator} {for _, requestPath := range []string{"/widgets/example/credentials.json", "/widgets/example/id_token"} {recorder := request(role, requestPath)@@ -98,23 +106,25 @@ func TestWidgetPublishReaderRefusesSensitiveFile(t *testing.T) {// TestPluginPublishReaderRefusesSensitiveFile 验证发布读者不能经静态路由读取插件包中的敏感文件。func TestPluginPublishReaderRefusesSensitiveFile(t *testing.T) {gin.SetMode(gin.TestMode)- previousData, previousConf := util.DataDir, model.Conf- util.DataDir = t.TempDir()+ previousWorkspace, previousData, previousConf := util.WorkspaceDir, util.DataDir, model.Conf+ util.WorkspaceDir = t.TempDir()+ util.DataDir = filepath.Join(util.WorkspaceDir, "data")model.Conf = model.NewAppConf()model.Conf.Sync.Store(conf.NewSync())model.Conf.Bazaar.Store(&conf.Bazaar{Trust: true})model.Conf.Lang = "en"- t.Cleanup(func() { util.DataDir, model.Conf = previousData, previousConf })+ t.Cleanup(func() { util.WorkspaceDir, util.DataDir, model.Conf = previousWorkspace, previousData, previousConf })pluginDir := filepath.Join(util.DataDir, "plugins", "example")- if err := os.MkdirAll(pluginDir, 0755); err != nil {+ if err := os.MkdirAll(filepath.Join(pluginDir, "resources"), 0755); err != nil {t.Fatal(err)}files := map[string]string{- "plugin.json": `{"name":"example","version":"1.0.0","minAppVersion":"0.0.1","publish":{"resources":["logo.txt","credentials.json"]}}`,- "index.js": "frontend",- "logo.txt": "logo",- "credentials.json": "API_TOKEN=secret",+ "plugin.json": `{"name":"example","version":"1.0.0","minAppVersion":"0.0.1","publish":{"resources":["logo.txt","credentials.json","resources/"]}}`,+ "index.js": "frontend",+ "logo.txt": "logo",+ "credentials.json": "API_TOKEN=secret",+ "resources/credentials.json": "API_TOKEN=nested-secret",}for name, content := range files {if err := os.WriteFile(filepath.Join(pluginDir, name), []byte(content), 0644); err != nil {@@ -137,6 +147,13 @@ func TestPluginPublishReaderRefusesSensitiveFile(t *testing.T) {return recorder}+ t.Run("reader short filename in declared directory", func(t *testing.T) {+ short := filepath.Base(testutil.ShortPath(t, filepath.Join(pluginDir, "resources", "credentials.json")))+ recorder := request(model.RoleReader, "/plugins/example/resources/"+short)+ if recorder.Code != http.StatusForbidden || recorder.Body.Len() != 0 {+ t.Fatalf("sensitive plugin alias %s: status = %d, body = %q", short, recorder.Code, recorder.Body.String())+ }+ })for _, role := range []model.Role{model.RoleReader, model.RoleAdministrator} {if recorder := request(role, "/plugins/example/credentials.json"); recorder.Code != http.StatusForbidden || recorder.Body.Len() != 0 {t.Fatalf("%v sensitive plugin resource: status = %d, body = %q", role, recorder.Code, recorder.Body.String())diff --git a/kernel/util/publish_file.go b/kernel/util/publish_file.goindex c601a5a5dc..dee76a7286 100644--- a/kernel/util/publish_file.go+++ b/kernel/util/publish_file.go@@ -72,7 +72,7 @@ func OpenPublishFile(rootPath, name string) (*os.File, error) {return file, nil}-// OpenPublishReaderFile 打开读者可见的包资源:先执行敏感文件名策略,再进入包内约束读取。+// OpenPublishReaderFile 打开读者可见的包资源,并在读取前检查请求路径和已打开文件的敏感性。// data/public 按设计公开,仍直接使用 OpenPublishFile,不经过本函数。func OpenPublishReaderFile(rootPath, name string) (*os.File, error) {if !IsPublishRelativePath(name) {@@ -81,5 +81,13 @@ func OpenPublishReaderFile(rootPath, name string) (*os.File, error) {if IsSensitivePath(filepath.Join(rootPath, filepath.FromSlash(name))) {return nil, fs.ErrPermission}- return OpenPublishFile(rootPath, name)+ file, err := OpenPublishFile(rootPath, name)+ if err != nil {+ return nil, err+ }+ if err = checkPublishReaderFile(file); err != nil {+ file.Close()+ return nil, err+ }+ return file, nil}diff --git a/kernel/util/publish_file_other.go b/kernel/util/publish_file_other.gonew file mode 100644index 0000000000..0f8fbfdb21--- /dev/null+++ b/kernel/util/publish_file_other.go@@ -0,0 +1,10 @@+//go:build !windows++package util++import "os"++// checkPublishReaderFile 非 Windows 平台无短文件名别名,使用打开前的路径检查。+func checkPublishReaderFile(_ *os.File) error {+ return nil+}diff --git a/kernel/util/publish_file_windows.go b/kernel/util/publish_file_windows.gonew file mode 100644index 0000000000..c8b655f351--- /dev/null+++ b/kernel/util/publish_file_windows.go@@ -0,0 +1,20 @@+package util++import (+ "io/fs"+ "os"++ "golang.org/x/sys/windows"+)++// checkPublishReaderFile 按已打开句柄的真实长路径检查,拒绝通过短文件名访问敏感文件。+func checkPublishReaderFile(file *os.File) error {+ path, err := resolveHandlePath(windows.Handle(file.Fd()))+ if err != nil {+ return err+ }+ if IsSensitivePath(path) {+ return fs.ErrPermission+ }+ return nil+}diff --git a/kernel/util/publish_file_windows_test.go b/kernel/util/publish_file_windows_test.goindex e71b78a697..81faa53e3f 100644--- a/kernel/util/publish_file_windows_test.go+++ b/kernel/util/publish_file_windows_test.go@@ -1,12 +1,18 @@//go:build windows-package util+package util_testimport (+ "errors"+ "io"+ "io/fs""os""os/exec""path/filepath""testing"++ "github.com/siyuan-note/siyuan/kernel/internal/testutil"+ "github.com/siyuan-note/siyuan/kernel/util")func TestPublishFileJunctionBoundary(t *testing.T) {@@ -21,8 +27,66 @@ func TestPublishFileJunctionBoundary(t *testing.T) {if out, err := exec.Command("cmd", "/c", "mklink", "/J", junction, outside).CombinedOutput(); err != nil {t.Skipf("junction creation unavailable: %v %s", err, out)}- if file, err := OpenPublishFile(root, "public/linked/secret.txt"); err == nil {+ if file, err := util.OpenPublishFile(root, "public/linked/secret.txt"); err == nil {file.Close()t.Fatal("junction target was exposed")}}++// TestPublishFileShortName 验证短文件名不能绕过包资源的敏感路径检查,同时保留普通资源与公开目录的读取。+func TestPublishFileShortName(t *testing.T) {+ root := t.TempDir()+ previousWorkspace := util.WorkspaceDir+ util.WorkspaceDir = root+ t.Cleanup(func() { util.WorkspaceDir = previousWorkspace })+ dir := filepath.Join(root, "widgets", "example")+ if err := os.MkdirAll(dir, 0755); err != nil {+ t.Fatal(err)+ }+ for _, name := range []string{"credentials.json", "id_private_token.json", "ordinary-resource-file.txt"} {+ if err := os.WriteFile(filepath.Join(dir, name), []byte(name), 0644); err != nil {+ t.Fatal(err)+ }+ t.Run(name, func(t *testing.T) {+ short := filepath.Base(testutil.ShortPath(t, filepath.Join(dir, name)))+ file, err := util.OpenPublishReaderFile(root, "widgets/example/"+short)+ if name != "ordinary-resource-file.txt" {+ if file != nil {+ file.Close()+ }+ if !errors.Is(err, fs.ErrPermission) {+ t.Fatalf("sensitive alias %s: err = %v, want permission denied", short, err)+ }+ return+ }+ if err != nil {+ t.Fatalf("ordinary alias rejected: %v", err)+ }+ defer file.Close()+ data, err := io.ReadAll(file)+ if err != nil || string(data) != name {+ t.Fatalf("ordinary alias content = %q, err = %v", data, err)+ }+ })+ }+ t.Run("public directory", func(t *testing.T) {+ public := filepath.Join(root, "public")+ if err := os.MkdirAll(public, 0755); err != nil {+ t.Fatal(err)+ }+ path := filepath.Join(public, "credentials.json")+ if err := os.WriteFile(path, []byte("explicitly public"), 0644); err != nil {+ t.Fatal(err)+ }+ short := filepath.Base(testutil.ShortPath(t, path))+ file, err := util.OpenPublishFile(root, "public/"+short)+ if err != nil {+ t.Fatalf("explicitly public alias rejected: %v", err)+ }+ defer file.Close()+ data, err := io.ReadAll(file)+ if err != nil || string(data) != "explicitly public" {+ t.Fatalf("public alias content = %q, err = %v", data, err)+ }+ })+}diff --git a/kernel/util/real_path_windows.go b/kernel/util/real_path_windows.goindex 5990b81741..d38249df79 100644--- a/kernel/util/real_path_windows.go+++ b/kernel/util/real_path_windows.go@@ -66,7 +66,11 @@ func ResolveRealPath(path string) (string, error) {defer func() {_ = windows.CloseHandle(handle)}()+ return resolveHandlePath(handle)+}+// resolveHandlePath 从现有句柄获取规范化长路径,避免重新按请求路径打开文件。+func resolveHandlePath(handle windows.Handle) (string, error) {buffer := make([]uint16, 512)for {length, callErr := windows.GetFinalPathNameByHandle(handle, &buffer[0], uint32(len(buffer)), 0)