Skip to content

feat(stack-encrypt,eql-bindings,golang): open one child of a JSON node, bound to its parent - #1751

Merged
coderdan merged 4 commits into
nextfrom
feat/json-open-one-child
Nov 23, 2026
Merged

coderdan merged 4 commits into
nextfrom
feat/json-open-one-child

Conversation

@coderdan

@coderdan coderdan commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A program can now read one child of an encrypted JSON node without fetching the parent's whole subtree. It sends the parent's token to eql_v4.json_tree_children (the EQL SQL function that lists a node's children) and opens each returned entry on its own. Each one opens to its authenticated member key or array index together with its value. The client checks that each entry really is a child of the node it asked about. This works in Rust (stack-encrypt and eql-bindings), in the Go SDK, and in the eql-json-v1 conformance profile. No stored bytes change.

Changes

  • stack-encrypt engine:
    • New JsonSubtreeKey::open_child(node), which returns a JsonChild. A JsonChild opens to a JsonChildPlaintext: the child's key or index (key(), index(), held in Protected) and its value (value()), which is a scalar, or {} / [] for an empty container. Debug is redacted.
    • New JsonFieldQuery<T, Children, Ready>::encrypt_keyed, which returns the children token together with the key that opens each child.
    • It reuses the subtree rebuild's own checks. ChainWalk::node is split into place_row, open_row and value, and the parent-token check from the private child() becomes check_parent_link. The whole-subtree walk and the new single-child read call the same functions.
  • Refusals:
    • An entry with no parent tag (the root, or a top-level field under the sealed root) is refused before any key request.
    • A key for a path with a wildcard is refused before any key request (JsonPathWildcard).
    • A child that is itself a container with members is refused with JsonNotALeaf.
  • Dynamic interface: dynamic::json_open and check_json_open take {"operation":"children","path":…}, and write each child as {"key"|"index", "json"}. The Go guest's existing se_json_open export carries this, so there's no new export and no ABI change.
  • eql-bindings: new JsonTreeChild::from_key(&JsonSubtreeKey, JsonTreeEntry), the EQL equivalent of JsonTreeExtracted and JsonTreeSubtree.
  • Go SDK:
    • The generated Children(ctx, c, path) returns encrypt.JSONChildren[E]. It has Token(), Open(ctx, entries) ([]json.RawMessage, error), and a Value() that binds as the token, and it is redacted when printed or logged.
    • Children is now allowed through a cipher with a binding, like Selector and Subtree.
    • stashgen's emitter is updated, and the *_stash.go files are regenerated.
  • Conformance: a new requirement, json-child-v1, in eql-json-v1.enrollment.json. Its evidence comes from the engine, eql-typed, postgres, go and go-postgres suites. The contract table, README and coverage.md are updated to match.
  • Docs:
    • EQL json-tree.md describes opening a child; the old line saying it "cannot be done" is gone.
    • Go README, doc.go, the stashgen README, rustdoc, and the plan item are updated.
  • Changelogs: entries for stack-encrypt, eql-bindings and Go (including a Breaking entry for Go), plus a changeset for @cipherstash/eql.

Verification

  • stack-encrypt:
    • cargo test -p stack-encrypt --all-features passes, including the trybuild ui binary with no snapshot change.
    • clippy and rustdoc with -D warnings, and cargo fmt --check, are clean.
  • cargo-mutants --in-diff: 52 mutants, 17 caught, 0 missed, 35 unviable.
  • EQL:
    • cargo test -p eql-encryption-tests -p eql-bindings --all-features passes.
    • The v4 bundle, built from the tree into a throwaway PostgreSQL 15, passes all 33 json_tree PostgreSQL tests, including the new postgres_each_child_opens_against_its_parent.
  • Go:
    • All guests were rebuilt.
    • go test ./... passes, as do the nested encrypt/example and encrypt/encrypttest modules.
    • go generate ./... produces no drift.
    • golangci-lint reports 0 issues.
    • The new tests pass under STACK_CONFORMANCE_REQUIRED=1.
    • The Go PostgreSQL JSON tests pass against the same database.
  • Scripts: the conformance script tests pass.
  • Not run: the mise run test:conformance:eql[:postgres] wrapper scripts. The suites they name were run directly instead.
  • cargo doc -D warnings for eql-bindings fails only on errors that already exist on next.

Related

Closes #1677. Stacked on #1750. Builds on the typed JSON field reference methods from #1687.

Review notes

  • Names follow the existing JSON API.
    • The operation is "children" everywhere it already existed: the SQL json_tree_children, Op::Children, Go Children, and the Rust field reference's children(path).
    • The thing opened is a child. Rust has JsonSubtreeKey::open_child returning JsonChild, matching JsonSelector::open returning JsonExtracted.
    • Go has Children → JSONChildren.Open, matching Selector → JSONSelection.Open.
  • What "bound to its parent" checks: the token in the child's self tag must equal the token that its decrypted, authenticated label (key or index) derives under the parent's token. This is the same check the subtree rebuild already makes.
    • The stored parent tag is only required to be present, not recomputed: a member's ordinal is random and a single row doesn't carry it.
    • The tags are not authenticated. So a database that forges them, using the token it was sent, can substitute the same field under a sibling array element. This is the same limit the docs already state for the node at the top of a read, and it is documented on open_child.
  • Empty containers open to {} / [], unlike ->'s JsonExtracted, because jsonb_array_elements returns them.
  • Breaking for Go: Children now returns JSONChildren instead of JSONToken. JSONChildren binds as its token, so existing db.Query(…, token) calls keep working. Only code that keeps the result typed as JSONToken breaks; it calls .Token().
  • eql-bindings is in scope. EQL's own reference page said opening a child "is not implemented", and eql-bindings already mirrors the other two reads.

@coderdan
coderdan requested a review from a team as a code owner November 22, 2026 14:32
@changeset-bot

changeset-bot Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ab20b29

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
@cipherstash/eql Minor
stash Patch
@cipherstash/stack-prisma Patch
@cipherstash/basic-example Patch
@cipherstash/e2e Patch
@cipherstash/prisma-example Patch
@cipherstash/stack-drizzle Patch
@cipherstash/stack-supabase Patch
@cipherstash/stack Patch
@cipherstash/wizard Patch
@cipherstash/bench Patch
@cipherstash/test-kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI; Enforced overrides: Organization
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 00baecad-8247-4d2a-b0d4-15034aab8856

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human cryptographic review is needed to validate the new child-opening API and shared subtree verifier’s assurance boundary, since unauthenticated tags permit documented sibling-element substitutions.

No open issues 🎉

4 files not reviewed
  • languages/golang/encrypt/example/json/customer_stash.go: Generated file
  • languages/golang/encrypt/internal/testusers/customer_stash.go: Generated file
  • languages/golang/encrypt/internal/testusers/entity_stash.go: Generated file
  • languages/golang/encrypt/internal/testusers/subscriber_stash.go: Generated file
What changed in this PR

Adds single-child JSON reads across Stack Encrypt, eql-bindings, and the Go SDK, addressing #1677 without fetching the parent’s whole subtree. Reads reuse existing parent-link checks; stored bytes, SQL, and the guest ABI remain unchanged.

Changes:

  • Adds Rust JsonChild and JsonTreeChild types, plus dynamic children opening support.
  • Changes Go’s generated Children method to return JSONChildren, with token binding, opening, and redacted logging; callers needing JSONToken use .Token().
  • Adds child-read tests and conformance enrollment, and updates documentation and release notes.
File Description
scripts/​__tests__/​stack-eql-conformance.test.mjs Requires the new child-read conformance entry.
packages/​stack-encrypt/​tests/​json_read_checks.rs Tests child values, parent checks, and refusals.
packages/​stack-encrypt/​tests/​json_operators.rs Tests dynamic child reads and input validation.
packages/​stack-encrypt/​src/​refusal.rs Documents child-group input refusals.
packages/​stack-encrypt/​src/​op/​mod.rs Documents opening children returned by the operator.
packages/​stack-encrypt/​src/​json/​query.rs Adds JsonSubtreeKey::open_child and its guarantees.
packages/​stack-encrypt/​src/​json/​mod.rs Defines the prepared JsonChild type.
packages/​stack-encrypt/​src/​json/​derive.rs Implements child decryption using shared walk checks.
packages/​stack-encrypt/​src/​field_reference/​json.rs Documents opening children through field-reference keys.
packages/​stack-encrypt/​src/​dynamic/​query.rs Documents the dynamic child-read contract.
packages/​stack-encrypt/​src/​dynamic/​json_query.rs Parses and opens dynamic children requests.
packages/​stack-encrypt/​src/​cipher.rs Extends non-leaf error guidance to child reads.
packages/​stack-encrypt/​CHANGELOG.md Records the engine API and refusal behavior.
packages/​eql/​tests/​encryption/​tests/​json_tree.rs Adds typed and PostgreSQL child-read coverage.
packages/​eql/​docs/​reference/​json-tree.md Replaces the old limitation with child-opening guidance.
packages/​eql/​crates/​eql-bindings/​src/​v4/​json_tree.rs Adds JsonTreeChild and delegates opening to the engine.
packages/​eql/​crates/​eql-bindings/​CHANGELOG.md Records the typed child-opening API.
languages/​golang/​stashgen/​testdata/​stubsdk/​encrypt/​gensupport/​gensupport.go Adds the generator-support stub.
languages/​golang/​stashgen/​testdata/​stubsdk/​encrypt/​encrypt.go Adds the JSONChildren SDK stub.
languages/​golang/​stashgen/​emit.go Generates prepared Children methods.
languages/​golang/​encrypt/​records.go Allows preparing child reads through bound ciphers.
languages/​golang/​encrypt/​README.md Adds child-read examples and usage guidance.
languages/​golang/​encrypt/​query.go Documents binding support for child-read operands.
languages/​golang/​encrypt/​json.go Implements JSONChildren, opening, binding, and redaction.
languages/​golang/​encrypt/​json_tree_test.go Tests guest child reads and scoped operands.
languages/​golang/​encrypt/​json_postgres_test.go Tests generated child reads against PostgreSQL.
languages/​golang/​encrypt/​json_fields_test.go Tests binding, zero values, redaction, and opening.
languages/​golang/​encrypt/​internal/​testusers/​subscriber_stash.go Regenerates subscriber child-read methods.
languages/​golang/​encrypt/​internal/​testusers/​entity_stash.go Regenerates the entity child-read method.
languages/​golang/​encrypt/​internal/​testusers/​customer_stash.go Regenerates customer child-read methods.
languages/​golang/​encrypt/​guest/​src/​ops.rs Documents children on the existing guest export.
languages/​golang/​encrypt/​gensupport/​json.go Prepares typed child reads for generated code.
languages/​golang/​encrypt/​example/​json/​customer_stash.go Regenerates the example child-read method.
languages/​golang/​encrypt/​doc.go Documents child reads through bound ciphers.
languages/​golang/​cmd/​stashgen/​README.md Updates generated API return types.
languages/​golang/​CHANGELOG.md Records functionality and the Go migration path.
docs/​plans/​2026-10-24-stack-encrypt-api-plan.md Records implementation of single-child opening.
conformance/​stack-encrypt/​v1/​eql-json-v1.enrollment.json Enrolls cross-language child-read evidence.
conformance/​stack-encrypt/​README.md Includes child reads in the profile summary.
conformance/​stack-encrypt/​eql-json-contract.md Defines the json-child-v1 requirement.
conformance/​stack-encrypt/​coverage.md Adds child reads to documented coverage.
.changeset/​eql-json-tree-open-one-child.md Adds an EQL minor-release changeset.

Review effort: Balanced

@cipherstash-agent cipherstash-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review attestation

Reviewed c146ba53 · posted from Claude Code (scheduled routine) · claude-opus-5-5 · effort medium
Sources: code-review · claude-opus-5-5; mattpocock-skills:code-review (Standards and Spec axes) · claude-opus-5-5; rust-security agent · claude-opus-5-5 (no findings)
Skills: code-review, mattpocock-skills:code-review, company-skills:rust-security, company-skills:review-cross-check, company-skills:review-post

Every finding below was re-verified against c146ba53 and cross-checked against existing feedback (none carried findings).

Findings (10): 1 major, 9 minor — 8 inline, 2 below.

  • docs/plans/2026-10-24-stack-encrypt-api-plan.md:973 (outside diff) minor Target state still says "A Go program cannot open one child of a JSON node, bound to its parent" under "Two parts … not in Go yet", which this PR's Remaining-work entry (1287-1295) marks done. Drop the bullet and make the intro singular.
  • docs/plans/2026-10-24-stack-encrypt-api-plan.md:966 (outside diff) minor "An upgrade alone does not change a committed *_stash.go file, apart from the one regeneration the handle rename needs" (repeated near 1763) is now false: the generated Children changes for every JsonSearch field. Name this as a second regeneration or reword as a target rule.

Source: code-review · claude-opus-5-5


Generated by Claude Code

Comment thread languages/golang/encrypt/README.md Resolved
Comment thread languages/golang/encrypt/json.go Resolved
Comment thread languages/golang/encrypt/records.go Resolved
@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Mutation testing (cargo-mutants, --in-diff, stack-auth + stack-encrypt + stack-encrypt-jsonpath)

caught missed unviable timeout
26 0 48 0

Every mutant in the changed lines was caught by a test.

@coderdan
coderdan force-pushed the fix/stack-encrypt-wipe-partial-serde-decode branch from aafa693 to 375e611 Compare November 22, 2026 15:15
…e, bound to its parent

Reading one child of a JSON node needed the parent's whole subtree: the
SQL returns a node's children (eql_v4.json_tree_children), but nothing
opened one of them and checked it belonged to the node the caller asked
about. So a program fetched and opened the parent's subtree, which
transfers more data and shows the server a different set of tokens.

The engine now opens one entry json_tree_children returned against the key
of its parent's path. JsonSubtreeKey::open_child(node) gives a JsonChild,
which opens to the child's value (a scalar, or {} or [] for an empty
container). The open reuses the subtree rebuild's own checks rather than a
second copy: ChainWalk::node is split into place_row, open_row and value,
and the parent-link check it made in child() is check_parent_link, which
both the rebuild and the lone child call. A lone child is placed one level
down, opened under the digest of no children (a container with children
does not open alone: JsonNotALeaf), its selector checked as its parent's
path extended by its decrypted label, and its self tag checked as the token
that label derives under the parent's token. An entry with no parent tag,
and a key of a path with a wildcard, are refused before any key request.

