{"record":{"id":"fc7a4f6f54a0c53f","repo":"JuliusBrussee/caveman","slug":"envelope-unwrap-data-key-w","errorCode":null,"errorMessage":"envelope: unwrap data key: %w","messagePattern":"envelope: unwrap data key: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/envelope/envelope.go","lineNumber":146,"sourceCode":"\t}\n\taad, scopeHash, err := scopeAAD(scope)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tif meta.ScopeHash != scopeHash {\n\t\treturn nil, fmt.Errorf(\"envelope: tenant scope mismatch\")\n\t}\n\treturn open(ciphertext, meta, aad)\n}\n\nfunc open(ciphertext []byte, meta Metadata, aad []byte) ([]byte, error) {\n\twrapped, err := base64.StdEncoding.DecodeString(meta.WrappedDataKey)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"envelope: decode wrapped key: %w\", err)\n\t}\n\tdataKey, err := secretbox.DecryptPayloadKey(wrapped)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"envelope: unwrap data key: %w\", err)\n\t}\n\tblock, err := aes.NewCipher(dataKey)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"envelope: aes: %w\", err)\n\t}\n\tgcm, err := cipher.NewGCM(block)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"envelope: gcm: %w\", err)\n\t}\n\tns := gcm.NonceSize()\n\tif len(ciphertext) < ns {\n\t\treturn nil, fmt.Errorf(\"envelope: ciphertext too short\")\n\t}\n\tnonce, ct := ciphertext[:ns], ciphertext[ns:]\n\tplaintext, err := gcm.Open(nil, nonce, ct, aad)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"envelope: open: %w\", err)\n\t}","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/envelope/envelope.go#L128-L164","documentation":"After base64-decoding, open calls secretbox.DecryptPayloadKey to unwrap the data key using the master key. If the wrapped key fails authentication/decryption (wrong master key, tampered blob, wrong key version), it returns this error and the payload is never decrypted.","triggerScenarios":"Calling Open/OpenForScope when the master key or key-encryption key has been rotated or differs from the one used at SealForScope time, the wrapped-key bytes were altered, or the metadata belongs to a different keyring/environment (e.g. staging blob opened in production).","commonSituations":"Key rotation without re-encrypting stored envelopes, copying objects between environments with different master keys, KMS/master-key config pointing at the wrong secret.","solutions":["Verify the master key configured for this environment matches the one that sealed the envelope","Check key-rotation tooling re-wrapped (or kept available) keys for all existing envelopes","Compare the envelope's metadata/key version against the active keyring version","Re-seal affected objects under the current key once the correct key is restored — decryption failures are permanent fail-closed"],"exampleFix":"// before\nmaster := loadKey(\"staging-master.key\") // wrong env\nplain, err := envelope.OpenForScope(ciphertext, metaJSON, scope)\n// after\nmaster := loadMasterKey(env) // env-derived key\nif _, err := envelope.OpenForScope(ciphertext, metaJSON, scope); err != nil {\n\tif strings.Contains(err.Error(), \"unwrap data key\") {\n\t\treturn fmt.Errorf(\"envelope sealed under a different master key (env=%s): %w\", env, err)\n\t}\n}","handlingStrategy":"retry","validationCode":"// Before bulk reads, verify master-key health:\nif _, _, err := envelope.Seal([]byte(\"probe\")); err != nil {\n\treturn fmt.Errorf(\"master key unusable in this env: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"plain, err := envelope.OpenForScope(ciphertext, metaJSON, scope)\nif err != nil && strings.Contains(err.Error(), \"unwrap data key\") {\n\t// wrong/rotated master key: do NOT retry blindly; re-check key config\n\treturn fmt.Errorf(\"key unwrap failed — master key mismatch: %w\", err)\n}","preventionTips":["Coordinate master-key rotation with re-encryption of all stored envelopes","Keep retired keys available for reads during rotation windows","Never share encrypted blobs across environments with distinct master keys","Version the keyring and record the key version in metadata"],"tags":["go","cryptography","key-management","envelope-encryption"],"backgroundTag":"checksum-mismatch","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}