Skip to content

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

:lock: Block TLS private keys in file access blocklist https://github.com/siyuan-note/siyuan/security/advisories/GHSA-4wwp-f6gw-6qm5

Daniel256d73aa7f94
diff --git a/kernel/api/file.go b/kernel/api/file.go
index 8ae3456630..e146014e3e 100644
--- a/kernel/api/file.go
+++ b/kernel/api/file.go
@@ -532,7 +532,8 @@ func getFile(c *gin.Context) {
}
func refuseToAccess(c *gin.Context, fileAbsPath string, ret *gulu.Result) bool {
- // 禁止访问敏感文件(conf/conf.json、data/snippets/conf.json、data/templates、data/.siyuan/publishAccess.json),
+ // 禁止访问敏感文件(conf 目录下的 conf.json 与 TLS 密钥材料、data/snippets/conf.json、
+ // data/templates、data/.siyuan/publishAccess.json),
// 规范化与符号链接解析见 util.NormalizeAndResolve,防止通过大小写或符号链接绕过
if util.IsForbiddenAbsPath(fileAbsPath) {
ret.Code = http.StatusForbidden
diff --git a/kernel/util/path_guard.go b/kernel/util/path_guard.go
index 5b345c199f..1c0d009231 100644
--- a/kernel/util/path_guard.go
+++ b/kernel/util/path_guard.go
@@ -25,14 +25,24 @@ import (
// IsForbiddenAbsPath 判断绝对路径是否为敏感路径,HTTP 文件 API(kernel/api/file.go 的 refuseToAccess)
// 与 MCP 文件工具(kernel/mcp/tools/file.go 的 resolvePath)共用同一黑名单:
-// conf/conf.json、data/snippets/conf.json、data/templates 目录以及 data/.siyuan/publishAccess.json。
+// conf 目录下的 conf.json 与 TLS 密钥材料、data/snippets/conf.json、data/templates 目录以及
+// data/.siyuan/publishAccess.json。
func IsForbiddenAbsPath(abs string) bool {
fileNorm := NormalizeAndResolve(abs)
- // 禁止访问配置文件 conf/conf.json(含 accessAuthCode/api.token/cookieKey 等明文凭据)
- confPath := NormalizeAndResolve(filepath.Join(ConfDir, "conf.json"))
- if fileNorm == confPath {
- return true
+ // 禁止访问 conf 目录下的敏感文件:conf.json(含 accessAuthCode/api.token/cookieKey 等明文凭据)
+ // 以及 TLS 私钥与证书(见 GetOrCreateTLSCert,私钥被读取可导致 HTTPS 流量被解密或证书被伪造)
+ forbiddenConfFiles := []string{
+ "conf.json",
+ TLSCACertFilename,
+ TLSCAKeyFilename,
+ TLSCertFilename,
+ TLSKeyFilename,
+ }
+ for _, filename := range forbiddenConfFiles {
+ if fileNorm == NormalizeAndResolve(filepath.Join(ConfDir, filename)) {
+ return true
+ }
}
// 数据目录内的敏感位置(snippets/conf.json、templates、.siyuan/publishAccess.json),
diff --git a/kernel/util/path_guard_test.go b/kernel/util/path_guard_test.go
index b6bb5179c9..e45d9d2078 100644
--- a/kernel/util/path_guard_test.go
+++ b/kernel/util/path_guard_test.go
@@ -25,7 +25,8 @@ import (
)
// TestIsForbiddenAbsPath 覆盖 HTTP 文件 API 与 MCP 文件工具共用的敏感路径黑名单:
-// conf/conf.json、data/snippets/conf.json、data/templates 目录以及 data/.siyuan/publishAccess.json。
+// conf 目录下的 conf.json 与 TLS 密钥材料、data/snippets/conf.json、data/templates 目录以及
+// data/.siyuan/publishAccess.json。
func TestIsForbiddenAbsPath(t *testing.T) {
tmpWorkspace := t.TempDir()
origWorkspace, origConf, origData := WorkspaceDir, ConfDir, DataDir
@@ -39,6 +40,10 @@ func TestIsForbiddenAbsPath(t *testing.T) {
rel string // 相对工作空间的路径
}{
{"conf", filepath.Join("conf", "conf.json")},
+ {"tls ca cert", filepath.Join("conf", TLSCACertFilename)},
+ {"tls ca key", filepath.Join("conf", TLSCAKeyFilename)},
+ {"tls cert", filepath.Join("conf", TLSCertFilename)},
+ {"tls key", filepath.Join("conf", TLSKeyFilename)},
{"snippets conf", filepath.Join("data", "snippets", "conf.json")},
{"templates dir", filepath.Join("data", "templates")},
{"templates file", filepath.Join("data", "templates", "a.txt")},
@@ -80,6 +85,10 @@ func TestIsForbiddenAbsPathCaseInsensitive(t *testing.T) {
// 模拟 MCP 工具输入的大小写变体路径(Windows 上 CONF.JSON 与 conf.json 指向同一文件)。
cases := []string{
filepath.Join(tmpWorkspace, "CONF", "CONF.JSON"),
+ filepath.Join(tmpWorkspace, "CONF", strings.ToUpper(TLSCACertFilename)),
+ filepath.Join(tmpWorkspace, "CONF", strings.ToUpper(TLSCAKeyFilename)),
+ filepath.Join(tmpWorkspace, "CONF", strings.ToUpper(TLSCertFilename)),
+ filepath.Join(tmpWorkspace, "CONF", strings.ToUpper(TLSKeyFilename)),
filepath.Join(tmpWorkspace, "DATA", "SNIPPETS", "CONF.JSON"),
filepath.Join(tmpWorkspace, "DATA", ".SIYUAN", "PUBLISHACCESS.JSON"),
strings.ToUpper(filepath.Join(tmpWorkspace, "data", "templates")),
@@ -121,6 +130,22 @@ func TestIsForbiddenAbsPathSymlinkBypass(t *testing.T) {
if got := IsForbiddenAbsPath(link); !got {
t.Errorf("IsForbiddenAbsPath(symlink -> publishAccess.json) = false, want true")
}
+
+ // 同样验证指向 TLS 私钥的符号链接无法绕过黑名单。
+ tlsKey := filepath.Join(tmpWorkspace, "conf", TLSKeyFilename)
+ if err := os.MkdirAll(filepath.Dir(tlsKey), 0755); err != nil {
+ t.Fatalf("mkdir: %v", err)
+ }
+ if err := os.WriteFile(tlsKey, []byte("-----BEGIN EC PRIVATE KEY-----\n"), 0600); err != nil {
+ t.Fatalf("write tls key: %v", err)
+ }
+ tlsKeyLink := filepath.Join(tmpWorkspace, "data", "assets", "leak.pem")
+ if err := os.Symlink(tlsKey, tlsKeyLink); err != nil {
+ t.Skipf("symlink not supported on this platform: %v", err)
+ }
+ if got := IsForbiddenAbsPath(tlsKeyLink); !got {
+ t.Errorf("IsForbiddenAbsPath(symlink -> key.pem) = false, want true")
+ }
}
// TestIsForbiddenDataRelPath 覆盖 /history 与 /repo/diff 路由共用的数据相对路径片段匹配黑名单。