The dynamic interface's json_open takes the operation "children" (one
entry or none per group), so the Go guest's existing se_json_open export
carries it and no export is added. eql-bindings has JsonTreeChild, the
EQL-typed twin of JsonTreeExtracted and JsonTreeSubtree.

In Go, the generated Children now returns encrypt.JSONChildren: Token()
to bind and Open(ctx, entries) to open each child. It binds as its token,
so code that passed the old JSONToken to a query keeps working. Children
prepares a read, so like Selector and Subtree it is no longer refused
through a cipher with a binding; ChildCount still is.

The eql-json-v1 conformance profile enrolls json-child-v1, with Rust
engine, EQL, Go and PostgreSQL evidence. No stored bytes change.

Closes #1677
… child

A child opened on its own now says which child of its parent it is.
json_tree_children returns an object's members in a random order, and
the opened child carried only its value, so a caller could not tell one
member from another. The engine already decrypts and checks the child's
label (its key, or its index); it now returns it.

- stack-encrypt: JsonChild opens to a JsonChildPlaintext, with key(),
  index() and value(). The key and the index are held in Protected and
  the value in a JsonPlaintext, so all three are wiped on drop, and Debug
  is redacted. dynamic::json_open writes each child as a map,
  {"key", "json"} or {"index", "json"}.
