{"record":{"id":"bd78e10aae4ccca3","repo":"siyuan-note/siyuan","slug":"boxid-mismatch-param-s-path-s","errorCode":null,"errorMessage":"boxID mismatch: param=%s, path=%s","messagePattern":"boxID mismatch: param=(.+?), path=(.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/upload.go","lineNumber":600,"sourceCode":"\t\t\t\t_ = os.MkdirAll(assets, 0755)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tassets = filepath.Join(util.DataDir, \"assets\")\n\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：全读 → 加密 → 落盘","sourceCodeStart":582,"sourceCodeEnd":618,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/upload.go#L582-L618","documentation":"writeAssetFile cross-validates the boxID passed by the caller against the boxID extracted from the target assets file path. If both are non-empty and differ, it refuses to write, preventing a caller from writing an asset into a different notebook (box) than declared — a cross-box write guard for the encrypted-notebook feature.","triggerScenarios":"Calling UploadAssets / InsertAssetBytes / insertLocalAssets / netAssets2LocalAssets0 / importSYAssets with an assetsDirPath resolved under one box's directory while passing a different non-empty boxID parameter (stale or wrong box id in the API payload).","commonSituations":"Plugin or frontend code caching an old boxID after a notebook was renamed/re-created; a docID belonging to box A combined with an assets path computed from box B; copy-pasted integration code passing the wrong box parameter to the upload API.","solutions":["Pass the correct boxID matching the notebook that owns the assets path, or omit it (empty) so the path-derived boxID is used","Verify the assetsDirPath is derived from the same box as the boxID parameter before calling","Check that the docID/notebook was not renamed or recreated; re-resolve the box from the document ID"],"exampleFix":"// before\nupload(boxID: \"20240101120000-abc\", assetsDirPath: \"/data/20240201101010-xyz/assets\", ...)\n// after\nupload(boxID: \"20240201101010-xyz\", assetsDirPath: \"/data/20240201101010-xyz/assets\", ...)","handlingStrategy":"validation","validationCode":"func checkBoxPath(boxID, writePath string) error {\n    pathBoxID := ExtractBoxIDFromAssetsPath(writePath)\n    if boxID != \"\" && pathBoxID != \"\" && boxID != pathBoxID {\n        return fmt.Errorf(\"boxID %s does not own path %s\", boxID, writePath)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err := writeAssetFile(writePath, src, boxID, name); err != nil {\n    if strings.HasPrefix(err.Error(), \"boxID mismatch\") {\n        // re-resolve boxID from the document/path and retry\n    }\n}","preventionTips":["Derive boxID and the assets path from the same source (the document's box) in one place","Never cache boxIDs across notebook rename/recreate operations","Prefer passing an empty boxID and letting the path determine the box when unsure"],"tags":["upload","assets","boxid","security"],"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"}