Skip to content

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

:bug: Allow leading slash in ValidateBoxRelativePath for block tree DB paths, skip validation for Ls empty/root paths https://github.com/siyuan-note/siyuan/issues/18034

Danield6ec84c6e9d4
diff --git a/kernel/filesys/tree.go b/kernel/filesys/tree.go
index 5fe9cc9e12..0052d28dad 100644
--- a/kernel/filesys/tree.go
+++ b/kernel/filesys/tree.go
@@ -145,19 +145,21 @@ func batchLoadTrees(boxIDs, paths []string, luteEngine *lute.Lute) (ret []*parse
}
// ValidateBoxRelativePath 校验 box 内相对路径是否安全。
-// 拒绝 ..、绝对路径、盘符,确保最终路径位于 <DataDir>/<boxID> 内。
+// 拒绝 ..、绝对路径,确保最终路径位于 <DataDir>/<boxID> 内。
+// 允许路径以 / 开头(如 /20230101/xxx.sy),会自动标准化再去掉前导斜杠。
func ValidateBoxRelativePath(boxID, p string) (string, error) {
p = filepath.ToSlash(p)
- if strings.HasPrefix(p, "..") || strings.Contains(p, "/../") || strings.HasSuffix(p, "/..") || p == ".." {
- return "", fmt.Errorf("path [%s] must not contain '..'", p)
- }
- if strings.HasPrefix(p, "/") {
- return "", fmt.Errorf("path [%s] must be relative", p)
- }
- resolved := filepath.Join(util.DataDir, boxID, p)
+ // 记录原始路径用于 IsSubPath 校验
+ origP := p
+ // 标准化:去掉前导 /
+ p = strings.TrimPrefix(p, "/")
+ if strings.HasPrefix(p, "..") || strings.Contains(p, "/../") || strings.HasSuffix(p, "/..") || p == ".." || p == "." {
+ return "", fmt.Errorf("path [%s] must not contain '..'", origP)
+ }
+ resolved := filepath.Join(util.DataDir, boxID, origP)
boxRoot := filepath.Join(util.DataDir, boxID)
if !gulu.File.IsSubPath(boxRoot, resolved) {
- return "", fmt.Errorf("path [%s] escapes box directory", p)
+ return "", fmt.Errorf("path [%s] escapes box directory", origP)
}
return p, nil
}
diff --git a/kernel/model/box.go b/kernel/model/box.go
index aa849cbdf1..9c0cfb76c1 100644
--- a/kernel/model/box.go
+++ b/kernel/model/box.go
@@ -275,8 +275,13 @@ func (box *Box) validateBoxPath(p string) (string, error) {
}
func (box *Box) Ls(p string) (ret []*FileInfo, totals int, err error) {
- if _, err = box.validateBoxPath(p + "/placeholder"); err != nil {
- return
+ if "/" == p {
+ p = ""
+ }
+ if p != "" {
+ if _, err = box.validateBoxPath(p); err != nil {
+ return
+ }
}
boxLocalPath := filepath.Join(util.DataDir, box.ID)
if strings.HasSuffix(p, ".sy") {