Skip to content

feat(eql-bindings): read an EQL ciphertext with the rc:1: prefix - #2056

Draft
coderdan wants to merge 3 commits into
nextfrom
feat/eql-ciphertext-prefix-rc
Draft

coderdan wants to merge 3 commits into
nextfrom
feat/eql-ciphertext-prefix-rc

Conversation

@coderdan

@coderdan coderdan commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Summary

EQL (Encrypt Query Language) is the SQL library that stores and queries encrypted values in PostgreSQL. An EQL value is a JSON object. Its c key stores the ciphertext: a text prefix, then base64 of a sealed leaf. A sealed leaf is one encrypted value as Stack Encrypt writes it. Stack Encrypt (stack-encrypt) is the Rust encryption crate.

Today every value is written with the prefix stack-encrypt:1:. The prefix will become rc:1:. Values that are already stored keep the old prefix, so a reader has to take both prefixes for good.

A reader that is already released refuses rc:1:. If a writer moved to the new prefix first, those readers could not open the new values. So the change takes two releases:

  1. This pull request. Every reader takes rc:1: and stack-encrypt:1:. Every writer still writes stack-encrypt:1:.
  2. A later pull request. The writer writes rc:1:.

A program upgrades every reader to the first release before any writer moves to the second. No stored value is encrypted again.

Changes

  • eql-bindings (the Rust crate released with @cipherstash/eql): the reader of c strips either prefix and reads the same leaf from the body. Both prefixes are in one place, encryption.rs, next to the prefix that is written. The two prefixes differ in their first character, so at most one matches a given c. Any other prefix is still refused with the same error as before: "unsupported EQL ciphertext producer or version". The refusal comes before the body is decoded and before any data key is requested.
  • The writer is unchanged: a new c still starts with stack-encrypt:1:.
  • The Go SDK reads through the Rust guest: the WebAssembly build of Stack Encrypt that the Go module runs. So Go takes the new prefix with no change to Go code.
  • Docs: the eql-bindings README, the [Unreleased] sections of the eql-bindings and Go changelogs, and a changeset for @cipherstash/eql.

Verification

Tests were written first and seen to fail before the reader changed:

  • eql-bindings unit tests (src/encryption.rs): a stored body reads to the same leaf under both prefixes. A new value is written with stack-encrypt:1:. A valid body under xx:1:, rc:2:, RC:1:, rc:1 or stack-encrypt:2: is refused with the same error as today. A body under rc:1: that is not base64 or not a sealed leaf is refused by the decoder, not by the prefix check. Before the change, a_body_under_the_rc_prefix_reads_as_the_same_leaf failed with "unsupported EQL ciphertext producer or version".
  • eql-encryption-tests (text_eq.rs, v4_types.rs): a freshly sealed TextEq opens to the same text with its prefix swapped to rc:1:. A tampered body under rc:1: fails authentication. A valid body under another prefix is refused by name and requests no data key. The frozen rows in stored_v4_rows_v1.json and stored_v4_json_rows_v1.json open to the same plaintexts with each c swapped to rc:1: in memory. No fixture file changes. Four of these failed before the change.
  • Go (eql_v4_test.go, TestV4ValuesUnderTheRCPrefixOpen): freshly sealed profiles and the frozen rows open under rc:1:. A new value still has stack-encrypt:1:. xx:1:, rc:2: and stack-encrypt:2: are refused with the same error. Against a guest built from the old reader, it failed with unsupported EQL ciphertext producer or version.

Checks run in packages/eql, after the change:

  • cargo fmt --all --check: clean.
  • cargo clippy -p eql-bindings -p eql-codegen -p eql-encryption-tests --all-features --all-targets -- -D warnings: clean.
  • cargo nextest run -p eql-bindings -p eql-codegen -p eql-encryption-tests --all-features: 623 passed, 63 skipped.
  • cargo test --doc -p eql-bindings --all-features: 18 passed, 3 ignored.
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps -p eql-bindings --all-features: clean.
  • cargo run -p eql-codegen -- bindings, -- go-eql and with no argument: git status showed no change to any generated file.

Go, with the guests rebuilt from this branch:

  • The four encrypt guest builds and the auth guest built, and their import checks passed.
  • Guest clippy and rustdoc: clean. Clippy ran for the host and for wasm32, with four feature sets each. Guest tests: 85 passed, and 89 passed with eql,deterministic-kms. Auth guest tests: 24 passed.
  • Every Go package with tests passed (scripts/go-binding-test.sh languages/golang, nested modules included). TestV4ValuesUnderTheRCPrefixOpen and TestFrozenV4RowsStillOpen were also run on their own with -v, and both passed.

Not run:

  • The tests marked #[ignore]: they need PostgreSQL.
  • Tests that need live CipherStash credentials.

Related

Review notes

Every place in the repository that reads, writes or mentions the prefix:

Place Role
packages/eql/crates/eql-bindings/src/encryption.rs, CIPHERTEXT_PREFIX and CIPHERTEXT_PREFIXES_READ The one place that knows the prefixes.
encryption.rs, CiphertextVisitor::sealed Writes stack-encrypt:1:. Unchanged.
encryption.rs, impl Reader for Ciphertext Reads. Now takes both prefixes.
languages/golang/encrypt/guest (eql feature) Reads and writes through eql-bindings, linked by path. No change of its own.
SQL under packages/eql/src Checks only that c is present, or that it is a string. It does not read the prefix, so it takes rc:1: already.
JSON Schemas under eql-bindings/schema/v3 These are for EQL v3 values from cipherstash-client, whose c has no prefix. Not a reader of this prefix.
languages/typescript No reader or writer. protect-ffi uses eql-bindings without the stack-encrypt feature, so it does not build this code. recipher-js does not use eql-bindings.
packages/eql/tests/encryption/fixtures/*.json Stored values with the old prefix. Unchanged, and they still open.
Tests that check a new value starts with stack-encrypt:1:: text_eq.rs, v4_types.rs, languages/golang/encrypt/eql_test.go, languages/golang/encrypt/eql_v4_json_test.go, languages/golang/encrypt/guest/src/ops.rs Pin the writer. Unchanged.
eql-bindings/tests/v4_inventory.rs, languages/golang/encrypt/eql/eql_test.go Test data only.
eql-bindings/README.md Updated: a reader takes both prefixes, a writer writes the old one.
packages/stack-encrypt/docs/language-integration.md Shows one stored value with the old prefix, which is still what is written. Unchanged.
docs/adr/0002-… and docs/plans/2026-10-04-plan-builder.md A decision record and an older plan. Left as written.

The writer still writes stack-encrypt:1: in this pull request, as the owner decided. A reader from a release before this one refuses rc:1:. So every reader has to be on this release before any writer changes. The second release makes the writer change.

The prefix on `c` becomes `rc:1:`, and production data has the old
prefix, so a reader has to take both for good. A reader that is already
released refuses `rc:1:`. So this release reads both prefixes and still
writes `stack-encrypt:1:`. A later release writes the new prefix, after
every reader has this one.

Both prefixes are in encryption.rs, next to the written one. The Go SDK
reads through the guest. The guest links this crate, so Go needs no change.

Refs #2040
A program has to upgrade every reader before any writer moves to the new
prefix. The changelogs and the changeset say so, so the order is clear
to anyone who upgrades.

Refs #2040
@changeset-bot

changeset-bot Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0e33fd2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
@cipherstash/eql Minor
stash Patch
@cipherstash/stack-prisma Patch
@cipherstash/basic-example Patch
@cipherstash/e2e Patch
@cipherstash/prisma-example Patch
@cipherstash/stack-drizzle Patch
@cipherstash/stack-supabase Patch
@cipherstash/stack Patch
@cipherstash/wizard Patch
@cipherstash/bench Patch
@cipherstash/test-kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

@coderdan coderdan left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of this pull request at 5b58519f9. No correctness problem in the reader change. There are four small findings. Three are inline. One is here, because its file is not in the diff.

The language integration guide still names one prefix

packages/stack-encrypt/docs/language-integration.md:784 says c is "the ciphertext under the stack-encrypt:1: prefix". That is true of the value shown: a writer wrote it. The guide does not say that a reader takes rc:1: too. Someone who writes a reader for another language from this guide would accept one prefix only. That reader would refuse every value once a writer moves to rc:1:.

"rc:1:~",
"rc:1:00000",
"rc:1:AAAA",
"rc:1:雪",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The six new rc: cases are checked with is_err() only, so any error passes them.

Suppose rc:1: were removed from the prefixes a reader takes. The five rc:1:… cases would still pass. The prefix check would refuse them, where today the body does.

Assert the cause. The rc:1: cases must not contain "unsupported EQL ciphertext producer or version". The rc:2: and stack-encrypt:2: cases must contain it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0e33fd2de. Each case now states whether the refusal names the prefix, and the test compares that with the error text. With rc:1: taken out of the reader, the test fails on the rc:1: case, as it should.

@@ -42,7 +42,8 @@ async fn text_round_trips_through_json_and_both_canonical_directions() {
// cipherstash-client ciphertext inside the payload; the `eql_v4`
// domain separates them in the database. Encryption
// and decryption read one constant, so a round trip cannot notice it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment still says "Encryption and decryption read one constant". After this change, the writer reads CIPHERTEXT_PREFIX and the reader reads a list of two.

The comment also no longer says why the literal is pinned here. Rows already stored have it. A round trip cannot notice the written prefix changing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0e33fd2de. The comment now gives both reasons the literal is pinned: stored rows start with it, and a round trip passes whichever prefix the writer writes.

Comment thread languages/golang/encrypt/eql_v4_test.go Outdated
}
opensToItsPlaintexts(t, c, frozen.Rows)

for _, prefix := range []string{"xx:1:", "rc:2:", "stack-encrypt:2:"} {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Go test sends three unknown prefixes through the guest. The Rust unit test also covers RC:1:, rc:1 with no final colon, and no prefix. If the guest build that Go embeds matched the prefix without regard to case, the Go suite would still pass. Add RC:1: and the empty prefix here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0e33fd2de. The Go test now sends RC:1: and a value with no prefix through the guest too, and both are refused with the same error.

The refusal list in `text_eq.rs` checked each case with `is_err()`, so
any error passed. With `rc:1:` removed from the prefixes a reader takes,
the `rc:1:` cases still passed. Each case now states whether the refusal
names the prefix. That removal now fails the test.

The Go test sends `RC:1:` and a value with no prefix through the guest
too. The comment in `text_eq.rs` and the language integration guide now
say that a reader takes two prefixes and a writer writes one.

Refs #2040
@coderdan

coderdan commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

The finding about the language integration guide is fixed in 0e33fd2de. The paragraph at packages/stack-encrypt/docs/language-integration.md:784 now says that a writer writes stack-encrypt:1: and a reader takes rc:1: too.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant