{"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/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/envelope/envelope.go#L87-L123","documentation":"Open parses the base64/JSON metadata blob attached to an envelope with json.Unmarshal before dispatching on the scheme. If the blob is not valid JSON, Open returns this wrapped error and fails closed without attempting any decryption. It protects the scheme dispatch from garbage input.","triggerScenarios":"Calling Open(ciphertext, metaJSON) where metaJSON is truncated, empty, not UTF-8 JSON, or was corrupted in storage/transport (e.g. stored as a string and re-encoded wrongly, or only the ciphertext was written).","commonSituations":"Metadata blob written to a DB column that mangles bytes (wrong charset), reading an envelope before the metadata column was fully written, or passing the ciphertext to Open by mistake instead of the metadata.","solutions":["Log/inspect the underlying %w error for the exact JSON syntax failure and byte offset","Verify metaJSON is the actual metadata blob produced by Seal (starts with '{'), not ciphertext or nil","Check the storage layer round-trips metadata bytes losslessly (binary-safe column, no charset conversion)","Re-write the object with SealForScope if the metadata is permanently corrupted — the payload is unrecoverable from ciphertext alone"],"exampleFix":"// before\nif len(metaJSON) == 0 { /* falls through and errors */ }\nplain, err := envelope.Open(ciphertext, metaJSON)\n// after\nif len(metaJSON) == 0 || metaJSON[0] != '{' {\n\treturn fmt.Errorf(\"missing or malformed envelope metadata\")\n}\nplain, err := envelope.Open(ciphertext, metaJSON)","handlingStrategy":"validation","validationCode":"func validMeta(metaJSON []byte) bool {\n\treturn len(metaJSON) > 0 && json.Valid(metaJSON)\n}\nif !validMeta(metaJSON) { return ErrCorruptEnvelope }","typeGuard":null,"tryCatchPattern":"plain, err := envelope.Open(ciphertext, metaJSON)\nif err != nil {\n\tvar syntaxErr *json.SyntaxError\n\tif errors.As(err, &syntaxErr) {\n\t\treturn fmt.Errorf(\"envelope metadata corrupt at offset %d: %w\", syntaxErr.Offset, err)\n\t}\n\treturn err\n}","preventionTips":["Store metadata in a binary-safe (BYTEA/blob) column, never a charset-converting text column","Persist ciphertext and metadata together in one transaction","Validate json.Valid(metaJSON) before calling Open","Never pass ciphertext bytes as the metadata argument"],"tags":["go","json","envelope-encryption","corrupt-input"],"backgroundTag":"json-unmarshal-failed","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"}