Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions .github/workflows/fuzz.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ name: Fuzz
# cache, so coverage compounds; it is minimised with `cargo fuzz cmin`, and
# any crash reproducer is uploaded as an artifact.
#
# * go-fuzz-campaign (schedule and manual, never on PRs): the same for the
# Go fuzz targets in languages/golang, which fuzz the WASI guests' ABI
# from the host side (`mise run go:fuzz`). Their regression replay is
# plain `go test`, in tests-golang.yml.
# * go-fuzz-campaign (schedule and manual, never on PRs): the same run for
# the Go fuzz targets in languages/golang (`mise run go:fuzz`). Those
# targets fuzz the ABI of the WASI guests from the host side, and the
# decoders of the frame codec. Their regression replay is plain
# `go test`, in tests-golang.yml.
#
# cargo-fuzz needs nightly; the tasks run `cargo +nightly fuzz`. Pull requests
# are the only filtered event, so `push` has no filter to mirror; recorded in
Expand Down Expand Up @@ -258,11 +259,15 @@ jobs:
strategy:
fail-fast: false
matrix:
# `guest` is the build the target's package runs.
# `guest` is the guest build that the package of the target runs. The
# targets of the frame codec, in encrypt/frame, run no guest.
include:
- { target: FuzzGuestExportPairs, slug: encrypt-export-pairs, guest: "wasm:guest:build:deterministic" }
- { target: FuzzGuestBufferRegistry, slug: encrypt-buffer-registry, guest: "wasm:guest:build:deterministic" }
- { target: FuzzAuthGuestExportPairs, slug: auth-export-pairs, guest: "wasm:auth-guest:build" }
- { target: FuzzUnmarshal, slug: frame-value, guest: "" }
- { target: FuzzUnmarshalCipherText, slug: frame-ciphertext, guest: "" }
- { target: FuzzUnmarshalStorage, slug: frame-storage, guest: "" }
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
with:
Expand All @@ -278,13 +283,15 @@ jobs:
cache_key_prefix: mise-v1-ubuntu-2404

- uses: Swatinem/rust-cache@e18b497796c12c097a38f9edb9d0641fb99eee32 # v2
if: matrix.guest != ''
with:
workspaces: |
languages/golang/encrypt/guest
languages/golang/auth/guest
key: go-fuzz-${{ matrix.slug }}

- name: Build the guest (${{ matrix.slug }})
if: matrix.guest != ''
run: mise run ${{ matrix.guest }}

