From 7337865cef5db5e31db20ba1a555aaf148ecdfd2 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Fri, 27 Nov 2026 03:15:41 +1100 Subject: [PATCH 1/9] docs(golang): compare keysets, scopes and context_field for separating tenants The encrypt README gains a section that compares the three ways to keep tenants' rows apart. It says when to use each one, and gives the error and diagnostic code for a wrong tenant with each, as the code returns them today. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk --- languages/golang/CHANGELOG.md | 4 ++ languages/golang/encrypt/README.md | 68 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/languages/golang/CHANGELOG.md b/languages/golang/CHANGELOG.md index 6833efd30..f7888204b 100644 --- a/languages/golang/CHANGELOG.md +++ b/languages/golang/CHANGELOG.md @@ -21,6 +21,10 @@ and `languages/golang/encrypt/encrypttest/vX.Y.Z` on the same commit. `vcvalue.UUID`, or a value of any type whose underlying type is `[16]byte` (`github.com/google/uuid.UUID`, say), which is converted to one. `stashgen` does not yet map a Go type to it. +- The `encrypt` README has a section on separating tenants (#1885). It + compares a keyset for each tenant, a scope, and a `context_field`. It + says when to use each one, and gives the error for a wrong tenant with + each one. ## [0.1.0-rc.7] - 2026-11-25 diff --git a/languages/golang/encrypt/README.md b/languages/golang/encrypt/README.md index 6e67a1e8a..c89bf94c7 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -355,6 +355,74 @@ also refuses a row whose stored context is another, with `ErrContextMismatch`; the `*Client` opens each row under the context it stores. +## Separating tenants + +A tenant is a customer, or another group of users, whose rows the others +must not read. The SDK has three ways to keep tenants' rows apart. Each one +gives a different error when the tenant does not match. + +| Way | How you set it | What it separates | +|---|---|---| +| A keyset for each tenant | `client.Keyset(encrypt.KeysetName("tenant42"))` | the keys: each tenant's rows are encrypted under keys of its own | +| A scope for each tenant | `cipher.Scope("tenant42")` | the context: one keyset, with the tenant added to the context of every field | +| A `context_field` | a field tagged `stash:"tenant,context_field"` | the context of each row: the row stores its tenant, and its other fields are sealed under it | + +For how to create a keyset and give a client access to it, see +[Keysets](#keysets). + +### When to use each way + +Use a keyset for each tenant when tenants need keys of their own. A client +opens a keyset's rows only when it has a grant for that keyset. So access +is controlled in ZeroKMS, one tenant at a time. You create each keyset, and +give each client a grant, before the client uses it. + +Use a scope when tenants share a keyset, and your program knows the tenant +of each call, such as from the request. A scope needs nothing created in +ZeroKMS. A scoped row opens only through a cipher with the same keyset and +scope. The `*Client` does not open it. + +Use a `context_field` when each row must store its own tenant. A value +copied to another tenant's row does not decrypt there. The `*Client`, and +a cipher with no `Context`, open each row under the tenant it stores. So one +reader can decrypt rows of many tenants without knowing each tenant first. + +The `context_field` is stored in the clear, like a `passthrough` field. +Nothing authenticates it. See the `context_field` note in +[The cipher](#the-cipher). + +A keyset combines with a scope, as in the example in +[The cipher](#the-cipher): `client.Keyset(..).Scope(..)`. + +### Errors when the tenant does not match + +A decrypt or an encrypt with the wrong tenant fails as follows. `Code` is +the code of the error's `*Diagnostic` (see [Errors](#errors)). + +| Wrong tenant | Error | `Code` | +|---|---|---| +| Keyset: a `*Cipher` for one keyset decrypts a row sealed under another | `ErrForeignKeyset`, before any key is retrieved | `stack_encrypt::foreign_keyset` | +| Scope: a cipher with another scope, or with no scope, decrypts a scoped row | `ErrForbidden`, when ZeroKMS refuses the data key | `stack_kms::retrieve_key_failed` | +| Scope: the `*Client` decrypts a scoped row | `ErrForbidden` | `stack_kms::retrieve_key_failed` | +| `context_field`: a cipher from `cipher.Context(..)` decrypts a row that stores another tenant | `ErrContextMismatch`, before any key is retrieved | `stack_encrypt::context_mismatch` | +| `context_field`: a cipher from `cipher.Context(..)` encrypts a value whose field names another tenant | `ErrContextMismatch`, before anything is sent | none: the SDK refuses the value itself | +| `context_field`: a row's stored tenant was changed after it was written | `ErrForbidden` | `stack_kms::retrieve_key_failed` | + +The `ErrForbidden` for a scope does not say that the scope is wrong, and +the `Help` of its `*Diagnostic` is empty. A client with no grant for a keyset gets the same error +and code when it asks for data keys. Issue #1893 proposes a `Help` for +each cause. + +A query with the wrong tenant returns no error, and matches none of the +other tenant's rows. The query term derives under the cipher's keyset and +scope, so it differs from the stored term. A query on a type with a `context_field` +derives under the tenant that `cipher.Context(..)` names. Through a cipher +with no `Context`, the query fails with `ErrEncoding`. + +The test client from `encrypttest` gives the same errors as ZeroKMS for a +wrong keyset, scope, or `context_field`. See +[Testing your application](#testing-your-application). + ## What is stored A field with `encrypt` and `index=` is a struct with one field for each From 252eb0b6043fcee31f5222f0702eb5ea9b13fdd2 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Fri, 27 Nov 2026 03:36:47 +1100 Subject: [PATCH 2/9] docs(golang): say a range query through the wrong tenant can match its rows A range query through the wrong keyset or scope returns no error, and in a test against EQL v4 it returned all of the other tenant's rows or none of them, for ORE and OPE indexes. Equality and match queries matched none. The section now says so, and the keyset link points at The cipher, which exists on this branch. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk --- languages/golang/CHANGELOG.md | 3 ++- languages/golang/encrypt/README.md | 33 ++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/languages/golang/CHANGELOG.md b/languages/golang/CHANGELOG.md index f7888204b..229833dc1 100644 --- a/languages/golang/CHANGELOG.md +++ b/languages/golang/CHANGELOG.md @@ -24,7 +24,8 @@ and `languages/golang/encrypt/encrypttest/vX.Y.Z` on the same commit. - The `encrypt` README has a section on separating tenants (#1885). It compares a keyset for each tenant, a scope, and a `context_field`. It says when to use each one, and gives the error for a wrong tenant with - each one. + each one. It also says that a range query through the wrong keyset or + scope can return the other tenant's rows, with no error. ## [0.1.0-rc.7] - 2026-11-25 diff --git a/languages/golang/encrypt/README.md b/languages/golang/encrypt/README.md index c89bf94c7..a30d36a88 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -367,8 +367,7 @@ gives a different error when the tenant does not match. | A scope for each tenant | `cipher.Scope("tenant42")` | the context: one keyset, with the tenant added to the context of every field | | A `context_field` | a field tagged `stash:"tenant,context_field"` | the context of each row: the row stores its tenant, and its other fields are sealed under it | -For how to create a keyset and give a client access to it, see -[Keysets](#keysets). +For how to select a keyset, see [The cipher](#the-cipher). ### When to use each way @@ -413,16 +412,34 @@ the `Help` of its `*Diagnostic` is empty. A client with no grant for a keyset ge and code when it asks for data keys. Issue #1893 proposes a `Help` for each cause. -A query with the wrong tenant returns no error, and matches none of the -other tenant's rows. The query term derives under the cipher's keyset and -scope, so it differs from the stored term. A query on a type with a `context_field` -derives under the tenant that `cipher.Context(..)` names. Through a cipher -with no `Context`, the query fails with `ErrEncoding`. - The test client from `encrypttest` gives the same errors as ZeroKMS for a wrong keyset, scope, or `context_field`. See [Testing your application](#testing-your-application). +### Queries with the wrong tenant + +A query with the wrong keyset or the wrong scope returns no error. What it +matches depends on the kind of query. + +An equality query (`Eq`) or a match query (`Matches`) matches none of the +other tenant's rows. The query term derives under the cipher's keyset and +scope, so it differs from every term the other tenant stored. + +A range query can return the other tenant's rows, with an ORE index or an +OPE index. The range queries are `Lt`, `Lte`, `Gt`, `Gte`, and `Between`. +The database still compares the query term with the other tenant's terms, +and the result of that comparison has no relation to the values. In a test +against EQL v4 columns, such a query returned all of the other tenant's +rows or none of them. + +So a count or a list of the rows that such a query returns is wrong. A +decrypt of those rows through the same cipher fails with the error in the +table above. + +A query on a type with a `context_field` derives under the tenant that +`cipher.Context(..)` names. Through a cipher with no `Context`, the query +fails with `ErrEncoding`. + ## What is stored A field with `encrypt` and `index=` is a struct with one field for each From b1ca1987a4d100800b1c1b906c26d331af2bd8c2 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Fri, 27 Nov 2026 03:40:15 +1100 Subject: [PATCH 3/9] docs(golang): add the Client case for keysets, and drop the stale cross-references The tenant section now says that the Client opens rows of every keyset it has a grant for, writes the keyset and scope call in full rather than pointing at an example that still uses Extend, narrows the shared ErrForbidden sentence to the Client, and drops the issue number. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk --- languages/golang/encrypt/README.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/languages/golang/encrypt/README.md b/languages/golang/encrypt/README.md index a30d36a88..9c95f1f33 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -376,6 +376,10 @@ opens a keyset's rows only when it has a grant for that keyset. So access is controlled in ZeroKMS, one tenant at a time. You create each keyset, and give each client a grant, before the client uses it. +The `*Client` opens the rows of every keyset it has a grant for, with no +error. To refuse another tenant's rows, decrypt through the tenant's +`*Cipher`. + Use a scope when tenants share a keyset, and your program knows the tenant of each call, such as from the request. A scope needs nothing created in ZeroKMS. A scoped row opens only through a cipher with the same keyset and @@ -390,8 +394,8 @@ The `context_field` is stored in the clear, like a `passthrough` field. Nothing authenticates it. See the `context_field` note in [The cipher](#the-cipher). -A keyset combines with a scope, as in the example in -[The cipher](#the-cipher): `client.Keyset(..).Scope(..)`. +A keyset combines with a scope: +`client.Keyset(encrypt.KeysetName(..)).Scope(..)`. ### Errors when the tenant does not match @@ -401,6 +405,7 @@ the code of the error's `*Diagnostic` (see [Errors](#errors)). | Wrong tenant | Error | `Code` | |---|---|---| | Keyset: a `*Cipher` for one keyset decrypts a row sealed under another | `ErrForeignKeyset`, before any key is retrieved | `stack_encrypt::foreign_keyset` | +| Keyset: the `*Client` decrypts a row of another keyset that it has a grant for | no error: the row opens | none | | Scope: a cipher with another scope, or with no scope, decrypts a scoped row | `ErrForbidden`, when ZeroKMS refuses the data key | `stack_kms::retrieve_key_failed` | | Scope: the `*Client` decrypts a scoped row | `ErrForbidden` | `stack_kms::retrieve_key_failed` | | `context_field`: a cipher from `cipher.Context(..)` decrypts a row that stores another tenant | `ErrContextMismatch`, before any key is retrieved | `stack_encrypt::context_mismatch` | @@ -408,9 +413,8 @@ the code of the error's `*Diagnostic` (see [Errors](#errors)). | `context_field`: a row's stored tenant was changed after it was written | `ErrForbidden` | `stack_kms::retrieve_key_failed` | The `ErrForbidden` for a scope does not say that the scope is wrong, and -the `Help` of its `*Diagnostic` is empty. A client with no grant for a keyset gets the same error -and code when it asks for data keys. Issue #1893 proposes a `Help` for -each cause. +the `Help` of its `*Diagnostic` is empty. The `*Client` gets the same error +and code when it decrypts rows of a keyset it has no grant for. The test client from `encrypttest` gives the same errors as ZeroKMS for a wrong keyset, scope, or `context_field`. See From e47ef95b6ec968019f9c05a14cf07a6fba7db3d1 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Fri, 27 Nov 2026 16:09:58 +1100 Subject: [PATCH 4/9] docs(golang): name the example tenant tenant42 throughout The tenant guide uses the keyset name tenant42, because the dashboard refuses a hyphen in a keyset name (#1865). The older examples used tenant-42, so one tenant had two names. The README, the Cipher.Scope doc comment, doc.go and the example program now all use tenant42. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk --- languages/golang/encrypt/README.md | 8 ++++---- languages/golang/encrypt/cipher.go | 2 +- languages/golang/encrypt/doc.go | 2 +- languages/golang/encrypt/example/main.go | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/languages/golang/encrypt/README.md b/languages/golang/encrypt/README.md index 9c95f1f33..006ab555a 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -60,7 +60,7 @@ The package reference is on [pkg.go.dev]; the generator's reference is return err } defer client.Close() - cipher := client.Keyset(encrypt.KeysetName("tenant-42")) + cipher := client.Keyset(encrypt.KeysetName("tenant42")) encrypted, err := users.Encrypt(ctx, cipher, people) opened, err := users.Decrypt(ctx, cipher, encrypted) @@ -203,12 +203,12 @@ A `Cipher` is the client bound to one keyset, and to any extension of the context. Every generated function takes one. ```go -cipher := client.Keyset(encrypt.KeysetName("tenant-42")).Extend("tenant-42") +cipher := client.Keyset(encrypt.KeysetName("tenant42")).Extend("tenant42") ``` `Extend` appends to the context that the tags declare, for every field, in every call through the cipher: the write, the query and the read. A row -written through `Extend("tenant-42")` opens and matches only through a cipher +written through `Extend("tenant42")` opens and matches only through a cipher with the same extension. No call takes a keyset or a context, so the three cannot use different ones. @@ -1079,7 +1079,7 @@ CipherStash credentials. Use it in CI that has no route to ZeroKMS. func TestSaveUser(t *testing.T) { client := encrypttest.NewClient(t) - cipher := client.Keyset(encrypt.KeysetName("tenant-42")) + cipher := client.Keyset(encrypt.KeysetName("tenant42")) ``` 3. Call your code with it, as you would call it with a production client. diff --git a/languages/golang/encrypt/cipher.go b/languages/golang/encrypt/cipher.go index 6f4a1b77a..a53507346 100644 --- a/languages/golang/encrypt/cipher.go +++ b/languages/golang/encrypt/cipher.go @@ -60,7 +60,7 @@ func (cph *Cipher) KeysetID(ctx context.Context) (KeysetID, error) { // Scope returns a cipher that adds a scope to the context of every field, // in every call through it: the write, the query and the read. A scope // narrows the data and the queries over it: a field sealed through -// cipher.Scope("tenant-42") opens and matches only through a cipher with the +// cipher.Scope("tenant42") opens and matches only through a cipher with the // same scope. It appends to the context the tags declare and never // replaces it. A part is a string, a byte slice or an integer (int32, // int64, uint32, uint64; Go's int is sent as int64); a byte slice is copied. diff --git a/languages/golang/encrypt/doc.go b/languages/golang/encrypt/doc.go index 3003f54b6..a333c8735 100644 --- a/languages/golang/encrypt/doc.go +++ b/languages/golang/encrypt/doc.go @@ -52,7 +52,7 @@ // return err // } // defer client.Close() -// cipher := client.Keyset(encrypt.KeysetName("tenant-42")) +// cipher := client.Keyset(encrypt.KeysetName("tenant42")) // encrypted, err := users.Encrypt(ctx, cipher, people) // []users.EncryptedUser // opened, err := users.Decrypt(ctx, cipher, encrypted) // []users.User // term, err := users.Fields.Email.Eq(ctx, cipher, "bob@example.com") diff --git a/languages/golang/encrypt/example/main.go b/languages/golang/encrypt/example/main.go index d4234d800..ae808fc55 100644 --- a/languages/golang/encrypt/example/main.go +++ b/languages/golang/encrypt/example/main.go @@ -28,7 +28,7 @@ func run(ctx context.Context) error { // One cipher for one tenant: the default keyset, and the tenant as a // part of every field's context. Every call through it carries both. - cipher := client.DefaultKeyset().Scope("tenant-42") + cipher := client.DefaultKeyset().Scope("tenant42") people := []User{ {ID: 1, Email: "alice@example.com", Age: 34}, From b230ea0cae7cbbdcd122017f601c2417a3acff47 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Fri, 27 Nov 2026 16:14:02 +1100 Subject: [PATCH 5/9] docs(golang): say which error a Cipher with no keyset grant gets A live check against ZeroKMS, with a client that had no grant for the keyset, showed that KeysetID and an encrypt through the Cipher both fail with ErrNotFound and stack_kms::keyset_not_found. A keyset that does not exist gives the same error. After a grant both calls work, and after a revoke they fail the same way again. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk --- languages/golang/encrypt/README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/languages/golang/encrypt/README.md b/languages/golang/encrypt/README.md index 006ab555a..3a5dbd6af 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -416,6 +416,12 @@ The `ErrForbidden` for a scope does not say that the scope is wrong, and the `Help` of its `*Diagnostic` is empty. The `*Client` gets the same error and code when it decrypts rows of a keyset it has no grant for. +A `*Cipher` for a keyset that its client has no grant for fails when it +loads the keyset, before any data key is asked for. `KeysetID` and an +encrypt both fail with `ErrNotFound` and the code +`stack_kms::keyset_not_found`. That is the same error as for a keyset +that does not exist, so the error cannot tell you which of the two it is. + The test client from `encrypttest` gives the same errors as ZeroKMS for a wrong keyset, scope, or `context_field`. See [Testing your application](#testing-your-application). From a2247eca676e4e8071f195d3390797eed0eacf75 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Fri, 27 Nov 2026 16:22:23 +1100 Subject: [PATCH 6/9] docs(golang): qualify what a wrong extension or context matches A range query does not check the tenant, so the README no longer says that a row written through Extend matches only through the same extension. It says that equality and match queries match none of another extension's rows, and that a range query can return them. The context_field guidance gets the same warning and the advice to filter on the tenant column. The scope errors now say to check for ErrAuthentication as well, as the Errors list does. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk --- languages/golang/encrypt/README.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/languages/golang/encrypt/README.md b/languages/golang/encrypt/README.md index 3a5dbd6af..435dbde28 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -208,9 +208,11 @@ cipher := client.Keyset(encrypt.KeysetName("tenant42")).Extend("tenant42") `Extend` appends to the context that the tags declare, for every field, in every call through the cipher: the write, the query and the read. A row -written through `Extend("tenant42")` opens and matches only through a cipher -with the same extension. No call takes a keyset or a context, so the three -cannot use different ones. +written through `Extend("tenant42")` opens only through a cipher with the +same extension. An equality or match query through another extension +matches none of its rows. A range query through another extension can still +return them: see [Queries with the wrong tenant](#queries-with-the-wrong-tenant). +No call takes a keyset or a context, so the three cannot use different ones. > **Take care** > @@ -416,6 +418,10 @@ The `ErrForbidden` for a scope does not say that the scope is wrong, and the `Help` of its `*Diagnostic` is empty. The `*Client` gets the same error and code when it decrypts rows of a keyset it has no grant for. +Check for `ErrAuthentication` too, as the [Errors](#errors) list says. A +decrypt with the wrong scope fails with `ErrAuthentication` when ZeroKMS +returns the key and the ciphertext then fails to open. + A `*Cipher` for a keyset that its client has no grant for fails when it loads the keyset, before any data key is asked for. `KeysetID` and an encrypt both fail with `ErrNotFound` and the code @@ -450,6 +456,10 @@ A query on a type with a `context_field` derives under the tenant that `cipher.Context(..)` names. Through a cipher with no `Context`, the query fails with `ErrEncoding`. +A range query through `cipher.Context(..)` can still return rows that store +another tenant, as a range query with the wrong scope can. Add a filter on +the `context_field` column to every range query on such a type. + ## What is stored A field with `encrypt` and `index=` is a struct with one field for each From 871a0e325c4092763d97cbefd9906953d4a4d1ed Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Fri, 27 Nov 2026 17:05:08 +1100 Subject: [PATCH 7/9] docs(golang): describe refused-key help and the 404 for a missing grant A refused data key now has a Help that names the likely cause, and the Client gets ErrNotFound for a keyset it has no grant for. Both come from #1945, so this guide describes them as they will be once it merges. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk --- languages/golang/encrypt/README.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/languages/golang/encrypt/README.md b/languages/golang/encrypt/README.md index 435dbde28..e3c23acec 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -414,9 +414,19 @@ the code of the error's `*Diagnostic` (see [Errors](#errors)). | `context_field`: a cipher from `cipher.Context(..)` encrypts a value whose field names another tenant | `ErrContextMismatch`, before anything is sent | none: the SDK refuses the value itself | | `context_field`: a row's stored tenant was changed after it was written | `ErrForbidden` | `stack_kms::retrieve_key_failed` | -The `ErrForbidden` for a scope does not say that the scope is wrong, and -the `Help` of its `*Diagnostic` is empty. The `*Client` gets the same error -and code when it decrypts rows of a keyset it has no grant for. +The `ErrForbidden` for a scope does not say that the scope is wrong. +ZeroKMS gives the same answer for a disabled keyset, and for an access +token that lacks the scope to retrieve keys. [Errors](#errors) says why +ZeroKMS does not name the cause. The `Help` of the error's `*Diagnostic` +says which cause is likely: + +- Through a `*Cipher`, the `Help` says that the context is the likely + cause, and names the keyset. +- Through the `*Client`, the `Help` names every cause. + +ZeroKMS answers a missing grant with HTTP 404. So the `*Client` fails with +`ErrNotFound` when it decrypts a row of a keyset it has no grant for. The +code is `stack_kms::retrieve_key_failed`. Check for `ErrAuthentication` too, as the [Errors](#errors) list says. A decrypt with the wrong scope fails with `ErrAuthentication` when ZeroKMS From f652115dac119b8058f04983947586b428d90b40 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Fri, 27 Nov 2026 17:13:09 +1100 Subject: [PATCH 8/9] docs(golang): say a match query can match another tenant's row by chance A match query is probabilistic, so a row of another tenant or scope can pass its filter check. Only an equality query matches none of them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk --- languages/golang/encrypt/README.md | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/languages/golang/encrypt/README.md b/languages/golang/encrypt/README.md index e3c23acec..83c213074 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -207,12 +207,14 @@ cipher := client.Keyset(encrypt.KeysetName("tenant42")).Extend("tenant42") ``` `Extend` appends to the context that the tags declare, for every field, in -every call through the cipher: the write, the query and the read. A row -written through `Extend("tenant42")` opens only through a cipher with the -same extension. An equality or match query through another extension -matches none of its rows. A range query through another extension can still -return them: see [Queries with the wrong tenant](#queries-with-the-wrong-tenant). -No call takes a keyset or a context, so the three cannot use different ones. +every call through the cipher: the write, the query, and the read. No call +takes a keyset or a context, so the three cannot use different ones. + +A row written through `Extend("tenant42")` opens only through a cipher with +the same extension. An equality query through another extension matches +none of its rows. A match query can match one of them by chance. A range +query through another extension can still return them: see +[Queries with the wrong tenant](#queries-with-the-wrong-tenant). > **Take care** > @@ -447,9 +449,14 @@ wrong keyset, scope, or `context_field`. See A query with the wrong keyset or the wrong scope returns no error. What it matches depends on the kind of query. -An equality query (`Eq`) or a match query (`Matches`) matches none of the -other tenant's rows. The query term derives under the cipher's keyset and -scope, so it differs from every term the other tenant stored. +An equality query (`Eq`) matches none of the other tenant's rows. The query +term derives under the cipher's keyset and scope, so it differs from every +term the other tenant stored. + +A match query (`Matches`) is probabilistic, as [Queries](#queries) says. +A row of the other tenant can match by chance, as a row without the query's +text can. A decrypt of such a row through the same cipher fails with the +error in the table above. A range query can return the other tenant's rows, with an ORE index or an OPE index. The range queries are `Lt`, `Lte`, `Gt`, `Gte`, and `Between`. From 906bec646f98bd5667d9347e37da676d2047955d Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Sat, 28 Nov 2026 00:21:23 +1100 Subject: [PATCH 9/9] docs(golang): say a wrong-tenant range query can return some rows A range query through the wrong keyset or scope compares ORE or OPE terms, and that comparison always gives an answer. So the query can return some of the other tenant's rows and not others. The README said such a query returned all of the rows or none, and the Scope doc comment said a scoped field matches only through the same scope. Only an equality query matches none of the other tenant's rows. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk --- languages/golang/encrypt/README.md | 6 +++--- languages/golang/encrypt/cipher.go | 11 +++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/languages/golang/encrypt/README.md b/languages/golang/encrypt/README.md index 83c213074..d128a5a2f 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -461,9 +461,9 @@ error in the table above. A range query can return the other tenant's rows, with an ORE index or an OPE index. The range queries are `Lt`, `Lte`, `Gt`, `Gte`, and `Between`. The database still compares the query term with the other tenant's terms, -and the result of that comparison has no relation to the values. In a test -against EQL v4 columns, such a query returned all of the other tenant's -rows or none of them. +and the result of that comparison has no relation to the values. So such +a query can return some of the other tenant's rows and not others. The +result looks like a normal result, but it is wrong. So a count or a list of the rows that such a query returns is wrong. A decrypt of those rows through the same cipher fails with the error in the diff --git a/languages/golang/encrypt/cipher.go b/languages/golang/encrypt/cipher.go index a53507346..8c09bd4a9 100644 --- a/languages/golang/encrypt/cipher.go +++ b/languages/golang/encrypt/cipher.go @@ -60,10 +60,13 @@ func (cph *Cipher) KeysetID(ctx context.Context) (KeysetID, error) { // Scope returns a cipher that adds a scope to the context of every field, // in every call through it: the write, the query and the read. A scope // narrows the data and the queries over it: a field sealed through -// cipher.Scope("tenant42") opens and matches only through a cipher with the -// same scope. It appends to the context the tags declare and never -// replaces it. A part is a string, a byte slice or an integer (int32, -// int64, uint32, uint64; Go's int is sent as int64); a byte slice is copied. +// cipher.Scope("tenant42") opens only through a cipher with the same scope. +// An equality query through another scope matches none of it, but a range +// query through another scope can still return it, with no error: see the +// README section "Queries with the wrong tenant". It appends to the context +// the tags declare and never replaces it. A part is a string, a byte slice +// or an integer (int32, int64, uint32, uint64; Go's int is sent as +// int64); a byte slice is copied. // Several parts nest in order: Scope(a, b) is Scope(a).Scope(b). An // unsupported part type is reported by the first call through the cipher, // as [ErrEncoding].