Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
21 changes: 11 additions & 10 deletions .cargo/mutants.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,18 @@ exclude_re = [
# the empty list (`dynamic::target::NoTargets`), which a test asserts on
# and `resolve` turns into `TargetError::NoTargets`; `vec![]` is the body.
'stack-encrypt/src/dynamic/target\.rs:\d+:\d+: replace <impl TargetResolver for NoTargets>::targets -> Vec<TargetDescriptor> with vec!\[\]$',
# A serde visitor's `expecting` writes only the "expected ..." half of a
# serde error's message, and no caller of these three reads that message:
# a context, statistics and a plan's root are each refused with one fixed
# `Reason` (`ContextKind`, `NotStats`, `NotAnObject`), and the plan's
# field-level reasons are read from the error's path, so writing nothing
# refuses the same input with the same error.
# A serde visitor's `expecting` writes only the "expected ..." part of
# the message of a serde error. No caller of these three visitors reads
# that message:
# - A context is refused with the fixed `Reason` `ContextKind`.
# - Statistics are refused with the fixed `Reason` `NotStats`.
# - The root of a plan is refused with `NotAFrame`, or with `NotAnObject`
# and the kind of value in the frame.
# - The reasons for one field of a plan are read from the path of the
# error.
# So a mutant that writes nothing refuses the same input with the same
# error.
'stack-encrypt/src/dynamic/(context|query|record)\.rs:\d+:\d+: replace <impl (Visitor<\x27de> for ContextVisitor|serde::Deserialize<\x27de> for Confidence>::deserialize::<impl serde::de::Visitor<\x27_> for V|Deserialize<\x27de> for PlanWire>::deserialize::<impl Visitor<\x27de> for V)>::expecting -> fmt::Result with Ok\(Default::default\(\)\)$',
# The same for the visitor that reads a refused plan's keys back
# (`entry_key`): its error is discarded (`.ok()`), so its message is never
# read at all.
'stack-encrypt/src/dynamic/record\.rs:\d+:\d+: replace entry_key::<impl Deserialize<\x27de> for Keys>::deserialize::<impl Visitor<\x27de> for V>::expecting -> fmt::Result with Ok\(Default::default\(\)\)$',
# The JSON index. `unhex` joins a high nibble shifted into the top four
# bits with a low nibble below 16: the bits never overlap, so `^` is `|`.
'stack-encrypt/src/json/mod\.rs:\d+:\d+: replace \| with \^ in unhex$',
Expand Down
2 changes: 1 addition & 1 deletion conformance/stack-encrypt/declaration-grammar.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ native engine diagnostic code and structured payload. Accepted cases may include
`normalized`. Runners reject unknown corpus keys and contradictory outcomes.

A malformed transport frame also has `boundary_error`. The native parser reports
`stack_encrypt::dynamic_plan` with `not_an_object`; the WASI frame check reports
`stack_encrypt::dynamic_plan` with `not_a_frame`; the WASI frame check reports
`stack_guest_abi::malformed_input` before reaching that parser. Both runners verify
the frame really is malformed. Valid-frame declaration errors must agree exactly
on code and payload; Go also requires `ErrEncoding`. Error prose is not contractual.
Expand Down
4 changes: 3 additions & 1 deletion conformance/stack-encrypt/raw-frames-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ diagnostic.
Within `authenticated`, two outcomes are distinguished. A leaf sealed as an
`int32` and read as `int64` or `string` authenticates and then fails the
declared type: `stack_encrypt::plan_field_type` naming the field and the
expected kind, as the read profile's authenticated cases do. A leaf moved
expected kind, as the read profile's authenticated cases do. The diagnostic
never names the kind that the leaf decrypted to. That kind was inside the
ciphertext, so the payload has no `found` field. A leaf moved
into another role (the single leaf under an empty-sequence tag) fails
authentication itself: `stack_encrypt::aead`, which Go reports as
`ErrAuthentication` carrying that code. Both runners compare the code and
Expand Down
41 changes: 32 additions & 9 deletions conformance/stack-encrypt/v1/record-declarations-v1.cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -2326,7 +2326,7 @@
"accepted": false,
"expected_error": {
"code": "stack_encrypt::dynamic_plan",
"fields": { "reason": "not_an_object" }
"fields": { "reason": "not_an_object", "found": "null" }
}
},
{
Expand All @@ -2335,7 +2335,7 @@
"accepted": false,
"expected_error": {
"code": "stack_encrypt::dynamic_plan",
"fields": { "reason": "not_an_object" }
"fields": { "reason": "not_an_object", "found": "array" }
}
},
{
Expand All @@ -2353,7 +2353,11 @@
"accepted": false,
"expected_error": {
"code": "stack_encrypt::dynamic_plan",
"fields": { "reason": "not_an_object", "field": "value" }
"fields": {
"reason": "not_an_object",
"field": "value",
"found": "string"
}
}
},
{
Expand All @@ -2362,7 +2366,11 @@
"accepted": false,
"expected_error": {
"code": "stack_encrypt::dynamic_plan",
"fields": { "reason": "not_an_object", "field": "value" }
"fields": {
"reason": "not_an_object",
"field": "value",
"found": "null"
}
}
},
{
Expand Down Expand Up @@ -3364,7 +3372,7 @@
"accepted": false,
"expected_error": {
"code": "stack_encrypt::dynamic_plan",
"fields": { "reason": "not_an_object" }
"fields": { "reason": "not_a_frame" }
},
"boundary_error": {
"code": "stack_guest_abi::malformed_input",
Expand All @@ -3377,7 +3385,7 @@
"accepted": false,
"expected_error": {
"code": "stack_encrypt::dynamic_plan",
"fields": { "reason": "not_an_object" }
"fields": { "reason": "not_a_frame" }
},
"boundary_error": {
"code": "stack_guest_abi::malformed_input",
Expand All @@ -3390,7 +3398,7 @@
"accepted": false,
"expected_error": {
"code": "stack_encrypt::dynamic_plan",
"fields": { "reason": "not_an_object" }
"fields": { "reason": "not_a_frame" }
},
"boundary_error": {
"code": "stack_guest_abi::malformed_input",
Expand All @@ -3405,7 +3413,7 @@
"accepted": false,
"expected_error": {
"code": "stack_encrypt::dynamic_plan",
"fields": { "reason": "not_an_object" }
"fields": { "reason": "not_a_frame" }
},
"boundary_error": {
"code": "stack_guest_abi::malformed_input",
Expand All @@ -3420,13 +3428,28 @@
"accepted": false,
"expected_error": {
"code": "stack_encrypt::dynamic_plan",
"fields": { "reason": "not_an_object" }
"fields": { "reason": "not_a_frame" }
},
"boundary_error": {
"code": "stack_guest_abi::malformed_input",
"fields": {}
}
},
{
"id": "frame-typed-null-spec",
"input": {
"frame_hex": "f1010000000500000076616c75651506000000"
},
"accepted": false,
"expected_error": {
"code": "stack_encrypt::dynamic_plan",
"fields": {
"reason": "not_an_object",
"field": "value",
"found": "null"
}
}
},
{
"id": "frame-field-order",
"input": {
Expand Down
8 changes: 6 additions & 2 deletions conformance/stack-encrypt/v1/record-reads-v1.cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,18 @@
"error": {
"code": "stack_encrypt::dynamic_record",
"fields": {
"reason": "not_an_object"
"reason": "not_an_object",
"row": 0,
"found": "passthrough"
}
}
},
"check_error": {
"code": "stack_encrypt::dynamic_record",
"fields": {
"reason": "not_an_object"
"reason": "not_an_object",
"row": 0,
"found": "passthrough"
}
}
},
Expand Down
13 changes: 13 additions & 0 deletions languages/golang/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ and `languages/golang/encrypt/encrypttest/vX.Y.Z` on the same commit.
`[16]byte` (`github.com/google/uuid.UUID`, say), which is converted to
one. `stashgen` does not yet map a Go type to it.

- `Diagnostic.Row` and `Diagnostic.Found` (#1985). The two accessors give
more detail for an error about a plan, a record, or a stored row that is
not an object:
- `Found` is the kind of value that the engine got, such as `"null"`,
`"array"`, `"string"`, `"passthrough"`, or `"sealed"`. `Found` never
contains the value.
- `Row` is the position of the row in a batch, counted from 0. The
message names the row too, as in `row 2 of the records to decrypt is
a passthrough value, not an object of stored fields`.
- `Found` names a kind only for a value that the program sent, or for a
node in the unencrypted structure of a stored row. An error about a
decrypted value never names the kind that the value decrypted to.

### Changed

- **A record field converts a number of another width or kind, as a
Expand Down
30 changes: 21 additions & 9 deletions languages/golang/encrypt/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,28 @@ var ErrStorageVersion = vcffi.ErrUnsupportedVersion
// "stack_kms::keyset_not_found", ...; stable), Message (what Error
// returns after the sentinel's text), Help, URL, Severity, Fields (the
// structured fields, by name) and Causes (the errors behind it, outermost
// first). Accessors read the fields a caller branches on: ExpectedKeyset
// and FoundKeyset on an [ErrForeignKeyset] (each a [KeysetID]'s bytes);
// Field and Reason on a refused plan, record or value; and Construct and At
// on a refused Match predicate, the construct in the grammar's words and
// the byte offset in the predicate where it starts.
// first). Accessors read the fields a caller branches on:
//
// What one may carry is fixed: keyset ids and names, field names, counts,
// index kinds, ZeroKMS request kinds and HTTP statuses, and a refused
// predicate's construct and offset. Never plaintext, key material,
// tokens, ciphertext or term bytes, or the values of an encryption context.
// - ExpectedKeyset and FoundKeyset, on an [ErrForeignKeyset]. Each is the
// bytes of a [KeysetID].
// - Field and Reason, on a refused plan, record, or value.
// - Row and Found, on a plan, a record, or a row that is not an object.
// Row is the position of the row in a batch, counted from 0. Found is
// the kind of value that was there, such as "null", "array", or
// "string".
// - Construct and At, on a refused Match predicate. Construct is the
// construct in the grammar's words. At is the byte offset in the
// predicate where the construct starts.
//
// A Diagnostic contains only these details:
//
// - keyset ids and names, field names, and counts
// - row positions, and the kinds of values
// - index kinds, ZeroKMS request kinds, and HTTP statuses
// - the construct and the offset of a refused predicate
//
// A Diagnostic never contains plaintext, key material, tokens, ciphertext
// bytes, term bytes, or the values of an encryption context.
//
// Errors the client raises itself carry none: a closed client ([ErrState]),
// a guest that did not return, [ErrMemoryLock], and an argument refused
Expand Down
2 changes: 2 additions & 0 deletions languages/golang/encrypt/guest/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,9 @@ fn parse_query<'f>(
.ok_or_else(|| {
fail_dynamic(&dynamic::Error::Source {
field: Some(field.to_owned()),
row: None,
reason: dynamic::Reason::UnknownOperator,
found: None,
})
})?;
Ok((plan, field, operator))
Expand Down
31 changes: 25 additions & 6 deletions languages/golang/encrypt/guest/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,15 @@ pub fn status_for_error(error: &stack_encrypt::Error) -> u32 {

/// A dynamic-path error as a status code.
///
/// The split the library draws is the one the ABI needs: `Context`, `Term`,
/// `Plan`, `UntypedIndex`, `Source`, `Record` and `Predicate` are each a
/// statement about the caller's input, decided before any key is minted or
/// retrieved, so they are
/// [`STATUS_ENCODING`] — named one by one, because that verdict is the
/// host's to act on and must be given deliberately. `Cipher` defers to
/// The library separates errors about the caller's input from every other
/// error, and the ABI needs the same separation. `Context`, `Term`, `Plan`,
/// `UntypedIndex`, `Source`, `SourceFailed`, `Record`, and `Predicate` are
/// each about the caller's input. The library decides each one before any
/// key is minted or retrieved. So each one is [`STATUS_ENCODING`]. The
/// match names them one by one, because the host acts on that status, so
/// the choice must be deliberate.
///
/// `Cipher` defers to
/// [`status_for_error`]; `Internal` is the library's own invariant failing
/// — a slot count that did not line up, a re-proof that could not fail —
/// and is [`STATUS_INTERNAL`], never a verdict on the input.
Expand All @@ -189,6 +192,7 @@ pub fn status_for_dynamic(error: &stack_encrypt::dynamic::Error) -> u32 {
| Error::Plan { .. }
| Error::UntypedIndex { .. }
| Error::Source { .. }
| Error::SourceFailed { .. }
| Error::Record { .. }
// A `match` predicate refused at an offset in its text: a construct
// the grammar does not read or the index cannot answer, a variable
Expand Down Expand Up @@ -433,7 +437,9 @@ mod tests {
use stack_encrypt::refusal::{Error, Reason};
let refused = Error::Source {
field: None,
row: None,
reason: Reason::JsonPathNoWildcard,
found: None,
};
assert_eq!(
status_for_error(&stack_encrypt::Error::Input(Box::new(refused))),
Expand Down Expand Up @@ -562,6 +568,7 @@ mod tests {
Error::Plan {
field: age(),
reason: Reason::DuplicateOutput,
found: None,
},
),
(
Expand All @@ -574,14 +581,26 @@ mod tests {
"a bad source",
Error::Source {
field: age(),
row: None,
reason: Reason::FieldMissing,
found: None,
},
),
(
"a source that failed while it was read",
Error::SourceFailed {
field: age(),
row: Some(2),
cause: stack_encrypt::dynamic::plaintext::SourceError::msg("getter threw"),
},
),
(
"a bad record",
Error::Record {
field: age(),
row: None,
reason: Reason::NoCiphertextNode,
found: None,
},
),
(
Expand Down
60 changes: 53 additions & 7 deletions languages/golang/internal/guest/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ import (
// var d *encrypt.Diagnostic
// if errors.As(err, &d) { log.Print(d.Code, d.Help) }
//
// What it may carry is the rule written on stack-profile's ErrorPayload:
// keyset ids and names, field names, counts, index kinds, ZeroKMS request
// kinds and HTTP statuses, workspace ids, CRNs and regions, profile file
// paths, and for a refused predicate the construct in the grammar's words
// and a byte offset. Never plaintext, key material, tokens, ciphertext or
// term bytes, or a context's values: a stored context's length and parts
// at most.
// A Diagnostic contains only the details that the rule on stack-profile's
// ErrorPayload allows:
//
// - keyset ids and names, field names, and counts
// - row positions, and the kinds of values
// - index kinds, ZeroKMS request kinds, and HTTP statuses
// - workspace ids, CRNs, and regions
// - profile file paths
// - for a refused predicate, the construct in the grammar's words and a
// byte offset
//
// A Diagnostic never contains plaintext, key material, tokens, ciphertext
// bytes, term bytes, or the values of a context. For a stored context, a
// Diagnostic contains at most its length and its number of parts.
//
// A guest built before se_last_error, or one whose error does not decode,
// returns the bare sentinel instead: the detail is never allowed to hide
Expand Down Expand Up @@ -129,6 +136,45 @@ func (d *Diagnostic) Reason() string {
return reason
}

// Row is the position of the row in a batch that the error is about,
// counted from 0. An error names a row in any of these cases:
//
// - A record to encrypt, or a stored record, is not an object of fields.
// - A value in the row returned an error while the engine read it.
// - The engine could not encode a value in the row.
//
// An error about one field of a record names the field and no row. Examples
// are a missing field, an unknown field, and a value of another type.
//
// Row returns ok as false when the error names no row, such as an error
// about a single record. Row also returns ok as false for a row that is
// negative, is not a whole number, or is too large for an int.
func (d *Diagnostic) Row() (row int, ok bool) {
return nonNegativeInt(d.Fields["row"])
}

// Found is the kind of value that the engine got where it takes an object.
// Found names the kind and never contains the value. The kinds are:
//
// - "null", "array", "string", or the schema name of another kind, such
// as "uint32" or "object"
// - "passthrough", for a value stored without encryption
// - "sealed", for an encrypted value in the place of a stored record or
// of a row
// - "undefined", for JavaScript's undefined
// - "unknown", for a value of a type that this build does not define
//
// Found names a kind only for a value that the program sent, or for a node
// in the unencrypted structure of a stored row. Found never names the kind
// that a value decrypted to.
//
// Found is "" when the error names no kind. A later version can add a kind,
// so keep a fallback case.
func (d *Diagnostic) Found() string {
found, _ := d.Fields["found"].(string)
return found
}

// Construct is what a refused predicate (a JSONSearch field's Match) names
// as refused, in the grammar's own words ("`like_regex`", "a position
// (`[2]`)", "a variable"), or "" for an error that names none. It is a
Expand Down
Loading
Loading