JuliusBrussee/caveman · error

envelope: decode wrapped key

Error message

envelope: decode wrapped key: %w

What it means

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.

Solutions

  1. Check the underlying %w error — base64 errors include the offending byte position
  2. Verify WrappedDataKey decodes cleanly (base64.StdEncoding.DecodeString) before calling Open
  3. Confirm the metadata bytes were not transformed in storage (URL-safe vs standard base64 mixing is a common cause)
  4. Re-seal the object; the wrapped key is unrecoverable if the base64 is corrupted

Example fix

// before
plain, err := envelope.OpenForScope(ciphertext, metaJSON, scope)
// after
var meta envelope.Metadata
if err := json.Unmarshal(metaJSON, &meta); err == nil {
	if _, derr := base64.StdEncoding.DecodeString(meta.WrappedDataKey); derr != nil {
		return fmt.Errorf("stored wrapped key is not valid base64: %w", derr)
	}
}
plain, err := envelope.OpenForScope(ciphertext, metaJSON, scope)
Defensive patterns

Strategy: validation

Validate before calling

var meta envelope.Metadata
if err := json.Unmarshal(metaJSON, &meta); err != nil { return err }
if _, err := base64.StdEncoding.DecodeString(meta.WrappedDataKey); err != nil {
	return fmt.Errorf("wrapped key not base64: %w", err)
}

Try / catch

plain, err := envelope.OpenForScope(ciphertext, metaJSON, scope)
if err != nil && strings.Contains(err.Error(), "decode wrapped key") {
	return fmt.Errorf("envelope metadata corrupted: %w", err)
}

Prevention

When it happens

Trigger: 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.

Common situations: DB columns that strip or mangle characters (e.g. '+' handling in URL-safe conversions), partial row writes, test fixtures with placeholder key strings.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/0336ef9fbf6429e9. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/envelope/envelope.go:142

		return open(ciphertext, meta, nil)
	}
	if meta.Scheme != schemeV2 {
		return nil, fmt.Errorf("envelope: unknown scheme %q", meta.Scheme)
	}
	aad, scopeHash, err := scopeAAD(scope)
	if err != nil {
		return nil, err
	}
	if meta.ScopeHash != scopeHash {
		return nil, fmt.Errorf("envelope: tenant scope mismatch")
	}
	return open(ciphertext, meta, aad)
}

func open(ciphertext []byte, meta Metadata, aad []byte) ([]byte, error) {
	wrapped, err := base64.StdEncoding.DecodeString(meta.WrappedDataKey)
	if err != nil {
		return nil, fmt.Errorf("envelope: decode wrapped key: %w", err)
	}
	dataKey, err := secretbox.DecryptPayloadKey(wrapped)
	if err != nil {
		return nil, fmt.Errorf("envelope: unwrap data key: %w", err)
	}
	block, err := aes.NewCipher(dataKey)
	if err != nil {
		return nil, fmt.Errorf("envelope: aes: %w", err)
	}
	gcm, err := cipher.NewGCM(block)
	if err != nil {
		return nil, fmt.Errorf("envelope: gcm: %w", err)
	}
	ns := gcm.NonceSize()
	if len(ciphertext) < ns {
		return nil, fmt.Errorf("envelope: ciphertext too short")
	}
	nonce, ct := ciphertext[:ns], ciphertext[ns:]

View on GitHub (pinned to 3ee70a1026)