{"record":{"id":"aca5142e5aa3c534","repo":"siyuan-note/siyuan","slug":"invalid-encrypted-asset-chunk-size","errorCode":null,"errorMessage":"invalid encrypted asset chunk size","messagePattern":"invalid encrypted asset chunk size","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":2452,"sourceCode":"\taadPrefix := \"siyuan:asset:\" + boxID + \":assets/\" + diskName\n\tplainMetadata, decryptErr := util.DecryptWithAAD(assetKey, encryptedMetadata, []byte(aadPrefix+\":metadata\"))\n\tif decryptErr != nil {\n\t\treturn \"\", decryptErr\n\t}\n\tdefer zeroAndClear(plainMetadata)\n\tmetadata, err := parseEncryptedAssetMetadata(plainMetadata)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\tvar written int64\n\tfor chunkIndex := uint64(0); chunkIndex < metadata.Chunks; chunkIndex++ {\n\t\tvar encryptedSize uint32\n\t\tif err = binary.Read(reader, binary.BigEndian, &encryptedSize); err != nil {\n\t\t\treturn \"\", err\n\t\t}\n\t\tif encryptedSize == 0 || encryptedSize > encryptedAssetChunkMaxCiphertextSize {\n\t\t\treturn \"\", errors.New(\"invalid encrypted asset chunk size\")\n\t\t}\n\t\tencryptedChunk := make([]byte, int(encryptedSize))\n\t\tif _, err = io.ReadFull(reader, encryptedChunk); err != nil {\n\t\t\treturn \"\", err\n\t\t}\n\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)","sourceCodeStart":2434,"sourceCodeEnd":2470,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L2434-L2470","documentation":"While iterating encrypted chunks, DecryptAssetToWriter reads each chunk's 4-byte ciphertext length and rejects the stream when it is 0 or exceeds encryptedAssetChunkMaxCiphertextSize. A chunk beyond the AEAD tag + plaintext limit cannot have been produced by EncryptAsset, so the stream is corrupt or tampered.","triggerScenarios":"DecryptAssetToWriter/DecryptAsset on a ciphertext where a chunk length field is 0, or larger than max chunk ciphertext size — spliced chunks from different encryption versions (see TestEncryptedAssetRejectsSplicedVersions), bit-flips in the length field, or byte-shifted streams after partial truncation.","commonSituations":"Rolling back asset history across format-spec changes; mixing chunks of an asset re-encrypted with a different chunking parameter; interrupted sync writing chunk boundaries; malicious tampering (length fields are outside AEAD protection until the chunk itself is read).","solutions":["Restore the asset from sync/history — the chunk stream is corrupt or from an incompatible format","Do NOT splice chunks from files encrypted by different SiYuan versions/specs; re-encrypt the whole asset","Confirm the full file was transferred (compare sizes/checksums between source and replica)","If it reproduces deterministically, re-export/re-upload the original asset to regenerate ciphertext"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"func envelopeSizeOK(data []byte) bool {\n    // expected total = 8 + metadataSize + sum(4+chunkLen) + 4; verify no chunk\n    // length is 0 or > max before calling decrypt\n    if len(data) < 12 { return false }\n    off := 8 + int(binary.BigEndian.Uint32(data[4:8]))\n    for off+4 <= len(data)-4 {\n        l := binary.BigEndian.Uint32(data[off : off+4])\n        if l == 0 || l > 1024*1024 { return false }\n        off += 4 + int(l)\n        if l < 1024*1024-16 { break } // last chunk\n    }\n    return off+4 <= len(data)\n}","typeGuard":null,"tryCatchPattern":"plain, err := model.DecryptAsset(boxID, diskName, dek, data)\nif err != nil {\n    log.Errorf(\"asset %s undecryptable: %v; restoring from history\", diskName, err)\n    return restoreFromHistory(boxID, diskName)\n}","preventionTips":["Never splice chunks between assets encrypted by different versions/specs","Keep whole-file checksums and verify after every sync/copy","Restore all-or-nothing from backups, never partial chunk ranges","Re-encrypt full assets after format upgrades rather than mixing old/new chunks"],"tags":["crypto","chunking","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"}