- eql-bindings: JsonTreeChild opens to the same JsonChildPlaintext.
  children(path).encrypt_keyed(&keyset) returns the token to bind with
  the JsonSubtreeKey that opens what it returns, from one derivation, so
  an EQL caller no longer derives the key a second time.
- Go: JSONChildren.Open returns []JSONChildValue, with Key() and Index()
  (comma-ok) and the JSON text, printed and logged redacted.
  Cipher.JSONOpen refuses the children operation, whose values it cannot
  carry. The three prepared reads print and log through one embedded
  redactedRead instead of three copies of four methods.
- Go: the read operators (selector, subtree, children) are constants in
  internal/record with IsJSONRead, used by operandPlan, gensupport and
  each read's Open. A test asks the engine which operators json_open
  opens and checks that set against IsJSONRead.
- Go: gensupport.JSONNodeToken derives as child_count, so a *_stash.go
  generated before JSONChildren, whose Children calls it with children,
  is still refused through a cipher with a binding. The token is the
  same. Other operators are refused.
- Docs: the Go README, CHANGELOG bullets and the generated Children
  comment are split into short sentences; the EQL leakage reference
  describes a child read; the API plan no longer lists the child read as
  missing from Go, and names the Children change a regeneration picks up.
- Conformance: json-child-v1 enrolls the depth refusal and the new Go
  tests.

