Skip to content

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

:lock: https://github.com/siyuan-note/siyuan/security/advisories/GHSA-vmm8-3ccv-ppvw

Danielb54fee401799
diff --git a/kernel/util/path.go b/kernel/util/path.go
index 1a5b35126d..0707495d6f 100644
--- a/kernel/util/path.go
+++ b/kernel/util/path.go
@@ -399,10 +399,27 @@ func IsPartitionRootPath(path string) bool {
}
// IsSensitivePath 对传入路径做统一的敏感性检测。
+// 为防止通过符号链接绕过黑名单,会先尝试解析路径上的符号链接,再对原始路径与解析后的真实路径
+// 分别检查,任一命中即判定为敏感。解析失败(如路径不存在)时回退到仅检查原始路径。
func IsSensitivePath(p string) bool {
if p == "" {
return false
}
+ if isSensitivePath(p) {
+ return true
+ }
+ // 解析符号链接后再次检查,防止在允许的目录内放置指向敏感目标的符号链接来绕过黑名单。
+ resolved, err := filepath.EvalSymlinks(p)
+ if err == nil && resolved != p {
+ if isSensitivePath(resolved) {
+ return true
+ }
+ }
+ return false
+}
+
+// isSensitivePath 执行实际的敏感性黑名单匹配,不解析符号链接。
+func isSensitivePath(p string) bool {
toCheckPathLower := filepath.Clean(strings.ToLower(p))
toCheckNameLower := filepath.Base(toCheckPathLower)
@@ -473,13 +490,26 @@ func IsSensitivePath(p string) bool {
return true
}
- // 用户家目录下的敏感目录(小写比较)
+ // 用户家目录下的敏感目录与凭据文件(小写比较)。
+ // 覆盖常见凭据 dotfile,防止通过 globalCopyFiles 等接受工作空间外绝对路径的接口把内核用户
+ // 家目录下的凭据复制进工作空间后外泄:Git push token、HTTP/API 凭据、Postgres 密码、
+ // K8s/Docker/容器仓库配置、GPG 私钥环、云厂商 CLI 凭据、包管理器 token 等。
homePrefixes := []string{
strings.ToLower(filepath.Join(HomeDir, ".ssh")),
strings.ToLower(filepath.Join(HomeDir, ".config")),
strings.ToLower(filepath.Join(HomeDir, ".bashrc")),
strings.ToLower(filepath.Join(HomeDir, ".zshrc")),
strings.ToLower(filepath.Join(HomeDir, ".profile")),
+ strings.ToLower(filepath.Join(HomeDir, ".git-credentials")),
+ strings.ToLower(filepath.Join(HomeDir, ".netrc")),
+ strings.ToLower(filepath.Join(HomeDir, ".pgpass")),
+ strings.ToLower(filepath.Join(HomeDir, ".kube")),
+ strings.ToLower(filepath.Join(HomeDir, ".docker")),
+ strings.ToLower(filepath.Join(HomeDir, ".gnupg")),
+ strings.ToLower(filepath.Join(HomeDir, ".aws")),
+ strings.ToLower(filepath.Join(HomeDir, ".azure")),
+ strings.ToLower(filepath.Join(HomeDir, ".npmrc")),
+ strings.ToLower(filepath.Join(HomeDir, ".pypirc")),
}
for _, hp := range homePrefixes {
if strings.HasPrefix(toCheckPathLower, hp) {
@@ -487,7 +517,7 @@ func IsSensitivePath(p string) bool {
}
}
- // 特定的文件名(小写比较)
+ // 特定的文件名前缀(小写比较)
namePrefixes := []string{
strings.ToLower("credentials"),
strings.ToLower("id_"),
diff --git a/kernel/util/path_test.go b/kernel/util/path_test.go
new file mode 100644
index 0000000000..0c02d0f07e
--- /dev/null
+++ b/kernel/util/path_test.go
@@ -0,0 +1,109 @@
+// SiYuan - Refactor your thinking
+// 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/>.
+
+package util
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+// TestIsSensitivePathCredentialDotfiles 覆盖 GHSA 报告中遗漏的家目录凭据 dotfile,
+// 确保它们在 globalCopyFiles 等接受工作空间外绝对路径的接口处被拒绝。
+func TestIsSensitivePathCredentialDotfiles(t *testing.T) {
+ tmpHome := t.TempDir()
+ tmpWorkspace := t.TempDir()
+ origHome, origWorkspace := HomeDir, WorkspaceDir
+ HomeDir, WorkspaceDir = tmpHome, tmpWorkspace
+ t.Cleanup(func() { HomeDir, WorkspaceDir = origHome, origWorkspace })
+
+ // 报告点名的攻击面 + 常见云/包管理器凭据。
+ cases := []struct {
+ name string
+ rel string // 相对 HomeDir 的路径
+ }{
+ {"git-credentials", ".git-credentials"},
+ {"netrc", ".netrc"},
+ {"pgpass", ".pgpass"},
+ {"kube config", filepath.Join(".kube", "config")},
+ {"docker config", filepath.Join(".docker", "config.json")},
+ {"gnupg private keyring", filepath.Join(".gnupg", "private-keys-v1.d", "key.key")},
+ {"aws credentials", filepath.Join(".aws", "credentials")},
+ {"azure token", filepath.Join(".azure", "accessTokens.json")},
+ {"npmrc", ".npmrc"},
+ {"pypirc", ".pypirc"},
+ // 原有黑名单项不应回归。
+ {"ssh id_rsa", filepath.Join(".ssh", "id_rsa")},
+ {"bashrc", ".bashrc"},
+ {"config dir", filepath.Join(".config", "some-app")},
+ }
+ for _, c := range cases {
+ abs := filepath.Join(tmpHome, c.rel)
+ if got := IsSensitivePath(abs); !got {
+ t.Errorf("IsSensitivePath(%q) = false, want true [%s]", abs, c.name)
+ }
+ }
+}
+
+// TestIsSensitivePathSymlinkBypass 验证通过符号链接绕过黑名单的尝试会被拦截:
+// 在非敏感目录下放置一个指向 ~/.ssh/id_rsa 的符号链接,解析后应判定为敏感。
+func TestIsSensitivePathSymlinkBypass(t *testing.T) {
+ tmpHome := t.TempDir()
+ tmpWorkspace := t.TempDir()
+ origHome, origWorkspace := HomeDir, WorkspaceDir
+ HomeDir, WorkspaceDir = tmpHome, tmpWorkspace
+ t.Cleanup(func() { HomeDir, WorkspaceDir = origHome, origWorkspace })
+
+ target := filepath.Join(tmpHome, ".ssh", "id_rsa")
+ if err := os.MkdirAll(filepath.Dir(target), 0700); err != nil {
+ t.Fatalf("mkdir: %v", err)
+ }
+ if err := os.WriteFile(target, []byte("PRIVATE"), 0600); err != nil {
+ t.Fatalf("write target: %v", err)
+ }
+
+ // 在一个看起来无害的位置(家目录外的临时目录)放符号链接。
+ innocentDir := t.TempDir()
+ link := filepath.Join(innocentDir, "link")
+ if err := os.Symlink(target, link); err != nil {
+ t.Skipf("symlink not supported on this platform: %v", err)
+ }
+
+ // link 自身的路径不命中黑名单,但解析后指向 .ssh,应被拒绝。
+ if got := IsSensitivePath(link); !got {
+ t.Errorf("IsSensitivePath(symlink -> .ssh) = false, want true")
+ }
+}
+
+// TestIsSensitivePathWorkspaceFilesNotBlocked 确保工作空间内的合法文件不会被误判。
+func TestIsSensitivePathWorkspaceFilesNotBlocked(t *testing.T) {
+ tmpWorkspace := t.TempDir()
+ origWorkspace := WorkspaceDir
+ WorkspaceDir = tmpWorkspace
+ t.Cleanup(func() { WorkspaceDir = origWorkspace })
+
+ cases := []string{
+ filepath.Join(tmpWorkspace, "data", "assets", "image.png"),
+ filepath.Join(tmpWorkspace, "data", "note.md"),
+ filepath.Join(tmpWorkspace, "temp", "export", "output.pdf"),
+ }
+ for _, p := range cases {
+ if got := IsSensitivePath(p); got {
+ t.Errorf("IsSensitivePath(%q) = true, want false (workspace file)", p)
+ }
+ }
+}