Skip to content

fix(golang): report a cipher's stored error from KeysetID - #2000

Open
auxesis wants to merge 1 commit into
nextfrom
fix/golang-keyset-stored-error
Open

auxesis wants to merge 1 commit into
nextfrom
fix/golang-keyset-stored-error

Conversation

@auxesis

@auxesis auxesis commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

In the Go SDK, client.Keyset(nil).KeysetID(ctx) returned the default keyset's id and no error. A program that checks a tenant's keyset at boot with KeysetID then believed it held a valid keyset. KeysetID now returns ErrEncoding for that cipher, as every other call through it already did.

Changes

  • encrypt/cipher.go: Cipher.KeysetID returns the error the cipher stores before it resolves the keyset. That error is a nil selector, or a value that Scope, Bind, Context, or Audit refused. Its doc comment says so.
  • encrypt/cipher_error_test.go: a test that calls every method on Cipher that returns an error (KeysetID, Contexts, Seal, Open, QueryTerm, Verify, TargetLimit, JSONOpen) on ciphers with a stored error. Each must return that error before the guest is asked. A second test uses the deterministic test client to show Keyset(nil).KeysetID no longer answers with the default keyset's id.
  • CHANGELOG.md: an entry under [Unreleased], Fixed.

The other methods already returned the stored error, through Cipher.plan. KeysetID was the only one that skipped it.

Verification

  • Without the fix, both new tests fail: KeysetID on Keyset(nil) returned 00000000-0000-0000-0000-000000000000 and a nil error. With the fix, both pass.
  • Built the guests with mise run wasm:guest:build, wasm:guest:build:eql, wasm:guest:build:deterministic, wasm:guest:build:eql:deterministic and wasm:auth-guest:build.
  • In languages/golang: CGO_ENABLED=0 go test -timeout=25m ./... passed for every package. go vet ./... passed, gofmt -l . printed nothing, and golangci-lint run ./... (2.14.0) reported 0 issues.
  • No live credentials were used.

Related

Closes #1998

🤖 Generated with Claude Code

https://claude.ai/code/session_01CtEQzxj55RUn35dkbhdwQk

Summary by CodeRabbit

  • Bug Fixes
    • Cipher.KeysetID now reports a stored cipher error when the cipher was created with a nil keyset selector, rather than returning the default keyset’s ID.
    • Refused ciphers now consistently report their stored errors across supported operations, including when access is refused by scope, binding, context, or audit checks.

Client.Keyset(nil) returns a cipher that stores an ErrEncoding and is
bound to the default keyset in the meantime. Every call that encrypts,
decrypts or derives checks the stored error first, through Cipher.plan.
KeysetID did not: it resolved the keyset straight away, so
Keyset(nil).KeysetID returned the default keyset's id and no error. A
program that validates a tenant's keyset at boot with KeysetID then
believed it held a valid one.

KeysetID now returns the stored error before it resolves anything. That
covers a nil selector and any value Scope, Bind, Context or Audit
refused. A new test calls every error-returning method on such ciphers
and checks each returns the stored error before the guest is asked.

Refs #1998

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 27, 2026
@changeset-bot

changeset-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: b8ad961

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

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Cipher.KeysetID now returns a stored cipher error before resolving the keyset. Tests cover stored errors across cipher operations and verify that a nil selector returns ErrEncoding with a zero ID.

Changes: Go cipher stored-error handling

Layer / File(s) Summary
Return stored errors and verify cipher behavior
languages/golang/encrypt/cipher.go, languages/golang/encrypt/cipher_error_test.go, languages/golang/CHANGELOG.md
Cipher.KeysetID returns the stored error before resolving the keyset. Tests check stored errors across eight cipher operations and confirm that a nil selector returns ErrEncoding and a zero ID. The changelog documents this behavior.

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

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • cipherstash/stack#1048 describes Cipher.KeysetID as the keyset resolution point. This change addresses its stored-error behavior for a nil selector.

Suggested reviewers: coderdan

Merge Risk: Minimal · up to b8ad9

