siyuan-note/siyuan · error

invalid encrypted asset content length

Error message

invalid encrypted asset content length

What it means

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.

Solutions

  1. Restore the asset from sync/history — a single consistent copy must contain matching metadata, chunks, and terminator
  2. Re-encrypt from the original source to rebuild consistent metadata and terminator
  3. Compare file size against expected envelope size (8 + metadataSize + chunks*(4+ciphertext) + 4) to locate truncation/extra data
  4. Never hand-edit or concatenate encrypted asset files
Defensive patterns

Strategy: validation

Validate before calling

func declaredSizeMatches(metaSize, chunks, size int64) bool {
    return metaSize >= 1 && metaSize <= 1024*1024 && size >= 0 && chunks >= 0
}

Try / catch

plain, name, err := model.DecryptAssetWithName(boxID, diskName, dek, data)
if err != nil && strings.Contains(err.Error(), "content length") {
    return restoreAsset(boxID, diskName) // inconsistent envelope: use backup
}

Prevention

When it happens

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

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

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/3de91918eb4b245f. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/crypto.go:2493

			zeroAndClear(plainChunk)
			return "", errors.New("invalid encrypted asset plaintext chunk size")
		}
		n, writeErr := writer.Write(plainChunk)
		written += int64(n)
		zeroAndClear(plainChunk)
		if writeErr != nil {
			return "", writeErr
		}
		if n != len(plainChunk) {
			return "", io.ErrShortWrite
		}
	}
	var terminator uint32
	if err = binary.Read(reader, binary.BigEndian, &terminator); err != nil {
		return "", err
	}
	if terminator != 0 || written != metadata.Size {
		return "", errors.New("invalid encrypted asset content length")
	}
	var trailing [1]byte
	if _, trailingErr := io.ReadFull(reader, trailing[:]); trailingErr != io.EOF {
		return "", errors.New("invalid trailing encrypted asset data")
	}
	return metadata.OriginalName, nil
}

// DecryptAsset 对应解密。
func DecryptAsset(boxID, diskName string, dek, ciphertext []byte) ([]byte, error) {
	plaintext, _, err := DecryptAssetWithName(boxID, diskName, dek, ciphertext)
	return plaintext, err
}

// notebookCryptoBackupPath 返回加密笔记本的独立 BoxCrypt 备份路径。
// 该文件在主 conf.json 丢失时用作"此笔记本是加密笔记本"的标识和降级恢复源。
// 与全局 NotebookCrypto 备份(<DataDir>/.siyuan/data-crypto-backup.json)配合使用,
// 全局备份存 MasterSalt/KEKVerifier,per-notebook 备份存 WrappedDEK/WrapNonce。

View on GitHub (pinned to 9f775e8a12)