Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions languages/golang/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion languages/golang/encrypt/cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit · pre-existing Calling KeysetID on a nil *Cipher panics on cph.err. Open instead returns ErrEncoding (records.go:115-119). Before this PR it panicked on cph.client, and every other *Cipher method except Open panics the same way. Guard all of them, for example in copy and plan plus KeysetID and the accessors, or guard none; guarding KeysetID alone would be inconsistent.

Source: code-review · claude-opus-5-5; mattpocock-skills:code-review · claude-opus-5-5


Generated by Claude Code

return KeysetID{}, cph.err
}
return cph.client.resolveKeyset(ctx, cph.keyset)
}

Expand Down
74 changes: 74 additions & 0 deletions languages/golang/encrypt/cipher_error_test.go
Original file line number Diff line number Diff line change
@@ -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, "[email protected]")}); 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)
}
}
Loading