Skip to content

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

:bug: Preserve appearance access with restricted parent directories https://github.com/siyuan-note/siyuan/issues/19683

Daniel35349db954b7
diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml
index 8961e92f9e..11cced33df 100644
--- a/.github/workflows/cd.yml
+++ b/.github/workflows/cd.yml
@@ -157,6 +157,10 @@ jobs:
- name: Check application and strict contract types
run: pnpm run typecheck
working-directory: app
+ - name: Install filesystem fault injection dependency
+ run: |
+ sudo apt-get update
+ sudo apt-get install -y strace
- name: Test all kernel packages
# 测试失败只记录结果,不阻塞后续发布构建。
continue-on-error: true
diff --git a/kernel/server/appearance_path.go b/kernel/server/appearance_path.go
index c8abf3cadd..1662bbbbed 100644
--- a/kernel/server/appearance_path.go
+++ b/kernel/server/appearance_path.go
@@ -23,6 +23,7 @@ import (
"strings"
"github.com/88250/gulu"
+ "github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/util"
)
@@ -64,10 +65,12 @@ func resolveAppearanceFilePath(root, requestPath string) (string, int) {
if err != nil {
return "", http.StatusInternalServerError
}
- root, err = filepath.EvalSymlinks(root)
+ resolvedRoot, err := evalAppearanceSymlinks(root)
if err != nil {
+ logging.LogWarnf("resolve appearance root [%s] failed: %s", root, err)
return "", http.StatusNotFound
}
+ root = resolvedRoot
allowedRoot, target := root, root
for i, segment := range segments {
target = filepath.Join(target, segment)
@@ -77,13 +80,16 @@ func resolveAppearanceFilePath(root, requestPath string) (string, int) {
return filepath.Join(append([]string{target}, segments[i+1:]...)...), 0
}
if statErr != nil {
+ logging.LogWarnf("stat appearance resource [%s] failed: %s", target, statErr)
return "", http.StatusForbidden
}
if info.Mode()&os.ModeSymlink != 0 {
- target, err = filepath.EvalSymlinks(target)
- if err != nil {
+ resolvedTarget, resolveErr := evalAppearanceSymlinks(target)
+ if resolveErr != nil {
+ logging.LogWarnf("resolve appearance link [%s] failed: %s", target, resolveErr)
return "", http.StatusForbidden
}
+ target = resolvedTarget
}
if i == packageRootIndex {
info, err = os.Stat(target)
diff --git a/kernel/server/appearance_path_linux_test.go b/kernel/server/appearance_path_linux_test.go
new file mode 100644
index 0000000000..0fff6da17f
--- /dev/null
+++ b/kernel/server/appearance_path_linux_test.go
@@ -0,0 +1,53 @@
+//go:build linux
+
+package server
+
+import (
+ "os"
+ "os/exec"
+ "path/filepath"
+ "runtime"
+ "testing"
+)
+
+func TestAppearanceRestrictedAncestor(t *testing.T) {
+ const probeEnv = "SIYUAN_TEST_APPEARANCE_ANCESTOR"
+ if ancestor := os.Getenv(probeEnv); ancestor != "" {
+ resource := filepath.Join(ancestor, "0", "probe.txt")
+ if _, err := os.ReadFile(resource); err != nil {
+ t.Fatalf("direct read: %s", err)
+ }
+ if _, err := filepath.EvalSymlinks(resource); !os.IsNotExist(err) {
+ t.Fatalf("expected restricted ancestor to fail with ENOENT, got %v", err)
+ }
+ // 在相同的父目录限制下验证正常读取、语言补全、包目录软链接及越界拒绝。
+ TestAppearanceFileBoundaries(t)
+ return
+ }
+ if runtime.GOARCH != "amd64" {
+ t.Skip("fault injection uses the amd64 newfstatat syscall")
+ }
+ strace, err := exec.LookPath("strace")
+ if err != nil {
+ t.Skip("strace is required for filesystem fault injection")
+ }
+ ancestor := filepath.Join(t.TempDir(), "storage", "emulated")
+ tempDir := filepath.Join(ancestor, "0", "tmp")
+ if err := os.MkdirAll(tempDir, 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(ancestor, "0", "probe.txt"), []byte("resource"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ executable, err := os.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+ // 只拒绝父目录本身的属性查询,子目录和文件仍可直接访问。
+ cmd := exec.Command(strace, "-f", "-qq", "-e", "trace=newfstatat", "-P", ancestor,
+ "-e", "inject=newfstatat:error=ENOENT", executable, "-test.run=^TestAppearanceRestrictedAncestor$", "-test.v")
+ cmd.Env = append(os.Environ(), probeEnv+"="+ancestor, "TMPDIR="+tempDir)
+ if output, err := cmd.CombinedOutput(); err != nil {
+ t.Fatalf("appearance requests with a restricted ancestor: %s\n%s", err, output)
+ }
+}
diff --git a/kernel/server/appearance_path_test.go b/kernel/server/appearance_path_test.go
index ee97876279..e9c40a3106 100644
--- a/kernel/server/appearance_path_test.go
+++ b/kernel/server/appearance_path_test.go
@@ -114,6 +114,18 @@ func TestAppearanceFileBoundaries(t *testing.T) {
check("icons/linked/icon.js", 200, "resource")
link("theme.css", "themes/local/alias.css")
check("themes/local/alias.css", 200, "resource")
+ link(appearanceTestResourcePath("themes/local/theme.css"), "themes/local/absolute.css")
+ check("themes/local/absolute.css", 200, "resource")
+ link(filepath.Join(outside, "theme.css"), "themes/linked/absolute.css")
+ check("themes/linked/absolute.css", 200, "resource")
+ link("en.json", "langs/alias.json")
+ check("langs/alias.json", 200, `"fallback":"English"`)
+ link(filepath.Join(util.AppearancePath, "langs/en.json"), "langs/absolute.json")
+ check("langs/absolute.json", 200, `"fallback":"English"`)
+ link("missing.css", "themes/local/broken.css")
+ check("themes/local/broken.css", 403, "")
+ link("loop.css", "themes/local/loop.css")
+ check("themes/local/loop.css", 403, "")
link(filepath.Dir(util.AppearancePath), "themes/local/escape")
link(filepath.Join(filepath.Dir(util.AppearancePath), "conf.json"), "langs/leak.json")
link(filepath.Join(filepath.Dir(util.AppearancePath), "conf.json"), "themes/local/theme.js")
diff --git a/kernel/server/appearance_symlink_linux.go b/kernel/server/appearance_symlink_linux.go
new file mode 100644
index 0000000000..f8cbf00843
--- /dev/null
+++ b/kernel/server/appearance_symlink_linux.go
@@ -0,0 +1,67 @@
+//go:build linux
+
+// 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/>.
+
+package server
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "strconv"
+
+ "golang.org/x/sys/unix"
+)
+
+// evalAppearanceSymlinks 保留真实路径校验,兼容 Android 可访问子目录但不能查询父目录属性的情况。
+func evalAppearanceSymlinks(name string) (string, error) {
+ resolved, walkErr := filepath.EvalSymlinks(name)
+ if walkErr == nil {
+ return resolved, nil
+ }
+ if !os.IsNotExist(walkErr) && !os.IsPermission(walkErr) {
+ return "", walkErr
+ }
+
+ // O_PATH 只取得路径句柄,不读取文件内容,也不会阻塞在命名管道上。
+ fd, err := unix.Open(name, unix.O_PATH|unix.O_CLOEXEC, 0)
+ if err != nil {
+ return "", fmt.Errorf("%w; open path: %v", walkErr, err)
+ }
+ file := os.NewFile(uintptr(fd), name)
+ defer file.Close()
+ resolved, err = os.Readlink("/proc/self/fd/" + strconv.Itoa(fd))
+ if err != nil {
+ return "", fmt.Errorf("%w; read path link: %v", walkErr, err)
+ }
+ if !filepath.IsAbs(resolved) {
+ return "", fmt.Errorf("resolved appearance path is not absolute: %q", resolved)
+ }
+ // 确认内核返回的路径仍指向同一个对象,拒绝已经删除或被替换的目标。
+ openedInfo, err := file.Stat()
+ if err != nil {
+ return "", err
+ }
+ resolvedInfo, err := os.Stat(resolved)
+ if err != nil {
+ return "", err
+ }
+ if !os.SameFile(openedInfo, resolvedInfo) {
+ return "", fmt.Errorf("resolved appearance path changed: %q", resolved)
+ }
+ return resolved, nil
+}
diff --git a/kernel/server/appearance_symlink_other.go b/kernel/server/appearance_symlink_other.go
new file mode 100644
index 0000000000..f8eaa99f3e
--- /dev/null
+++ b/kernel/server/appearance_symlink_other.go
@@ -0,0 +1,25 @@
+//go:build !linux
+
+// 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/>.
+
+package server
+
+import "path/filepath"
+
+func evalAppearanceSymlinks(name string) (string, error) {
+ return filepath.EvalSymlinks(name)
+}