{"record":{"id":"921dc7b35dbd8835","repo":"siyuan-note/siyuan","slug":"invalid-encrypted-notebook-key-envelope-w","errorCode":null,"errorMessage":"invalid encrypted notebook key envelope: %w","messagePattern":"invalid encrypted notebook key envelope: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":1619,"sourceCode":"}\n\nfunc decryptWrappedDEK(boxID string, enc *conf.BoxEncryption, kek []byte) ([]byte, error) {\n\tif err := validateWrappedDEKEnvelope(enc); err != nil {\n\t\treturn nil, err\n\t}\n\treturn util.DecryptWithAAD(kek, enc.WrappedDEK, wrappedDEKAAD(boxID))\n}\n\nfunc validateWrappedDEKEnvelope(enc *conf.BoxEncryption) error {\n\tif enc == nil || enc.Spec != boxEncryptionSpec {\n\t\treturn errors.New(\"unsupported encrypted notebook key envelope\")\n\t}\n\tif enc.CreatedAt <= 0 {\n\t\treturn errors.New(\"encrypted notebook key envelope creation time is missing\")\n\t}\n\tnonce, err := util.EncryptionNonce(enc.WrappedDEK)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"invalid encrypted notebook key envelope: %w\", err)\n\t}\n\tif !bytes.Equal(nonce, enc.WrapNonce) {\n\t\treturn errors.New(\"encrypted notebook key envelope nonce mismatch\")\n\t}\n\treturn nil\n}\n\nfunc validateBoxEncryption(enc *conf.BoxEncryption) error {\n\tif err := validateWrappedDEKEnvelope(enc); err != nil {\n\t\treturn err\n\t}\n\tif _, err := util.EncryptionNonce(enc.Metadata); err != nil {\n\t\treturn fmt.Errorf(\"invalid encrypted notebook metadata envelope: %w\", err)\n\t}\n\treturn nil\n}\n\n// mustEncryptionNonce 从刚刚成功生成的密文中提取 nonce。生成密文格式错误属于内部不变量被破坏，直接终止执行。","sourceCodeStart":1601,"sourceCodeEnd":1637,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/kernel/model/crypto.go#L1601-L1637","documentation":"Thrown by validateWrappedDEKEnvelope when util.EncryptionNonce(enc.WrappedDEK) returns an error. EncryptionNonce parses the SENC envelope header (magic 'SENC' + spec byte + algorithm byte + nonce-length byte) and extracts the nonce. Failure means the WrappedDEK bytes don't conform to this binary envelope format at all.","triggerScenarios":"Fires when WrappedDEK is not a valid SENC envelope: missing magic header, too short, wrong spec/algorithm byte, or invalid nonce-length. Reached during unlock and ChangeMasterPassword via decryptWrappedDEK. The wrapped %w error carries the specific envelope-parse failure (e.g., 'invalid encrypted envelope magic', 'encrypted envelope too short').","commonSituations":"WrappedDEK was stored as a base64 string instead of raw bytes (or vice versa) due to a serialization mismatch. conf.json was partially overwritten or truncated. A sync tool corrupted the binary field. A third-party plugin wrote a non-envelope value into WrappedDEK.","solutions":["Restore conf.json from a DataDir backup with a valid WrappedDEK envelope.","Restore the per-notebook crypt backup.","Inspect the wrapped error message (the %w) to identify the specific envelope defect: 'invalid encrypted envelope magic' means the bytes aren't SENC at all; 'too short' means truncation; 'unsupported spec' means a version mismatch.","If migrating programmatically, ensure WrappedDEK is the raw output of util.EncryptWithAAD, not a re-encoded form."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Pre-validate the WrappedDEK envelope format:\nboxCrypt, _ := model.GetBoxEncryption(boxID)\nif _, err := util.EncryptionNonce(boxCrypt.WrappedDEK); err != nil {\n    // WrappedDEK is not a valid SENC envelope — restore from backup\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never base64-encode or re-encode the WrappedDEK field — it must be raw SENC envelope bytes.","Keep DataDir backups to restore valid WrappedDEK.","Avoid third-party tools that might re-serialize conf.json fields."],"tags":["encryption","envelope-validation","data-corruption","go"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}