{"record":{"id":"0d70d528de01b619","repo":"siyuan-note/siyuan","slug":"invalid-encrypted-asset-plaintext-chunk-size","errorCode":null,"errorMessage":"invalid encrypted asset plaintext chunk size","messagePattern":"invalid encrypted asset plaintext chunk size","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":2476,"sourceCode":"\t\taad := encryptedAssetChunkAAD(aadPrefix, metadata.ContainerID, chunkIndex)\n\t\tif metadata.Spec == encryptedAssetLegacySpec {\n\t\t\taad = []byte(fmt.Sprintf(\"%s:content:%d\", aadPrefix, chunkIndex))\n\t\t}\n\t\tplainChunk, chunkErr := util.DecryptWithAAD(\n\t\t\tassetKey,\n\t\t\tencryptedChunk,\n\t\t\taad,\n\t\t)\n\t\tif chunkErr != nil {\n\t\t\treturn \"\", chunkErr\n\t\t}\n\t\texpectedSize := int64(encryptedAssetChunkSize)\n\t\tif remaining := metadata.Size - written; remaining < expectedSize {\n\t\t\texpectedSize = remaining\n\t\t}\n\t\tif int64(len(plainChunk)) != expectedSize {\n\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}","sourceCodeStart":2458,"sourceCodeEnd":2494,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L2458-L2494","documentation":"After AEAD-decrypting a chunk, DecryptAssetToWriter checks that the plaintext length equals encryptedAssetChunkSize, except for the final chunk which may equal the remaining bytes of metadata.Size. A mismatch means the ciphertext does not match the declared metadata (Size/Chunks) — the stream is internally inconsistent, so the plaintext buffer is zeroed and decryption aborts. AEAD authenticated the chunk in isolation, but its length contradicts the envelope.","triggerScenarios":"Decrypting an asset whose metadata.Size or Chunks field was forged/altered and re-protected, or whose chunks come from a differently-chunked encryption (chunk splicing across specs/versions); also a chunk decrypting to an unexpected length due to tampering that happened to pass AAD for that index.","commonSituations":"Manually assembled or repaired ciphertext; mixing chunks across container IDs where the last chunk AAD coincidentally validates; restoring half of an asset from one backup and the rest from another.","solutions":["Restore the whole asset from a single consistent backup/sync source — never merge chunks from different versions","Re-encrypt the asset from the original file (EncryptAsset) to regenerate consistent metadata + chunks","Verify metadata integrity: the Size/Chunks must correspond to the chunk stream actually present","If reproducible with known-good input, report as a regression — the regression suite covers chunk-boundary and splicing cases"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// ensure the asset came from a single consistent source before decrypting\nif srcChecksum, ok := checksums[diskName]; ok && checksum(srcPath) != srcChecksum {\n    return errors.New(\"asset checksum mismatch: refusing to decrypt\")\n}","typeGuard":null,"tryCatchPattern":"plain, err := model.DecryptAssetWithName(boxID, diskName, dek, data)\nif err != nil {\n    if strings.Contains(err.Error(), \"plaintext chunk size\") {\n        return reencryptFromOriginal(boxID, diskName) // rebuild metadata+chunks\n    }\n    return err\n}","preventionTips":["Never merge ciphertext from different backups or encryption specs","After ChangeMasterPassword, verify round-trip decryption of samples","Store and compare a digest of the full envelope with each asset","Treat any hand-repaired ciphertext as untrusted and re-encrypt from source"],"tags":["crypto","chunking","integrity"],"backgroundTag":"tensor-shape-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"}