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
8 changes: 8 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,14 @@ monorepo, which is where the silent failures are.
`jsonb @>` over the plaintexts, is `packages/eql/tests/encryption/tests/json_tree.rs`,
run by `mise run test:encryption:postgres`. The design and its
implementation notes are `docs/plans/2026-10-17-json-tree-index.md`.
Tests that read a stored document without PostgreSQL use
`stack_encrypt::eql_simulator`, Rust copies of the EQL read functions
(`json_tree_children`, `json_tree_subtree`). Production code never calls
them. The functions are behind the `eql-simulator` feature.
stack-encrypt's own tests turn it on with a dev-dependency on the crate
itself. The EQL encryption test crate turns it on in its `Cargo.toml`. A
change to how a read finds or walks entries changes the SQL and the
simulator together.
In v4 it carries v3's names for the searchable JSON document: the target
`JsonSearch` stores `public.eql_v4_json_search`, queried with
`eql_v4.query_json` and `eql_v4.query_json_leaf`, with entries
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/plans/2026-10-17-json-tree-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ The index, the SQL and the live oracle landed in the pull request stacked on thi
- *`AtPath<Eq>` is the containment of the needle document that holds the value at the path*, with a position in the path read as the wildcard, rather than a single `{vs}` entry: a leaf inside an array element sits in that element's group of the projection, and the needle has to say which group. Containment of that needle is equality only for a scalar, so a container value is refused (`TermError::JsonValueNotScalar`; `op::Contains` asks the containment question), and the needle carries no `alt`, so `("$", 1)` does not match a stored `[1]`.
- *The data-plan form and the catalog are not done.* The Go guest, `stashgen`, the target table and `v4::all()` do not know the family; `IndexSpec::Json` crosses the wire and is refused with a reason. The family is hand-written in `eql-bindings` as the SteVec structs are. Both are the next bindings issue.
- *A document is at most 127 levels deep*, under either root option. The sealed root's plaintext is the document as JSON, and `serde_json` parses JSON back only to 128 levels of nesting, so the 512 levels the walk first admitted sealed documents that then never opened. The bound is the reader's, so a document too deep fails when it is sealed, while the caller still holds it, and not when it is read.
- *A stored document is read as untrusted input.* Its chain fields are not authenticated, and whoever holds a token can write tags that form a cycle or a chain deeper than any sealed document. `JsonDocument::children` checks each entry at its own ordinal instead of looping over a fan-out the document chooses; `JsonDocument::subtree` and the SQL walks stop below 127 levels, and the Rust walk also stops once it has returned as many entries as the document holds; the rebuild of a subtree's rows refuses a repeated row and a nesting deeper than 127 levels as `OpenedType`. Each would otherwise have overflowed the stack, looped, or allocated what the document asked for. (Phase 3b replaced `children` with a probe from ordinal 0 to the first miss, bounded by the entry count, and derives a child's token from its ordinal by hash, so no stored chain can form a cycle and the SQL walks need no depth bound; the Rust walk keeps both of its bounds and the rebuild keeps its own.)
- *A stored document is read as untrusted input.* Its chain fields are not authenticated, and whoever holds a token can write tags that form a cycle or a chain deeper than any sealed document. `eql_simulator::json_tree_children` checks each entry at its own ordinal instead of looping over a fan-out the document chooses; `eql_simulator::json_tree_subtree` and the SQL walks stop below 127 levels, and the Rust walk also stops once it has returned as many entries as the document holds; the rebuild of a subtree's rows refuses a repeated row and a nesting deeper than 127 levels as `OpenedType`. Each would otherwise have overflowed the stack, looped, or allocated what the document asked for. (Phase 3b replaced `children` with a probe from ordinal 0 to the first miss, bounded by the entry count, and derives a child's token from its ordinal by hash, so no stored chain can form a cycle and the SQL walks need no depth bound; the Rust walk keeps both of its bounds and the rebuild keeps its own. #1755 moved these two Rust copies of the SQL reads out of `JsonDocument` and into the `stack_encrypt::eql_simulator` module, behind the `eql-simulator` feature.)
- *Nothing wipes the plaintext copies the walk makes* (the per-node scalars it clones before sealing), where the scalar paths hold theirs in `Zeroizing`. A `serde_json::Value` does not zeroize; a follow-up should walk the caller's value without copying scalars out of it. (2026-10-23: done. The JSON plaintext is held under vitaminc's `Protected` as the scalar paths hold theirs. The walk no longer copies a scalar or a key out of the caller's value: it encodes each node's plaintext straight into a `Protected<Vec<u8>>`, sized before it is written so it never reallocates, and hands that to the cipher, which takes a `Protected` anyway; the sealed root's JSON is measured first and written once into its own. On the way back, the bytes the cipher opens stay in its `Protected<Vec<u8>>` while they are parsed, and every key, scalar and sealed-root document read out of them is wrapped as it is made (`zip_ref` over the bytes), in `Protected<PlainJson>` for a value: `PlainJson` is a newtype over `serde_json::Value` whose `Zeroize` wipes every string, values and object keys alike, and overwrites numbers and booleans in place. A subtree is rebuilt inside a `Protected`, each member moved in from its own, and a value leaves the wrapper only where it is handed to the caller. The sealed-root fallback selects the walked path inside the document's `Protected`, so the whole document is wiped when the selection is made and only the selected nodes come out. What remains: numbers and booleans have no volatile overwrite without unsafe code, which this crate denies, so theirs is best-effort; and outside the decrypted plaintext, the query and term side still holds plaintext in places vitaminc does not reach from here: an accessor's needle document (`JsonPath::needle`), the canonical bytes of a value selector inside vitaminc's PAE `Context`, and the key names in each term node's positional path.)

