Repository navigation
feat(recipher-js): records through the engine's Planned #1776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
coderdan
wants to merge
17
commits into
next
Choose a base branch
from
feat/recipher-js-records
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ce0079f
feat(recipher-js): records through the engine's Planned
coderdan 7c0d0f1
fix(recipher-js): name Json and Value in a wrong argument's type error
coderdan bbfc95c
refactor(recipher-js): record types in record-types.ts, as the value …
coderdan ebc81f3
Merge branch 'feat/recipher-js-values' into feat/recipher-js-records
coderdan adf506e
Merge branch 'feat/recipher-js-values' into feat/recipher-js-records
coderdan b9766e2
Merge branch 'rjs-values-plain' into rjs-records-plain
coderdan 1591215
docs(recipher-js): plain language for the records prose
coderdan 7ba3aab
Merge branch 'feat/recipher-js-values' into feat/recipher-js-records
coderdan ea69a41
fix(recipher-js): error messages for records in the SDK's own words
coderdan 3cfffae
fix(recipher-js): merge `feat/recipher-js-values`, for the UUID text …
coderdan ca8c1b3
Merge feat/recipher-js-values into feat/recipher-js-records
coderdan 555320d
Merge feat/recipher-js-values into feat/recipher-js-records
coderdan 9aff924
fix(recipher-js): name a field's unknown null mode recipher_js::null_…
coderdan c6ad7ef
Merge feat/recipher-js-values into feat/recipher-js-records
coderdan f21233a
Merge feat/recipher-js-values into feat/recipher-js-records
coderdan 1c837c1
Merge next into feat/recipher-js-records
coderdan 2ce9f4b
feat(recipher-js)!: take the row first and the record in the options
coderdan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,7 @@ The value bridge converts JavaScript values in both directions: | |
| To read one with a kind, use `nullable(..)`: see [Stored bytes](#stored-bytes). | ||
| - A plain object is refused, because an object has no single stored form. | ||
| You choose the form. | ||
| `{ record }` in the options encrypts the object as a row of a record: see [Records](#records). | ||
| `json(..)` encrypts the object as a JSON document. | ||
| `opaque(..)`, for a whole object in the engine's own format, is not built yet. | ||
| - The package exports `RcUuid`, for a universally unique identifier (UUID). | ||
|
|
@@ -139,7 +140,8 @@ const opened = (await cipher.decrypt(age)).unwrap() | |
| - A value declared as `kinds.json` must be a `json(..)` value. | ||
| A `json(..)` value declared as another kind is refused. | ||
| Each is an `Err` with the code `recipher_js::json_kind`. | ||
| - The options take only `kind`. | ||
| - For one value, the options take only `kind`. | ||
| `record` and `field` are for a row: see [Records](#records). | ||
| Any other key is an `Err` with the code `recipher_js::options`, so an option that this version does not have is never ignored. | ||
| `null` as the options is refused with the same code. | ||
| - A kind from `kinds` takes no `null`, as a Rust type that is not an `Option` takes no `None`. | ||
|
|
@@ -254,6 +256,109 @@ const age = (await cipher.decrypt(stored)).unwrap() // bigint | null | |
| `nullable(..)` works with every kind when you read. | ||
| A stored null can exist for a value of any type: Rust's `encrypt_tree(None::<String>)` writes one. | ||
|
|
||
| ## Records | ||
|
|
||
| A record declares each field of a row once. | ||
| A field is encrypted, encrypted with indexes, or passed through unencrypted. | ||
|
|
||
| ```ts | ||
| import { Cipher, encrypt, kinds, passthrough, record } from '@cipherstash/recipher' | ||
|
|
||
| export const UserRecord = record('users', { | ||
| id: passthrough<bigint>(), | ||
| email: encrypt(kinds.string, { index: ['equality'] }), | ||
| age: encrypt(kinds.int32, { index: ['equality'], null: 'sealed' }), | ||
| }) | ||
|
|
||
| const cipher = (await Cipher.create({ records: [UserRecord] })).unwrap() | ||
| const keyset = cipher.defaultKeyset() | ||
|
|
||
| const user = { id: 1n, email: '[email protected]', age: 36n } | ||
|
|
||
| // One row, or an array of rows in one key request. | ||
| const stored = (await keyset.encrypt(user, { record: UserRecord })).unwrap() | ||
| const opened = (await cipher.decrypt(stored, { record: UserRecord })).unwrap() | ||
|
|
||
| // One field of a row. | ||
| const email = (await keyset.encrypt('[email protected]', { record: UserRecord, field: 'email' })).unwrap() | ||
| const sameEmail = (await cipher.decrypt(email, { record: UserRecord, field: 'email' })).unwrap() | ||
| ``` | ||
|
|
||
| - The first argument is the thing to encrypt, or the stored thing to decrypt. | ||
| The second argument is always an options object. | ||
| `{ kind }` is for one value. | ||
| `{ record }` is for a row. | ||
| `{ record, field }` is for one field of a row. | ||
| - `kind` and `record` together do not compile. | ||
| `field` without `record` does not compile, and a plain object with no `record` does not compile either. | ||
| When the code runs anyway, each is an `Err` whose help says what to pass. | ||
| The code is `recipher_js::options`, or `recipher_js::plain_object` for the plain object. | ||
| Any other key in the options is refused with `recipher_js::options` too. | ||
| - `keyset.encrypt(user, { record: UserRecord })` returns a stored row. | ||
| A stored row is the encrypted form of a row. | ||
| - A declaration is named for what it is, such as `UserRecord`. | ||
| A row gets the plain name, such as `user`. | ||
| - A declaration is a plain, frozen object: `{ type: 'record', name, fields }`. | ||
| A library can read the record's name, its fields, and each field's declaration from it. | ||
| - The package has an ECMAScript module (ESM) build and a CommonJS build. | ||
| A declaration made by either build works with a cipher from the other. | ||
| - Each encrypted field names its kind, by the engine's name for it. | ||
| The kinds are `kinds.string`, `kinds.boolean`, `kinds.int8` to `kinds.int128`, `kinds.uint8` to `kinds.uint128`, `kinds.float32`, `kinds.float64`, `kinds.decimal`, `kinds.date`, `kinds.timestamp`, and `kinds.bytes`. | ||
| - An integer field accepts a `bigint` at every width. | ||
| The field refuses a `bigint` outside its range. | ||
| - A context is data that a ciphertext is bound to. | ||
| The ciphertext decrypts only under the same context. | ||
| A field's context is its field path: the record's name, then the field's name. | ||
| The engine's typed Rust API decrypts the stored `email` field's ciphertext under `("users", "email")`. | ||
| - An index stores extra bytes, called index terms, beside a field's ciphertext. | ||
| A search can compare index terms without decrypting the field. | ||
| `equality` finds exact matches. | ||
| `match` finds text matches. | ||
| `ore` and `ope` compare order. | ||
| - The stored row has one object for each encrypted field. | ||
| The object has a `ciphertext` property. | ||
| The object also has one property for each index the field declares: `equality`, `match`, `ore`, or `ope`. | ||
| Each property is opaque bytes. | ||
| - A passthrough field keeps its value in the stored row. | ||
| - A field with `null: 'sealed'` encrypts a missing value like any other value. | ||
| A field with `null: 'sql'` is `null` in the stored row when the value is missing. | ||
| - A field with a null mode takes `null`, and decrypts as `T | null`. | ||
| A field with no null mode takes no `null`, and decrypts as `T`. | ||
| So the null mode does for a field what `nullable(..)` does for one value. | ||
| A field's kind is one of `kinds`. | ||
| `encrypt(nullable(kinds.int64))` does not compile. | ||
| When the code runs anyway, it is an `Err` with the code `recipher_js::nullable_field`. | ||
| - `Plaintext<typeof UserRecord>` is the type of a row. | ||
| `Stored<typeof UserRecord>` is the type of a stored row. | ||
| A row of another shape does not compile. | ||
| `stored.email.match` does not compile either, because `email` declares only the `equality` index. | ||
| - A passthrough field is **not encrypted and not authenticated.** | ||
| Anyone with the stored row can read it, change it, or remove it. | ||
| Use a passthrough field only for values that are not secret. | ||
| Treat the value read back as untrusted. | ||
| - The engine checks a declaration the first time a cipher uses it. | ||
| The engine then keeps the plan it built from the declaration. | ||
| - `Cipher.create({ records })` checks the declarations before it looks for credentials. | ||
| `checkDeclarations(records)` checks them with no credentials. | ||
| A refusal names the record in `error.record`. | ||
| Where there is a field at fault, the refusal names it in `error.field`. | ||
| - Node's `inspect` shows a decrypted row's field names and hides their values. | ||
| `console.log` uses `inspect`, so it hides the values too. | ||
| The values are ordinary properties. | ||
| `JSON.stringify` or a spread still shows them. | ||
|
|
||
| To update one field of a stored row, encrypt that field on its own, with `{ record, field }`. | ||
| An array of values gives an array of stored fields, with one key request: | ||
|
|
||
| ```ts | ||
| const emails = ( | ||
| await keyset.encrypt(['[email protected]', '[email protected]'], { record: UserRecord, field: 'email' }) | ||
| ).unwrap() | ||
| ``` | ||
|
|
||
| The stored field has the same form as that field in a whole stored row. | ||
| A passthrough field has no ciphertext, so its name is refused as a `field`. | ||
|
|
||
| ## Results, not exceptions | ||
|
|
||
| Every call returns, or resolves to, a `Result`. | ||
|
|
@@ -315,7 +420,7 @@ if (opened.isErr()) { | |
| | `help` | What to do about the failure, when the error has that advice. | | ||
| | `url` | A documentation page for the error, where it has one. | | ||
| | `severity` | `error`, unless the engine's report states `warning` or `advice`. | | ||
| | `fields` | The error's structured fields, by `snake_case` name. An example is a ZeroKMS failure's `request_kind`. | | ||
| | `fields` | The error's structured fields, by `snake_case` name. Examples are a refused declaration's `reason` and a ZeroKMS failure's `request_kind`. | | ||
| | `field` | The field at fault, where one is known. | | ||
| | `record` | The record at fault, where there is one. | | ||
| | `causes` | The errors behind this one, outermost first. Each cause has a `message`, and an engine error also has a `code`. A cause from another library has a fixed message, never that library's own text. | | ||
|
|
@@ -386,7 +491,7 @@ The ECMAScript module (ESM) build and the CommonJS build load the same addon, so | |
| These features are not built yet: | ||
|
|
||
| - `opaque(..)`, for a whole object; | ||
| - records; | ||
| - a `json` record field: this field needs `JsonSearch` from the Encrypt Query Language (EQL) extension for PostgreSQL; | ||
| - search terms; | ||
| - credentials passed in code; | ||
| - named keysets; | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,11 @@ const read = cjs.Ciphertext.fromBytes(sealed.toBytes()).unwrap() | |
| const fromCjs_ = cjs.Ciphertext.fromBytes(new Uint8Array(1)) | ||
| const fromEsm_ = esm.Ciphertext.fromBytes(new Uint8Array(1)) | ||
| const okCjs = cjs.Ciphertext.fromBytes(sealed.toBytes()) | ||
| const PersonRecord = cjs.record('people', { email: cjs.encrypt(cjs.kinds.string, { index: ['equality'] }) }) | ||
| const person = { email: '[email protected]' } | ||
| const row = (await fromEsm.defaultKeyset().encrypt(person, { record: PersonRecord })).unwrap() | ||
| const out = { | ||
| recordAcrossBuilds: (await fromEsm.decrypt(row, { record: PersonRecord })).unwrap().email, | ||
| sameClasses: esm.Cipher === cjs.Cipher && esm.Ciphertext === cjs.Ciphertext, | ||
| cipherIsCipher: fromCjs instanceof esm.Cipher && fromEsm instanceof cjs.Cipher, | ||
| ciphertextIsCiphertext: sealed instanceof cjs.Ciphertext, | ||
|
|
@@ -122,6 +126,7 @@ describe('the ESM and CommonJS builds in one process', () => { | |
| { encoding: 'utf8' }, | ||
| ) | ||
| expect(JSON.parse(output)).toEqual({ | ||
| recordAcrossBuilds: '[email protected]', | ||
| sameClasses: true, | ||
| cipherIsCipher: true, | ||
| ciphertextIsCiphertext: true, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the type of
stored? It'd be good to show what the returned encrypted value looks like in this example.