{"record":{"id":"76c05a56402cac00","repo":"siyuan-note/siyuan","slug":"encrypted-box-asset-must-be-written-inside-the-box","errorCode":null,"errorMessage":"encrypted box asset must be written inside the box directory, got global path: %s","messagePattern":"encrypted box asset must be written inside the box directory, got global path: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/upload.go","lineNumber":604,"sourceCode":"\t\t}\n\t}\n\treturn\n}\n\n// writeAssetFile 把 src 的内容写入 writePath。从 writePath 反查真实 boxID 决定是否加密——\n// 不轻信传入的 boxID（调用方可能未传，或 assetsDirPath 指向加密笔记本但 id 为空）。\n// 加密笔记本必须已解锁（DEK 在内存）才写入；加密但未解锁返回错误（fail-closed，避免明文落盘）。\n// 非加密笔记本按 reader 直接写（走 filelock.WriteFileByReader 原路径，保留锁语义）。\nfunc writeAssetFile(writePath string, src io.Reader, boxID, originalName string) (err error) {\n\t// 从 writePath 反查真实 boxID，与传入 boxID 交叉校验\n\tpathBoxID := ExtractBoxIDFromAssetsPath(writePath)\n\t// 传入 boxID 与路径 box 都非空但不一致：路径指向另一个 box，拒绝（防跨 box 写入）\n\tif boxID != \"\" && pathBoxID != \"\" && boxID != pathBoxID {\n\t\treturn fmt.Errorf(\"boxID mismatch: param=%s, path=%s\", boxID, pathBoxID)\n\t}\n\t// 路径不在 box 下但传入的是加密 box：加密内容只能写 box 内，拒绝写全局 assets\n\tif pathBoxID == \"\" && boxID != \"\" && IsEncryptedBox(boxID) {\n\t\treturn fmt.Errorf(\"encrypted box asset must be written inside the box directory, got global path: %s\", writePath)\n\t}\n\tactualBoxID := pathBoxID\n\tif actualBoxID == \"\" {\n\t\tactualBoxID = boxID // 路径不在 box 下（如全局 assets），回退传入值\n\t}\n\tif actualBoxID != \"\" && IsEncryptedBox(actualBoxID) {\n\t\tHoldBoxReadLock(actualBoxID)\n\t\tdefer ReleaseBoxReadLock(actualBoxID)\n\t\tdek, dekErr := GetDEKIfUnlocked(actualBoxID)\n\t\tif dekErr != nil {\n\t\t\t// 加密笔记本未解锁：拒绝写入，避免明文落盘（深度防御，见 issue #18034）\n\t\t\treturn dekErr\n\t\t}\n\t\t// 已解锁的加密 box：全读 → 加密 → 落盘\n\t\traw, readErr := io.ReadAll(src)\n\t\tif readErr != nil {\n\t\t\treturn readErr\n\t\t}","sourceCodeStart":586,"sourceCodeEnd":622,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/upload.go#L586-L622","documentation":"writeAssetFile refuses to write an asset for an encrypted notebook when the target path is outside any box directory (e.g. the global data/assets folder). Encrypted-notebook content must stay inside its box so it can be encrypted with the box's DEK; a global path would force plaintext on disk, so the write fails closed.","triggerScenarios":"Calling one of writeAssetFile's callers (UploadAssets, InsertAssetBytes, insertLocalAssets, netAssets2LocalAssets0, importSYAssets) with boxID set to an encrypted notebook while the resolved writePath falls back to the global data/assets directory — typically because the box-level assets dir could not be resolved.","commonSituations":"An encrypted notebook whose doc directory lacks an assets folder combined with code relying on the legacy global-assets fallback; plugins constructing a global assets path for an encrypted notebook; migrations porting non-encrypted asset logic to encrypted boxes.","solutions":["Ensure the target path is inside the encrypted notebook's own directory, e.g. <box>/assets/, before writing","Create/resolve the box-level assets directory (getAssetsDir creates it for encrypted boxes) instead of falling back to data/assets","If the target notebook need not be encrypted, use a non-encrypted box or a global path without an encrypted boxID"],"exampleFix":"// before: global path for encrypted box\nwritePath = \"/data/assets/photo.png\"; boxID = \"20240101-enc-box\"\n// after: path inside the encrypted box\nwritePath = \"/data/20240101-enc-box/assets/photo.png\"; boxID = \"20240101-enc-box\"","handlingStrategy":"validation","validationCode":"func ensureInsideEncryptedBox(writePath, boxID string) error {\n    if boxID != \"\" && IsEncryptedBox(boxID) && ExtractBoxIDFromAssetsPath(writePath) == \"\" {\n        return fmt.Errorf(\"path %s is outside box %s\", writePath, boxID)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err := writeAssetFile(writePath, src, boxID, name); err != nil {\n    if strings.Contains(err.Error(), \"must be written inside the box directory\") {\n        // recompute writePath under the box's assets dir and retry\n    }\n}","preventionTips":["Always resolve asset paths via getAssetsDir, which creates box-level assets for encrypted boxes","Do not rely on the legacy global data/assets fallback for encrypted notebooks","Check IsEncryptedBox(boxID) before choosing a global assets path"],"tags":["upload","assets","encryption","path"],"backgroundTag":"invalid-argument-value","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}