Skip to content
Merged
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
12 changes: 12 additions & 0 deletions .changeset/eql-json-tree-open-one-child.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@cipherstash/eql': minor
---

`eql-bindings`, the Rust crate released with this package, can now decrypt one child of a JSON node on its own. A read of one child no longer needs the parent's whole subtree.

- Your app pairs each entry that `eql_v4.json_tree_children` returned with the parent's `JsonSubtreeKey`, through `JsonTreeChild::from_key(&key, entry)`.
- Decrypting checks that the entry is a child of that parent.
- Your app decrypts into a `JsonPlaintext` for the child's value. Decrypt into a `JsonChildPlaintext` for its key or index too.
- A child with members of its own does not decrypt alone. Read it through `JsonTreeSubtree`.

The EQL v4 JSON tree reference describes the child read. The stored format and the SQL bundle do not change.
1 change: 1 addition & 0 deletions .plain-language.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "names": ["CipherStash", "ZeroKMS", "EQL", "PostgreSQL"] }
7 changes: 4 additions & 3 deletions conformance/stack-encrypt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ cleanup gaps and unobserved behavior alongside the passing requirements.
## EQL and JSON operations

The [EQL/JSON contract](eql-json-contract.md) enrolls resolver capabilities,
document writes, queries, extraction/subtree reads, element-match and predicate
matching. It reuses the native, Go and independent PostgreSQL suites and shares
write/predicate case data. Run `mise run test:conformance:eql` and the separate
document writes, queries, extraction reads, subtree reads, reads of one
child checked against its parent, element-match, and predicate matching. The
contract reuses the native, Go, and independent PostgreSQL suites. The
contract also shares write and predicate case data. Run `mise run test:conformance:eql` and the separate
required `test:conformance:eql:postgres` task; see the contract for guest builds,
disposable database setup, intentional SQL differences and version reports.

Expand Down
4 changes: 2 additions & 2 deletions conformance/stack-encrypt/coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ unsupported feature silently advertised by the Go/dynamic record profile.

The [EQL/JSON contract](eql-json-contract.md) and its enrollment index cover
resolver/catalog capabilities, JSON writes and queries, request-bound reads,
element-match and predicate-match. Existing Rust, Go and PostgreSQL suites stay
in place. The common document and predicate-refusal tables now live in
reads of one child checked against its parent, element-match, and
predicate-match. Existing Rust, Go, and PostgreSQL suites stay in place. The common document and predicate-refusal tables now live in
`v1/eql-json-v1.cases.json`: Rust and Go execute them, retaining native key-call checks,
Go diagnostic mapping and all extra cases from the original suites.

