siyuan-note/siyuan · error

invalid encrypted asset chunk size

Error message

invalid encrypted asset chunk size

What it means

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.

Solutions

  1. Restore the asset from sync/history — the chunk stream is corrupt or from an incompatible format
  2. Do NOT splice chunks from files encrypted by different SiYuan versions/specs; re-encrypt the whole asset
  3. Confirm the full file was transferred (compare sizes/checksums between source and replica)
  4. If it reproduces deterministically, re-export/re-upload the original asset to regenerate ciphertext
Defensive patterns

Strategy: validation

Validate before calling

func envelopeSizeOK(data []byte) bool {
    // expected total = 8 + metadataSize + sum(4+chunkLen) + 4; verify no chunk
    // length is 0 or > max before calling decrypt
    if len(data) < 12 { return false }
    off := 8 + int(binary.BigEndian.Uint32(data[4:8]))
    for off+4 <= len(data)-4 {
        l := binary.BigEndian.Uint32(data[off : off+4])
        if l == 0 || l > 1024*1024 { return false }
        off += 4 + int(l)
        if l < 1024*1024-16 { break } // last chunk
    }
    return off+4 <= len(data)
}

Try / catch

plain, err := model.DecryptAsset(boxID, diskName, dek, data)
if err != nil {
    log.Errorf("asset %s undecryptable: %v; restoring from history", diskName, err)
    return restoreFromHistory(boxID, diskName)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Understand the failure class

Background: Checksum mismatch errors: "checksum verification failed", "digest mismatch", "expected vs actual checksum" — what they mean and how to fix them — this error's family across 41 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/aca5142e5aa3c534. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/crypto.go:2452

	aadPrefix := "siyuan:asset:" + boxID + ":assets/" + diskName
	plainMetadata, decryptErr := util.DecryptWithAAD(assetKey, encryptedMetadata, []byte(aadPrefix+":metadata"))
	if decryptErr != nil {
		return "", decryptErr
	}
	defer zeroAndClear(plainMetadata)
	metadata, err := parseEncryptedAssetMetadata(plainMetadata)
	if err != nil {
		return "", err
	}

	var written int64
	for chunkIndex := uint64(0); chunkIndex < metadata.Chunks; chunkIndex++ {
		var encryptedSize uint32
		if err = binary.Read(reader, binary.BigEndian, &encryptedSize); err != nil {
			return "", err
		}
		if encryptedSize == 0 || encryptedSize > encryptedAssetChunkMaxCiphertextSize {
			return "", errors.New("invalid encrypted asset chunk size")
		}
		encryptedChunk := make([]byte, int(encryptedSize))
		if _, err = io.ReadFull(reader, encryptedChunk); err != nil {
			return "", err
		}
		aad := encryptedAssetChunkAAD(aadPrefix, metadata.ContainerID, chunkIndex)
		if metadata.Spec == encryptedAssetLegacySpec {
			aad = []byte(fmt.Sprintf("%s:content:%d", aadPrefix, chunkIndex))
		}
		plainChunk, chunkErr := util.DecryptWithAAD(
			assetKey,
			encryptedChunk,
			aad,
		)
		if chunkErr != nil {
			return "", chunkErr
		}
		expectedSize := int64(encryptedAssetChunkSize)

View on GitHub (pinned to 9f775e8a12)