An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Harden encrypted notebook initialization https://github.com/siyuan-note/siyuan/issues/18266
Daniel8961c66bed3a
diff --git a/kernel/filesys/tree.go b/kernel/filesys/tree.goindex 0012133645..cbc939711b 100644--- a/kernel/filesys/tree.go+++ b/kernel/filesys/tree.go@@ -286,7 +286,9 @@ func DocIAL(absPath string) (ret map[string]string) {// 注意:filelock.ReadFile 内部已加锁,不能在外面再 Lock/Unlock(会死锁)raw, readErr := filelock.ReadFile(absPath)if readErr != nil {- logging.LogErrorf("read file [%s] failed: %s", absPath, readErr)+ if !errors.Is(readErr, os.ErrNotExist) {+ logging.LogErrorf("read file [%s] failed: %s", absPath, readErr)+ }return nil}relPath := filepath.ToSlash(strings.TrimPrefix(absPath, filepath.Join(util.DataDir, boxID)+string(os.PathSeparator)))@@ -316,7 +318,9 @@ func DocIAL(absPath string) (ret map[string]string) {filelock.Lock(absPath)file, err := os.Open(absPath)if err != nil {- logging.LogErrorf("open file [%s] failed: %s", absPath, err)+ if !errors.Is(err, os.ErrNotExist) {+ logging.LogErrorf("open file [%s] failed: %s", absPath, err)+ }filelock.Unlock(absPath)return nil}diff --git a/kernel/model/blockinfo.go b/kernel/model/blockinfo.goindex a23e4f8fe5..d66a6b3d35 100644--- a/kernel/model/blockinfo.go+++ b/kernel/model/blockinfo.go@@ -17,6 +17,7 @@package modelimport (+ "errors""os""path/filepath""sort"@@ -63,7 +64,11 @@ func GetDocInfoInBox(blockID, boxID string) (ret *BlockInfo, err error) {tree, err := loadTreeByBlockIDInBox(blockID, boxID)if err != nil {- logging.LogErrorf("load tree by root id [%s] failed: %s", blockID, err)+ if errors.Is(err, ErrIndexing) {+ logging.LogWarnf("load tree by root id [%s] failed: %s", blockID, err)+ } else {+ logging.LogErrorf("load tree by root id [%s] failed: %s", blockID, err)+ }return}diff --git a/kernel/model/crypto.go b/kernel/model/crypto.goindex cef74d99be..aba158e1c5 100644--- a/kernel/model/crypto.go+++ b/kernel/model/crypto.go@@ -1719,7 +1719,7 @@ func CreateEncryptedBox(name, password string) (id string, err error) {}defer zeroAndClear(kek)- id, err = CreateBox(name)+ id, err = createBox(name, false)if err != nil {return "", err}@@ -1728,6 +1728,12 @@ func CreateEncryptedBox(name, password string) (id string, err error) {boxCreated := truedefer func() {if err != nil && boxCreated {+ cachedDEKsLock.Lock()+ if cachedDEK, ok := cachedDEKs[id]; ok {+ zeroAndClear(cachedDEK)+ delete(cachedDEKs, id)+ }+ cachedDEKsLock.Unlock()sql.RemoveEncryptedDBFile(id)treenode.RemoveEncryptedBlockTreeDBFile(id)boxDir := filepath.Join(util.DataDir, id)@@ -1762,15 +1768,21 @@ func CreateEncryptedBox(name, password string) (id string, err error) {// 复用刚派生的 DEK 直接开 db + 缓存,省去再次 Argon2id 解锁cachedDEKsLock.Lock()- defer cachedDEKsLock.Unlock()if err = sql.OpenEncryptedDB(id, dek); err != nil {+ cachedDEKsLock.Unlock()return "", err}if err = treenode.OpenEncryptedBlockTreeDB(id, dek); err != nil {sql.CloseEncryptedDB(id)+ cachedDEKsLock.Unlock()return "", err}cachedDEKs[id] = dek+ cachedDEKsLock.Unlock()++ if _, err = EnsureBoxDoc(id); err != nil {+ return "", fmt.Errorf("initialize encrypted notebook document failed: %w", err)+ }// 初始化自动锁定访问时间戳,与 UnlockBox 对称newVal := &atomic.Int64{}diff --git a/kernel/model/history.go b/kernel/model/history.goindex cba86357f1..872aaced93 100644--- a/kernel/model/history.go+++ b/kernel/model/history.go@@ -818,6 +818,9 @@ func createAssetsHistory(assets []string) (err error) {}if err = filelock.Copy(file, historyPath); err != nil {+ if os.IsNotExist(err) {+ continue+ }return fmt.Errorf("copy asset [%s] to [%s] failed: %w", file, historyPath, err)}}@@ -1135,6 +1138,7 @@ func indexHistoryDir(name string, luteEngine *lute.Lute) {})var histories []*sql.History+ encryptedHistoryBoxes := map[string]bool{}for _, doc := range docs {relDoc := strings.TrimPrefix(doc, entryPath+string(os.PathSeparator))relDoc = filepath.ToSlash(relDoc)@@ -1146,7 +1150,22 @@ func indexHistoryDir(name string, luteEngine *lute.Lute) {p := strings.TrimPrefix(doc, util.HistoryDir)p = filepath.ToSlash(p[1:])- if histBoxID != "" && IsEncryptedBox(histBoxID) {+ isEncrypted := IsEncryptedBox(histBoxID)+ if histBoxID != "" && !isEncrypted {+ if cached, ok := encryptedHistoryBoxes[histBoxID]; ok {+ isEncrypted = cached+ } else {+ boxConfData, readErr := os.ReadFile(filepath.Join(entryPath, histBoxID, ".siyuan", "conf.json"))+ if readErr == nil {+ boxConf := &conf.BoxConf{}+ if json.Unmarshal(boxConfData, boxConf) == nil {+ isEncrypted = boxConf.Encrypted+ }+ }+ encryptedHistoryBoxes[histBoxID] = isEncrypted+ }+ }+ if isEncrypted {// 加密笔记本:content 留空,只存路径用于文件列表展示docID := strings.TrimSuffix(filepath.Base(doc), ".sy")histories = append(histories, &sql.History{diff --git a/kernel/model/mount.go b/kernel/model/mount.goindex 499e6ea210..d25778424f 100644--- a/kernel/model/mount.go+++ b/kernel/model/mount.go@@ -48,6 +48,10 @@ func GetBoxByName(name string) (ret *Box) {}func CreateBox(name string) (id string, err error) {+ return createBox(name, true)+}++func createBox(name string, initializeBoxDoc bool) (id string, err error) {name = normalizeBoxName(name)if 512 < utf8.RuneCountInString(name) {// 限制笔记本名和文档名最大长度为 `512` https://github.com/siyuan-note/siyuan/issues/6299@@ -81,13 +85,15 @@ func CreateBox(name string) (id string, err error) {if err := box.SaveConf(boxConf); err != nil {logging.LogErrorf("save box conf [%s] failed: %s", id, err)}- if _, err = ensureBoxDoc0(id); err != nil {- treenode.RemoveBlockTreesByBoxID(id)- sql.DeleteBoxQueue(id)- if removeErr := filelock.Remove(boxLocalPath); nil != removeErr {- logging.LogErrorf("remove box [%s] after initializing box document failed: %s", id, removeErr)+ if initializeBoxDoc {+ if _, err = ensureBoxDoc0(id); err != nil {+ treenode.RemoveBlockTreesByBoxID(id)+ sql.DeleteBoxQueue(id)+ if removeErr := filelock.Remove(boxLocalPath); nil != removeErr {+ logging.LogErrorf("remove box [%s] after initializing box document failed: %s", id, removeErr)+ }+ return "", err}- return "", err}IncSync()logging.LogInfof("created box [%s]", id)