Skip to content

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

:lock: Resolve symlinks before getFile authorization and workspace checks https://github.com/siyuan-note/siyuan/security/advisories/GHSA-g7gf-v79m-jwrm

Danieldb72eeaff68e
diff --git a/kernel/api/file.go b/kernel/api/file.go
index e146014e3e..7b42c8e9ad 100644
--- a/kernel/api/file.go
+++ b/kernel/api/file.go
@@ -471,6 +471,25 @@ func getFile(c *gin.Context) {
return
}
+ // 解析符号链接(Windows 下含目录联接)后再做授权判断,防止 reader 通过 data/assets
+ // 等目录下的链接读取工作空间外的文件(security advisory GHSA-g7gf-v79m-jwrm)
+ resolvedPath, err := model.ResolveRealPath(fileAbsPath)
+ if err != nil {
+ logging.LogErrorf("resolve symlinks for [%s] failed: %s", fileAbsPath, err)
+ ret.Code = http.StatusInternalServerError
+ ret.Msg = http.StatusText(http.StatusInternalServerError) + errMsgSeeKernelLog
+ c.JSON(http.StatusAccepted, ret)
+ return
+ }
+ // 符号链接指向加密笔记本时同样拒绝读取,防止密文泄漏
+ if rejectEncryptedBoxPath(resolvedPath) {
+ ret.Code = -3
+ ret.Msg = model.Conf.Language(321)
+ c.JSON(http.StatusAccepted, ret)
+ return
+ }
+ fileAbsPath = resolvedPath
+
info, err := os.Stat(fileAbsPath)
if os.IsNotExist(err) {
ret.Code = http.StatusNotFound
@@ -495,6 +514,14 @@ func getFile(c *gin.Context) {
// REF: https://github.com/siyuan-note/siyuan/issues/11364
if !model.IsAdminRoleContext(c) {
+ // 符号链接解析后的真实路径必须仍位于工作空间内(admin 不受此限制,兼容 assets
+ // 指向工作空间外目录的合法用法),发布权限与敏感路径检查也基于解析后的路径执行
+ if !gulu.File.IsSubPath(util.NormalizeAndResolve(util.WorkspaceDir), util.NormalizeAndResolve(fileAbsPath)) {
+ ret.Code = http.StatusForbidden
+ ret.Msg = http.StatusText(http.StatusForbidden)
+ c.JSON(http.StatusAccepted, ret)
+ return
+ }
if refuseToAccess(c, fileAbsPath, ret) {
return
}
diff --git a/kernel/api/file_test.go b/kernel/api/file_test.go
index 9c6df71e68..899d0e4869 100644
--- a/kernel/api/file_test.go
+++ b/kernel/api/file_test.go
@@ -48,3 +48,89 @@ func TestGetFileAllowsWorkspaceTemp(t *testing.T) {
t.Fatalf("unexpected workspace temp file content: %q", recorder.Body.String())
}
}
+
+func TestGetFileReaderRejectsAssetsSymlinkOutsideWorkspace(t *testing.T) {
+ originalWorkspaceDir := util.WorkspaceDir
+ originalDataDir := util.DataDir
+ workspaceDir := t.TempDir()
+ util.WorkspaceDir = workspaceDir
+ util.DataDir = filepath.Join(workspaceDir, "data")
+ defer func() {
+ util.WorkspaceDir = originalWorkspaceDir
+ util.DataDir = originalDataDir
+ }()
+
+ outsideDir := t.TempDir()
+ outsideFile := filepath.Join(outsideDir, "secret.txt")
+ content := []byte("outside workspace secret")
+ if err := os.WriteFile(outsideFile, content, 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ assetsDir := filepath.Join(util.DataDir, "assets")
+ if err := os.MkdirAll(assetsDir, 0755); err != nil {
+ t.Fatal(err)
+ }
+ linkPath := filepath.Join(assetsDir, "leak.txt")
+ if err := os.Symlink(outsideFile, linkPath); err != nil {
+ t.Skipf("symlinks are not supported on this system: %s", err)
+ }
+
+ recorder := httptest.NewRecorder()
+ context, _ := gin.CreateTestContext(recorder)
+ context.Set(model.RoleContextKey, model.RoleReader)
+ request := httptest.NewRequest(http.MethodPost, "/api/file/getFile", strings.NewReader(`{"path":"data/assets/leak.txt"}`))
+ request.Header.Set("Content-Type", "application/json")
+ context.Request = request
+ getFile(context)
+
+ if recorder.Code == http.StatusOK {
+ t.Fatalf("reader must not read workspace-external files via assets symlink, got status %d: %s", recorder.Code, recorder.Body.String())
+ }
+ if strings.Contains(recorder.Body.String(), string(content)) {
+ t.Fatalf("workspace-external file content leaked to reader: %s", recorder.Body.String())
+ }
+}
+
+func TestGetFileAdminCanFollowAssetsSymlinkOutsideWorkspace(t *testing.T) {
+ originalWorkspaceDir := util.WorkspaceDir
+ originalDataDir := util.DataDir
+ workspaceDir := t.TempDir()
+ util.WorkspaceDir = workspaceDir
+ util.DataDir = filepath.Join(workspaceDir, "data")
+ defer func() {
+ util.WorkspaceDir = originalWorkspaceDir
+ util.DataDir = originalDataDir
+ }()
+
+ outsideDir := t.TempDir()
+ outsideFile := filepath.Join(outsideDir, "external.txt")
+ content := []byte("admin readable external content")
+ if err := os.WriteFile(outsideFile, content, 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ assetsDir := filepath.Join(util.DataDir, "assets")
+ if err := os.MkdirAll(assetsDir, 0755); err != nil {
+ t.Fatal(err)
+ }
+ linkPath := filepath.Join(assetsDir, "external-link.txt")
+ if err := os.Symlink(outsideFile, linkPath); err != nil {
+ t.Skipf("symlinks are not supported on this system: %s", err)
+ }
+
+ recorder := httptest.NewRecorder()
+ context, _ := gin.CreateTestContext(recorder)
+ context.Set(model.RoleContextKey, model.RoleAdministrator)
+ request := httptest.NewRequest(http.MethodPost, "/api/file/getFile", strings.NewReader(`{"path":"data/assets/external-link.txt"}`))
+ request.Header.Set("Content-Type", "application/json")
+ context.Request = request
+ getFile(context)
+
+ if recorder.Code != http.StatusOK {
+ t.Fatalf("admin should keep legacy symlink access, got status %d: %s", recorder.Code, recorder.Body.String())
+ }
+ if recorder.Body.String() != string(content) {
+ t.Fatalf("unexpected symlink target content: %q", recorder.Body.String())
+ }
+}
diff --git a/kernel/api/file_windows_test.go b/kernel/api/file_windows_test.go
new file mode 100644
index 0000000000..bea2358fd8
--- /dev/null
+++ b/kernel/api/file_windows_test.go
@@ -0,0 +1,95 @@
+// 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.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+//go:build windows
+
+package api
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/gin-gonic/gin"
+ "github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+// 目录联接(junction)无需管理员权限即可创建,是 Windows 上对符号链接漏洞的同源变体,
+// 用于验证 getFile 的链接解析与工作空间边界检查(security advisory GHSA-g7gf-v79m-jwrm)
+func TestGetFileJunctionBoundary(t *testing.T) {
+ originalWorkspaceDir := util.WorkspaceDir
+ originalDataDir := util.DataDir
+ workspaceDir := t.TempDir()
+ util.WorkspaceDir = workspaceDir
+ util.DataDir = filepath.Join(workspaceDir, "data")
+ defer func() {
+ util.WorkspaceDir = originalWorkspaceDir
+ util.DataDir = originalDataDir
+ }()
+
+ outsideDir := t.TempDir()
+ outsideFile := filepath.Join(outsideDir, "secret.txt")
+ content := []byte("outside workspace secret")
+ if err := os.WriteFile(outsideFile, content, 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ assetsDir := filepath.Join(util.DataDir, "assets")
+ if err := os.MkdirAll(assetsDir, 0755); err != nil {
+ t.Fatal(err)
+ }
+ junctionPath := filepath.Join(assetsDir, "out")
+ if out, err := exec.Command("cmd", "/c", "mklink", "/J", junctionPath, outsideDir).CombinedOutput(); err != nil {
+ t.Skipf("junction creation not supported: %s: %s", err, out)
+ }
+
+ recorder := httptest.NewRecorder()
+ context, _ := gin.CreateTestContext(recorder)
+ context.Set(model.RoleContextKey, model.RoleReader)
+ request := httptest.NewRequest(http.MethodPost, "/api/file/getFile", strings.NewReader(`{"path":"data/assets/out/secret.txt"}`))
+ request.Header.Set("Content-Type", "application/json")
+ context.Request = request
+ getFile(context)
+
+ if recorder.Code == http.StatusOK {
+ t.Fatalf("reader must not read workspace-external files via junction, got status %d: %s", recorder.Code, recorder.Body.String())
+ }
+ if !strings.Contains(recorder.Body.String(), `"code":403`) {
+ t.Fatalf("reader junction escape should be rejected with 403, got: %s", recorder.Body.String())
+ }
+ if strings.Contains(recorder.Body.String(), string(content)) {
+ t.Fatalf("workspace-external file content leaked to reader: %s", recorder.Body.String())
+ }
+
+ recorder = httptest.NewRecorder()
+ context, _ = gin.CreateTestContext(recorder)
+ context.Set(model.RoleContextKey, model.RoleAdministrator)
+ request = httptest.NewRequest(http.MethodPost, "/api/file/getFile", strings.NewReader(`{"path":"data/assets/out/secret.txt"}`))
+ request.Header.Set("Content-Type", "application/json")
+ context.Request = request
+ getFile(context)
+ if recorder.Code != http.StatusOK {
+ t.Fatalf("admin should keep legacy junction access, got status %d: %s", recorder.Code, recorder.Body.String())
+ }
+ if recorder.Body.String() != string(content) {
+ t.Fatalf("unexpected junction target content: %q", recorder.Body.String())
+ }
+}
diff --git a/kernel/model/asset_path_resolver_other.go b/kernel/model/asset_path_resolver_other.go
index 2e530a3d57..c60f02ff26 100644
--- a/kernel/model/asset_path_resolver_other.go
+++ b/kernel/model/asset_path_resolver_other.go
@@ -20,6 +20,6 @@ package model
import "path/filepath"
-func resolveAssetRealPath(path string) (string, error) {
+func ResolveRealPath(path string) (string, error) {
return filepath.EvalSymlinks(path)
}
diff --git a/kernel/model/asset_path_resolver_windows.go b/kernel/model/asset_path_resolver_windows.go
index abc3db48c2..9713200db4 100644
--- a/kernel/model/asset_path_resolver_windows.go
+++ b/kernel/model/asset_path_resolver_windows.go
@@ -25,8 +25,8 @@ import (
"golang.org/x/sys/windows"
)
-// resolveAssetRealPath 使用文件句柄解析符号链接和目录联接,返回现有资源的最终绝对路径。
-func resolveAssetRealPath(path string) (string, error) {
+// ResolveRealPath 使用文件句柄解析符号链接和目录联接,返回现有文件的最终绝对路径。
+func ResolveRealPath(path string) (string, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return "", err
diff --git a/kernel/model/assets.go b/kernel/model/assets.go
index 81b96baecc..1adfd9b05d 100644
--- a/kernel/model/assets.go
+++ b/kernel/model/assets.go
@@ -971,15 +971,15 @@ func ResolveDataAssetPath(assetPath string) (relativePath, absPath string, err e
return
}
- resolvedRoot, evalErr := resolveAssetRealPath(assetRoot)
+ resolvedRoot, evalErr := ResolveRealPath(assetRoot)
if evalErr != nil {
err = fmt.Errorf("resolve assets directory [%s] failed: %w", assetRoot, evalErr)
return
}
if assetDirIndex > 0 {
notebookRoot := filepath.Join(util.DataDir, parts[0])
- resolvedDataDir, dataEvalErr := resolveAssetRealPath(util.DataDir)
- resolvedNotebookRoot, notebookEvalErr := resolveAssetRealPath(notebookRoot)
+ resolvedDataDir, dataEvalErr := ResolveRealPath(util.DataDir)
+ resolvedNotebookRoot, notebookEvalErr := ResolveRealPath(notebookRoot)
if dataEvalErr != nil || notebookEvalErr != nil ||
!gulu.File.IsSubPath(resolvedDataDir, resolvedNotebookRoot) ||
!gulu.File.IsSubPath(resolvedNotebookRoot, resolvedRoot) {
@@ -987,7 +987,7 @@ func ResolveDataAssetPath(assetPath string) (relativePath, absPath string, err e
return
}
}
- resolvedPath, evalErr := resolveAssetRealPath(absPath)
+ resolvedPath, evalErr := ResolveRealPath(absPath)
if evalErr != nil {
err = fmt.Errorf("resolve asset [%s] failed: %w", absPath, evalErr)
return
@@ -1016,10 +1016,10 @@ func ResolveUnusedDataAssetPath(assetPath string) (relativePath, absPath string,
}
func unusedAssetsContainPath(relativePath, absPath string, items []*UnusedItem) bool {
- resolvedPath, _ := resolveAssetRealPath(absPath)
+ resolvedPath, _ := ResolveRealPath(absPath)
for _, item := range items {
if item.AbsPath != "" {
- resolvedItemPath, evalErr := resolveAssetRealPath(item.AbsPath)
+ resolvedItemPath, evalErr := ResolveRealPath(item.AbsPath)
samePath, relErr := filepath.Rel(resolvedPath, resolvedItemPath)
if resolvedPath != "" && evalErr == nil && relErr == nil && samePath == "." {
return true