siyuan-note/siyuan · error

invalid trailing encrypted asset data

Error message

invalid trailing encrypted asset data

What it means

After the terminator check, DecryptAssetToWriter attempts to read one more byte and requires exactly io.EOF. Any extra trailing byte means the file contains data beyond the declared envelope — likely appended content, a different format, or corruption — so decryption fails to prevent silently processing unverified data.

Solutions

  1. Re-copy the asset from a known-good source ensuring truncation on write (O_TRUNC)
  2. Trim the file to the exact envelope length only after verifying integrity elsewhere — prefer re-download/re-sync instead
  3. Check the copying tool for append-on-resume behavior and redo the transfer
  4. Restore from history/snapshot where the byte length matches the metadata

Example fix

// before
data, _ := os.ReadFile(src)
os.WriteFile(dst, data, 0644) // appends if dst exists via some helpers
// after
data, _ := os.ReadFile(src)
os.WriteFile(dst, data, 0644) // ensure O_TRUNC
// or: os.Remove(dst) before copying on resume-capable transports
Defensive patterns

Strategy: validation

Validate before calling

func envelopeLengthExact(data []byte) (bool, int64) {
    if len(data) < 12 { return false, 0 }
    off := 8 + int(binary.BigEndian.Uint32(data[4:8]))
    for {
        if off+4 > len(data) { return false, 0 }
        l := binary.BigEndian.Uint32(data[off : off+4])
        off += 4 + int(l)
        if off+4 > len(data) { return false, 0 }
        if binary.BigEndian.Uint32(data[off-4:off]) == 0 && l < uint32(1<<20)-16 { break }
    }
    return off+4 == len(data), int64(off + 4)
}

Try / catch

_, err := model.DecryptAsset(boxID, diskName, dek, data)
if err != nil && strings.Contains(err.Error(), "trailing") {
    // recopy with truncation from the authoritative source
    return recopyWithTrunc(srcURL, diskName)
}

Prevention

When it happens

Trigger: Reading an encrypted asset file that has bytes appended after the terminating uint32 — e.g. someone appended data, a sync tool concatenated two copies, or the wrong file (a different container) was passed in.

Common situations: rsync/backup tools appending instead of replacing on resume; log or error text appended to the asset; copy operations targeting an existing file without truncation (O_APPEND).

Related errors


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

Appendix: source

Thrown at kernel/model/crypto.go:2497

		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。
const notebookCryptoBackupFilename = "notebook-crypto-backup.json"

func notebookCryptoBackupPath(boxID string) string {
	return filepath.Join(util.DataDir, boxID, ".siyuan", notebookCryptoBackupFilename)

View on GitHub (pinned to 9f775e8a12)