{"record":{"id":"8f0c26816b4c0c6a","repo":"siyuan-note/siyuan","slug":"conf-language-317","errorCode":null,"errorMessage":"Conf.Language(317)","messagePattern":"Conf\\.Language\\(317\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":331,"sourceCode":"// 安全：备份文件不含主密码（salt 不保密、verifier 是密文），导入只恢复配置，解锁仍需主密码。\n// 防呆：本机已有完整且已启用的加密配置时拒绝导入，避免覆盖现有 salt/verifier 孤立现有 WrappedDEK。\n// ImportNotebookCryptoBackup 接收用户导入的密钥备份文件内容（JSON 字节）+ 主密码，\n// 校验主密码能解开备份里的 verifier 后才写回配置。防止 crafted 备份设置弱 KDFParams 等攻击。\n// RecoveryRequired 状态允许导入，但候选 KEK 必须能解开所有现存笔记本和已删除笔记本历史。\nfunc ImportNotebookCryptoBackup(data []byte, password string) error {\n\tnotebookCryptoMu.Lock()\n\tdefer notebookCryptoMu.Unlock()\n\n\tConf.m.RLock()\n\tcurrent := *Conf.NotebookCrypto\n\tConf.m.RUnlock()\n\tif current.Enabled && notebookCryptoConfigurationComplete(&current) {\n\t\treturn errors.New(Conf.Language(324))\n\t}\n\n\tnc := &conf.NotebookCrypto{}\n\tif err := json.Unmarshal(data, nc); err != nil {\n\t\treturn errors.New(Conf.Language(317))\n\t}\n\tif !notebookCryptoConfigurationComplete(nc) {\n\t\treturn errors.New(Conf.Language(317))\n\t}\n\n\t// 用导入的 salt + 用户输入的主密码派生 KEK，校验能否解开备份里的 verifier\n\tparams, validErr := util.ValidateArgon2Params(nc.KDFParams)\n\tif validErr != nil {\n\t\treturn errors.New(Conf.Language(317))\n\t}\n\tkek := util.DeriveKey(password, nc.MasterSalt, params)\n\tdefer zeroAndClear(kek)\n\tif nc.Checksum != computeBackupChecksum(nc) {\n\t\treturn errors.New(Conf.Language(317))\n\t}\n\tif !verifyKEKMAC(nc, kek) {\n\t\treturn errors.New(Conf.Language(317))\n\t}","sourceCodeStart":313,"sourceCodeEnd":349,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L313-L349","documentation":"Conf.Language(317) is the localized 'invalid key backup file' message. ImportNotebookCryptoBackup returns it when the uploaded backup content is not valid JSON that unmarshals into a conf.NotebookCrypto structure — the file is malformed, truncated, or not a SiYuan crypto backup at all.","triggerScenarios":"Calling model.ImportNotebookCryptoBackup(data, password) where json.Unmarshal(data, nc) fails (kernel/model/crypto.go:331) — the payload is empty, HTML from a failed download, a renamed unrelated file, or JSON with the wrong schema.","commonSituations":"A browser saved an error page instead of the backup JSON; the file was truncated by an incomplete transfer; user picked the wrong file (e.g. a sync snapshot or conf.json); the backup was hand-edited and syntax broke.","solutions":["Re-export the key backup from the original device (ExportNotebookCryptoBackup) and import that exact file","Open the file in a text editor and confirm it is valid JSON containing fields like MasterSalt, KDFParams, KEKVerifier, Checksum","If the only copy is corrupted, restore it from the workspace/sync backup; otherwise the encryption configuration must be recreated with the original master password and recovery procedure"],"exampleFix":"// caller-side precheck\nlet parsed;\ntry { parsed = JSON.parse(fileText); } catch { showMessage(\"Not a valid backup file (invalid JSON)\"); return; }\nif (!parsed.MasterSalt || !parsed.KDFParams) { showMessage(\"Not a SiYuan key backup file\"); return; }\nawait fetchPost(\"/api/notebook/importNotebookCryptoBackup\", {data: fileText, password});","handlingStrategy":"validation","validationCode":"let parsed;\ntry { parsed = JSON.parse(fileText); } catch { showMessage(\"File is not valid JSON\"); return; }\nif (!parsed || typeof parsed !== \"object\" || !parsed.MasterSalt || !parsed.KEKVerifier || !parsed.Checksum) {\n  showMessage(\"Not a SiYuan notebook-crypto backup file\"); return;\n}","typeGuard":"const isCryptoBackup = (o) => !!o && typeof o === \"object\" &&\n  typeof o.MasterSalt === \"string\" && !!o.KDFParams && typeof o.Checksum === \"string\";","tryCatchPattern":"try {\n  const res = await fetchSyncPost(\"/api/notebook/importNotebookCryptoBackup\", {data: fileText, password});\n  if (res.code !== 0) showMessage(res.msg);\n} catch (e) { showMessage(\"Invalid backup file\"); }","preventionTips":["Always import the exact file produced by ExportNotebookCryptoBackup; never retype or reconstruct it","Verify downloads complete fully (check file size / JSON validity) before import","Keep backups in plain .json without encoding conversions during transfer"],"tags":["json","validation","backup-import","encryption"],"backgroundTag":"json-unmarshal-failed","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"}