Repository navigation
Description
Background
Stack Encrypt is the Rust engine in packages/stack-encrypt. It has two ways to encrypt:
- A record encrypts several fields under a plan. Each field's plan says how it stores a null (
NullModeinpackages/stack-encrypt/src/dynamic/record.rs). With"null": "sealed", a null of a fixed-width type is sealed as a typed null. That null is as long as a value of the type, so the stored record does not show which rows are empty. The fixed-width types are a boolean, a number, a date, a timestamp, a decimal, and a UUID. - A single value encrypts one scalar into one
Ciphertext. In Rust the call iskeyset.encrypt(value). For a value whose type is known only at run time, it isdynamic::value::encrypt(keyset, frame, kind)(feat(stack-encrypt)!: encrypt and decrypt one value known only at run time, and bound the typed decrypt to a scalar #2011).
Problem
The single-value path refuses every null, so a customer cannot leave a single encrypted value empty without leaving the engine.
- Rust's typed call:
keyset.encrypttakes only anOpaqueEncodetype, andOption<T>is not one (packages/stack-encrypt/src/scalar.rs). - The run-time call:
dynamic::value::encryptrefuses every null withError::Value { reason: not_a_scalar, found: null }(packages/stack-encrypt/src/dynamic/value.rs, the "What is refused" section). It refuses a typed null too, even with a declared fixed-width kind. A record field would seal that null. - The SDKs inherit it: the TypeScript SDK (feat(recipher-js): every value type, arrays and JSON documents #1775) encrypts a lone
nulltoday, and routing it through feat(stack-encrypt)!: encrypt and decrypt one value known only at run time, and bound the typed decrypt to a scalar #2011's call would take that away.
Decrypt already reads a null: Option<T> is a target of keyset.decrypt, and a null leaf opens as None.
Every data type needs empty values, and where a null can be sealed safely it should be. Records follow this rule already.
Proposal
One PR to next:
-
Run-time call.
dynamic::value::encryptseals a null as a padded typed null in two cases:- a typed null frame of a fixed-width type;
- a plain null with a declared fixed-width kind.
It is sealed exactly as a record's
"null": "sealed"field seals one. The leaf is the same, it is as long as a value of the type, and it opens as a null.Two kinds of null are still refused: a plain null with no declared kind, and a null of a variable-width kind (text, bytes, JSON). The refusal's reason says why, and says to store a database
NULL. -
Rust's typed call.
Option<T>can be encrypted withkeyset.encryptwhenTis a fixed-width scalar, withNonesealed asT's typed null.Option<String>and other variable-width types stay refused at compile time, with a#[diagnostic::on_unimplemented]message saying why. -
The same ciphertext both ways. The run-time call's typed null and the typed call's
None::<T>give the same leaf plaintext and the same length. Each decrypts as the other's null. -
Conformance. Add accepted cases to the
single-value-v1profile for each fixed-width typed null. Keep its refusal cases for an untyped null and a variable-width null. -
Docs and CHANGELOG (stack-encrypt
[0.3.0]).
Relationship to other work
- feat(stack-encrypt)!: encrypt and decrypt one value known only at run time, and bound the typed decrypt to a scalar #2011: added the single-value run-time call this changes.
- feat(recipher-js): every value type, arrays and JSON documents #1775: the TypeScript SDK follows this rule once it lands. A lone untyped
nullis refused; a null with a declared fixed-width kind is sealed. - Go SDK: encrypt one value into one ciphertext, as Rust and TypeScript do #2010: the Go SDK's whole-value encrypt follows the same rule.
Activity
feat(recipher-js)!: encrypt one value through dynamic::value, with a …
Merge feat/recipher-js-records into feat/recipher-js-uuid