An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:lock: Place encrypted notebook .sy.zip/Markdown exports under managed dir #18034
Danielf36a9e75d0f1
diff --git a/kernel/api/export.go b/kernel/api/export.goindex 59fe813e6d..257b482559 100644--- a/kernel/api/export.go+++ b/kernel/api/export.go@@ -28,6 +28,7 @@ import ("time""github.com/88250/gulu"+ "github.com/88250/lute/ast""github.com/88250/lute/parse""github.com/gin-gonic/gin""github.com/mssola/useragent"@@ -996,6 +997,28 @@ func copyExportFile(c *gin.Context) {return}+ // 加密笔记本的导出产物位于 temp/export/<boxID>/... 受控目录,复制前同样需校验:+ // 该 box 必须处于解锁状态、且产物仍登记在托管下载表中(锁定后会被清除并撤销)。+ // relativePath 去掉 "/export/" 前缀以与 serveExport 的守卫及托管注册 key(<boxID>/kind/<name>)对齐。+ relativeExportPath := strings.TrimPrefix(srcPath, "/export/")+ relativeExportPath = strings.TrimPrefix(relativeExportPath, "export/")+ if parts := strings.SplitN(strings.TrimPrefix(relativeExportPath, "/"), "/", 2); len(parts) >= 1 && ast.IsNodeIDPattern(parts[0]) && model.IsEncryptedBox(parts[0]) {+ boxID, _, ok := model.ResolveManagedEncryptedExport(relativeExportPath)+ if !ok || boxID != parts[0] {+ ret.Code = -1+ ret.Msg = "export file is not available"+ return+ }+ model.HoldBoxReadLock(boxID)+ if _, dekErr := model.GetDEKIfUnlocked(boxID); dekErr != nil {+ model.ReleaseBoxReadLock(boxID)+ ret.Code = -1+ ret.Msg = "encrypted notebook locked"+ return+ }+ defer model.ReleaseBoxReadLock(boxID)+ }+if util.IsSensitivePath(dest) {ret.Code = -2ret.Msg = "refuse to copy to sensitive path: " + destdiff --git a/kernel/model/encrypted_export.go b/kernel/model/encrypted_export.goindex 863ad60a58..d5965114fd 100644--- a/kernel/model/encrypted_export.go+++ b/kernel/model/encrypted_export.go@@ -45,8 +45,10 @@ func newManagedEncryptedExportID() (string, error) {return hex.EncodeToString(random), nil}-func registerManagedEncryptedExport(boxID, artifact string) string {- relativePath := path.Join(boxID, "resources", filepath.Base(artifact))+// registerManagedEncryptedExport 登记一个加密笔记本的导出产物,返回相对路径作为下载令牌。+// kind 标识产物来源(resources/sy/markdown),仅影响注册 key 的分段,解析与撤销均按 boxID 前缀匹配。+func registerManagedEncryptedExport(boxID, kind, artifact string) string {+ relativePath := path.Join(boxID, kind, filepath.Base(artifact))managedEncryptedExports.Lock()managedEncryptedExports.jobs[relativePath] = managedEncryptedExport{boxID: boxID,diff --git a/kernel/model/encrypted_export_test.go b/kernel/model/encrypted_export_test.goindex c1cd63a2a9..1018ac0182 100644--- a/kernel/model/encrypted_export_test.go+++ b/kernel/model/encrypted_export_test.go@@ -13,7 +13,7 @@ func TestManagedEncryptedExportRevocation(t *testing.T) {t.Fatal(err)}- relativePath := registerManagedEncryptedExport(boxID, artifact)+ relativePath := registerManagedEncryptedExport(boxID, "resources", artifact)resolvedBoxID, resolvedArtifact, ok := ResolveManagedEncryptedExport("/" + relativePath)if !ok || resolvedBoxID != boxID || resolvedArtifact != artifact {t.Fatalf("managed export was not resolved correctly: box=%q artifact=%q ok=%t", resolvedBoxID, resolvedArtifact, ok)diff --git a/kernel/model/export.go b/kernel/model/export.goindex 0b27187a9a..9b423ceb95 100644--- a/kernel/model/export.go+++ b/kernel/model/export.go@@ -790,7 +790,7 @@ func ExportResources(resourcePaths []string, mainName string) (exportFilePath stexportFilePath = path.Join("temp", "export")if encryptedBoxID != "" {- exportFilePath = path.Join("temp", "export", registerManagedEncryptedExport(encryptedBoxID, zipFilePath))+ exportFilePath = path.Join("temp", "export", registerManagedEncryptedExport(encryptedBoxID, "resources", zipFilePath))} else {exportFilePath = path.Join(exportFilePath, filepath.Base(zipFilePath))}@@ -2135,7 +2135,7 @@ func ExportPandocConvertZip(ids []string, pandocTo, ext string) (name, zipPath s}defBlockIDs, docPaths := prepareExportTrees(docPaths)- zipPath = exportPandocConvertZip(baseFolderName, docPaths, defBlockIDs, "gfm+footnotes+hard_line_breaks", pandocTo, ext)+ zipPath = exportPandocConvertZip(block.BoxID, baseFolderName, docPaths, defBlockIDs, "gfm+footnotes+hard_line_breaks", pandocTo, ext)name = util.GetTreeID(block.Path)return}@@ -2161,7 +2161,7 @@ func ExportNotebookMarkdown(boxID string) (zipPath string) {}defBlockIDs, docPaths := prepareExportTrees(docPaths)- zipPath = exportPandocConvertZip(box.Name, docPaths, defBlockIDs, "", "", ".md")+ zipPath = exportPandocConvertZip(boxID, box.Name, docPaths, defBlockIDs, "", "", ".md")return}@@ -2280,7 +2280,28 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (baseFolderName = path.Join(dir, name)box := Conf.Box(boxID)+ // 加密笔记本的导出全程持读锁,并把明文中间目录与产物写到 temp/export/<boxID>/sy/<exportID>/ 受控目录下,+ // 以便 LockBox 清理与托管下载校验。普通笔记本保持既有以 boxName 为名的导出路径,行为不变。+ encrypted := IsEncryptedBox(boxID)+ var exportID string+ if encrypted {+ HoldBoxReadLock(boxID)+ defer ReleaseBoxReadLock(boxID)+ if _, dekErr := GetDEKIfUnlocked(boxID); dekErr != nil {+ logging.LogErrorf("export .sy.zip of encrypted box [%s] failed: locked", boxID)+ return+ }+ var idErr error+ exportID, idErr = newManagedEncryptedExportID()+ if idErr != nil {+ logging.LogErrorf("new export id failed: %s", idErr)+ return+ }+ }exportDir := filepath.Join(util.TempDir, "export", baseFolderName)+ if encrypted {+ exportDir = filepath.Join(util.TempDir, "export", boxID, "sy", exportID)+ }if err := os.MkdirAll(exportDir, 0755); err != nil {logging.LogErrorf("create export temp folder failed: %s", err)return@@ -2533,8 +2554,15 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (}}+ // 加密笔记本先写 .partial 再原子 rename,并把产物登记为托管下载令牌;普通笔记本保持原行为。+ zipBaseName := baseFolderName + ".sy.zip"zipPath = exportDir + ".sy.zip"- zip, err := gulu.Zip.Create(zipPath)+ zipPartialPath := zipPath + ".partial"+ if encrypted {+ zipPath = filepath.Join(util.TempDir, "export", boxID, "sy", exportID+"-"+zipBaseName)+ zipPartialPath = zipPath + ".partial"+ }+ zip, err := gulu.Zip.Create(zipPartialPath)if err != nil {logging.LogErrorf("create export .sy.zip [%s] failed: %s", exportDir, err)return ""@@ -2552,9 +2580,17 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (if err = zip.Close(); err != nil {logging.LogErrorf("close export .sy.zip failed: %s", err)}+ if err = os.Rename(zipPartialPath, zipPath); err != nil {+ logging.LogErrorf("publish export .sy.zip [%s] failed: %s", zipPath, err)+ return ""+ }os.RemoveAll(exportDir)- zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))+ if encrypted {+ zipPath = "/export/" + registerManagedEncryptedExport(boxID, "sy", zipPath)+ } else {+ zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))+ }return}@@ -3834,7 +3870,7 @@ func processFileAnnotationRef(refID string, n *ast.Node, fileAnnotationRefMode ireturn ast.WalkSkipChildren}-func exportPandocConvertZip(baseFolderName string, docPaths, defBlockIDs []string, pandocFrom, pandocTo, ext string) (zipPath string) {+func exportPandocConvertZip(boxID, baseFolderName string, docPaths, defBlockIDs []string, pandocFrom, pandocTo, ext string) (zipPath string) {defer util.ClearPushProgress(100)dir, name := path.Split(baseFolderName)@@ -3846,7 +3882,28 @@ func exportPandocConvertZip(baseFolderName string, docPaths, defBlockIDs []strin}baseFolderName = path.Join(dir, name)+ // 加密笔记本的导出全程持读锁,并把明文中间目录与产物写到 temp/export/<boxID>/markdown/<exportID>/ 受控目录下,+ // 以便 LockBox 清理与托管下载校验。普通笔记本保持既有以 baseFolderName 为名的导出路径,行为不变。+ encrypted := IsEncryptedBox(boxID)+ var exportID string+ if encrypted {+ HoldBoxReadLock(boxID)+ defer ReleaseBoxReadLock(boxID)+ if _, dekErr := GetDEKIfUnlocked(boxID); dekErr != nil {+ logging.LogErrorf("export markdown of encrypted box [%s] failed: locked", boxID)+ return+ }+ var idErr error+ exportID, idErr = newManagedEncryptedExportID()+ if idErr != nil {+ logging.LogErrorf("new export id failed: %s", idErr)+ return+ }+ }exportFolder := filepath.Join(util.TempDir, "export", baseFolderName+ext)+ if encrypted {+ exportFolder = filepath.Join(util.TempDir, "export", boxID, "markdown", exportID)+ }os.RemoveAll(exportFolder)if err := os.MkdirAll(exportFolder, 0755); err != nil {logging.LogErrorf("create export temp folder failed: %s", err)@@ -3904,7 +3961,7 @@ func exportPandocConvertZip(baseFolderName string, docPaths, defBlockIDs []strin}// 解析导出后的标准 Markdown,汇总 assets- boxID := tree.Box+ treeBoxID := tree.Boxtree = parse.Parse("", gulu.Str.ToBytes(md), luteEngine.ParseOptions)removeAssetsID(tree, assetsOldNew, assetsNewOld)@@ -3933,8 +3990,8 @@ func exportPandocConvertZip(baseFolderName string, docPaths, defBlockIDs []strinspaceDecodedOldAsset := strings.ReplaceAll(oldAsset, "%20", " ")srcPath := ""- if IsEncryptedBox(boxID) {- srcPath, _ = GetAssetAbsPathInBox(spaceDecodedOldAsset, boxID)+ if IsEncryptedBox(treeBoxID) {+ srcPath, _ = GetAssetAbsPathInBox(spaceDecodedOldAsset, treeBoxID)}if "" == srcPath {srcPath = assetsPathMap[AssetPathWithoutQuery(spaceDecodedOldAsset)]@@ -3966,8 +4023,15 @@ func exportPandocConvertZip(baseFolderName string, docPaths, defBlockIDs []strinutil.PushEndlessProgress(Conf.language(65) + " " + fmt.Sprintf(Conf.language(70), fmt.Sprintf("%d/%d %s", i+1, len(docPaths), name)))}+ // 加密笔记本先写 .partial 再原子 rename,并把产物登记为托管下载令牌;普通笔记本保持原行为。+ zipBaseName := baseFolderName + ext + ".zip"zipPath = exportFolder + ".zip"- zip, err := gulu.Zip.Create(zipPath)+ zipPartialPath := zipPath + ".partial"+ if encrypted {+ zipPath = filepath.Join(util.TempDir, "export", boxID, "markdown", exportID+"-"+zipBaseName)+ zipPartialPath = zipPath + ".partial"+ }+ zip, err := gulu.Zip.Create(zipPartialPath)if err != nil {logging.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)return ""@@ -4000,9 +4064,17 @@ func exportPandocConvertZip(baseFolderName string, docPaths, defBlockIDs []strinif err = zip.Close(); err != nil {logging.LogErrorf("close export markdown zip failed: %s", err)}+ if err = os.Rename(zipPartialPath, zipPath); err != nil {+ logging.LogErrorf("publish export markdown zip [%s] failed: %s", zipPath, err)+ return ""+ }os.RemoveAll(exportFolder)- zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))+ if encrypted {+ zipPath = "/export/" + registerManagedEncryptedExport(boxID, "markdown", zipPath)+ } else {+ zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))+ }return}