Skip to content

stack-encrypt: five JSON targets implement only the old EncryptFrom and DecryptInto — add EncryptTarget and DecryptSource #1660

Open

Description

@coderdan

Background

Stack Encrypt (packages/stack-encrypt) is the Rust engine that every CipherStash language SDK runs on. A target is the type a value is encrypted into. Stack Encrypt has two sets of traits that say how a target is encrypted and decrypted:

The JSON targets hold an encrypted JSON document that can be searched:

Stack Encrypt eql-bindings (the EQL v4 type) What it holds
JsonDocument JsonTreeDocument A whole document
JsonExtracted JsonTreeExtracted One entry of a document, as the -> operator returns it
JsonSubtree JsonTreeSubtree The rows of a read at a path

EQL is the Encrypt Query Language: the SQL library installed in the customer's database, which stores and queries encrypted values. eql-bindings is the Rust crate with the types for those values.

Problem

The old traits on these targets use serde_json::Value as the plaintext. serde_json::Value does not implement Zeroize, so it cannot be the plaintext of an owned trait. JsonPlaintext (packages/stack-encrypt/src/json/plaintext.rs:165) wraps a Value and wipes it when it is dropped. It is the plaintext type the owned traits can use.

#1659 gives JsonTreeDocument the owned traits over JsonPlaintext. The other five targets have only the old traits:

  • JsonDocument: EncryptFrom<serde_json::Value> (src/target/operations.rs:1307) and DecryptInto<Value> (src/json/derive.rs:1367).
  • JsonExtracted: DecryptInto<Value> (src/json/derive.rs:1406).
  • JsonSubtree: DecryptInto<Vec<Value>>, DecryptInto<Option<Value>> and DecryptInto<Value> (src/json/derive.rs:1481, 1498, 1514).
  • JsonTreeExtracted: DecryptInto<Value> (packages/eql/crates/eql-bindings/src/v4/json_tree.rs:633).
  • JsonTreeSubtree: DecryptInto<Value>, DecryptInto<Option<Value>> and DecryptInto<Vec<Value>> (json_tree.rs:648, 660, 672).

This has three effects:

  1. The old traits cannot be removed. These targets would have no way to encrypt or decrypt. This blocks the rest of stack-encrypt refactor: remove transitional traits and redundant execution APIs #1488.
  2. A caller who uses the owned API cannot read one entry or one path of a document. They must call the old decrypt_as.
  3. A document or a part of one that is decrypted through the old traits comes back as a plain Value. It is not wiped when it is dropped.

Nothing reports this today, because the old traits still compile and their tests pass.

Proposal

  1. In stack-encrypt, add the owned traits over JsonPlaintext:
    • JsonDocument: EncryptTarget<JsonPlaintext> and DecryptSource<JsonPlaintext>.
    • JsonExtracted: DecryptSource<JsonPlaintext>.
    • JsonSubtree: DecryptSource<JsonPlaintext>, DecryptSource<Option<JsonPlaintext>> and DecryptSource<Vec<JsonPlaintext>>.
  2. In eql-bindings, add the same DecryptSource implementations to JsonTreeExtracted and JsonTreeSubtree.
  3. Check the layout for a document sealed under options other than the defaults (Json::new(options) and JsonTree::new(options)). It is a Layout<Value> today. Give it a JsonPlaintext form if a caller of the owned API needs one.
  4. Keep the old implementations. stack-encrypt: EncryptFrom, DecryptInto, encrypt_as and decrypt_as are still a second way to encrypt — remove them and their derives #1661 deletes them.
  5. Test each new implementation against the old one: the same document, entry and path give the same answer and the same refusal. A document sealed through one trait opens through the other.

Acceptance

  • Each of the five targets has the owned traits listed above.
  • The stored bytes do not change. The frozen stored-bytes tests pass with no fixture change.
  • Each README or rustdoc example added for these traits runs as a test.
  • No test of a JSON target needs encrypt_as or decrypt_as to reach behaviour the owned API lacks.

Evidence

Line numbers are from commit 775d37645, the head of #1659.

Relationship to other work

Metadata

Metadata

Assignees

Labels

SDKrustPull requests that update Rust code

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions