Repository navigation
Conversation
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").
|
Contributor
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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>()andkinds. A declaration is plain, frozen data: a library readsuser.name,user.fieldsand each field'stype,kind,indexandnull. 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, asealednull on a field with an order index) is anErrof kinddeclarationthat 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)andkeyset.encrypt(user, rows)(one key request),cipher.decrypt(user, stored)andkeyset.decrypt(user, stored). Each row crosses as one frame, read by vitaminc'sJsRef, and runs throughPlanned::new(&plan, row)with the engine's collection path. Before that, each value is written at its field's kind: abigintat the field's integer width (refused outside it, naming the field), and anumberin afloat32field 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
ciphertextand one property per declared index (equality,match,ore,ope), each opaque bytes. A passthrough field keeps its value. A field declarednull: 'sql'isnullwhen the value is absent. This is built as one frame in the core crate and turned into JavaScript by vitaminc'sJsBuilder.One field.
keyset.encryptField(user, 'email', value)andcipher.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 abigint; a field that takes a null may benull) andStored<typeof user>. A row of another shape, a missing or extra field, a null where none is declared,stored.notes.equalityon a field with no index, andencryptFieldon an unknown or passthrough field do not compile.What the tests prove
Rust (
crates/core/tests/records.rs):NULLopens as a null.keyset.decrypt_with_context::<String>(ct, ("users", "email"))opens it, and the same call under("users", "name")refuses it.jsonfield 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;encryptFieldgiving 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.createrefusing one, an altered ciphertext).type-tests/records.tspins 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:debugthentest:addon: 50 passed.pnpm --filter @cipherstash/recipher test(unit suites andtscwith 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
jsonrecord field. The engine refuses a plan field of typejsonunless it names an EQL target ("ajsonfield names a target that stores a document"): a searchable document is EQL'sJsonSearch. Sokindshas nojsonin the core; it comes with the EQL extension (step 7). A test pins the engine's refusal.bigintat 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.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 namesPlaintextandStoredonly. Each kind opens as the type it takes (atimestamptakes and givesDate | 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.TargetResolveris not a parameter yet. The core crate runsPlanned::new, which usesNoTargets. The EQL extension (step 7) passes its resolver.Needs from the engine
Plannedcalls areSend" is answered yes: the core crate's record futures areSend + 'staticin their types, and napi'sspawn_future_with_callback, which requires it, compiles over them.Related
Refs #1747 (the plan). Based on #1775.