{"record":{"id":"826cd214b4f00c4c","repo":"JuliusBrussee/caveman","slug":"envelope-tenant-scope-required-for-scheme-q","errorCode":null,"errorMessage":"envelope: tenant scope required for scheme %q","messagePattern":"envelope: tenant scope required for scheme %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/envelope/envelope.go","lineNumber":108,"sourceCode":"\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 {\n\t\treturn open(ciphertext, meta, nil)\n\t}\n\tif meta.Scheme != schemeV2 {","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/envelope/envelope.go#L90-L126","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Switch the call site to OpenForScope(ciphertext, metaJSON, scope), passing the tenant Scope that owns the object","Confirm the object was sealed with SealForScope, not Seal","If both schemes must be read, add scheme detection via OpenForScope (it also reads v1 during migration)","Check scope construction (tenant ID, object class) matches what was used at seal time"],"exampleFix":"// before\nplain, err := envelope.Open(ciphertext, metaJSON)\n// after\nplain, err := envelope.OpenForScope(ciphertext, metaJSON, scope)","handlingStrategy":"validation","validationCode":"var meta envelope.Metadata\nif json.Unmarshal(metaJSON, &meta) == nil && meta.Scheme != \"v1\" {\n\t// route to OpenForScope instead of Open\n}","typeGuard":null,"tryCatchPattern":"plain, err := envelope.Open(ciphertext, metaJSON)\nif err != nil && strings.Contains(err.Error(), \"tenant scope required\") {\n\t// caller used the legacy API on a v2 envelope\n\tplain, err = envelope.OpenForScope(ciphertext, metaJSON, scope)\n}","preventionTips":["Migrate all readers to OpenForScope; treat Open as legacy-only","Never cache Open as a generic 'decrypt any envelope' helper","Add a lint/test asserting new write paths use SealForScope"],"tags":["go","envelope-encryption","api-migration","scope"],"backgroundTag":"unsupported-operation","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"}