Skip to content

fix(golang): bind a JSONSelection and a JSONRead as their operand - #1912

Open
auxesis wants to merge 2 commits into
nextfrom
fix/go-jsonselection-valuer
Open

auxesis wants to merge 2 commits into
nextfrom
fix/go-jsonselection-valuer

Conversation

@auxesis

@auxesis auxesis commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

In the Go SDK, a program can now pass a JSONSelection straight to a query as a parameter. The query then finds the right rows. Before this fix, the query matched no rows and returned no error. JSONRead had the same gap, and this pull request fixes it too.

A JSONSelection contains the selector for a JSON path query (col -> $1::text). A JSONRead contains the tokens for a subtree read (eql_v4.json_tree_subtree(col, $1::text[])). Neither type had a Value method. That method is the driver.Valuer interface, and database/sql uses it to turn a Go value into a query parameter. So pgx bound the text that the type's String method returns. That text is redacted, so that a selector or a token never appears in a log.

  • A JSONSelection bound that way matched no rows, and the query returned no error.
  • A JSONRead bound that way failed with malformed array literal.

Binding .Selector() or .Tokens() worked before this fix, and still works.

Changes

  • SDK (encrypt/json.go): JSONSelection.Value returns the selector as text. JSONRead.Value returns the tokens as a text[] literal. Each one calls the Value method its operand already has, as JSONChildren.Value already did. A zero selection or read is refused with ErrEncoding, as a zero selector or empty tokens are. Both methods use a value receiver, so a value and a pointer both bind.

  • Tests (encrypt/json_fields_test.go): a new test, TestJSONSelectionAndReadBindAsTheirOperand. For a value and a pointer of each type, it checks these things:

    • the value is a driver.Valuer
    • Value() returns the operand
    • the conversion that database/sql applies (driver.DefaultParameterConverter) gives the operand, not the redacted text

    It also checks that a zero value of each type is refused.

  • Tests (encrypt/json_postgres_test.go): TestJSONDocumentsQueryAndReadInPostgres now binds the selection itself and the read itself, and checks the rows each query returns.

  • Docs: a CHANGELOG.md entry under Unreleased, and a note in encrypt/README.md and cmd/stashgen/README.md that each type binds as its operand.

Verification

  • New unit test, without the fix: it did not compile, because Value was undefined. With the zero-value checks left out, all four subtests failed with ... is not a driver.Valuer. With the fix, all four pass.

  • PostgreSQL test: I ran it against PostgreSQL 17 with EQL v4 installed. mise run go:test:postgres does not run on my machine: it installs sqlx-cli. That build needs OpenSSL development headers, and the machine does not have them. So I did its steps by hand:

    1. Built the EQL v4 bundle from this tree: MISE_DISABLE_TOOLS=cargo:sqlx-cli,cargo:cargo-expand mise -C packages/eql run build.
    2. Started a throwaway postgres:17 container.
    3. Installed packages/eql/release/cipherstash-encrypt-v4.sql into it with psql.
    4. Ran go test -run '^TestJSONDocumentsQueryAndReadInPostgres$' ./encrypt with DATABASE_URL set to that container.

    Without the fix, the test failed in the way the issue describes:

    • age < 40, the selection bound: rows [], want [1 2]
    • ERROR: malformed array literal: "encrypt.JSONRead{field: "profile", path: [redacted], tokens: 65}"

    With the fix, it passes.

  • Full suite: CGO_ENABLED=0 go test -timeout=25m -count=1 ./encrypt/... with DATABASE_URL set: every package passes.

  • Race detector: go test -race passes for ./encrypt/... (all but ./encrypt itself), ./stashgen/... and ./cmd/.... For ./encrypt, it passes for every JSON, PostgreSQL, and customer-field test. A whole-package race run of ./encrypt hit Go's default 10-minute timeout, because the WebAssembly guests run slowly under the race detector. CI does not run this module with -race.

  • Lint: gofmt -l . printed nothing, go vet ./encrypt/... passed, and golangci-lint run ./encrypt/ reported 0 issues.

  • Generated code: go generate ./... changed no file.

  • No live CipherStash credentials were used. The tests use the deterministic guest builds.

Related

Closes #1849

Review notes

I checked the other types a program might pass as a parameter. JSONChildren, JSONSelector, JSONToken, JSONTokens, the term types, and the query types already have a Value method. JSONRange and Range each contain two bounds that bind as two parameters, so they need no Value method.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk

Summary by CodeRabbit

  • New Features
    • Go JSON selections and reads can now be passed directly as SQL query parameters, binding their selector or tokens automatically.
  • Bug Fixes
    • Prevented zero-value selections and reads from being bound as query parameters.
  • Documentation
    • Clarified which selector, subtree, and child values can be bound in queries.
    • Updated encrypted JSON guidance to describe direct parameter binding and sensitive-value redaction when printing or logging.

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 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk
@auxesis auxesis added bug Something isn't working SDK Golang Pull requests that update Go code labels Nov 26, 2026
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

JSONSelection and JSONRead now implement driver.Valuer and bind as their selector or tokens. Tests cover driver conversion, zero values, and PostgreSQL queries. Documentation describes direct query-parameter use and redaction.

Changes: JSON query parameter binding

Layer / File(s) Summary
Valuer methods and query binding coverage
languages/golang/encrypt/json.go, languages/golang/encrypt/json_fields_test.go, languages/golang/encrypt/json_postgres_test.go, languages/golang/encrypt/README.md, languages/golang/cmd/stashgen/README.md, languages/golang/CHANGELOG.md
JSONSelection.Value delegates to selector binding, and JSONRead.Value delegates to token binding. Tests cover value and pointer conversion, zero-value errors, and PostgreSQL queries that bind these values directly. Documentation describes the binding behavior and redaction.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • cipherstash/stack#1852 — The issue describes a selector binding as redacted text; this change makes JSONSelection bind as selector text.