## Implementation notes (2026-10-21, Phase 3: first measurements and the fixes they forced)
Expand Down
4 changes: 3 additions & 1 deletion packages/eql/tests/encryption/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ eql-domains = { path = "../../crates/eql-domains" }
# `dynamic`: `ValueFrame`, the runtime plaintext the target dispatch takes,
# as eql-bindings names it.
# `testing`: `assert_context!`, which pins the contexts of the EQL types.
stack-encrypt = { path = "../../../stack-encrypt", default-features = false, features = ["dynamic", "testing"] }
# `eql-simulator`: Rust copies of the EQL reads of a stored JSON document, so
# the tests that need no PostgreSQL read one without it.
stack-encrypt = { path = "../../../stack-encrypt", default-features = false, features = ["dynamic", "testing", "eql-simulator"] }
# `plaintext::Sink`, for a plaintext type with its own `Encrypt` impl; and
# jiff's and rust_decimal's own sources, which seal the leaf a date,
# timestamp or decimal from any other producer seals.
Expand Down
7 changes: 4 additions & 3 deletions packages/eql/tests/encryption/tests/json_tree_data_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use eql_bindings::v4::json_tree::{JsonTree, JsonTreeDocument, JsonTreeLeafQuery,
use eql_bindings::Identifier;
use serde_json::{json, Value};
use stack_encrypt::dynamic::{self, record, TargetError, ValueFrame};
use stack_encrypt::eql_simulator;
use stack_encrypt::json::{JsonDocument, JsonOptions, JsonPlaintext, JsonQuery};
use stack_encrypt::{CipherText, DecryptFrom, Encrypt, KeysetCipher, Label, NonEmpty};

Expand Down Expand Up @@ -883,10 +884,10 @@ fn subtree_rows(doc: &stack_encrypt::json::JsonDocument, tokens: &[String]) -> V
.unwrap()
})
.collect();
doc.subtree(&tokens)
eql_simulator::json_tree_subtree(doc, &tokens)
.into_iter()
.map(|(j, _, d)| {
let mut wire = serde_json::to_value(doc.node(j).unwrap()).unwrap();
let mut wire = serde_json::to_value(eql_simulator::entry_row(doc, j).unwrap()).unwrap();
wire["d"] = json!(d);
wire.to_string()
})
Expand All @@ -907,7 +908,7 @@ fn extracted_row(doc: &stack_encrypt::json::JsonDocument, selector: &str) -> Opt
.iter()
.position(|e| e.selector() == &selector)?;
Some(
serde_json::to_value(doc.node(j).unwrap())
serde_json::to_value(eql_simulator::entry_row(doc, j).unwrap())
.unwrap()
.to_string(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use eql_bindings::v4::json_tree::{
use eql_bindings::Identifier;
use serde_json::{json, Value};
use stack_encrypt::dynamic::{self, record, ValueFrame};
use stack_encrypt::eql_simulator;
use stack_encrypt::json::{JsonChildPlaintext, JsonDocument, JsonPlaintext, JsonQuery};
use stack_encrypt::sem::TermError;
use stack_encrypt::target::{DecryptSource, EncryptTarget};
Expand Down Expand Up @@ -255,7 +256,8 @@ async fn the_children_token_comes_with_the_key_that_opens_each_child() {
// depth below the node.
let document = &stored.profile;
let mut members = Vec::new();
for (slot, _, _) in index_document(document).children(&key.token_bytes()[0]) {
let indexed = index_document(document);
for (slot, _, _) in eql_simulator::json_tree_children(&indexed, &key.token_bytes()[0]) {
let mut entry = document.sv[slot].clone();
entry.h = Some(document.h.clone());
entry.j = Some(slot as u64);
Expand Down
24 changes: 24 additions & 0 deletions packages/stack-encrypt/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`operation`. That index would compile. The index's queries would then
send the stored value's tokens, and miss rows. No stored byte changes.

- **The JSON document read helpers moved to `eql_simulator`** (#1755).
`JsonDocument` had four methods that copy, in Rust, what the database
(EQL) does when your app reads part of a document. Production code does
not call them, because the database does that work. The four methods are
now functions in the module `stack_encrypt::eql_simulator`. The module is
behind a new feature, `eql-simulator`. The feature is off by default. Use
it in tests and examples that run without PostgreSQL.

| Removed method | Replacement | What it copies |
|---|---|---|
| `doc.children(token)` | `eql_simulator::json_tree_children(&doc, token)` | `eql_v4.json_tree_children` |
| `doc.subtree(tokens)` | `eql_simulator::json_tree_subtree(&doc, tokens)` | `eql_v4.json_tree_subtree(doc, tokens, 0)` |
| `doc.find(token)` | `eql_simulator::find_by_token(&doc, token)` | How EQL finds the entry a token names |
| `doc.node(slot)` | `eql_simulator::entry_row(&doc, slot)` | The row EQL returns for one entry |

Pass the document first, then the method's argument. Each function
returns what its method returned. To upgrade:

1. Turn the feature on in your dev-dependencies:
`stack-encrypt = { version = "0.4", features = ["eql-simulator"] }`.
2. Change each call to the function in the table.

No stored byte changes.

- **Seven `PlanError` variants that nothing raised are removed** (#1671).
Only the plan builder and `plan::all` raised them, and both are gone,
so no call returned them: `IdentityWithoutField`, `IndexNotDeclared`,
Expand Down
11 changes: 11 additions & 0 deletions packages/stack-encrypt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,19 @@ test-support = ["stack-kms/test-support"]
# declaration or a call in a test. No dependency; a dev-dependency on this
# crate with the feature on is enough.
testing = []
# `stack_encrypt::eql_simulator`: Rust copies of what the database (EQL)
# does to read a stored JSON document, so tests and examples run without
# PostgreSQL. Production code does not use it. No dependency; turn it on in
# a dev-dependency.
eql-simulator = []

[dev-dependencies]
# This crate's own tests and doc examples read stored JSON documents through
# `eql_simulator`. A dev-dependency on the crate itself turns the feature on
# for them under every feature set, and adds nothing else:
# `default-features = false` keeps `--no-default-features` builds free of
# `http`. A path-only dev-dependency is dropped when the crate is published.
stack-encrypt = { path = ".", default-features = false, features = ["eql-simulator"] }
# The types `value::{Date, Timestamp, Decimal}` are built from, for the
# tests and doc examples that build one.
jiff = { version = "1.0.0", default-features = false }
Expand Down
Loading
Loading