Cipher.KeysetID now returns a cipher's stored error instead of the default keyset's ID. This closes a gap where a cipher created with a nil selector, or one that refused a value, reported a successful ID. The change is small, backed by tests and a changelog entry, and has no known merge risk.

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: Cipher.KeysetID now reports the cipher's stored error before resolving its keyset. The changeset matches this. It edits cipher.go, adds a CHANGELOG entry, and adds tes…
Linked Issues check Passed [#1998] Requirement 1: Cipher.KeysetID must return the stored cipher error before it resolves the keyset. At head, languages/golang/encrypt/cipher.go checks `if cph.err != nil { return KeysetID{}, cph…
Out of Scope Changes check Passed All changed files relate to #1998. cipher.go changes KeysetID only, and its doc comment describes the new behavior. cipher_error_test.go holds the two tests required by the issue's Proposal. CHANGELOG…
Full details: Title check

Explanation

The title names the main change: Cipher.KeysetID now reports the cipher's stored error before resolving its keyset. The changeset matches this. It edits cipher.go, adds a CHANGELOG entry, and adds tests for the nil-selector case and the stored-error behaviour. The title is specific and follows the conventional-commit style used in the repository.

Full details: Linked Issues check

Explanation

[#1998] Requirement 1: Cipher.KeysetID must return the stored cipher error before it resolves the keyset. At head, languages/golang/encrypt/cipher.go checks if cph.err != nil { return KeysetID{}, cph.err } before calling cph.client.resolveKeyset. The doc comment states that a cipher from Client.Keyset(nil) fails with ErrEncoding and does not return the default keyset's id. Requirement 2: a test must call every error-returning method on a cipher with a stored error and check that each returns that error. cipher_error_test.go, TestEveryCallReportsTheCiphersStoredError, covers KeysetID, Contexts, Seal, Open, QueryTerm, Verify, TargetLimit and JSONOpen, over refused ciphers from Keyset(nil), Scope, Bind, Context and Audit. It asserts errors.Is(err, ErrEncoding) and errors.Is(err, cph.err). TestKeysetIDOfANilSelectorIsRefused checks the reported case with the deterministic client. Limitation: the diff for cipher.go could not be read through the diff operation, so the change was verified from the full file at head. Client.Keyset (client.go) was not read directly; its stored ErrEncoding is established by the issue text and by the test's assertions, which would fail if no error were stored.

Full details: Out of Scope Changes check

Explanation

All changed files relate to #1998. cipher.go changes KeysetID only, and its doc comment describes the new behavior. cipher_error_test.go holds the two tests required by the issue's Proposal. CHANGELOG.md adds an Unreleased Fixed entry for the same fix. No unrelated behavior change was found in the reviewed content. The cipher.go diff could not be read directly, so the absence of other edits in that file rests on the full file at head and the PR summary.

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: 2304af4c-238e-4f9d-9566-fa326cc9808a
Commits

Reviewing files that changed from the base of the PR and between 9cd9c1c and b8ad961.

Files selected for processing: 3

  • languages/golang/CHANGELOG.md
  • languages/golang/encrypt/cipher.go
  • languages/golang/encrypt/cipher_error_test.go

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

@auxesis
auxesis marked this pull request as ready for review November 27, 2026 14:02
@auxesis
auxesis requested a review from a team as a code owner November 27, 2026 14:02
@auxesis

auxesis commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 27, 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.

@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 b8ad961a · 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 agents) · claude-opus-5-5
Skills: code-review, mattpocock-skills:code-review, review-cross-check, rust-security (loaded; the diff has no Rust, so none of its rules applied)

Findings (2): 1 minor, 1 nit (pre-existing). 1 inline, 1 below.

  • languages/golang/encrypt/cipher.go:37-40 (outside diff) minor The comment on the err field still says it holds "a refused extension part, context or audit value". The new KeysetID doc and the CHANGELOG entry say it also holds a nil selector from Client.Keyset(nil) (client.go:457) and a binding that Bind refused (cipher.go:104-105). Update it to name all of them, and say Scope instead of the deprecated alias Extend.

The fix matches the guard in Cipher.plan, and the new test calls all eight *Cipher methods that return an error. The Fixed placement in the CHANGELOG matches earlier entries such as rc.3 and rc.5, so it is not flagged.


Generated by Claude Code

// Client.Keyset(nil) fails with [ErrEncoding], and does not answer with the
// default keyset's id.
func (cph *Cipher) KeysetID(ctx context.Context) (KeysetID, error) {
if cph.err != nil {

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.

nit · pre-existing Calling KeysetID on a nil *Cipher panics on cph.err. Open instead returns ErrEncoding (records.go:115-119). Before this PR it panicked on cph.client, and every other *Cipher method except Open panics the same way. Guard all of them, for example in copy and plan plus KeysetID and the accessors, or guard none; guarding KeysetID alone would be inconsistent.

Source: code-review · claude-opus-5-5; mattpocock-skills:code-review · claude-opus-5-5


Generated by Claude Code

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: KeysetID on a cipher from Client.Keyset(nil) returns the default keyset's id with no error

2 participants