No stored bytes change.
@coderdan
coderdan force-pushed the feat/json-open-one-child branch from c146ba5 to 3c584e6 Compare November 22, 2026 16:03
@coderdan

Copy link
Copy Markdown
Contributor Author

Review round 1, addressed in 3c584e6. The branch was also rebased onto #1750's new head and force-pushed.

  • Opening a child now returns its key or index with its value (the major finding). New public names, chosen to match the existing JSON API:
    • Rust JsonChildPlaintext, with key(), index() and value(). The key and index are held in Protected, and Debug is redacted.
    • Go JSONChildValue, with comma-ok Key() and Index() and a JSON field. It prints and logs redacted, because a member key can be customer data.
    • Rust JsonFieldQuery<T, Children, Ready>::encrypt_keyed, which returns the token together with the key that opens its children.
    • "Label" is avoided in these public names, because Label already means a context label.
  • Cipher.JSONOpen now refuses the children operation with ErrEncoding, because its byte-slice result can't carry the key. That path is new in this PR and unreleased.
  • API plan (~973, ~966, ~1763), from the review body: updated as the review asked.
  • Copilot's request for a human cryptographic review: agreed, and it's left for the maintainer. The limit is stated in three places: JsonSubtreeKey::open_child ("Not covered, by design"), JSONChildren.Open, and packages/eql/docs/reference/json-tree-leakage.md. That page now has a "child read" bullet and names the sibling-element substitution under "What the checks do not cover".
  • CI, "WASI check, guests and Go (Linux)": the unused import came from refactor(stack-encrypt)!: remove the seven PlanError variants nothing raises #1749, which fixes it. The fix is under this branch now.