Expand Down
1 change: 1 addition & 0 deletions conformance/stack-encrypt/eql-json-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ native Rust and Go. Python and TypeScript have no enrolled runner yet.
| `json-request-bound-reads-v1` | Extraction and subtree/wildcard reads open against the prepared path, column, position and bounds. Wrong requests and detectable tampering fail. | Native adversarial read suite, EQL request/open tests, Go selector reads, PostgreSQL extraction/subtree and hostile-row suites |
| `json-extraction-v1` | Single-node extraction opens the requested scalar. | Native scalar extraction, Go selectors and PostgreSQL plaintext extraction oracle |
| `json-subtree-v1` | Complete bounded subtrees and wildcard reads match plaintext queries. | Native/EQL prepared reads and Go/PostgreSQL read paths |
| `json-child-v1` | Your app opens one entry that `json_tree_children` returned, checked against the parent path that prepared the read. The check covers the entry's selector, depth, parent tag, and self tag: the one its decrypted key or index gives under the parent's token. The entry opens to its value and its authenticated key or index. The check refuses another node's child, an entry at another depth, a top-level field under a sealed root, and a parent path with a wildcard. A child with members does not open alone. | Native child reads and data-path reads, EQL typed child open, Go raw and generated `Children` (with and without a binding), PostgreSQL `json_tree_children` oracle |
| `json-tamper-v1` | Detectable mixed-document, truncated-chain and substituted-column answers are refused. | Native and PostgreSQL adversarial suites; Go observes wrong-column rejection |
| `json-element-match-v1` | Every condition must hold on one element. Ordering paths and resolver capability are required; a probe beyond the declared bound raises rather than silently truncating. | Native operator/capability tests, Go generated member APIs, SQL domain and independent plaintext predicate tests |
| `json-predicate-match-v1` | Supported text compiles to existing operators; refusals preserve reason, construct and byte offset, with no caller text in diagnostics. Missing and null paths obey the documented truth rules. | Shared negative corpus, native compiler/property tests, Go generated `Match`, SQL `jsonb_path_match` and handwritten-operator oracles |
Expand Down
29 changes: 29 additions & 0 deletions conformance/stack-encrypt/v1/eql-json-v1.enrollment.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,35 @@
"go-postgres": ["TestJSONDocumentsQueryAndReadInPostgres"]
}
},
{
"id": "json-child-v1",
"evidence": {
"engine-reads": [
"given_one_child_of_a_node::each_leaf_member_opens_to_its_key_and_value",
"given_one_child_of_a_node::each_element_opens_to_its_index_and_value_in_position_order",
"given_one_child_of_a_node::a_child_of_another_node_is_refused_by_its_selector",
"given_one_child_of_a_node::an_element_read_as_another_position_is_refused",
"given_one_child_of_a_node::a_member_under_the_element_rule_is_refused_by_its_label",
"given_one_child_of_a_node::a_row_at_another_depth_is_refused",
"given_one_child_of_a_node::a_chain_head_is_refused_before_any_key_is_retrieved",
"given_one_child_of_a_node::a_key_with_a_wildcard_is_refused_before_any_key_is_retrieved"
],
"engine-queries": [
"a_child_opens_against_the_parent_its_request_names",
"a_malformed_read_is_refused_before_any_key"
],
"eql-typed": ["a_child_opens_through_the_key_of_its_parent"],
"postgres": ["postgres_each_child_opens_against_its_parent"],
"go": [
"TestJSONChildrenOpenAgainstTheirParent",
"TestTheReadOperatorsAreTheOnesTheEngineOpens",
"TestACustomersDocumentReadsThroughACipherWithABinding",
"TestAnOlderGeneratedChildrenTokenIsAQueryOperand",
"TestJSONReadTypesPrintAndLogRedacted"
],
"go-postgres": ["TestJSONDocumentsQueryAndReadInPostgres"]
}
},
{
"id": "json-tamper-v1",
"evidence": {
Expand Down
44 changes: 34 additions & 10 deletions docs/plans/2026-10-24-stack-encrypt-api-plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -963,16 +963,23 @@ The generated file builds its
declaration through `gensupport` calls, and the module builds the frames
it sends to the engine. So a change to the serialised `Planned`'s shape is a change inside the
module. A program generates again only to adopt a new
declaration, such as a scope or binding on a field. The generator keeps its output identical for unchanged input. So an
upgrade alone does not change a committed `*_stash.go` file, apart from
the one regeneration the handle rename needs.
declaration, such as a scope or binding on a field, or a new generated
method. The generator keeps its output identical for unchanged input. So
an upgrade alone does not change a committed `*_stash.go` file.

Two parts of the target state are not in Go yet:
A regeneration picks up two changes to the generated code. The handle
rename is the one a program needs. The other is the new `Children`. The
new `Children` returns `encrypt.JSONChildren`, and that type opens each
child.

A file generated before this change still compiles and runs. That file's
`Children` returns the token alone.

One part of the target state is not in Go yet:

- A Go program cannot set match options.
- A Go program cannot open one child of a JSON node, bound to its parent.

Both are in [Remaining work](#remaining-work).
[Remaining work](#remaining-work) lists it.

### Rules in the target state

Expand Down Expand Up @@ -1284,8 +1291,22 @@ release leaves behind.
- **Why.** Today a program must read and open the parent's whole subtree to
see one child. That transfers more data and reveals a different set of
tokens.
- **Done already.** Nothing.
- **Tracked in.** Issue #1677 is open. Pull request #1353, the Go JSON plan document, lists it as remaining.
- **Done already.**
- The engine opens one entry that `json_tree_children` returned. The
engine checks the entry against the key of the parent's path, with
the same parent-link checks as a subtree read
(`JsonSubtreeKey::open_child`, a `JsonChild`).
- A child opens to its value (`JsonPlaintext`). A child also opens to
its value with its authenticated key or index (`JsonChildPlaintext`;
Go's `JSONChildValue`).
- `eql-bindings` has `JsonTreeChild`. `children(path).encrypt_keyed`
returns the token with the key that opens what the query returns.
- The dynamic interface's `json_open` takes the `children` operation.
- Go's generated `Children` returns an `encrypt.JSONChildren`.
`JSONChildren.Open` opens each entry.
- The `eql-json-v1` profile enrolls the read as `json-child-v1`, with
Rust, Go, and PostgreSQL evidence.
- **Tracked in.** Issue #1677. Pull request #1353, the Go JSON plan document, lists it as remaining.
- **Finished when** Go can open one child, and the `eql-json-v1`
conformance profile covers it.

Expand Down Expand Up @@ -1644,7 +1665,9 @@ Base64.
program runs it is `predicate(text).var(..)`, so the operand is the text
form's and the data plan's `match` operand.
- **Tracked in.** Issue #1459 closes with the `predicate!` pull request.
Issue #1677 is open for opening one child. Issue #1486 is closed.
Issue #1677, for opening one child, closes with its pull request: a
`JsonDocument` field's `children(path)` key opens each child
(`open_child`). Issue #1486 is closed.
- **Finished when** these are all true:
- the [JSON query example](#query-a-json-document) compiles;
- a path, a member or a value type the field does not declare does not
Expand Down Expand Up @@ -1751,7 +1774,8 @@ Decided on 7 November 2026, as area one of the query design review.
`Planned` with the engine's expected answer beside it;
- a Go program generated for rc.3 runs against the module that sends
serialised `Planned` values, apart from the one regeneration the
handle rename needs;
handle rename needs. That regeneration also changes `Children` to
return `encrypt.JSONChildren`. That type still binds as the token;
- the chain in `plan/chain.rs` and the builder in `plan/build.rs`
are gone.

Expand Down
54 changes: 54 additions & 0 deletions languages/golang/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,44 @@ and `languages/golang/encrypt/encrypttest/vX.Y.Z` on the same commit.

### Added

- **Read one child of a JSON node** (#1677). Your app can now read one
level of a `JsonSearch` document without fetching the parent's whole
subtree. The read takes three steps.
1. Your app calls `Children(ctx, c, path)` on the field. `Children` now
returns an `encrypt.JSONChildren`.
2. The database (EQL) runs `eql_v4.json_tree_children(col, $1::text)`,
with `Token()` as `$1`. The database returns the parent's direct
children as encrypted entries.
3. Your app passes the entries to `Open(ctx, entries)`. `Open` checks
each entry, then decrypts it.

More detail:

- Each entry opens to an `encrypt.JSONChildValue`. `Key()` gives a
member's key, and `Index()` gives an element's index. `JSON` is the
child's value as JSON text, or `{}` or `[]` for an empty object or
array.
- The key or index is encrypted with the value, and authenticated. The
database returns an object's members in a random order. So the key is
how your app tells the members apart.
- `Open` checks each entry against the node at `path`. The entry's
selector must be that path extended by its key or index. The entry's
self tag must be the one that key or index gives under the parent's
token.
- An entry that is not a child of that node is `ErrEncoding`, with code
`stack_encrypt::json_wrong_entry`.
- A child that is an object or an array with members does not open
alone. `Open` refuses it as `ErrEncoding`, with code
`stack_encrypt::json_not_a_leaf`. Read such a child with `Subtree` at
its own path.
- No guest export is added. `se_json_open` takes the operation
`"children"`, and returns each child's key or index with its value.
`Cipher.JSONOpen` refuses that operation. Open a children read with
`JSONChildren.Open`.
- `JSONChildren` prints and logs with the path and the token redacted,
as `JSONSelection` and `JSONRead` do. `JSONChildValue` prints and logs
redacted, since a key can be a customer's data.

- **Audit metadata for the ZeroKMS audit log** (#1067). ZeroKMS logs every
data-key request with the identity that made it. `encrypt.AuditMetadata`
adds facts about the call to that log, such as its purpose or a request
Expand Down Expand Up @@ -92,8 +130,24 @@ and `languages/golang/encrypt/encrypttest/vX.Y.Z` on the same commit.
in the database until the row is written again or the column is
dropped.

- **`Children` returns `encrypt.JSONChildren`, not `encrypt.JSONToken`**
(#1677). The new type contains the token and opens the children.
Regenerate with `stashgen` to get it.
- Code that passes the result to a query keeps working, because
`JSONChildren` binds as its token.
- Code that keeps the result as a `JSONToken` takes `.Token()`.
- `ChildCount` still returns a `JSONToken`.
- A `*_stash.go` file generated before this release still compiles and
runs. Its `Children` returns a `JSONToken`, and a cipher with a binding
still refuses it.

### Changed

- `Children` through a cipher with a binding now prepares the read, as
`Selector` and `Subtree` do (#1677). Before, the cipher refused it. The
token derives under the cipher's scopes alone. The children open under
the scopes and the binding. A cipher with a binding still refuses
`ChildCount`, since a count opens nothing.
- `vcffi` and `vcvalue` move to vitaminc `f1fc9ae74`, the commit vitaminc
0.10.0 was released from. The host now checks the same date and
timestamp range as the guests.
Expand Down
3 changes: 2 additions & 1 deletion languages/golang/cmd/stashgen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ The field's `Fields` entry has a method per operation:
| `At.Age.Lt(ctx, c, v)`, `Lte`, `Gt`, `Gte`, `Between` | `eql.JSONSearchLeafQuery` | `(col -> $sel::text) < $1::eql_v4.query_json_leaf`, `$sel` from `At.Age.Selector(ctx, c)` |
| `Selector(ctx, c, path)` | `encrypt.JSONSelection`: `Selector()` and `Open` | `col -> $1::text` |
| `Subtree(ctx, c, path, opts)` | `encrypt.JSONRead`: `Tokens()`, `Open` and `OpenOne` | `eql_v4.json_tree_subtree(col, $1::text[], 64)` |
| `Children(ctx, c, path)`, `ChildCount(ctx, c, path)` | `encrypt.JSONToken` | `eql_v4.json_tree_children(col, $1::text)`, `eql_v4.json_tree_child_count(col, $1::text)` |
| `Children(ctx, c, path)` | `encrypt.JSONChildren`: `Token()`, and `Open` to each child's key or index and value | `eql_v4.json_tree_children(col, $1::text)` |
| `ChildCount(ctx, c, path)` | `encrypt.JSONToken` | `eql_v4.json_tree_child_count(col, $1::text)` |
| `Elements.Visits.Match(ctx, c, opts, predicates...)` | `eql.JSONSearchElementsQuery` | `eql_v4.json_tree_element_match(col, $1::eql_v4.query_json_elements)` |
| `Match(ctx, c, predicate, vars, opts)` | `eql.JSONSearchPredicateQuery` | `col @@ $1::eql_v4.query_json_predicate`, or `eql_v4.json_tree_match(col, $1::eql_v4.query_json_predicate)` |

Expand Down
61 changes: 51 additions & 10 deletions languages/golang/encrypt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -737,12 +737,12 @@ The EQL value stores the table and the column alone, in `i`, so:

- open the value through a cipher with the same scope and binding;
- query it through a cipher with the same scope. A binding is in no query;
- read part of a `JsonSearch` document (`Selector`, `Subtree`) through a
cipher with the same scope and binding. The selector or the tokens derive
under the scope alone, as a query does, and the entries open under the
scope and the binding. Every row of one `Open` call opens under that
binding, so a read through a cipher with a binding reads that binding's
row.
- read part of a `JsonSearch` document (`Selector`, `Subtree`, `Children`)
through a cipher with the same scope and binding. The selector or the
tokens derive under the scope alone, as a query does, and the entries
open under the scope and the binding. Every row of one `Open` call opens
under that binding, so a read through a cipher with a binding reads that
binding's row.

### Encrypted JSON documents

Expand Down Expand Up @@ -798,15 +798,56 @@ and a selector or token as `text`; cast each parameter, as above, so
PostgreSQL picks EQL's operator and not `jsonb`'s; a zero one is refused
rather than bound. A token is a capability over its node's subtree in every
row of the column, and a path's keys can be your customer's data, so
`JSONToken`, `JSONTokens`, `JSONSelection` and `JSONRead` print and log with
the tokens, the selector and the path redacted. A `JSONValue` holds the
plaintext a read opened, and prints it. Numbers order as `float64`,
so integers past 2^53 can tie at an ordered path; containment and `EqAt`
`JSONToken`, `JSONTokens`, `JSONSelection`, `JSONRead`, and `JSONChildren`
print and log with the tokens, the selector, and the path redacted. A
`JSONValue` contains the plaintext a read opened, and prints it. A
`JSONChildValue` contains a child's key or index and its value. A
`JSONChildValue` prints and logs redacted, since a key can be your
customer's data. Numbers order as
`float64`, so integers past 2^53 can tie at an ordered path; containment and `EqAt`
compare them exactly. At a wildcard path, `->` returns an unspecified
element of each row, so an ordering over every element is written over
`eql_v4.json_tree_path_query(col, $sel)`, its matches (the stashgen README
shows the SQL).

#### Read the children of one node

`Children` reads one level of a document, without fetching the parent's
whole subtree. It takes three steps.

1. **Your app** calls `Children` with the parent's path, a path with no
wildcard. `Children` derives the parent's token. A token is a value the
database uses to find a node's entry. Deriving it asks ZeroKMS for
nothing.
2. **The database (EQL)** runs `eql_v4.json_tree_children(profile, $1::text)`,
with `children.Token()` as `$1`. In each row, it finds the parent's
entry and returns the parent's direct children as encrypted entries. It
never decrypts anything, and it never receives a key.
3. **Your app** passes the returned entries to `children.Open`. The engine
checks that each entry is a child of the parent. Then it fetches the
data keys from ZeroKMS and decrypts each child.

```go
// Step 1: derive the parent's token.
children, err := users.Fields.Profile.Children(ctx, cipher, "$.address")

// Step 2: run this query with children.Token() as $1, and collect every entry.
// SELECT e FROM users, eql_v4.json_tree_children(profile, $1::text) AS e WHERE id = $2

// Step 3: decrypt the entries.
members, err := children.Open(ctx, entries)
for _, member := range members {
key, _ := member.Key() // "city", "street", ...
value, err := encrypt.DecodeJSON[string](member.JSON)
}
```

Each child has a key, for a member of an object, or an index, for an
element of an array. The database returns an object's members in a random
order, so the key is how your app tells them apart. A child that is an
object or an array with members does not decrypt alone. Read it with
`Subtree` at its own path.

#### Choosing a JSON query

| To find rows where… | Use | Engages an index |
Expand Down
Loading
Loading