Skip to content

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

:lock: Reject dotenv configs and platform app data in sensitive path checks https://github.com/siyuan-note/siyuan/security/advisories/GHSA-q768-4j87-gf3w

Daniel9ffcc7342358
diff --git a/kernel/mcp/tools/asset_test.go b/kernel/mcp/tools/asset_test.go
index aa93b21f8b..397b206291 100644
--- a/kernel/mcp/tools/asset_test.go
+++ b/kernel/mcp/tools/asset_test.go
@@ -51,9 +51,10 @@ func TestHTMLAssetIFrameBlockDOM(t *testing.T) {
}
func TestValidateAssetUploadPaths(t *testing.T) {
- origWorkspace := util.WorkspaceDir
+ origWorkspace, origHome := util.WorkspaceDir, util.HomeDir
util.WorkspaceDir = filepath.Join(t.TempDir(), "workspace")
- t.Cleanup(func() { util.WorkspaceDir = origWorkspace })
+ util.HomeDir = filepath.Join(t.TempDir(), "home")
+ t.Cleanup(func() { util.WorkspaceDir, util.HomeDir = origWorkspace, origHome })
inside := filepath.Join(util.WorkspaceDir, "data", "assets", "foo.png")
normalized, err := validateAssetUploadPaths([]string{inside})
@@ -81,6 +82,13 @@ func TestValidateAssetUploadPaths(t *testing.T) {
if _, err = validateAssetUploadPaths([]string{sshKey}); err == nil {
t.Fatal("credential path must be rejected")
}
+
+ // GHSA-q768-4j87-gf3w:家目录 dotenv 配置文件同样必须拒绝
+ for _, envFile := range []string{".env", ".env.local", ".env.production"} {
+ if _, err = validateAssetUploadPaths([]string{filepath.Join(util.HomeDir, envFile)}); err == nil {
+ t.Fatalf("dotenv credential path must be rejected: %s", envFile)
+ }
+ }
}
func TestNewAssetUploadToolResultPreservesPartialResults(t *testing.T) {
diff --git a/kernel/util/path.go b/kernel/util/path.go
index e977b51e07..c68477fd43 100644
--- a/kernel/util/path.go
+++ b/kernel/util/path.go
@@ -562,6 +562,43 @@ func isHTMLAssetUserPath(p string) bool {
return false
}
+// sensitiveHomeDotEntries 是家目录下默认敏感的凭据条目,按名称前缀匹配。
+// .env 使用前缀匹配,以覆盖 .env.local、.env.production 等派生文件。
+var sensitiveHomeDotEntries = []string{
+ ".env",
+ ".ssh", ".config", ".bashrc", ".zshrc", ".profile", ".git-credentials", ".netrc", ".pgpass",
+ ".kube", ".docker", ".gnupg", ".aws", ".azure", ".npmrc", ".pypirc",
+}
+
+// sensitiveHomeEntry 是家目录下的敏感条目,按名称前缀匹配。
+type sensitiveHomeEntry struct {
+ name string
+ // exemptSystemTemp 表示该条目放行系统临时目录:Windows 的 AppData\Local 下即系统临时目录,
+ // 而剪贴板及 HTML 粘贴的本地资源允许取自该目录。
+ exemptSystemTemp bool
+}
+
+// platformSensitiveHomeEntries 是家目录下按平台附加的敏感条目:macOS 的用户资源库包含钥匙串等凭据,
+// Windows 的漫游与本地应用数据包含各类 CLI 与桌面应用的凭据。
+var platformSensitiveHomeEntries = map[string][]sensitiveHomeEntry{
+ "darwin": {{name: "Library"}},
+ "windows": {{name: filepath.Join("AppData", "Roaming")}, {name: filepath.Join("AppData", "Local"), exemptSystemTemp: true}},
+}
+
+// isSystemTempPath 判断小写路径是否位于系统临时目录中。
+func isSystemTempPath(lowerPath string) bool {
+ for _, root := range []string{os.TempDir(), SystemTempDir} {
+ if root == "" {
+ continue
+ }
+ root = strings.ToLower(filepath.Clean(root))
+ if lowerPath == root || strings.HasPrefix(lowerPath, root+string(filepath.Separator)) {
+ return true
+ }
+ }
+ return false
+}
+
// isSensitivePath 执行敏感性黑名单匹配,必要时解析工作空间路径,但不解析目标路径。
func isSensitivePath(p string, htmlAsset bool) bool {
p = resolveNamespaceAlias(p)
@@ -643,8 +680,8 @@ func isSensitivePath(p string, htmlAsset bool) bool {
}
// 用户家目录下的敏感目录与凭据文件(小写比较)。
- // 覆盖常见凭据 dotfile,防止通过 globalCopyFiles 等接受工作空间外绝对路径的接口把内核用户
- // 家目录下的凭据复制进工作空间后外泄:Git push token、HTTP/API 凭据、Postgres 密码、
+ // 覆盖常见凭据 dotfile、dotenv 配置文件与平台应用数据目录,防止接受工作空间外绝对路径的接口
+ // 把内核用户家目录下的凭据复制进工作空间后外泄:Git push token、HTTP/API 凭据、Postgres 密码、
// K8s/Docker/容器仓库配置、GPG 私钥环、云厂商 CLI 凭据、包管理器 token 等。
homeDirs := []string{HomeDir}
// 自定义配置主目录不能使系统用户主目录中的凭据失去保护。
@@ -671,16 +708,25 @@ func isSensitivePath(p string, htmlAsset bool) bool {
}
}
for _, homeDir := range homeDirs {
- for _, name := range []string{
- ".ssh", ".config", ".bashrc", ".zshrc", ".profile", ".git-credentials", ".netrc", ".pgpass",
- ".kube", ".docker", ".gnupg", ".aws", ".azure", ".npmrc", ".pypirc",
- } {
+ for _, name := range sensitiveHomeDotEntries {
for _, checkPath := range homeCheckPaths {
if strings.HasPrefix(checkPath, strings.ToLower(filepath.Join(homeDir, name))) {
return true
}
}
}
+ for _, entry := range platformSensitiveHomeEntries[runtime.GOOS] {
+ prefix := strings.ToLower(filepath.Join(homeDir, entry.name))
+ for _, checkPath := range homeCheckPaths {
+ if !strings.HasPrefix(checkPath, prefix) {
+ continue
+ }
+ if entry.exemptSystemTemp && isSystemTempPath(checkPath) {
+ continue
+ }
+ return true
+ }
+ }
}
// 特定的文件名前缀(小写比较)
diff --git a/kernel/util/path_test.go b/kernel/util/path_test.go
index 7649aaf518..3380866218 100644
--- a/kernel/util/path_test.go
+++ b/kernel/util/path_test.go
@@ -20,6 +20,7 @@ import (
"os"
"path/filepath"
"reflect"
+ "runtime"
"testing"
)
@@ -98,6 +99,10 @@ func TestIsSensitivePathCredentialDotfiles(t *testing.T) {
name string
rel string // 相对 HomeDir 的路径
}{
+ // GHSA-q768-4j87-gf3w 点名的 dotenv 配置文件,.env 前缀同时覆盖派生文件。
+ {"env file", ".env"},
+ {"env local file", ".env.local"},
+ {"env production file", ".env.production"},
{"git-credentials", ".git-credentials"},
{"netrc", ".netrc"},
{"pgpass", ".pgpass"},
@@ -121,6 +126,69 @@ func TestIsSensitivePathCredentialDotfiles(t *testing.T) {
}
}
+// TestIsSensitivePathPlatformHomeDirs 覆盖 GHSA-q768-4j87-gf3w 点名的平台应用数据目录:
+// macOS 用户资源库中的钥匙串、Windows 漫游与本地应用数据中的凭据。
+func TestIsSensitivePathPlatformHomeDirs(t *testing.T) {
+ type platformCase struct {
+ name string
+ rel string
+ }
+ var home string
+ var cases []platformCase
+ if runtime.GOOS == "darwin" {
+ home = t.TempDir()
+ cases = append(cases, platformCase{"macOS keychain", filepath.Join("Library", "Keychains", "login.keychain-db")})
+ }
+ if runtime.GOOS == "windows" {
+ // AppData\Local 条目放行系统临时目录,而 t.TempDir() 正位于该目录中,因此使用真实用户主目录
+ profile, err := os.UserHomeDir()
+ if err != nil || profile == "" {
+ t.Skipf("user home dir unavailable: %v", err)
+ }
+ home = profile
+ cases = append(cases,
+ platformCase{"Windows roaming credentials", filepath.Join("AppData", "Roaming", "gcloud", "credentials.db")},
+ platformCase{"Windows local profile data", filepath.Join("AppData", "Local", "Google", "Chrome", "User Data", "Default", "Cookies")})
+ }
+ if len(cases) == 0 {
+ t.Skipf("no platform-specific home entries on %s", runtime.GOOS)
+ }
+
+ origHome, origWorkspace := HomeDir, WorkspaceDir
+ HomeDir, WorkspaceDir = home, t.TempDir()
+ t.Cleanup(func() { HomeDir, WorkspaceDir = origHome, origWorkspace })
+
+ for _, c := range cases {
+ abs := filepath.Join(home, c.rel)
+ if got := IsSensitivePath(abs); !got {
+ t.Errorf("IsSensitivePath(%q) = false, want true [%s]", abs, c.name)
+ }
+ }
+}
+
+// TestIsSensitivePathSystemTempExemption 验证 Windows 系统临时目录中的剪贴板及 HTML 粘贴资源
+// 不被 AppData\Local 条目误判。
+func TestIsSensitivePathSystemTempExemption(t *testing.T) {
+ if runtime.GOOS != "windows" {
+ t.Skip("the system temp dir lives under AppData only on Windows")
+ }
+ profile, err := os.UserHomeDir()
+ if err != nil || profile == "" {
+ t.Skipf("user home dir unavailable: %v", err)
+ }
+ origHome := HomeDir
+ HomeDir = profile
+ t.Cleanup(func() { HomeDir = origHome })
+
+ p := filepath.Join(os.TempDir(), "siyuan-clipboard-image.png")
+ if IsSensitivePath(p) {
+ t.Errorf("IsSensitivePath(%q) = true, want false", p)
+ }
+ if IsSensitiveHTMLAssetPath(p) {
+ t.Errorf("IsSensitiveHTMLAssetPath(%q) = true, want false", p)
+ }
+}
+
// TestIsSensitivePathSymlinkBypass 验证通过符号链接绕过黑名单的尝试会被拦截:
// 在非敏感目录下放置一个指向 ~/.ssh/id_rsa 的符号链接,解析后应判定为敏感。
func TestIsSensitivePathSymlinkBypass(t *testing.T) {