Possibly related PRs

  • cipherstash/stack#1374 — It introduced the JSON read and query types whose direct query-parameter binding this PR changes.

Suggested reviewers: coderdan, cipherstash-bot, freshtonic

Merge Risk: Minimal · up to f10d0

This change lets JSONSelection and JSONRead be passed directly as query parameters, instead of binding their redacted text. It is additive and covered by tests, and no merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: JSONSelection and JSONRead now bind as their operand (selector text or tokens) for query parameters. The changeset adds Value() methods to both types, tests for bindin…
Linked Issues check Passed Directly linked issue #1849 requires a Value method on JSONSelection that returns the selector, and a test that binds a JSONSelection through database/sql and finds the row.

Implementation: the revie…
Out of Scope Changes check Passed Changes in the PR:
- JSONSelection.Value (encrypt/json.go): within #1849.
- JSONRead.Value (encrypt/json.go): not named in #1849. It fixes the same binding gap in the same file, using the same delegat…
Full details: Title check

Explanation

The title names the main change: JSONSelection and JSONRead now bind as their operand (selector text or tokens) for query parameters. The changeset adds Value() methods to both types, tests for binding, and a PostgreSQL query check. The title uses the conventional commit prefix and stays concise. It does not mention the zero-value refusal, which is a secondary detail.

Full details: Linked Issues check

Explanation

Directly linked issue #1849 requires a Value method on JSONSelection that returns the selector, and a test that binds a JSONSelection through database/sql and finds the row.

Implementation: the reviewed head file languages/golang/encrypt/json.go defines func (s JSONSelection[E]) Value() (driver.Value, error) { return s.selector.Value() }. This delegates to JSONSelector.Value, which returns the selector text and refuses a zero value with ErrEncoding. The method uses a value receiver, so a value and a pointer both bind. This meets the first requirement.

Tests: the PR summary reports TestJSONSelectionAndReadBindAsTheirOperand (unit, checks driver.Valuer and DefaultParameterConverter output) and an updated TestJSONDocumentsQueryAndReadInPostgres that binds the JSONSelection itself and checks returned rows. The PostgreSQL test was reported as run against PostgreSQL 17 with EQL v4, failing without the fix and passing with it. I did not read the test files at head, so this test evidence rests on the PR summary. The unit test does not exercise pgx directly, but the PostgreSQL test covers the pgx path as described.

The JSONRead Value method is outside #1849. It is assessed under the out-of-scope check.

Full details: Out of Scope Changes check

Explanation

Changes in the PR:

  • JSONSelection.Value (encrypt/json.go): within #1849.
  • JSONRead.Value (encrypt/json.go): not named in #1849. It fixes the same binding gap in the same file, using the same delegation pattern to JSONTokens.Value. The PR summary states that the JSONRead bug produced a malformed array literal error. This is a closely related fix, not an unrelated change. I could not read the JSONRead.Value hunk itself at head because the file read was truncated, so its exact code is based on the PR description.
  • Tests (json_fields_test.go, json_postgres_test.go): support the Value methods. They are in scope.
  • CHANGELOG.md, encrypt/README.md, cmd/stashgen/README.md: documentation of the new binding behavior. They are in scope.

No change was shown to be unrelated to the linked issue or to the bound JSON read behavior. The PR summary does not report other unrelated changes.

Usage-based review receipt

Note

This review was completed with usage-based billing: files reviewed beyond your plan's included limits are billed at $0.25/file. View usage-based billing.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI; Enforced overrides: Organization
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d3381395-2151-42bf-bd6d-72845514e524
Commits

Reviewing files that changed from the base of the PR and between 1885e92 and f10d039.

Files selected for processing: 6

  • languages/golang/CHANGELOG.md
  • languages/golang/cmd/stashgen/README.md
  • languages/golang/encrypt/README.md
  • languages/golang/encrypt/json.go
  • languages/golang/encrypt/json_fields_test.go
  • languages/golang/encrypt/json_postgres_test.go

Limit details: You’ve used all 2 included reviews currently available.

@changeset-bot

changeset-bot Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: f10d039

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@auxesis
auxesis marked this pull request as ready for review November 26, 2026 13:57
@auxesis
auxesis requested a review from a team as a code owner November 26, 2026 13:57

@cipherstash-agent cipherstash-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review attestation

Reviewed 75e2d16 · posted from Claude Code (scheduled routine) · claude-opus-5-5 · effort medium
Sources: code-review · claude-opus-5-5; mattpocock-skills:code-review (Standards and Spec sub-agents) · claude-opus-5-5
Skills: code-review, mattpocock-skills:code-review, company-skills:review-cross-check; company-skills:rust-security was loaded but does not apply to this Go-only change

Findings (4): 4 minor — 4 inline, 0 below.

Each finding was verified at 75e2d16 by company-skills:review-cross-check. Two candidate findings were refuted there and are not posted: CI does run TestJSONDocumentsQueryAndReadInPostgres (through go:test:postgres → the EQL conformance lane, which passed on this commit), and the three one-line Value methods delegate to operands of different types, so they share no logic to extract. The fix itself (JSONSelection.Value, JSONRead.Value) is correct. No tests were run by this review.


Generated by Claude Code

Comment thread languages/golang/cmd/stashgen/README.md Resolved
Comment thread languages/golang/encrypt/README.md Resolved
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 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk
@auxesis

auxesis commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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

bug Something isn't working Golang Pull requests that update Go code SDK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Go SDK: JSONSelection has no Value method, so pgx binds its redacted text and the query matches nothing

2 participants