From 75e2d16e80391af3624fdf07c10bb1677aeb5272 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Fri, 27 Nov 2026 00:20:22 +1100 Subject: [PATCH 1/2] fix(golang): bind a JSONSelection and a JSONRead as their operand JSONSelection and JSONRead had no driver.Valuer Value method. When a program passed one as a query parameter, pgx bound the text that String returns. That text is redacted. A JSONSelection then matched no rows and returned no error. A JSONRead failed with a malformed array literal. Each type now has a Value method that returns its operand's own Value: the selector as text, and the tokens as a text[] literal. A zero selection or read is refused, as a zero selector or empty tokens are. JSONChildren already bound its token in the same way. Refs #1849 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk --- languages/golang/CHANGELOG.md | 11 ++++ languages/golang/cmd/stashgen/README.md | 4 +- languages/golang/encrypt/README.md | 4 +- languages/golang/encrypt/json.go | 13 ++++ languages/golang/encrypt/json_fields_test.go | 66 +++++++++++++++++++ .../golang/encrypt/json_postgres_test.go | 5 ++ 6 files changed, 100 insertions(+), 3 deletions(-) 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..96296e8af 100644 --- a/languages/golang/cmd/stashgen/README.md +++ b/languages/golang/cmd/stashgen/README.md @@ -199,8 +199,8 @@ 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)` | +| `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 | `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)` | diff --git a/languages/golang/encrypt/README.md b/languages/golang/encrypt/README.md index 6e67a1e8a..ad0de4d92 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -809,7 +809,9 @@ 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 +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 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..3de4db39c 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" @@ -215,6 +216,71 @@ func TestACustomersFieldsDeriveEachQuery(t *testing.T) { // child a read opened is plaintext, its key included, and prints and logs // redacted too. The three prepared reads print through one embedded type, // each with its own name and operand. +// A selection and a subtree read bind as their operand. database/sql +// converts a parameter through its driver.Valuer. Without one, pgx binds +// the String form, which is redacted, and the query matches no row and +// returns no error. +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 conversion database/sql applies to a query argument. + 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) + } +} + func TestJSONReadTypesPrintAndLogRedacted(t *testing.T) { c := deterministicEQLClient(t) ctx := context.Background() 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)) From f10d039b118c4cf2b0f243e231f8c71e9e782d37 Mon Sep 17 00:00:00 2001 From: Lindsay Holmwood Date: Fri, 27 Nov 2026 01:13:48 +1100 Subject: [PATCH 2/2] docs(golang): correct the comments and docs for binding a JSON read Review found four small problems in the previous commit: - The new test sat between TestJSONReadTypesPrintAndLogRedacted and its doc comment. The comment now sits above its own test again. - The new test's comment said that a JSONRead bound as its String form matches no row. It fails with a malformed array literal instead. - The new test's comment also said that database/sql always applies the default converter. pgx skips that converter and calls the driver.Valuer itself. The comment now says which path each test covers. - The stashgen README did not say that a JSONChildren binds as its token, and the encrypt README paragraph was not re-wrapped. Refs #1849 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk --- languages/golang/cmd/stashgen/README.md | 2 +- languages/golang/encrypt/README.md | 13 ++++++----- languages/golang/encrypt/json_fields_test.go | 24 ++++++++++++-------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/languages/golang/cmd/stashgen/README.md b/languages/golang/cmd/stashgen/README.md index 96296e8af..2f33c11d8 100644 --- a/languages/golang/cmd/stashgen/README.md +++ b/languages/golang/cmd/stashgen/README.md @@ -201,7 +201,7 @@ The field's `Fields` entry has a method per operation: | `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`; 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 | `eql_v4.json_tree_children(col, $1::text)` | +| `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 ad0de4d92..c7416ef40 100644 --- a/languages/golang/encrypt/README.md +++ b/languages/golang/encrypt/README.md @@ -809,12 +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 `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 +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_fields_test.go b/languages/golang/encrypt/json_fields_test.go index 3de4db39c..2011850d8 100644 --- a/languages/golang/encrypt/json_fields_test.go +++ b/languages/golang/encrypt/json_fields_test.go @@ -211,15 +211,13 @@ func TestACustomersFieldsDeriveEachQuery(t *testing.T) { } } -// 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 -// redacted too. The three prepared reads print through one embedded type, -// each with its own name and operand. -// A selection and a subtree read bind as their operand. database/sql -// converts a parameter through its driver.Valuer. Without one, pgx binds -// the String form, which is redacted, and the query matches no row and -// returns no error. +// 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() @@ -261,7 +259,8 @@ func TestJSONSelectionAndReadBindAsTheirOperand(t *testing.T) { if err != nil || got != tt.want { t.Fatalf("Value() = %v, %v; want the operand", got, err) } - // The conversion database/sql applies to a query argument. + // 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) @@ -281,6 +280,11 @@ func TestJSONSelectionAndReadBindAsTheirOperand(t *testing.T) { } } +// 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 +// redacted too. The three prepared reads print through one embedded type, +// each with its own name and operand. func TestJSONReadTypesPrintAndLogRedacted(t *testing.T) { c := deterministicEQLClient(t) ctx := context.Background()