{"record":{"id":"0336ef9fbf6429e9","repo":"JuliusBrussee/caveman","slug":"envelope-decode-wrapped-key-w","errorCode":null,"errorMessage":"envelope: decode wrapped key: %w","messagePattern":"envelope: decode wrapped key: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/envelope/envelope.go","lineNumber":142,"sourceCode":"\t\treturn open(ciphertext, meta, nil)\n\t}\n\tif meta.Scheme != schemeV2 {\n\t\treturn nil, fmt.Errorf(\"envelope: unknown scheme %q\", meta.Scheme)\n\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:]","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/envelope/envelope.go#L124-L160","documentation":"The Metadata.WrappedDataKey field is a base64 string; open decodes it before handing it to secretbox to unwrap. If the string is not valid base64 (or empty), this wrapped error is returned and no key material is touched.","triggerScenarios":"Calling Open or OpenForScope with metadata whose WrappedDataKey is empty, truncated, or contains invalid base64 characters — typically from a corrupted or hand-edited metadata blob, or a Metadata struct serialized by different code.","commonSituations":"DB columns that strip or mangle characters (e.g. '+' handling in URL-safe conversions), partial row writes, test fixtures with placeholder key strings.","solutions":["Check the underlying %w error — base64 errors include the offending byte position","Verify WrappedDataKey decodes cleanly (base64.StdEncoding.DecodeString) before calling Open","Confirm the metadata bytes were not transformed in storage (URL-safe vs standard base64 mixing is a common cause)","Re-seal the object; the wrapped key is unrecoverable if the base64 is corrupted"],"exampleFix":"// before\nplain, err := envelope.OpenForScope(ciphertext, metaJSON, scope)\n// after\nvar meta envelope.Metadata\nif err := json.Unmarshal(metaJSON, &meta); err == nil {\n\tif _, derr := base64.StdEncoding.DecodeString(meta.WrappedDataKey); derr != nil {\n\t\treturn fmt.Errorf(\"stored wrapped key is not valid base64: %w\", derr)\n\t}\n}\nplain, err := envelope.OpenForScope(ciphertext, metaJSON, scope)","handlingStrategy":"validation","validationCode":"var meta envelope.Metadata\nif err := json.Unmarshal(metaJSON, &meta); err != nil { return err }\nif _, err := base64.StdEncoding.DecodeString(meta.WrappedDataKey); err != nil {\n\treturn fmt.Errorf(\"wrapped key not base64: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"plain, err := envelope.OpenForScope(ciphertext, metaJSON, scope)\nif err != nil && strings.Contains(err.Error(), \"decode wrapped key\") {\n\treturn fmt.Errorf(\"envelope metadata corrupted: %w\", err)\n}","preventionTips":["Use blob/binary columns for metadata; avoid transformations that rewrite '+' or '/'","Never hand-edit stored metadata","Round-trip test: seal, store, fetch, open in integration tests"],"tags":["go","base64","envelope-encryption","corrupt-input"],"backgroundTag":"invalid-argument-format","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"}