Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
3607062
fix(golang): say what a "not an object" error found, and that guest a…
coderdan Nov 27, 2026
de8f63f
Merge branch 'fix/engine-not-an-object-errors' into fix/go-not-an-obj…
coderdan Nov 27, 2026
4398299
fix(golang): read every guest answer through one path, and name more …
coderdan Nov 27, 2026
b8ad372
Merge branch 'fix/engine-not-an-object-errors' into fix/go-not-an-obj…
coderdan Nov 27, 2026
c214720
fix(golang): a decrypt error never names what the value opened as
coderdan Nov 27, 2026
f567084
Merge branch 'fix/engine-not-an-object-errors' into fix/go-not-an-obj…
coderdan Nov 27, 2026
94e0961
fix(golang): decrypt errors name no kind, and every guest answer read…
coderdan Nov 27, 2026
0f4eafd
Merge branch 'fix/engine-not-an-object-errors' into fix/go-not-an-obj…
coderdan Nov 28, 2026
190de0f
docs(golang): rewrite the guest answer and decrypt error docs in plai…
coderdan Nov 28, 2026
26719c3
docs(stack-encrypt): rewrite the match vars error docs in plain language
coderdan Nov 28, 2026
70040a1
docs(golang): rewrite the changelog entries for decrypt errors and gu…
coderdan Nov 28, 2026
605a0f8
docs(golang): split two long sentences about match vars and map fields
coderdan Nov 28, 2026
25d6cf4
Merge branch 'fix/engine-not-an-object-errors' into fix/go-not-an-obj…
coderdan Nov 28, 2026
d74be3a
Merge branch 'fix/engine-not-an-object-errors' into fix/go-not-an-obj…
coderdan Nov 28, 2026
6c0017f
fix(stack-encrypt): say that the vars of a match are not a JSON object
coderdan Nov 28, 2026
409aa27
fix(golang): say what was wrong in the answer and decrypt error messages
coderdan Nov 28, 2026
100df4e
Merge branch 'fix/engine-not-an-object-errors' into fix/go-not-an-obj…
coderdan Nov 28, 2026
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
54 changes: 54 additions & 0 deletions languages/golang/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,62 @@ and `languages/golang/encrypt/encrypttest/vX.Y.Z` on the same commit.
node in the unencrypted structure of a stored row. An error about a
decrypted value never names the kind that the value decrypted to.

### Breaking

- **`Match` refuses `vars` that are not a JSON object with the reason
Comment thread
coderdan marked this conversation as resolved.
Resolved
`json_vars_not_object`, not `json_input`** (#1987). If your program
compares `Reason()` with `json_input` for this case, change it to the
new name. The error also has more detail:
- `Found` is the kind of value that the vars are, such as `"array"`,
`"uint64"`, or `"null"`.
- `Field` names the field.
- The message for vars that are null is `the vars of the match are
null, not a JSON object`. The field follows.

### Changed

- **A decrypt error names the field and the Go type that the field
needs, and never the kind that the value decrypted to** (#1987). The Go
type, the kind, and the length of a decrypted value were inside the
ciphertext, so an error message leaves them out.
- A map field has the type `map[string]V`, `Values`, or
`map[string]any`. The stored value of a map field can decrypt to
something that is not an object. The message is then `decrypt: field
"tags": ... the decrypted value is not an object, and map[string]any
needs an object`. Before, the message said `opened as []interface {},
not an object`.
- The message for every other mismatch says `the decrypted value is not
of type`, then the type that the field needs. Those mismatches are:
- a value of another type
- a null for a type that does not take a null
- a list or an array of another length
- a document that is not its JSON text
- an opaque value that is not bytes

- **An error about a guest answer that this module cannot read says that
the fault is internal** (#1987). A guest is a WebAssembly module that
this module embeds. An answer is the value that an exported function of
a guest returns. This module cannot read an answer that does not decode,
or that does not have the format that the function writes. The error for
such an answer is still `ErrInternal`. The message of the error now has
more detail:
- The message names the exported function, as in `cannot read the
guest's answer from se_query_term`.
- The message says that the cause is a bug, or a guest that does not
match the version of this module. The cause is not the program's
input.
- When the answer is not plaintext, the message names the kind of value
that the answer is.

The change covers the answers of `se_query_term`, `se_targets`,
`se_operators`, `se_contexts`, `se_verify`, `se_target_limit`,
`se_json_open`, `se_encrypt_record`, `se_decrypt_record`, `se_io`,
`sa_token`, `sa_secret_key`, `sa_device_identity`,
`sa_list_workspaces`, `sa_auth_runnable`, and `sa_auth_io`. The encrypt
guest also returns steps. A step is a request for the host to do I/O.
The error for a step that this module cannot read is now `ErrInternal`
too.

- **A record field converts a number of another width or kind, as a
query does.** A value sealed through `Cipher.Seal` for a field that
declares a type is read as that type when the conversion is exact: an
Expand Down
146 changes: 146 additions & 0 deletions languages/golang/auth/answer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package auth

import (
"errors"
"strings"
"testing"

"github.com/cipherstash/vitaminc/bindings/go/vcffi"
"github.com/cipherstash/vitaminc/bindings/go/vcvalue"
)

// When the answer of an auth guest is not an object, the error is
// ErrInternal. The error names the export and the kind of value that the
// answer is. The error never contains the value, because the value can be a
// key or a token.
func TestAnAnswerThatIsNotAnObjectNamesTheExportAndItsKind(t *testing.T) {
for _, tc := range []struct {
answer any
kind string
}{
{[]any{"secret-token"}, "a list"},
{"secret-token", "a string"},
{[]byte("secret-key"), "bytes"},
} {
encoded, err := vcffi.Marshal(tc.answer)
if err != nil {
t.Fatal(err)
}
_, err = object("sa_token", encoded)
if !errors.Is(err, ErrInternal) {
t.Fatalf("%s: %v, want ErrInternal", tc.kind, err)
}
want := "cipherstash: internal guest failure: cannot read the guest's answer from sa_token: it is " + tc.kind + ", not an object. The cause is a bug, or a guest that does not match the version of this module"
if err.Error() != want {
t.Errorf("message:\n got %q\nwant %q", err.Error(), want)
}
if strings.Contains(err.Error(), "secret") {
t.Errorf("the error holds the answer: %v", err)
}
}
encoded, err := vcffi.Marshal(vcvalue.Object{{Key: "access_token", Value: "t"}})
if err != nil {
t.Fatal(err)
}
if fields, err := object("sa_token", encoded); err != nil || len(fields.obj) != 1 {
t.Fatalf("an object: %v, %v", fields, err)
}
if _, err := object("sa_token", []byte{0xff}); !errors.Is(err, ErrInternal) ||
!strings.Contains(err.Error(), "cannot read the guest's answer from sa_token") ||
!strings.Contains(err.Error(), "does not match the version of this module") {
t.Errorf("bytes that do not decode: %v", err)
}
}

// The workspace list and the runnable operations are read the same way.
// For an answer that does not decode, or that is not what the export
// writes, the error names the export. The error also names the kind of
// value that the answer is.
func TestListAnswersNameTheExportAndTheirKind(t *testing.T) {
marshal := func(v any) []byte {
out, err := vcffi.Marshal(v)
if err != nil {
t.Fatal(err)
}
return out
}
for _, tc := range []struct {
read func([]byte) error
answer []byte
want string
}{
{func(b []byte) error { _, err := workspaceIDs(b); return err }, []byte{0xff}, "cannot read the guest's answer from sa_list_workspaces: "},
{func(b []byte) error { _, err := workspaceIDs(b); return err }, marshal("secret"), "from sa_list_workspaces: it is a string, not a list"},
{func(b []byte) error { _, err := workspaceIDs(b); return err }, marshal([]any{"ws", int64(1)}), "from sa_list_workspaces: workspace id 1 is a number, not a string"},
{func(b []byte) error { _, err := decodeRunnable(b); return err }, []byte{0xff}, "cannot read the guest's answer from sa_auth_runnable: "},
{func(b []byte) error { _, err := decodeRunnable(b); return err }, marshal("secret"), "from sa_auth_runnable: it is a string, not a list"},
{func(b []byte) error { _, err := decodeRunnable(b); return err }, marshal([]any{uint64(1), "secret"}), "from sa_auth_runnable: operation 1 is a string, not a handle"},
} {
err := tc.read(tc.answer)
if !errors.Is(err, ErrInternal) || !strings.Contains(err.Error(), tc.want) || strings.Contains(err.Error(), "secret") {
t.Errorf("%v, want %q", err, tc.want)
}
}
if ids, err := workspaceIDs(marshal([]any{"a", "b"})); err != nil || len(ids) != 2 {
t.Errorf("a good list: %v, %v", ids, err)
}
if handles, err := decodeRunnable(marshal([]any{uint64(3)})); err != nil || len(handles) != 1 || handles[0] != 3 {
t.Errorf("good handles: %v, %v", handles, err)
}
}

// When a field of an answer is missing or is of the wrong kind, the error
// is ErrInternal. The error names the export and the field. The error says
// that the cause is a bug, or a guest of another version. The error never
// contains the value of the field, because the value can be a token or a
// key.
func TestAnAnswersFieldsNameTheExport(t *testing.T) {
answer := fields{export: "sa_token", obj: vcvalue.Object{
{Key: "access_token", Value: int64(7)},
{Key: "client_key", Value: "secret-key"},
{Key: "region", Value: []byte("secret")},
{Key: "expires_at", Value: "secret"},
}}
for _, tc := range []struct {
read func() error
want string
}{
{func() error { _, err := answer.text("token_type"); return err }, "it has no token_type"},
{func() error { _, err := answer.text("access_token"); return err }, "access_token is a number, not a string"},
{func() error { _, err := answer.bytes("missing"); return err }, "it has no missing"},
{func() error { _, err := answer.bytes("client_key"); return err }, "client_key is a string, not bytes"},
{func() error { _, err := answer.optionalText("region"); return err }, "region is bytes, not a string"},
{func() error { _, err := answer.uint64Field("missing"); return err }, "it has no missing"},
{func() error { _, err := answer.uint64Field("expires_at"); return err }, "expires_at is a string, not an unsigned integer"},
} {
err := tc.read()
want := "cipherstash: internal guest failure: cannot read the guest's answer from sa_token: " + tc.want + ". The cause is a bug, or a guest that does not match the version of this module"
if !errors.Is(err, ErrInternal) || err == nil || err.Error() != want {
t.Errorf("message:\n got %v\nwant %q", err, want)
}
}
if s, err := answer.optionalText("absent"); err != nil || s != "" {
t.Errorf("an absent optional field: %q, %v", s, err)
}
}

// The error for a step that this package cannot read names sa_auth_io.
func TestAStepThatDoesNotReadNamesTheExport(t *testing.T) {
for _, raw := range [][]byte{{0xff}, mustMarshalAuth(t, "secret")} {
_, err := decodeAuthStep(raw)
if !errors.Is(err, ErrInternal) || !errors.Is(err, errStepMalformed) ||
!strings.Contains(err.Error(), "cannot read the guest's answer from sa_auth_io: the step is malformed") ||
strings.Contains(err.Error(), "secret") {
t.Errorf("%x: %v", raw, err)
}
}
}

func mustMarshalAuth(t *testing.T, v any) []byte {
t.Helper()
out, err := vcffi.Marshal(v)
if err != nil {
t.Fatal(err)
}
return out
}
21 changes: 14 additions & 7 deletions languages/golang/auth/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,22 @@ func (s *authStep) wipe() {
}
}

var errMalformedStep = fmt.Errorf("%w: the guest's step is malformed", ErrInternal)
// errStepMalformed describes the problem with a step that this package
// cannot read. The error for such a step wraps errStepMalformed.
// guest.BadAnswer builds that error. The error is ErrInternal, and it
// names sa_auth_io, the export that returns the step.
var errStepMalformed = errors.New("the step is malformed")

// errMalformedStep is the error for a step that this package cannot read.
var errMalformedStep = guest.BadAnswer("sa_auth_io", errStepMalformed)

// decodeAuthStep reads `{"handle": u64, "parked": bool, "requests":
// [{"method", "url", "headers", "body"}, ...]}`. Every byte slice it
// returns is its own copy.
func decodeAuthStep(raw []byte) (*authStep, error) {
decoded, err := vcffi.Unmarshal(raw)
if err != nil {
return nil, fmt.Errorf("%w: %w", errMalformedStep, err)
return nil, guest.BadAnswer("sa_auth_io", fmt.Errorf("%w: %w", errStepMalformed, err))
}
return authStepFrom(decoded)
}
Expand Down Expand Up @@ -167,19 +174,19 @@ func decodeAuthRequest(item any) (authRequest, bool) {

// decodeRunnable reads the list of parked handles the guest may resume.
func decodeRunnable(raw []byte) ([]uint64, error) {
decoded, err := vcffi.Unmarshal(raw)
decoded, err := guest.DecodeAnswer("sa_auth_runnable", raw)
if err != nil {
return nil, fmt.Errorf("%w: the runnable operations: %w", ErrInternal, err)
return nil, err
}
items, ok := decoded.([]any)
if !ok {
return nil, fmt.Errorf("%w: the runnable operations are not a list", ErrInternal)
return nil, guest.BadAnswer("sa_auth_runnable", fmt.Errorf("it is %s, not a list", guest.KindOf(decoded)))
}
handles := make([]uint64, 0, len(items))
for _, item := range items {
for i, item := range items {
h, ok := item.(uint64)
if !ok {
return nil, fmt.Errorf("%w: a runnable operation is not a handle", ErrInternal)
return nil, guest.BadAnswer("sa_auth_runnable", fmt.Errorf("operation %d is %s, not a handle", i, guest.KindOf(item)))
}
handles = append(handles, h)
}
Expand Down
Loading
Loading