Skip to content

stack-encrypt: single-value encrypt refuses every null — seal a typed fixed-width null as records do #2023

Backlog

Description

@coderdan

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 (NullMode in packages/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 is keyset.encrypt(value). For a value whose type is known only at run time, it is dynamic::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.

  1. Rust's typed call: keyset.encrypt takes only an OpaqueEncode type, and Option<T> is not one (packages/stack-encrypt/src/scalar.rs).
  2. The run-time call: dynamic::value::encrypt refuses every null with Error::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.
  3. The SDKs inherit it: the TypeScript SDK (feat(recipher-js): every value type, arrays and JSON documents #1775) encrypts a lone null today, 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:

  1. Run-time call. dynamic::value::encrypt seals 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.

  2. Rust's typed call. Option<T> can be encrypted with keyset.encrypt when T is a fixed-width scalar, with None sealed as T's typed null. Option<String> and other variable-width types stay refused at compile time, with a #[diagnostic::on_unimplemented] message saying why.

  3. 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.

  4. Conformance. Add accepted cases to the single-value-v1 profile for each fixed-width typed null. Keep its refusal cases for an untyped null and a variable-width null.

  5. Docs and CHANGELOG (stack-encrypt [0.3.0]).

Relationship to other work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    RustSDKenhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions