JuliusBrussee/caveman · error

envelope: tenant scope required for scheme

Error message

envelope: tenant scope required for scheme %q

What it means

Open is the legacy, scope-less entry point and only supports scheme v1. If the metadata says the envelope was sealed with scheme v2 (tenant-scoped AEAD), Open deliberately refuses to open it rather than silently bypassing scope authentication. The caller must use OpenForScope with the tenant scope.

Solutions

  1. Switch the call site to OpenForScope(ciphertext, metaJSON, scope), passing the tenant Scope that owns the object
  2. Confirm the object was sealed with SealForScope, not Seal
  3. If both schemes must be read, add scheme detection via OpenForScope (it also reads v1 during migration)
  4. Check scope construction (tenant ID, object class) matches what was used at seal time

Example fix

// before
plain, err := envelope.Open(ciphertext, metaJSON)
// after
plain, err := envelope.OpenForScope(ciphertext, metaJSON, scope)
Defensive patterns

Strategy: validation

Validate before calling

var meta envelope.Metadata
if json.Unmarshal(metaJSON, &meta) == nil && meta.Scheme != "v1" {
	// route to OpenForScope instead of Open
}

Try / catch

plain, err := envelope.Open(ciphertext, metaJSON)
if err != nil && strings.Contains(err.Error(), "tenant scope required") {
	// caller used the legacy API on a v2 envelope
	plain, err = envelope.OpenForScope(ciphertext, metaJSON, scope)
}

Prevention

When it happens

Trigger: Calling Open on metadata whose Scheme field equals schemeV2 — i.e. any envelope written after the tenant-scoping migration (SealForScope output) — through the legacy API.

Common situations: Old code paths or cached helpers still calling Open after the codebase migrated to scoped envelopes; reading a newly-written tenant object with a pre-migration reader.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

		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 {
		return open(ciphertext, meta, nil)
	}
	if meta.Scheme != schemeV2 {

View on GitHub (pinned to 3ee70a1026)