Repository navigation
Conversation
A binding moves the stored record to its language and back. Until now, each binding walked the tree itself, and the TypeScript binding renamed its keys. Stack Encrypt now does this once, for every binding. dynamic::record::stored_frame writes a stored record, or a batch, as a frame under the record's own keys. read_stored reads the frame back. It takes the plan, because only the plan says which output is sealed. Two more functions do the same for one field's node. vitaminc-aead gains Frame::entries. It reads a map's entries with no copy, as Frame::elements reads a sequence's elements. Closes #2029.
|
|
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 |
…namic module docs
coderdan
left a comment
There was a problem hiding this comment.
Review at medium effort. The review found no bug in the conversion. There are four minor findings, posted inline.
Two more points, from reading the PR's report:
- Add the output key to
Error::Record. The PR leaves it out because the Go guest's tests build that variant with every field. That is a workaround. A test that has to change is not a reason to keep an error less useful. Add the slot, and update the Go guest's tests in the same PR. - Add a conformance profile now, with the Go runner marked pending. #2030 does this for
record-fields-v1, assingle-value-v1did before it. Cases for the frame form fix that form for the next binding, including the newempty_seqandempty_mapmarkers.
Each will be fixed or answered in a reply.
| records | ||
| .enumerate() | ||
| .map(|(at, record)| match record.entries() { | ||
| Some(fields) => read_record(fields, plan).map(CipherText::Map), |
There was a problem hiding this comment.
Nit: in a batch, a refusal names no row.
read_stored and stored_frame set row only for NotAnObject. The other refusals from read_record and write_record drop it: UnknownField, UndeclaredOutput, NotSealed, OutputsNotMap, and the rest.
Example: a binding sends a batch of 500 rows, and row 312 has an extra output foo. The error is Error::Record { field: Some("email"), row: None, reason: UndeclaredOutput }, so the caller cannot tell which row to fix.
Fix: in the batch loop, attach the row index to the error (row: Some(at)). decrypt's record_rows has the same gap, so this repeats an existing one.
There was a problem hiding this comment.
Fixed in 8eb4d74.
The batch loops of read_stored and stored_frame now put the row on every refusal of a row, not only on not_an_object. In your example, the error is now { field: "email", output: "foo", row: 312, reason: "undeclared_output" }.
Two new tests check this, one for each direction: it_names_the_row_of_a_refusal_in_a_batch in reading_a_frame_back and in writing_a_frame. I ran both against the code with the row step disabled first, and both failed.
I did not change decrypt's record_rows. It is not the same small change:
record-reads-v1stores every record as a batch, and both runners compare each refusal's payload exactly.- With the row added, the payload of every field refusal in that profile changes: it has
"row": 0. - So the change also needs the corpus updated, the Go runner rerun against rebuilt guests, and the error docs changed for
decryptandcheck_record.
I suggest a separate issue for it.
| //! | ||
| //! ## Refusals | ||
| //! | ||
| //! Each refusal is [`Error::Record`], and names the field. The one |
There was a problem hiding this comment.
Nit: the docs say that every refusal names the field, but several do not.
These refusals have field: None:
- the errors of
writer.map(..)andrecord.end()inwrite_record; - the batch's
seq,element, andenderrors (Error::bad_record(Reason::Unframeable)); NotAnObject;Error::Internal.
The CHANGELOG entry makes the same claim.
Example: a binding author trusts the docs, and unwraps error.field() for each Error::Record from stored_frame. A batch nested too deep returns field: None, and that code panics.
Fix: say "names the field when there is one", as the functions' # Errors sections do, and list Error::Internal.
There was a problem hiding this comment.
Fixed in 8eb4d74.
The record module docs now say that a refusal names the field, the output, and the row of a batch, "where there is one". They give a record that is not a map as an example of a refusal with no field. They list the two errors that are not Error::Record: Error::Plan, from the functions for one field, and Error::Internal, for a defect in the crate.
The # Errors section of stored_frame now lists Error::Internal. The CHANGELOG entry says "where there is one" too.
| //! | ||
| //! No refusal contains a value, or the text of a key inside a sealed node. | ||
| //! | ||
| //! [`read_stored`] refuses a frame that is not in the frame form. It also |
There was a problem hiding this comment.
Nit: read_stored does not check the form of an output in the clear.
read_outputs copies any frame under a term key or under eql, as it is. The term keys are eq, match, ore, and ope. So a term given as text, an object, or a null is accepted.
Example: a binding sends { email: { c: <bytes>, eq: "oops" } }. read_stored accepts it. stored_frame then writes "oops" back as the term. The binding finds out only when it compares that term later.
Fix: refuse a node under a term key or eql that is not bytes, with NotPassthrough. Or change the docs to say that only c is checked for form.
There was a problem hiding this comment.
Fixed in 8eb4d74.
read_stored now refuses a node under a term key or eql that is not bytes. The reason is not_passthrough, and the error names the output. stored_frame refuses the same thing on the way out, so the two directions agree. A passthrough output still takes any value, because a passthrough field's value can be of any kind.
New tests:
it_refuses_a_term_or_an_eql_value_that_is_not_byteschecks text, null, an object, and a number undereqandore, and text undereql. It also checks that a passthrough of text is still accepted.- The write-side test
it_refuses_an_output_in_the_clear_that_is_not_one_framehas a new case: a passthrough of text undereq.
Both failed first, with the bytes check disabled.
| Content::Sealed => read_sealed(node).map_err(at_fault)?, | ||
| // A passthrough is in the clear by contract. Its frame is kept | ||
| // as it is, as `encrypt` keeps it. | ||
| Content::Clear => CipherText::Passthrough(PassthroughFrame(node.as_bytes().to_vec())), |
There was a problem hiding this comment.
Nit: on the read side, a passthrough field's plaintext is copied into memory that is not wiped.
read_outputs builds PassthroughFrame(node.as_bytes().to_vec()). stored_frame writes into a wiped Buffer, because a passthrough field's value is caller plaintext. So one direction wipes that plaintext and the other does not.
Example: a binding reads back many rows whose passthrough fields contain caller plaintext, such as a tenant name. Each value stays in freed heap memory after the StackCipherText drops.
Fix: check whether PassthroughFrame is meant to be unwiped. If it is not, keep the copy in a wiped buffer. If it is, say so in read_stored's docs.
There was a problem hiding this comment.
I checked: PassthroughFrame is meant not to be wiped. Its doc says so (packages/vitaminc-aead/src/frame.rs:525): "Non-sensitive by contract; neither sealed nor authenticated, and never wiped."
encrypt follows the same contract. passthrough_frame in record.rs copies a passthrough field's value into an unwiped PassthroughFrame when it builds the stored record. So the value is already unwiped in the StackCipherText before a binding writes the frame. Wiping only the copy that read_stored makes would not change what stays in memory.
So I did not change the code. In 8eb4d74 I:
- said in
read_stored's docs that an output in the clear is copied into memory that is not wiped, asencryptstores it; - said there that a passthrough field must not contain a secret;
- fixed
stored_frame's docs. They said the buffer is wiped "because a passthrough field's value is caller plaintext". Now they say it is wiped because every frame the engine returns is.
Wiping passthroughs would be a change to PassthroughFrame itself. Its field is a public Vec<u8>. A Drop impl would stop every binding from moving that field out. That is a decision for Dan, not a fix for this pull request.
A refusal of a stored record in a batch did not say which row was at fault. No refusal said which output was at fault. `Error::Record` now has an `output` field. `stored_frame` and `read_stored` set it, and they set the row in a batch. `decrypt` and `check_record` leave the output unset. So their payloads do not change. read_stored and stored_frame now refuse a term or an EQL value that is not bytes. The docs say where a refusal names the field, list Error::Internal, and say that a passthrough's frame is not wiped, as PassthroughFrame's contract says. The Go guest's test that builds an Error::Record sets the new field. A new guest test checks that the output reaches the host.
…ending record-stored-frames-v1 fixes the frame form of a stored record for every binding. The cases cover these: - every output kind, and a passthrough of each value kind - a batch, and both null modes - lists, maps, and empty markers under c - the refusals, with their field, output, and row The Rust runner is stored_frame_conformance. The Go runner waits until the Go guest uses the frame form.
…e range The property test drew dates up to 3,000,000 days, past the last date the engine accepts (year 9999). A row with such a date was refused before it was sealed, so the test failed at random. It now draws dates and timestamps from the range that the engine accepts.
|
Answers to the two points in review 6103483734. 1. The output key in
2. A conformance profile. Added in a2fd232, as
Its 25 cases cover:
I checked that the runner fails when an expectation is changed. The three open decisions are unchanged: the marker form, refusing undeclared fields and outputs, and accepting a null for a |
…rectory The CRAP gate skips files under a tests directory. The unit tests of the stored-frame module were in stored/tests.rs, so the gate scored their helpers as source with no coverage, and one helper failed it. The tests now live in stored/tests/mod.rs. No test changed.
Summary
Stack Encrypt now converts a stored record to a frame and back. So every
binding moves the encrypted record to its language with one function call,
and no binding walks the tree or renames a key.
dynamic::record::stored_frame(&stored, &plan)writes the stored record asa frame.
dynamic::record::read_stored(frame, &plan)reads it back as theStackCipherTextthatdecrypttakes. Two more functions do the same forone field's node.
A binding builds its language's value from the frame, as it does for any
value. So in TypeScript a row comes out as
{ email: { c: Uint8Array, eq: Uint8Array }, id: { passthrough: 7n } },under Stack Encrypt's own keys.
Closes #2029.
Changes
New functions in
stack_encrypt::dynamic::record, in the new filesrc/dynamic/record/stored.rs:The frame form:
SealedValue::to_bytes)c["empty_seq", <bytes>]["empty_map", <bytes>]Other changes:
undeclared_outputandnot_sealed. They are atthe end of
Reason, so no existing value moves.Error::Recordhas a new field,output: the key of the output atfault, such as
coreq.Error::output()returns it, and the payloadhas it as
output. The new functions set it, and they also set the rowof every refusal in a batch.
decryptandcheck_recordleave itNone, so their payloads do not change.is not, with
not_passthrough.that the output reaches the host.
record-stored-frames-v1, with its contract,25 cases, and a Rust runner,
tests/stored_frame_conformance.rs. The Gorunner is pending.
Reason::Unframeableno longer says "passthrough". Itwas already given for values that are not passthroughs.
Frame::entriesinvitaminc-aead. It lends a map's keys andvalues' frames with no copy, as
Frame::elementsdoes for a sequence.recipher-framere-exports its iterator,Entries.src/dynamic/record/stored/tests/mod.rs, and two in the framemodule of
vitaminc-aead. The test resolverFakeEqlinrecord.rsisnow visible to the new tests. Nothing else in it changed.
docs/language-integration.mdhas a paragraph in "Encrypt a record" andone in "Decrypt a record". The
CHANGELOG.mdfiles ofstack-encrypt,vitaminc-aead, andrecipher-framehave entries.No stored byte changes. The Go guest and the TypeScript binding are not
changed. Moving them to these functions is follow-up work.
Verification
I ran these in a scratch worktree with
MISE_DISABLE=1 RUSTUP_TOOLCHAIN=1.94.1.cargo fmt --all -- --checkcargo clippy --workspace --all-features --all-targets -- -D warningscargo clippy -p stack-encrypt --no-default-features --all-targets -- -D warnings, and with default featurescargo nextest run -p stack-encrypt -p vitaminc-aead -p recipher-frame --all-features, which includes the trybuildtests/ui, after the review fixescargo nextest run -p vitaminc-aead, with default features and with--no-default-featuresstack-encrypt,vitaminc-aead, andrecipher-frame, all featuresRUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-featuresfor the three cratescargo check --locked --target wasm32-wasip1 --all-features, clippy for wasm32 and native with-D warnings, andcargo test --locked --all-featurespnpm run test:scriptseql-release-assetstests timed out under load, and passed when run again.pnpm run code:checkcargo llvm-cov0.9.1 andcargo crap0.6.1 with the flags ofcrap:stack-encryptcargo mutants --in-diffover the review fixescargo mutants --in-diffover the three crates, before the review fixesThe new tests include two property tests:
cor passed through, decrypts the same afterthe trip through the frame. The values cover every scalar kind, a date, a
timestamp, a decimal, a UUID, and nested lists and maps.
c, empty markers included, reads back toa record that writes the same frame.
I did not run these:
rebuilt for them.
functions.
Related
field's node.
stored_field_frameandread_stored_fieldconvert thatnode.
stored_rowandstored_tree.Review notes
The frame form
The keys are Stack Encrypt's:
c, the index keys,passthrough, andeql. A sealed value is its stable leaf bytes, so a binding stores thesame bytes as it does today. A passthrough node is the value in it, put
into the frame as it is. The rustdoc says that the frame is a transport
form and not a storage format, as the leaf's serde docs say.
Lists, maps, and empty markers under
cA field typed
objectorarrayseals a list or map underc. So does afield with no type. An empty list or map seals as a marker. The marker is
a sealed value of its own.
In a frame, a marker cannot be plain bytes. It would read back as an
ordinary sealed value, and then fail to open. An object such as
{ "empty_seq": <bytes> }would clash with a sealed map that has that key.A sealed node is never text, so an array that starts with text has no other
meaning. So the marker is
["empty_seq", <bytes>]. I chose this over atagged object around every list and map. Tags would double the depth of
the frame. A frame is limited to 128 levels.
Why the plan is an argument
A sealed value and a term are both bytes in the frame. Only the plan says
which output is sealed. So
read_storedtakes the plan, and never decidesfrom the bytes.
stored_frametakes the plan too, and refuses a recordthat the plan could not read back. Functions in
recordtake&Planinthe same way as
encrypt,decrypt, andcheck_record.The issue suggests serde on
CipherText. Serde does not work here, for tworeasons:
recipher_frame::serderefuses a date, a decimal, and a timestamp. Apassthrough field can contain any of them.
plan avoids.
Fields with no value
A
"null": "sql"field with no value has no entry in the stored record.So it has no entry in the frame.
read_storedreads a field that is leftout, or given as null, as no entry.
decryptopens a missing entry as anull.
So for a column that contains SQL
NULL, a binding sends either one. Anull for any other field is refused, with
outputs_not_map. This followsNullMode::Sql. On the encrypt side, it already treats a null and amissing field the same way.
What is refused, and what is left to decrypt
read_storedrefuses a frame that is not in the frame form. It alsorefuses a field or output that the plan does not declare, because it
cannot tell what that node is. That is stricter than
decrypt.decryptignores such entries.
decryptmakes every other check, such as the check for a missingc. Soeach check is made in one place. A passthrough anywhere under
cisrefused in both directions, as
decryptrefuses it.Naming the output and the row at fault
Error::Recordhas a new field,output. The new functions set it forevery refusal about one output, an undeclared key included, and set the
row of every refusal in a batch.
decryptandcheck_recordstill set neither for a field refusal.record-reads-v1compares their payloads exactly, in Rust and in Go, sothat change also needs the corpus, the Go runner, and rebuilt guests. It is
left for a separate issue.
Code outside this crate that builds an
Error::Recordmust now setoutput. In this repository, that was one Go guest test.One field's node
stored_field_frameandread_stored_fieldconvert the map of onefield's outputs. A row has the same node under the field's name. The two
functions refuse a null, because a field with no value has no node.
Plaintext
OwnedFrame. Its buffer is wiped when it is dropped.read_storedcopies a passthrough node's frame into aPassthroughFrame, asencryptdoes. That memory is not wiped:PassthroughFrame's doc says it is "non-sensitive by contract" and"never wiped".
read_stored's docs now say so, and that a passthroughfield must not contain a secret. Wiping passthroughs would be a change to
PassthroughFrameitself. That is a decision for Dan.Debugoutput contains a value. The tests check thisfor a forged value and for a key inside
c.Depth
The frame has one more level than the value, for the field's outputs. An
empty marker adds one more. So a value nested close to the frame's limit
can be sealed but not written as a frame. That is refused with
unframeable.Conformance
record-stored-frames-v1fixes the frame form for the next binding. Thecases use a notation that any language can read:
$sealedis a sealed value's bytes.$bytesis a term.{"$value": ...}is a passthrough value.Objects compare by key, because the corpus file's key order is not
kept by every JSON reader. The Go runner is marked pending, as for
single-value-v1, until the Go guest uses the frame form.The profile does not cover the write-side refusals, because a binding never
builds a stored record itself. It does not cover a field that names an EQL
type, or one field's node. The native tests cover those.
For Dan to decide
["empty_seq", <bytes>]. It is the one new piece ofwire text in this pull request.
read_storedshould refuse a field or output that the plan doesnot declare, as it does now, or drop it, as
decryptignores it."null": "sql"field should be accepted, asit is now, or only a missing entry.