Checks, run on the stack at #1752's head, which includes this PR:

  • cargo fmt passes.
  • stack-encrypt clippy with -D warnings passes with all features and with no default features.
  • cargo test -p stack-encrypt passes with all features and with no default features.
  • eql-bindings, eql-codegen and eql-encryption-tests pass their tests and clippy.
  • All guests were rebuilt. go test ./... passes, and go generate produces no drift.

The fix agent's own run, before the restack:

  • cargo-mutants on the diff: 26 mutants, 10 caught, 16 unviable, 0 missed.
  • The 33 EQL json_tree PostgreSQL tests and the Go PostgreSQL JSON tests pass against a throwaway PostgreSQL.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Partial decryption and the shared verifier refactor need human cryptographic review of unauthenticated-tag substitution limits, while cross-language and PostgreSQL validation remains independently unverified.

No open issues 🎉

4 files not reviewed
  • languages/golang/encrypt/example/json/customer_stash.go: Generated file
  • languages/golang/encrypt/internal/testusers/customer_stash.go: Generated file
  • languages/golang/encrypt/internal/testusers/entity_stash.go: Generated file
  • languages/golang/encrypt/internal/testusers/subscriber_stash.go: Generated file

Review effort: Balanced

Base automatically changed from fix/stack-encrypt-wipe-partial-serde-decode to next November 22, 2026 22:59

@coderdan coderdan left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some open questions and API improvements warranted.

The plain-language checker reads a project's own names from .plain-language.json and treats each as one word, so product names do not read as jargon or unexplained abbreviations.
…alue, and explain the read in plain words

A child of a JSON node could only decrypt into a JsonChildPlaintext, so a
caller who wanted the value had a different type from a whole-document or
subtree read. A JsonChild, and eql-bindings' JsonTreeChild, now also
decrypt into a JsonPlaintext. Both targets share one code path: the
label is still decrypted and checked, then wiped, and only the value is
returned. JsonChildPlaintext stays for a caller that needs the member's
key or the element's index, since the database returns an object's
members in a random order.

JsonChildPlaintext's Debug is now derived with vitaminc's OpaqueDebug in
place of a hand-written impl. JsonPlaintext keeps its own Debug: the
derive prints a different string for it, so it is not a drop-in.

The docs this change set added are rewritten in plain words. Each one
that introduces the child read walks the same three steps: your app
derives the parent's token and key, the database (EQL) returns the
parent's children as encrypted entries, and your app checks and decrypts
each one, fetching the data key from ZeroKMS. The doc examples that read
entries from a stored JsonDocument say they stand in for the database,
and show the SQL that returns those entries in production.

Tests through the public API cover the new target: each value opens, and
every wrong entry is refused with the same error for both targets.
@coderdan
coderdan merged commit ec95e61 into next Nov 23, 2026
76 of 78 checks passed
@coderdan
coderdan deleted the feat/json-open-one-child branch November 23, 2026 01:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Go SDK and stack-encrypt: reading one child of a JSON node needs the whole subtree — open one child, bound to its parent

3 participants