{"record":{"id":"3baa5a6e06161c60","repo":"JuliusBrussee/caveman","slug":"envelope-parse-metadata-w","errorCode":null,"errorMessage":"envelope: parse metadata: %w","messagePattern":"envelope: parse metadata: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/envelope/envelope.go","lineNumber":105,"sourceCode":"\n\twrapped, err := secretbox.EncryptPayloadKey(dataKey)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: wrap data key: %w\", err)\n\t}\n\tmeta := Metadata{Scheme: scheme, WrappedDataKey: base64.StdEncoding.EncodeToString(wrapped), ScopeHash: scopeHash}\n\tmetaJSON, err = json.Marshal(meta)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: marshal metadata: %w\", err)\n\t}\n\treturn ciphertext, metaJSON, nil\n}\n\n// Open reverses Seal: it unwraps the data key from metadata and decrypts the\n// ciphertext. An unknown scheme fails closed.\nfunc Open(ciphertext []byte, metaJSON []byte) ([]byte, error) {\n\tvar meta Metadata\n\tif err := json.Unmarshal(metaJSON, &meta); err != nil {\n\t\treturn nil, fmt.Errorf(\"envelope: parse metadata: %w\", err)\n\t}\n\tif meta.Scheme == schemeV2 {\n\t\treturn nil, fmt.Errorf(\"envelope: tenant scope required for scheme %q\", meta.Scheme)\n\t}\n\tif meta.Scheme != schemeV1 {\n\t\treturn nil, fmt.Errorf(\"envelope: unknown scheme %q\", meta.Scheme)\n\t}\n\treturn open(ciphertext, meta, nil)\n}\n\n// OpenForScope opens v2 ciphertext only for its authenticated scope. It also\n// reads v1 ciphertext during migration; all new tenant-object writes use v2.\nfunc OpenForScope(ciphertext []byte, metaJSON []byte, scope Scope) ([]byte, error) {\n\tvar meta Metadata\n\tif err := json.Unmarshal(metaJSON, &meta); err != nil {\n\t\treturn nil, fmt.Errorf(\"envelope: parse metadata: %w\", err)\n\t}\n\tif meta.Scheme == schemeV1 {","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/766dce6b1394ebb56a3090748d5a0240a5aefb36/shared/platform/envelope/envelope.go#L87-L123","documentation":"Open could not json.Unmarshal the stored metadata JSON into the Metadata struct. The ciphertext's companion metadata is corrupt or foreign (truncated row, wrong column, hand-edited value), so the wrapped key cannot be located and decryption must fail closed.","triggerScenarios":"Thrown at shared/platform/envelope/envelope.go:105 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check the row for corruption; restore metadata from a backup if available","Verify the caller is passing the metadata blob (not ciphertext or another column) to Open"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"766dce6b1394ebb56a3090748d5a0240a5aefb36","analyzedAt":"2026-08-18T03:14:35.516Z","contentChangedAt":"2026-08-18T03:14:35.516Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}