Skip to content

feat(recipher-js): records through the engine's Planned - #1776

Draft
coderdan wants to merge 1 commit into
feat/recipher-js-valuesfrom
feat/recipher-js-records
Draft

coderdan wants to merge 1 commit into
feat/recipher-js-valuesfrom
feat/recipher-js-records

Conversation

@coderdan

Copy link
Copy Markdown
Contributor

Summary

Step 3 of phase 1 of the TypeScript SDK plan (#1747), on top of #1775. A program declares a record once (each field encrypted, encrypted and indexed, or passed through) and encrypts and decrypts whole rows of it, or one field at a time. Rows go through the engine's dynamic::Planned, so the stored bytes are the engine's, and many rows make one key request.

A record here is a declaration like a table's: a name, and for each field what is stored. An index is a search term stored beside a field's ciphertext (an equality term, a match term, an order term). Querying with those terms is step 4.

The package is still private and takes no changeset.

Changes

Declaring. record(name, fields), encrypt(kinds.x, { index, null }), passthrough<T>() and kinds. A declaration is plain, frozen data: a library reads user.name, user.fields and each field's type, kind, index and null. The kinds are the engine's (string, bool, int8…uint128, float32, float64, decimal, date, timestamp, bytes).

The plan. The core crate writes the engine's plan from the declaration (crates/core/src/record.rs). Each field's context is its field path, [record name, field name], as the plan's phase 1 says. The plan is read by the engine's own reader (dynamic::record::plan), so a declaration the engine refuses (a match index on an integer, an order index on a boolean, a sealed null on a field with an order index) is an Err of kind declaration that names the field. A plan is built once per declaration and cached. Cipher.create({ records }) checks declarations up front; checkDeclarations(records) checks them with no credentials.

Rows. keyset.encrypt(user, row) and keyset.encrypt(user, rows) (one key request), cipher.decrypt(user, stored) and keyset.decrypt(user, stored). Each row crosses as one frame, read by vitaminc's JsRef, and runs through Planned::new(&plan, row) with the engine's collection path. Before that, each value is written at its field's kind: a bigint at the field's integer width (refused outside it, naming the field), and a number in a float32 field only when it is exact at 32 bits. The engine's value model checks every value by exact kind, and JavaScript has no integer widths, so this step is needed for any integer field.

The stored row. For each sealed field, an object with ciphertext and one property per declared index (equality, match, ore, ope), each opaque bytes. A passthrough field keeps its value. A field declared null: 'sql' is null when the value is absent. This is built as one frame in the core crate and turned into JavaScript by vitaminc's JsBuilder.

One field. keyset.encryptField(user, 'email', value) and cipher.decryptField(user, 'email', stored) run a plan of that one field, under the same field path. So a field encrypted alone has exactly the form and bytes it has in a whole row, and a whole row's field opens alone.

Types. Plaintext<typeof user> (an integer field is a bigint; a field that takes a null may be null) and Stored<typeof user>. A row of another shape, a missing or extra field, a null where none is declared, stored.notes.equality on a field with no index, and encryptField on an unknown or passthrough field do not compile.

What the tests prove

Rust (crates/core/tests/records.rs):

  • A row round-trips; the field stored as SQL NULL opens as a null.
  • The stored row has exactly the outputs declared.
  • A field's ciphertext is sealed under its field path: the engine's typed keyset.decrypt_with_context::<String>(ct, ("users", "email")) opens it, and the same call under ("users", "name") refuses it.
  • One field opens alone (a stored row holding only that field's ciphertext, through the field's plan), and a field encrypted alone opens through the engine's typed API under its field path. This answers one of the plan's unchecked questions; see below.
  • Three rows make one key request to encrypt and one to decrypt (a counting key source).
  • An integer is written at its field's width and refused outside it; the engine refuses a bad declaration naming the field; a json field is refused (see deviations).

TypeScript (__tests__/addon/records.test.ts, 15 tests): round trips of one row and of many; the stored shape; the same equality term for the same value and different ciphertexts; sealed and SQL nulls; every kind; encryptField giving the same equality term a whole row stores, and a whole row taking the updated field; refusals (out of range, a null where none is declared, an extra field, a declaration the engine refuses, Cipher.create refusing one, an altered ciphertext). type-tests/records.ts pins the types above.

Verification

Run locally on macOS (arm64), Rust 1.94.1, Node 24, pnpm 10.33.2:

  • cargo fmt --all --check: passed.
  • cargo clippy --locked --all-targets --all-features -- -D warnings: passed.
  • cargo test --locked --all-features: 40 passed (15 unit, 3 boundary, 8 record, 7 round-trip, 7 value).
  • pnpm --filter @cipherstash/recipher build:debug then test:addon: 50 passed.
  • pnpm --filter @cipherstash/recipher test (unit suites and tsc with the type tests): 16 passed, typecheck clean.
  • pnpm run code:check: no errors.
  • pnpm run test:scripts: 96 files passed.

Not run: anything against a live ZeroKMS, and the conformance profiles (see deviations).

Deviations from the plan

  • No json record field. The engine refuses a plan field of type json unless it names an EQL target ("a json field names a target that stores a document"): a searchable document is EQL's JsonSearch. So kinds has no json in the core; it comes with the EQL extension (step 7). A test pins the engine's refusal.
  • Integers are written at the field's width in the core crate. The plan says "the kind checks the value". The engine checks by exact kind, and vitaminc reads a bigint at its narrowest width, so the core crate converts each integer to the field's width first, and refuses one outside it. This is the only conversion of values in the SDK, and it is between frames in Rust.
  • The conformance profiles are not run (record-declarations-v1, the runtime and read profiles). There is no TypeScript conformance runner yet (stack-encrypt conformance: add TypeScript runner and public SDK mapping tests #1479). The tests above check the same engine behaviour directly.
  • Plaintext<R> is also the opened row's type. The plan names Plaintext and Stored only. Each kind opens as the type it takes (a timestamp takes and gives Date | VcTimestamp), so one type serves both.
  • Cipher.create({ records }) checks declarations; it does not build plans per cipher. A plan does not depend on the cipher, so it is built once per declaration and shared.
  • The engine's TargetResolver is not a parameter yet. The core crate runs Planned::new, which uses NoTargets. The EQL extension (step 7) passes its resolver.

Needs from the engine

  • Nothing blocking for this step. The plan's unchecked question "that one field of a stored row opens alone" is answered yes, by a plan of that one field under the same field path: the engine opens a stored row holding only that field, and a field sealed alone opens under its field path through the typed API. Index terms are not needed to decrypt.
  • The plan's unchecked question "that pending Planned calls are Send" is answered yes: the core crate's record futures are Send + 'static in their types, and napi's spawn_future_with_callback, which requires it, compiles over them.

Related

Refs #1747 (the plan). Based on #1775.

Declare a record once (record, encrypt(kinds.x, { index, null }),
passthrough), and encrypt and decrypt its rows through
dynamic::Planned, one key request however many rows.

The core crate writes the engine's plan from the declaration, giving
each field its field path as its context: the record's name, then the
field's name. The engine's own plan reader checks it, so a refusal is
the engine's and names the field. Before a row reaches the engine,
each value is written at its field's kind: a bigint at the field's
integer width, refused outside it.

The stored row has, for each sealed field, its ciphertext and its
index terms as bytes; a passthrough field keeps its value, and a field
stored as SQL NULL is null. Plaintext<typeof r> and Stored<typeof r>
type both, and type tests pin them.

encryptField and decryptField run a plan of the one field, under the
same field path, so a field encrypted alone has the bytes it has in a
row: the engine's typed decrypt_with_context opens it under
("users", "email").
@changeset-bot

changeset-bot Bot commented Nov 23, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 82b9483

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@coderabbitai

coderabbitai Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

This branch has not been deployed

No deployments
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.

1 participant