Repository navigation
fix(golang): report a cipher's stored error from KeysetID #2000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
auxesis
wants to merge
1
commit into
next
Choose a base branch
from
fix/golang-keyset-stored-error
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit · pre-existing Calling
KeysetIDon a nil*Cipherpanics oncph.err.Openinstead returnsErrEncoding(records.go:115-119). Before this PR it panicked oncph.client, and every other*Ciphermethod exceptOpenpanics the same way. Guard all of them, for example incopyandplanplusKeysetIDand the accessors, or guard none; guardingKeysetIDalone would be inconsistent.Source: code-review · claude-opus-5-5; mattpocock-skills:code-review · claude-opus-5-5
Generated by Claude Code