Skip to content

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

:bug: Avoid panic when listing documents with invalid property IDs

Daniele4fee743a588
diff --git a/kernel/model/file.go b/kernel/model/file.go
index d7834aaf00..d9cc3a8630 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -79,6 +79,16 @@ type File struct {
}
func (box *Box) docFromFileInfo(fileInfo *FileInfo, ial map[string]string) (ret *File) {
+ // 文档路径以根块 ID 命名,使用路径 ID 避免异常文档属性导致越界或返回错误的文档 ID。
+ pathID := util.GetTreeID(fileInfo.path)
+ if !ast.IsNodeIDPattern(pathID) {
+ logging.LogWarnf("invalid document path ID in file [%s]", fileInfo.path)
+ return nil
+ }
+ if ialID := ial["id"]; ialID != pathID {
+ logging.LogWarnf("document ID [%s] does not match path ID [%s], use path ID", ialID, pathID)
+ }
+
ret = &File{}
ret.Path = fileInfo.path
ret.Size = uint64(fileInfo.size)
@@ -87,7 +97,7 @@ func (box *Box) docFromFileInfo(fileInfo *FileInfo, ial map[string]string) (ret
if icon, ok := util.FilterIconValue(ial["icon"]); ok {
ret.Icon = icon
}
- ret.ID = ial["id"]
+ ret.ID = pathID
ret.Name1 = ial["name"]
ret.Alias = ial["alias"]
ret.Memo = ial["memo"]
@@ -508,6 +518,9 @@ func ListDocTree(boxID, listPath string, sortMode int, flashcard, showHidden boo
}
doc := box.docFromFileInfo(parentDocFile, ial)
+ if nil == doc {
+ continue
+ }
subFiles, err := os.ReadDir(filepath.Join(boxLocalPath, file.path))
if err == nil {
for _, subFile := range subFiles {
@@ -555,6 +568,9 @@ func ListDocTree(boxID, listPath string, sortMode int, flashcard, showHidden boo
}
doc := box.docFromFileInfo(file, ial)
+ if nil == doc {
+ continue
+ }
if flashcard {
rootID := util.GetTreeID(file.path)
diff --git a/kernel/model/file_test.go b/kernel/model/file_test.go
index 7a5786f141..ff767beb6f 100644
--- a/kernel/model/file_test.go
+++ b/kernel/model/file_test.go
@@ -109,6 +109,51 @@ func setupFileOperationTest(t *testing.T) *fileOperationTestFixture {
}
}
+func TestListDocTreeUsesPathIDForInvalidPropertiesID(t *testing.T) {
+ fixture := setupFileOperationTest(t)
+ ial := filesys.DocIAL(filepath.Join(util.DataDir, fixture.box.ID, fixture.sourcePath))
+ tests := []struct {
+ name string
+ id string
+ }{
+ {name: "missing"},
+ {name: "short", id: "legacy"},
+ {name: "mismatch", id: "20260718000009-hijklmn"},
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ invalidIAL := make(map[string]string, len(ial))
+ for key, value := range ial {
+ invalidIAL[key] = value
+ }
+ if "" == test.id {
+ delete(invalidIAL, "id")
+ } else {
+ invalidIAL["id"] = test.id
+ }
+ cache.PutDocIALInBox(fixture.sourcePath, fixture.box.ID, invalidIAL)
+ t.Cleanup(func() {
+ cache.RemoveDocIALInBox(fixture.sourcePath, fixture.box.ID)
+ })
+
+ files, _, err := ListDocTree(fixture.box.ID, "/", util.SortModeNameASC, false, false, 128)
+ if nil != err {
+ t.Fatalf("list documents failed: %v", err)
+ }
+ for _, file := range files {
+ if fixture.sourceID == file.ID {
+ if 0 >= file.CTime {
+ t.Fatalf("invalid creation time for recovered document: %d", file.CTime)
+ }
+ return
+ }
+ }
+ t.Fatalf("document [%s] was not listed", fixture.sourceID)
+ })
+ }
+}
+
func TestResolveDocTreeSortModeInheritance(t *testing.T) {
fixture := setupFileOperationTest(t)
Conf.FileTree.Sort = util.SortModeSizeDESC