{"record":{"id":"336ce7e953f0e6f7","repo":"siyuan-note/siyuan","slug":"encrypted-envelope-too-short","errorCode":null,"errorMessage":"encrypted envelope too short","messagePattern":"encrypted envelope too short","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/kdf.go","lineNumber":107,"sourceCode":"\n// Encrypt 用 AES-256-GCM 加密。每次调用生成随机 nonce，因此同一明文多次加密结果不同。\n// 返回格式：magic(4B) || spec(1B) || algorithm(1B) || nonceLength(1B) || nonce || ciphertext || GCM tag(16B)。\nfunc Encrypt(key, plaintext []byte) ([]byte, error) {\n\treturn encryptGCM(key, plaintext, nil, \"Encrypt\")\n}\n\n// Decrypt 对应 Encrypt 的解密。密钥错误、格式无效或密文被篡改时返回错误。\nfunc Decrypt(key, ciphertext []byte) ([]byte, error) {\n\treturn decryptGCM(key, ciphertext, nil, \"Decrypt\")\n}\n\n// EncryptionNonce 从 AES-GCM 密文信封中提取 nonce。\nfunc EncryptionNonce(ciphertext []byte) ([]byte, error) {\n\tif !hasEncryptionMagic(ciphertext) {\n\t\treturn nil, errors.New(\"invalid encrypted envelope magic\")\n\t}\n\tif len(ciphertext) < encryptionEnvelopeHeaderSize {\n\t\treturn nil, errors.New(\"encrypted envelope too short\")\n\t}\n\tif ciphertext[len(encryptionMagic)] != EncryptionSpec {\n\t\treturn nil, errors.New(\"unsupported encrypted envelope spec\")\n\t}\n\tif ciphertext[len(encryptionMagic)+1] != encryptionAlgorithmAES256GCM {\n\t\treturn nil, errors.New(\"unsupported encrypted envelope algorithm\")\n\t}\n\tnonceLength := int(ciphertext[len(encryptionMagic)+2])\n\tif nonceLength == 0 || len(ciphertext) < encryptionEnvelopeHeaderSize+nonceLength {\n\t\treturn nil, errors.New(\"invalid encrypted envelope nonce length\")\n\t}\n\treturn append([]byte(nil), ciphertext[encryptionEnvelopeHeaderSize:encryptionEnvelopeHeaderSize+nonceLength]...), nil\n}\n\n// DeriveSubKey 用 HKDF-SHA256 从主 DEK 派生用途隔离的子密钥。\n// 同一 (dek, purpose) 多次调用结果一致；不同 purpose 派生出相互独立的子密钥，\n// 实现用途分离——.sy/assets/AV 各用独立子密钥，互不可替代，限制单点密钥泄漏的影响面。\nfunc DeriveSubKey(dek []byte, purpose string) []byte {","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/kernel/util/kdf.go#L89-L125","documentation":"Raised by EncryptionNonce when the blob passes the magic check but is shorter than encryptionEnvelopeHeaderSize (7 bytes: 4 magic + spec + algorithm + nonce-length). A blob that is exactly the magic or magic-plus-a-few-bytes hits this. It means the envelope is truncated below the minimum header.","triggerScenarios":"EncryptionNonce receives a buffer that starts with 'SENC' but has fewer than 7 total bytes. Occurs with truncated/corrupted ciphertext blobs read from conf or asset files.","commonSituations":"A write was interrupted leaving a partial envelope; storage corruption truncated the blob; a bug elsewhere produced a too-short buffer; reading past end-of-file in a chunked asset.","solutions":["Restore the affected blob from a known-good backup.","Validate blob length against the envelope minimum before calling EncryptionNonce.","Investigate the writer: ensure encryptGCM's full output (header+nonce+ct+tag) is persisted atomically."],"exampleFix":"// before\nnonce, err := util.EncryptionNonce(blob)\n\n// after: reject obviously truncated envelopes early\nif len(blob) < 7 {\n    return errors.New(\"ciphertext blob too short to be an envelope\")\n}\nnonce, err := util.EncryptionNonce(blob)","handlingStrategy":"validation","validationCode":"if len(blob) < 7 { // encryptionEnvelopeHeaderSize\n    return errors.New(\"ciphertext too short to contain an envelope header\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate minimum envelope length before parsing.","Write envelopes atomically to avoid truncated blobs.","Restore truncated ciphertext from backup."],"tags":["crypto","aes-gcm","encryption","validation","corruption"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}