siyuan-note/siyuan · error

invalid encrypted asset chunk count

Error message

invalid encrypted asset chunk count

What it means

The metadata's Chunks field is cross-checked against the value derived from Size: chunks = ceil(Size / encryptedAssetChunkSize). This error means the declared chunk count does not match the declared size, so the container's content region cannot be parsed reliably and the kernel refuses to decrypt it.

Solutions

  1. Restore/re-sync the asset so content and metadata come from the same write
  2. Re-encrypt from the original source with the current kernel's EncryptAsset
  3. Check whether the producing tool used a different encryptedAssetChunkSize and regenerate the container
Defensive patterns

Strategy: try-catch

Try / catch

plain, name, err := model.DecryptAssetWithName(box, disk, dek, data)
if err != nil && strings.Contains(err.Error(), "invalid encrypted asset chunk count") {
    return restoreAssetFromSyncHistory(box, disk)
}

Prevention

When it happens

Trigger: Decrypting a container where metadata.Chunks was written inconsistently with metadata.Size — e.g. truncated content region plus an unadjusted chunk count, an encryptor bug, or a container assembled from mixed parts; also fires for Size==0 with a nonzero chunk count since zero-size assets are rejected upstream.

Common situations: Partially copied/stitched encrypted assets; containers written by an older kernel with a different chunk-size constant then partially updated; third-party encryptors using a different chunk size.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/crypto.go:2358

	// 仅认证元数据同时缺少两个版本字段时按旧容器读取,显式空值或不完整的新格式不能降级。
	if len(version.Spec) == 0 && len(version.ContainerID) == 0 {
		metadata.Spec = encryptedAssetLegacySpec
	} else if metadata.Spec != encryptedAssetSpec || len(metadata.ContainerID) != encryptedAssetContainerIDSize {
		return nil, errors.New("unsupported encrypted asset container version")
	}
	if metadata.OriginalName == "" || metadata.OriginalName == "." ||
		filepath.Base(metadata.OriginalName) != metadata.OriginalName || strings.ContainsAny(metadata.OriginalName, `/\`) {
		return nil, errors.New("invalid encrypted asset original name")
	}
	if metadata.Size < 0 {
		return nil, errors.New("invalid encrypted asset content metadata")
	}
	chunks := uint64(metadata.Size) / encryptedAssetChunkSize
	if metadata.Size%encryptedAssetChunkSize != 0 || metadata.Size == 0 {
		chunks++
	}
	if metadata.Chunks != chunks {
		return nil, errors.New("invalid encrypted asset chunk count")
	}
	return metadata, nil
}

// DecryptAssetWithName 解密资源内容并返回原始名称。
func DecryptAssetWithName(boxID, diskName string, dek, ciphertext []byte) (plaintext []byte, originalName string, err error) {
	var output bytes.Buffer
	originalName, err = DecryptAssetToWriter(boxID, diskName, dek, bytes.NewReader(ciphertext), &output)
	if err != nil {
		return nil, "", err
	}
	return output.Bytes(), originalName, nil
}

// DecryptAssetName 只解密资源的名称元数据,不处理资源内容。
func DecryptAssetName(boxID, diskName string, dek, ciphertext []byte) (originalName string, err error) {
	metadata, _, err := decryptAssetMetadata(boxID, diskName, dek, ciphertext)
	if err != nil {

View on GitHub (pinned to 9f775e8a12)