diff --git a/languages/golang/CHANGELOG.md b/languages/golang/CHANGELOG.md index bd7ebcd19..3e6789af8 100644 --- a/languages/golang/CHANGELOG.md +++ b/languages/golang/CHANGELOG.md @@ -13,6 +13,17 @@ and `languages/golang/encrypt/encrypttest/vX.Y.Z` on the same commit. ## [Unreleased] +### Fixed + +- **A `JSONSelection` or a `JSONRead` passed as a query parameter now binds + as its selector or its tokens** (#1849). Before this fix, neither type had + a `Value` method. So pgx bound the redacted text that `String` returns. A + query given a `JSONSelection` matched no rows and returned no error. A + query given a `JSONRead` failed with a malformed array literal. Binding + `Selector()` or `Tokens()` worked before this fix, and still works. A zero + `JSONSelection` or `JSONRead` is refused, as a zero selector or empty + tokens are. + ## [0.1.0-rc.7] - 2026-11-25 ### Upgrading from rc.6 diff --git a/languages/golang/cmd/stashgen/README.md b/languages/golang/cmd/stashgen/README.md index 5c4b1800a..2f33c11d8 100644 --- a/languages/golang/cmd/stashgen/README.md +++ b/languages/golang/cmd/stashgen/README.md @@ -199,9 +199,9 @@ The field's `Fields` entry has a method per operation: | `Contains(ctx, c, needle)`, `ContainedBy(ctx, c, needle)` | `eql.JSONSearchQuery` | `col @> $1::eql_v4.query_json`, `col <@ $1::eql_v4.query_json` | | `EqAt(ctx, c, path, v)` | `eql.JSONSearchQuery` | `col @> $1::eql_v4.query_json` | | `At.Age.Lt(ctx, c, v)`, `Lte`, `Gt`, `Gte`, `Between` | `eql.JSONSearchLeafQuery` | `(col -> $sel::text) < $1::eql_v4.query_json_leaf`, `$sel` from `At.Age.Selector(ctx, c)` | -| `Selector(ctx, c, path)` | `encrypt.JSONSelection`: `Selector()` and `Open` | `col -> $1::text` | -| `Subtree(ctx, c, path, opts)` | `encrypt.JSONRead`: `Tokens()`, `Open` and `OpenOne` | `eql_v4.json_tree_subtree(col, $1::text[], 64)` | -| `Children(ctx, c, path)` | `encrypt.JSONChildren`: `Token()`, and `Open` to each child's key or index and value | `eql_v4.json_tree_children(col, $1::text)` | +| `Selector(ctx, c, path)` | `encrypt.JSONSelection`: `Selector()` and `Open`; binds as its selector | `col -> $1::text` | +| `Subtree(ctx, c, path, opts)` | `encrypt.JSONRead`: `Tokens()`, `Open` and `OpenOne`; binds as its tokens | `eql_v4.json_tree_subtree(col, $1::text[], 64)` | +| `Children(ctx, c, path)` | `encrypt.JSONChildren`: `Token()`, and `Open` to each child's key or index and value; binds as its token | `eql_v4.json_tree_children(col, $1::text)` | | `ChildCount(ctx, c, path)` | `encrypt.JSONToken` | `eql_v4.json_tree_child_count(col, $1::text)` | | `Elements.Visits.Match(ctx, c, opts, predicates...)` | `eql.JSONSearchElementsQuery` | `eql_v4.json_tree_element_match(col, $1::eql_v4.query_json_elements)` | | `Match(ctx, c, predicate, vars, opts)` | `eql.JSONSearchPredicateQuery` | `col @@ $1::eql_v4.query_json_predicate`, or `eql_v4.json_tree_match(col, $1::eql_v4.query_json_predicate)` | diff --git a/languages/golang/encrypt/README.md b/languages/golang/encrypt/README.md index 6e67a1e8a..c7416ef40 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -809,10 +809,13 @@ for. It does not prove the database returned every row, or rows of the database row you meant. `encrypt.JSONTokens` binds as a `text[]` literal, and a selector or token as `text`; cast each parameter, as above, so PostgreSQL picks EQL's operator and not `jsonb`'s; a zero one is refused -rather than bound. A token is a capability over its node's subtree in every -row of the column, and a path's keys can be your customer's data, so -`JSONToken`, `JSONTokens`, `JSONSelection`, `JSONRead`, and `JSONChildren` -print and log with the tokens, the selector, and the path redacted. A +rather than bound. A `JSONSelection` binds as its selector, a `JSONRead` +as its tokens, and a `JSONChildren` as its token, so you can pass the +value itself as the parameter. A token is a capability over its node's +subtree in every row of the column, and a path's keys can be your +customer's data, so `JSONToken`, `JSONTokens`, `JSONSelection`, +`JSONRead`, and `JSONChildren` print and log with the tokens, the +selector, and the path redacted. A `JSONValue` contains the plaintext a read opened, and prints it. A `JSONChildValue` contains a child's key or index and its value. A `JSONChildValue` prints and logs redacted, since a key can be your diff --git a/languages/golang/encrypt/json.go b/languages/golang/encrypt/json.go index 20d6e110a..eb322bc2d 100644 --- a/languages/golang/encrypt/json.go +++ b/languages/golang/encrypt/json.go @@ -368,6 +368,13 @@ func NewJSONSelection[E ~[]byte](c *Cipher, declaration *record.Plan, field, pat // Selector is the selector to bind: `col -> $1::text`. func (s JSONSelection[E]) Selector() JSONSelector { return s.selector } +// Value binds the selector as text, as [JSONSelection.Selector] does. So a +// JSONSelection can be passed to the query where the selector goes. Without +// this method, a driver such as pgx binds the String form, which is +// redacted, and the query matches no row. A zero JSONSelection is refused, +// as a zero [JSONSelector] is. +func (s JSONSelection[E]) Value() (driver.Value, error) { return s.selector.Value() } + // Open opens the entries `col -> selector` returned, one per database row, // nil for a row where it returned SQL NULL, with one ZeroKMS request. Each // opens to the scalar it holds (Found), or for the sealed root at "$" the @@ -445,6 +452,12 @@ func NewJSONRead[E ~[]byte](c *Cipher, declaration *record.Plan, field string, i // `eql_v4.json_tree_subtree(col, $1::text[], 64)`. func (r JSONRead[E]) Tokens() JSONTokens { return r.tokens } +// Value binds the tokens as a text[] literal, as [JSONRead.Tokens] does. So +// a JSONRead can be passed to the query where the tokens go. Without this +// method, a driver such as pgx binds the String form, which is redacted. A +// zero JSONRead is refused, as empty [JSONTokens] are. +func (r JSONRead[E]) Value() (driver.Value, error) { return r.tokens.Value() } + // Open opens the rows the read returned, one group per database row, with // one ZeroKMS request: per row, the value at each concrete path the read // named that the row has, in document order. A row with no node there diff --git a/languages/golang/encrypt/json_fields_test.go b/languages/golang/encrypt/json_fields_test.go index 9312d57a9..2011850d8 100644 --- a/languages/golang/encrypt/json_fields_test.go +++ b/languages/golang/encrypt/json_fields_test.go @@ -3,6 +3,7 @@ package encrypt_test import ( "bytes" "context" + "database/sql/driver" "encoding/json" "errors" "fmt" @@ -210,6 +211,75 @@ func TestACustomersFieldsDeriveEachQuery(t *testing.T) { } } +// A selection and a subtree read bind as their operand. Without a +// driver.Valuer, pgx binds the String form, and that form is redacted. A +// selection bound that way matches no row and returns no error. A read +// bound that way fails with a malformed array literal. pgx calls the +// driver.Valuer itself, and the PostgreSQL test covers that path. This test +// checks the Valuer, and the default converter that other database/sql +// drivers use. +func TestJSONSelectionAndReadBindAsTheirOperand(t *testing.T) { + c := deterministicEQLClient(t) + ctx := context.Background() + cipher := c.DefaultKeyset() + f := testusers.CustomerFields.Profile + sel, err := f.Selector(ctx, cipher, "$.name") + if err != nil { + t.Fatal(err) + } + read, err := f.Subtree(ctx, cipher, "$.visits[*]", encrypt.JSONReadOptions{WildcardBound: 2}) + if err != nil { + t.Fatal(err) + } + selector, err := sel.Selector().Value() + if err != nil { + t.Fatal(err) + } + tokens, err := read.Tokens().Value() + if err != nil { + t.Fatal(err) + } + tests := []struct { + name string + param any + want driver.Value + }{ + {"selection", sel, selector}, + {"pointer to a selection", &sel, selector}, + {"subtree read", read, tokens}, + {"pointer to a subtree read", &read, tokens}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + valuer, ok := tt.param.(driver.Valuer) + if !ok { + t.Fatalf("%T is not a driver.Valuer", tt.param) + } + got, err := valuer.Value() + if err != nil || got != tt.want { + t.Fatalf("Value() = %v, %v; want the operand", got, err) + } + // The default converter, which database/sql applies to a query + // argument for a driver that does not check the value itself. + converted, err := driver.DefaultParameterConverter.ConvertValue(tt.param) + if err != nil || converted != tt.want { + t.Fatalf("ConvertValue = %v, %v; want the operand", converted, err) + } + if s, _ := converted.(string); strings.Contains(s, "redacted") { + t.Fatalf("bound the redacted text: %s", s) + } + }) + } + // A zero selection or read binds nothing, as a zero selector or zero + // tokens do. + if _, err := (encrypt.JSONSelection[eql.JSONEntry]{}).Value(); !errors.Is(err, encrypt.ErrEncoding) { + t.Fatalf("a zero selection binds: %v", err) + } + if _, err := (encrypt.JSONRead[eql.JSONEntry]{}).Value(); !errors.Is(err, encrypt.ErrEncoding) { + t.Fatalf("a zero read binds: %v", err) + } +} + // A token is a capability and a path's keys can be a customer's data: the // read types print and log neither, under any fmt verb, or through slog. A // child a read opened is plaintext, its key included, and prints and logs diff --git a/languages/golang/encrypt/json_postgres_test.go b/languages/golang/encrypt/json_postgres_test.go index c8fa61a99..a724761c0 100644 --- a/languages/golang/encrypt/json_postgres_test.go +++ b/languages/golang/encrypt/json_postgres_test.go @@ -144,6 +144,9 @@ func TestJSONDocumentsQueryAndReadInPostgres(t *testing.T) { t.Fatal(err) } want("age < 40", ids(`SELECT id FROM customers WHERE (profile -> $1::text) < $2::eql_v4.query_json_leaf ORDER BY id`, age.Selector(), lt), 1, 2) + // The selection itself binds as its selector, through its + // driver.Valuer, and finds the same rows. + want("age < 40, the selection bound", ids(`SELECT id FROM customers WHERE (profile -> $1::text) < $2::eql_v4.query_json_leaf ORDER BY id`, age, lt), 1, 2) rng, err := f.At.Age.Between(ctx, cipher, json.RawMessage(`30`), json.RawMessage(`70`)) if err != nil { t.Fatal(err) @@ -216,6 +219,8 @@ func TestJSONDocumentsQueryAndReadInPostgres(t *testing.T) { if err != nil { t.Fatal(err) } + // The read itself binds as its tokens, through its driver.Valuer. + want("subtree, the read bound", ids(`SELECT DISTINCT id FROM customers, eql_v4.json_tree_subtree(profile, $1::text[]) AS e ORDER BY id`, read), 1, 2) walk := func(budget int) [][]json.RawMessage { t.Helper() groups := make([][]eql.JSONEntry, len(jsonCustomers))