- name: Find the Go fuzz cache
Expand Down
16 changes: 9 additions & 7 deletions .github/workflows/release-golang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ name: Release (Go)
#
# gh workflow run release-golang.yml -f version=0.1.0-alpha.1 --ref <branch>
#
# The ref must carry the EQL v4 bundle (packages/eql's v4 build), or the
# build job stops at "Build the EQL SQL bundles". Its
# languages/golang/CHANGELOG.md must have a `## [<version>]` section, which
# becomes the release notes, or the run stops at the `notes` job. A version with a pre-release
# suffix may be cut from any branch and is published as a GitHub pre-release;
# a final version is cut from main only, and not while go.mod pins vitaminc's
# Go bindings at a pseudo-version (scripts/go-release-version.mjs).
# The ref must have the EQL v4 bundle (the v4 build of packages/eql). Without
# it, the build job stops at "Build the EQL SQL bundles". The file
# languages/golang/CHANGELOG.md at the ref must have a `## [<version>]` section.
# That section becomes the release notes. Without it, the run stops at the
# `notes` job.
#
# A version with a pre-release suffix can be released from any branch. The
# workflow publishes it as a GitHub pre-release. A final version can be released
# only from main.
#
# Four jobs, split by what they may touch:
#
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/tests-golang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ on:
- packages/stack-kms/**
- packages/stack-encrypt/**
- conformance/stack-encrypt/**
# encrypt/frame's tests read the value corpus.
- conformance/vitaminc-aead/**
- packages/stack-encrypt-derive/**
- packages/stack-encrypt-jsonpath/**
- packages/stack-guest-abi/**
Expand Down Expand Up @@ -88,6 +90,8 @@ on:
- packages/stack-kms/**
- packages/stack-encrypt/**
- conformance/stack-encrypt/**
# encrypt/frame's tests read the value corpus.
- conformance/vitaminc-aead/**
- packages/stack-encrypt-derive/**
- packages/stack-encrypt-jsonpath/**
- packages/stack-guest-abi/**
Expand Down
8 changes: 5 additions & 3 deletions AGENTS.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion conformance/stack-encrypt/exchange-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mise run test:conformance:exchange
|---|---|---|
| `values` | One record per positive case of `v1/record-values-v1.cases.json` (the value-boundary corpus), under the plan that case declares, with context `["conformance", "exchange"]`: the sealed leaf and every declared index term; nothing for a SQL null | The record opens to the case's typed input. Every stored term equals the term the consumer's own query of the same value derives (`eq` for equality, `lt` for an order index), and the `equal_query` spelling derives the same equality term. |
| `record` | The rows of the corpus's one record: a passthrough `int64` id (held on the host by Go, lowered as a `passthrough` output by Rust), text, `uint32`, decimal, date and timestamp fields with equality and order terms, a SQL-null text field and a sealed-null `float32` field, under context `["conformance", "exchange", "people"]` | The passthrough carried equals the corpus input; every sealed field opens to its input, a sealed null opens as null and a SQL null is absent from storage and opens as null; every stored term equals the consumer's derived query term |
| `blobs` | A whole struct (`name`, `age`, `tags`, optional `nickname`) as one opaque ciphertext under `["conformance", "exchange", "blob"]`, field `value`: Rust through `OpaqueBlob<T>`, Go through the storage blob the generated opaque path writes (`vcffi.MarshalStorage` sealed as the one `bytes` field) | The ciphertext opens and decodes to the corpus value through the other SDK's opaque reader (`OpaqueBlob<T>` or `gensupport.Opaque`). A nil Go slice and an empty Rust `Vec` are one value; Rust's `Some` marker and Go's bare pointer are one value |
| `blobs` | A whole struct (`name`, `age`, `tags`, optional `nickname`) as one opaque ciphertext under `["conformance", "exchange", "blob"]`, field `value`. Rust writes it through `OpaqueBlob<T>`. Go writes it through the generated opaque path, as a blob in the storage profile, sealed as the one `bytes` field | The ciphertext opens and decodes to the corpus value through the other SDK's opaque reader (`OpaqueBlob<T>` or `gensupport.Opaque`). A nil Go slice and an empty Rust `Vec` are one value; Rust's `Some` marker and Go's bare pointer are one value |
| `eql-rows` | The EQL v4 `profiles` rows: every column of the committed fixture's plaintexts sealed afresh, Rust through eql-bindings' typed targets, Go through its generated code | Each column opens to the plaintext through the other side's typed target, and each equality column's stored `hm` equals the term the consumer's own equality query derives for that plaintext (the generated `*Eq` query in Go, the typed `*EqQuery` target in Rust) |

Each kind runs Go to Rust and Rust to Go. Ciphertext bytes are never
Expand Down
13 changes: 8 additions & 5 deletions conformance/vitaminc-aead/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Value conformance corpus

`value-conformance.json` holds the shared vectors that Rust, Node, the Go
codec and the Go WASM encryption guest all check. Payload bytes are frozen by
`value-conformance.json` contains the shared vectors that three codecs
check: Rust, Node, and the Go frame codec in
`languages/golang/encrypt/frame`.

Payload bytes are frozen by
`vitaminc-aead`'s scalar tag table and leaf codec ([`vitaminc-aead`'s ADR 0001](../../packages/vitaminc-aead/docs/adr/0001-describe-plaintext-once-and-consume-it-with-sinks.md)
section 3):
fixed-width integers and float bits are little-endian, dates count days from
Expand Down Expand Up @@ -87,9 +90,9 @@ without their transport decoder, then check both directions: decoding the
bytes gives that value, and encoding that value gives the bytes. A bug that
decodes and encodes symmetrically wrong therefore still fails. Adding a kind
means teaching each language's `expected` helper to build it. The Go fuzz
targets seed themselves from `vectors`, `malformed`, `normalized` and
`ciphertext_malformed`, so every row is also a starting point for the
differential run against the guest.
targets take their seeds from `vectors`, `malformed`, `normalized`, and
`ciphertext_malformed`. Every row is therefore also a starting input for
fuzzing the Go decoders.

These are the scalar/value-level vectors needed by #375 and #414. The wider
#332 cross-language ciphertext, guest-object and SDK corpus remains separate
Expand Down
29 changes: 28 additions & 1 deletion docs/fuzzing.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,34 @@ The fuzz tests never pass a pair inside memory but outside the host's own
buffers. That breaks the host's side of the contract: an export that wipes
its input would zero the guest's own memory.

Run them with mise, after building the guests:
## Go: fuzzing the frame codec

The Go frame codec, `languages/golang/encrypt/frame`, decodes bytes that
the guest wrote and blobs read from a database. The decoders treat that
input as hostile. Three fuzz targets check two rules for each decoder:

- The decoder must not panic.
- Any input that the decoder accepts must be written back as the same
bytes.

Some changes to the bytes are allowed, because a Go value cannot avoid
them. For example, an `undefined` comes back as null. A timestamp in
seconds, milliseconds, or microseconds comes back in nanoseconds.

The targets need no guest. The seeds come from two files: the value corpus,
`conformance/vitaminc-aead/value-conformance.json`, and the storage corpus,
`packages/recipher-frame/testdata/storage-v1.json`.

| Go fuzz target | package | what it decodes |
|---|---|---|
| `FuzzUnmarshal` | `encrypt/frame` | a value frame (`Unmarshal`) |
| `FuzzUnmarshalCipherText` | `encrypt/frame` | a ciphertext tree (`UnmarshalCipherText`) |
| `FuzzUnmarshalStorage` | `encrypt/frame` | a storage blob (`UnmarshalStorage`) |

## Running the Go targets

Run them with mise. Build the guests first for the targets that call a
guest. The targets of the frame codec need no guest.

```bash
mise run wasm:guest:build:deterministic
Expand Down
33 changes: 33 additions & 0 deletions docs/plans/2026-11-14-move-vitaminc-aead-into-the-stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,39 @@ The work:
5. Delete `vcencrypt` and its guest from vitaminc. It was a demonstration of
the approach, and the Go module replaces it.

### Done

Items 1 to 4 are done, on the branch `refactor/go-frame-codec-in-module`:

- The two packages moved, and stay two packages. `vcffi` is now
`languages/golang/encrypt/frame`: the codec and the storage profile.
`vcvalue` is now `languages/golang/encrypt/value`: the value model, which
imports nothing outside the standard library. Both are public, as they
were in vitaminc. Both were copied from vitaminc commit `f1fc9ae74`, with
their tests and fuzz targets. The tests read the value corpus
(`conformance/vitaminc-aead`) and the storage corpus
(`packages/recipher-frame/testdata`) from this repository.
- `encrypt.Date`, `Decimal`, `Int128`, and `Uint128` are still aliases,
now of the types in `encrypt/value`. `encrypt.UUID`, `Encryptable`, and
`Encoder` are new aliases, of `value.UUID`, `frame.Encryptable` and
`frame.Encoder`. Each type is declared once, so the codec needs no
conversion.
- The sealed leaf family, `VCValueLeaves`, `TypedNull`, `FixedWidth`, and
`FixedType` are removed. The typed null for a nil pointer is built from
the tag table, so the codec has no list of types. `record.Kind` is the
only list of type names.
- No `go.mod` names a vitaminc module: not the one of the module, and not
those of its two nested modules. `scripts/go-release-version.mjs` no
longer refuses a final version because of a vitaminc pin.
- The Go campaign in `fuzz.yml` fuzzes the three decoders every night.

Two things are still to do:

- Item 5, in vitaminc.
- The differential fuzzers that compared the Go codec with a Rust guest, in
vitaminc's `vcencrypt`. Those fuzzers could run against
`languages/golang/encrypt/guest`.

## What waits on this plan

- The TypeScript SDK: #1775, #1776 and the prototype #1829.
Expand Down
8 changes: 8 additions & 0 deletions languages/golang/.golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ linters:
- path: (_test\.go|internal/guesttest/)
linters:
- gosec
# The frame codec and its value types convert between integer widths
# on purpose. They read and write integers in two's complement, and
# check every length, count and range against a limit before they
# convert one.
- path: encrypt/(frame|value)/
linters:
- gosec
text: G115

formatters:
enable:
Expand Down
64 changes: 58 additions & 6 deletions languages/golang/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,28 @@ and `languages/golang/encrypt/encrypttest/vX.Y.Z` on the same commit.

### Added

- `gensupport.UUID`, the kind `"uuid"`: a field the engine seals and
opens as a UUID, sixteen bytes under vitaminc's UUID logical type. It
takes an equality index and no order or match index. Its value is a
`vcvalue.UUID`, or a value of any type whose underlying type is
`[16]byte` (`github.com/google/uuid.UUID`, say), which is converted to
one. `stashgen` does not yet map a Go type to it.
- `gensupport.UUID`, the kind `"uuid"`: a field that the engine seals and
opens as a UUID. A UUID is sixteen bytes with the logical type UUID. The
field takes an equality index, and no order or match index. The value of
the field is an `encrypt.UUID`. A value of any other type whose
underlying type is `[16]byte`, such as `github.com/google/uuid.UUID`, is
converted to an `encrypt.UUID`. `stashgen` does not yet map a Go type to
this kind.
- `encrypt.UUID` and `ParseUUID`: sixteen bytes that are sealed as a UUID.
An opened UUID has this type. `encrypt.UUID` is an alias of `value.UUID`.
- `encrypt.Encryptable` and `encrypt.Encoder`, aliases of
`frame.Encryptable` and `frame.Encoder`, for a type that writes its own
value. Give the type a method `EncryptValue(enc encrypt.Encoder) error`.
A field of that type is sealed as the one value that the method writes,
with one of the Encoder's methods, such as `Int64`, `String`, or `Date`.
`Encoder.Err` returns the first error of the encode.

The zero Encoder keeps nothing and does not panic. The `Err` method of
the zero Encoder returns an error that says so. A unit test of your type
can therefore pass the zero Encoder to `EncryptValue`.
- Two public packages, moved here from vitaminc: `encrypt/frame`, the frame
codec and the storage profile (vitaminc's `vcffi`), and `encrypt/value`,
the value types that the codec reads and writes (vitaminc's `vcvalue`).

- `Diagnostic.Row` and `Diagnostic.Found` (#1985). The two accessors give
more detail for an error about a plan, a record, or a stored row that is
Expand All @@ -63,6 +79,34 @@ and `languages/golang/encrypt/encrypttest/vX.Y.Z` on the same commit.

### Breaking

- **vitaminc's Go packages `vcvalue` and `vcffi` are now `encrypt/value`
and `encrypt/frame`.** `encrypt.Date`, `Decimal`, `Int128` and `Uint128`
are still aliases, now of the types in `encrypt/value`. Code that names
only `encrypt` types needs no change. The stored format is unchanged:
every value has the same ciphertext and the same terms as before.

Code that imports `vcvalue` or `vcffi` changes like this:

- Import `github.com/cipherstash/stack/languages/golang/encrypt/value` in
place of `vcvalue`, and `.../encrypt/frame` in place of `vcffi`. Or use
the `encrypt` names: `encrypt.Date`, `encrypt.UUID`,
`encrypt.Encoder`, and so on.
- The unsigned methods of the Encoder are `Uint8` to `Uint128`, not
`UInt8` to `UInt128`.
- Error text starts with "value: " or "frame: ", not "vcvalue: " or
"vcffi: ". `errors.Is` matches the same error values as before.
- What only vitaminc's own binding used is gone: `Sealed`,
`SealedEmptySeq`, `SealedEmptyMap`, `TypedNull`, `FixedType`,
`ErrInvalidFixedType`, `Plain.Value`, `Encoder.TypedNull`,
`FixedWidth` and `VCValueLeaves`.
- Without `FixedWidth`, a nil pointer to an `Encryptable` is written as
a null. The engine pads a sealed null to the width of the type that the
field declares, as it does for every null. A field that declares no
type has no width to pad to, so the length of the stored null would
show that it is a null. `Cipher.Seal` therefore refuses a nil
`Encryptable` for such a field, with `ErrEncoding`. Generated code
always declares a type.

- **`Match` refuses `vars` that are not a JSON object with the reason
`json_vars_not_object`, not `json_input`** (#1987). If your program
compares `Reason()` with `json_input` for this case, change it to the
Expand All @@ -75,6 +119,14 @@ and `languages/golang/encrypt/encrypttest/vX.Y.Z` on the same commit.

### Changed

- **The module no longer depends on vitaminc's Go packages.** The frame
codec, the storage profile and the value types are now in this module,
in `encrypt/frame` and `encrypt/value`. `go.mod` therefore names no
vitaminc module, so a final version of the module can be released. The
codec writes the same bytes as before. The tests of the codec read the
same value corpus and storage corpus as the Rust codec, from this
repository.

- **A decrypt error names the field and the Go type that the field
needs, and never the kind that the value decrypted to** (#1987). The Go
type, the kind, and the length of a decrypted value were inside the
Expand Down
14 changes: 7 additions & 7 deletions languages/golang/auth/answer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
"testing"

"github.com/cipherstash/vitaminc/bindings/go/vcffi"
"github.com/cipherstash/vitaminc/bindings/go/vcvalue"
"github.com/cipherstash/stack/languages/golang/encrypt/frame"
"github.com/cipherstash/stack/languages/golang/encrypt/value"
)

// When the answer of an auth guest is not an object, the error is
Expand All @@ -22,7 +22,7 @@ func TestAnAnswerThatIsNotAnObjectNamesTheExportAndItsKind(t *testing.T) {
{"secret-token", "a string"},
{[]byte("secret-key"), "bytes"},
} {
encoded, err := vcffi.Marshal(tc.answer)
encoded, err := frame.Marshal(tc.answer)
if err != nil {
t.Fatal(err)
}
Expand All @@ -38,7 +38,7 @@ func TestAnAnswerThatIsNotAnObjectNamesTheExportAndItsKind(t *testing.T) {
t.Errorf("the error holds the answer: %v", err)
}
}
encoded, err := vcffi.Marshal(vcvalue.Object{{Key: "access_token", Value: "t"}})
encoded, err := frame.Marshal(value.Object{{Key: "access_token", Value: "t"}})
if err != nil {
t.Fatal(err)
}
Expand All @@ -58,7 +58,7 @@ func TestAnAnswerThatIsNotAnObjectNamesTheExportAndItsKind(t *testing.T) {
// value that the answer is.
func TestListAnswersNameTheExportAndTheirKind(t *testing.T) {
marshal := func(v any) []byte {
out, err := vcffi.Marshal(v)
out, err := frame.Marshal(v)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestListAnswersNameTheExportAndTheirKind(t *testing.T) {
// contains the value of the field, because the value can be a token or a
// key.
func TestAnAnswersFieldsNameTheExport(t *testing.T) {
answer := fields{export: "sa_token", obj: vcvalue.Object{
answer := fields{export: "sa_token", obj: value.Object{
{Key: "access_token", Value: int64(7)},
{Key: "client_key", Value: "secret-key"},
{Key: "region", Value: []byte("secret")},
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestAStepThatDoesNotReadNamesTheExport(t *testing.T) {

func mustMarshalAuth(t *testing.T, v any) []byte {
t.Helper()
out, err := vcffi.Marshal(v)
out, err := frame.Marshal(v)
if err != nil {
t.Fatal(err)
}
Expand Down
Loading
Loading