JuliusBrussee/caveman · error
envelope: parse metadata
Error message
envelope: parse metadata: %w
What it means
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.
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
Example fix
// before
if len(metaJSON) == 0 { /* falls through and errors */ }
plain, err := envelope.Open(ciphertext, metaJSON)
// after
if len(metaJSON) == 0 || metaJSON[0] != '{' {
return fmt.Errorf("missing or malformed envelope metadata")
}
plain, err := envelope.Open(ciphertext, metaJSON) Defensive patterns
Strategy: validation
Validate before calling
func validMeta(metaJSON []byte) bool {
return len(metaJSON) > 0 && json.Valid(metaJSON)
}
if !validMeta(metaJSON) { return ErrCorruptEnvelope } Try / catch
plain, err := envelope.Open(ciphertext, metaJSON)
if err != nil {
var syntaxErr *json.SyntaxError
if errors.As(err, &syntaxErr) {
return fmt.Errorf("envelope metadata corrupt at offset %d: %w", syntaxErr.Offset, err)
}
return err
} Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.
Related errors
- envelope: decode wrapped key
- envelope: marshal metadata
- envelope: encode scope
- envelope: tenant scope mismatch
- envelope: tenant scope required for scheme
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/3baa5a6e06161c60.
Report an issue: GitHub.
Appendix: source
Thrown at shared/platform/envelope/envelope.go:105
wrapped, err := secretbox.EncryptPayloadKey(dataKey)
if err != nil {
return nil, nil, fmt.Errorf("envelope: wrap data key: %w", err)
}
meta := Metadata{Scheme: scheme, WrappedDataKey: base64.StdEncoding.EncodeToString(wrapped), ScopeHash: scopeHash}
metaJSON, err = json.Marshal(meta)
if err != nil {
return nil, nil, fmt.Errorf("envelope: marshal metadata: %w", err)
}
return ciphertext, metaJSON, nil
}
// Open reverses Seal: it unwraps the data key from metadata and decrypts the
// ciphertext. An unknown scheme fails closed.
func Open(ciphertext []byte, metaJSON []byte) ([]byte, error) {
var meta Metadata
if err := json.Unmarshal(metaJSON, &meta); err != nil {
return nil, fmt.Errorf("envelope: parse metadata: %w", err)
}
if meta.Scheme == schemeV2 {
return nil, fmt.Errorf("envelope: tenant scope required for scheme %q", meta.Scheme)
}
if meta.Scheme != schemeV1 {
return nil, fmt.Errorf("envelope: unknown scheme %q", meta.Scheme)
}
return open(ciphertext, meta, nil)
}
// OpenForScope opens v2 ciphertext only for its authenticated scope. It also
// reads v1 ciphertext during migration; all new tenant-object writes use v2.
func OpenForScope(ciphertext []byte, metaJSON []byte, scope Scope) ([]byte, error) {
var meta Metadata
if err := json.Unmarshal(metaJSON, &meta); err != nil {
return nil, fmt.Errorf("envelope: parse metadata: %w", err)
}
if meta.Scheme == schemeV1 {View on GitHub (pinned to 3ee70a1026)