{"record":{"id":"3de91918eb4b245f","repo":"siyuan-note/siyuan","slug":"invalid-encrypted-asset-content-length","errorCode":null,"errorMessage":"invalid encrypted asset content length","messagePattern":"invalid encrypted asset content length","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":2493,"sourceCode":"\t\t\tzeroAndClear(plainChunk)\n\t\t\treturn \"\", errors.New(\"invalid encrypted asset plaintext chunk size\")\n\t\t}\n\t\tn, writeErr := writer.Write(plainChunk)\n\t\twritten += int64(n)\n\t\tzeroAndClear(plainChunk)\n\t\tif writeErr != nil {\n\t\t\treturn \"\", writeErr\n\t\t}\n\t\tif n != len(plainChunk) {\n\t\t\treturn \"\", io.ErrShortWrite\n\t\t}\n\t}\n\tvar terminator uint32\n\tif err = binary.Read(reader, binary.BigEndian, &terminator); err != nil {\n\t\treturn \"\", err\n\t}\n\tif terminator != 0 || written != metadata.Size {\n\t\treturn \"\", errors.New(\"invalid encrypted asset content length\")\n\t}\n\tvar trailing [1]byte\n\tif _, trailingErr := io.ReadFull(reader, trailing[:]); trailingErr != io.EOF {\n\t\treturn \"\", errors.New(\"invalid trailing encrypted asset data\")\n\t}\n\treturn metadata.OriginalName, nil\n}\n\n// DecryptAsset 对应解密。\nfunc DecryptAsset(boxID, diskName string, dek, ciphertext []byte) ([]byte, error) {\n\tplaintext, _, err := DecryptAssetWithName(boxID, diskName, dek, ciphertext)\n\treturn plaintext, err\n}\n\n// notebookCryptoBackupPath 返回加密笔记本的独立 BoxCrypt 备份路径。\n// 该文件在主 conf.json 丢失时用作\"此笔记本是加密笔记本\"的标识和降级恢复源。\n// 与全局 NotebookCrypto 备份（<DataDir>/.siyuan/data-crypto-backup.json）配合使用，\n// 全局备份存 MasterSalt/KEKVerifier，per-notebook 备份存 WrappedDEK/WrapNonce。","sourceCodeStart":2475,"sourceCodeEnd":2511,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L2475-L2511","documentation":"Final integrity check of DecryptAssetToWriter: after all chunks are written it reads the 4-byte terminator, which must be 0, and requires that total bytes written equal metadata.Size. Any mismatch means the content length recorded in metadata disagrees with the actual decrypted content, so the stream is rejected.","triggerScenarios":"DecryptAssetToWriter / DecryptAssetWithName / DecryptAsset on ciphertext whose declared metadata.Size is wrong, chunks were added/removed, or the terminator field is nonzero — tampering, corruption, or spliced streams.","commonSituations":"Files repaired or truncated then padded; asset re-wrapped under ChangeMasterPassword where metadata was copied from a different file; sync conflicts resolved by concatenating variants.","solutions":["Restore the asset from sync/history — a single consistent copy must contain matching metadata, chunks, and terminator","Re-encrypt from the original source to rebuild consistent metadata and terminator","Compare file size against expected envelope size (8 + metadataSize + chunks*(4+ciphertext) + 4) to locate truncation/extra data","Never hand-edit or concatenate encrypted asset files"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"func declaredSizeMatches(metaSize, chunks, size int64) bool {\n    return metaSize >= 1 && metaSize <= 1024*1024 && size >= 0 && chunks >= 0\n}","typeGuard":null,"tryCatchPattern":"plain, name, err := model.DecryptAssetWithName(boxID, diskName, dek, data)\nif err != nil && strings.Contains(err.Error(), \"content length\") {\n    return restoreAsset(boxID, diskName) // inconsistent envelope: use backup\n}","preventionTips":["Copy encrypted assets atomically and verify byte counts after transfer","Never concatenate or append to envelope files","After key/password changes, confirm Size/Chunks still match by test decryption","Restore from a single consistent snapshot, not merged variants"],"tags":["crypto","integrity","corruption"],"backgroundTag":"checksum-mismatch","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"}