From b8ad961a06c6a9f8d8a2047162a78f9a518dd6c0 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Sat, 28 Nov 2026 00:32:42 +1100 Subject: [PATCH] fix(golang): report a cipher's stored error from KeysetID Client.Keyset(nil) returns a cipher that stores an ErrEncoding and is bound to the default keyset in the meantime. Every call that encrypts, decrypts or derives checks the stored error first, through Cipher.plan. KeysetID did not: it resolved the keyset straight away, so Keyset(nil).KeysetID returned the default keyset's id and no error. A program that validates a tenant's keyset at boot with KeysetID then believed it held a valid one. KeysetID now returns the stored error before it resolves anything. That covers a nil selector and any value Scope, Bind, Context or Audit refused. A new test calls every error-returning method on such ciphers and checks each returns the stored error before the guest is asked. Refs #1998 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk --- languages/golang/CHANGELOG.md | 6 ++ languages/golang/encrypt/cipher.go | 8 +- languages/golang/encrypt/cipher_error_test.go | 74 +++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 languages/golang/encrypt/cipher_error_test.go diff --git a/languages/golang/CHANGELOG.md b/languages/golang/CHANGELOG.md index 2396d5666..c2664b1d3 100644 --- a/languages/golang/CHANGELOG.md +++ b/languages/golang/CHANGELOG.md @@ -40,6 +40,12 @@ and `languages/golang/encrypt/encrypttest/vX.Y.Z` on the same commit. ### Fixed +- **`Cipher.KeysetID` on a cipher from `Client.Keyset(nil)` now fails with + `ErrEncoding`** (#1998). Before, it returned the default keyset's id and + no error, so a program that checked a keyset with `KeysetID` at boot + believed it held one. Like every other call through the cipher, + `KeysetID` now returns any error the cipher stores. That error is a nil + selector, or a value that `Scope`, `Bind`, `Context`, or `Audit` refused. - **Other calls no longer wait behind a token refresh** (#1958). An `auth.Strategy` is shared by every encrypt and decrypt in a process, and about once every 15 minutes its token is refreshed with the token diff --git a/languages/golang/encrypt/cipher.go b/languages/golang/encrypt/cipher.go index 6f4a1b77a..5c56fc7fb 100644 --- a/languages/golang/encrypt/cipher.go +++ b/languages/golang/encrypt/cipher.go @@ -52,8 +52,14 @@ func (cph *Cipher) Keyset() KeysetSelector { return cph.keyset } // since a Cipher is made without a request. The first use of a name or id // on the client is one ZeroKMS round trip, later uses come from the // guest's cache; the default keyset never makes a request. Use it at boot -// to validate a tenant's keyset and learn its id. +// to validate a tenant's keyset and learn its id. A cipher that holds a +// refusal reports it here as every other call does: a cipher from +// Client.Keyset(nil) fails with [ErrEncoding], and does not answer with the +// default keyset's id. func (cph *Cipher) KeysetID(ctx context.Context) (KeysetID, error) { + if cph.err != nil { + return KeysetID{}, cph.err + } return cph.client.resolveKeyset(ctx, cph.keyset) } diff --git a/languages/golang/encrypt/cipher_error_test.go b/languages/golang/encrypt/cipher_error_test.go new file mode 100644 index 000000000..054c1ef8a --- /dev/null +++ b/languages/golang/encrypt/cipher_error_test.go @@ -0,0 +1,74 @@ +package encrypt + +import ( + "context" + "errors" + "testing" + + "github.com/cipherstash/stack/languages/golang/internal/record" + "github.com/cipherstash/vitaminc/bindings/go/vcvalue" +) + +// A cipher that holds a refusal reports it from every call that returns an +// error, KeysetID included, and before the guest is asked: the +// uninitialised instance would answer ErrState. +func TestEveryCallReportsTheCiphersStoredError(t *testing.T) { + ctx := context.Background() + c := rawInstance(t) + refused := map[string]*Cipher{ + "Keyset(nil)": c.Keyset(nil), + "Keyset(nil).Scope": c.Keyset(nil).Scope("tenant-42"), + "Keyset(nil).Bind": c.Keyset(nil).Bind("row-1"), + "Keyset(nil).Audit": c.Keyset(nil).Audit(NewAuditMetadata().Text("service", "billing")), + "a scope the codec refuses": c.DefaultKeyset().Scope(1.5), + "an empty binding": c.DefaultKeyset().Bind(""), + "a context that is no label": c.DefaultKeyset().Context("tenants//acme"), + "audit metadata it refuses": c.DefaultKeyset().Audit(NewAuditMetadata().Text("service", "\xff")), + "a named keyset with a scope": c.Keyset(KeysetName("acme")).Scope(1.5), + } + plan := usersPlan() + read := vcvalue.Object{{Key: "operation", Value: "selector"}, {Key: "path", Value: "$.name"}} + for name, cph := range refused { + calls := map[string]func() error{ + "KeysetID": func() error { _, err := cph.KeysetID(ctx); return err }, + "Contexts": func() error { _, err := cph.Contexts(ctx, plan); return err }, + "Seal": func() error { _, err := cph.Seal(ctx, plan, []record.Source{usersRow(1, "a@b.c")}); return err }, + "Open": func() error { _, err := cph.Open(ctx, plan, []record.Sealed{fixtureRecord()}); return err }, + "QueryTerm": func() error { _, err := cph.QueryTerm(ctx, plan, "age", "eq", uint32(34)); return err }, + "Verify": func() error { _, err := cph.Verify(ctx, plan, "email", "matches", "alice", []any{"alice"}); return err }, + "TargetLimit": func() error { + _, err := cph.TargetLimit(ctx, plan, "email", "matches", []byte{1}, 1, Stats{}) + return err + }, + "JSONOpen": func() error { _, err := cph.JSONOpen(ctx, plan, "email", read, nil); return err }, + } + for method, call := range calls { + err := call() + if !errors.Is(err, ErrEncoding) || !errors.Is(err, cph.err) { + t.Errorf("%s: %s: %v, want the cipher's stored error %v", name, method, err, cph.err) + } + } + } +} + +// KeysetID on Client.Keyset(nil) is refused, and does not answer with the +// default keyset's id: a caller that validates a tenant's keyset at boot +// with KeysetID would otherwise believe it holds one. +func TestKeysetIDOfANilSelectorIsRefused(t *testing.T) { + ctx := context.Background() + c, err := NewDeterministicClient(ctx, [32]byte{}) + if errors.Is(err, ErrDeterministicGuestNotBuilt) || errors.Is(err, ErrGuestNotBuilt) { + t.Skip(err) + } + if err != nil { + t.Fatal(err) + } + defer c.Close() + if _, err := c.DefaultKeyset().KeysetID(ctx); err != nil { + t.Fatalf("the default keyset: %v", err) + } + id, err := c.Keyset(nil).KeysetID(ctx) + if !errors.Is(err, ErrEncoding) || id != (KeysetID{}) { + t.Fatalf("Keyset(nil).KeysetID = %v, %v; want ErrEncoding and no id", id, err) + } +}