From afcd3041afea70d7a401f1b65ba8a9cc70837124 Mon Sep 17 00:00:00 2001 From: Jan Rose Date: Tue, 17 Nov 2026 16:26:23 +0000 Subject: [PATCH 1/3] Add a test pinning the OS keyring secret size cap behind #7707 `databricks auth login` fails on macOS with `store token: data passed to Set was too big` when the U2M access token is ~3250 bytes (https://github.com/databricks/cli/issues/7707). zalando/go-keyring v0.2.8 caps the base64-encoded `security -i add-generic-password` line at 4096 bytes on macOS and the credential blob at 2560 bytes on Windows, and the CLI stores the whole token envelope in one keyring item, so the access token alone exceeds both caps. The test calls the real backend with a token of that size and asserts ErrSetDataTooBig. The size check runs before the keyring is contacted, so it is deterministic on CI runners with a locked keychain. It pins the current behavior so a fix has to flip it on purpose. Co-authored-by: Isaac --- libs/auth/storage/keyring_size_limit_test.go | 52 ++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 libs/auth/storage/keyring_size_limit_test.go diff --git a/libs/auth/storage/keyring_size_limit_test.go b/libs/auth/storage/keyring_size_limit_test.go new file mode 100644 index 00000000000..7871c9367fc --- /dev/null +++ b/libs/auth/storage/keyring_size_limit_test.go @@ -0,0 +1,52 @@ +//go:build darwin || windows + +package storage + +import ( + "strings" + "testing" + "time" + "uuid" + + "github.com/stretchr/testify/require" + "github.com/zalando/go-keyring" + "golang.org/x/oauth2" +) + +// issue7707AccessTokenBytes is the size of the U2M access tokens reported in +// https://github.com/databricks/cli/issues/7707. Tokens of 800-950 bytes from +// the same account store fine. +const issue7707AccessTokenBytes = 3250 + +// The OS backends behind zalando/go-keyring cap the secret size and return +// ErrSetDataTooBig before touching the keyring, so this test does not need an +// unlocked keychain or Credential Manager: +// - macOS: the base64-encoded `security -i add-generic-password` line must be +// at most 4096 bytes (https://github.com/zalando/go-keyring/blob/v0.2.8/keyring_darwin.go), +// which leaves ~3000 bytes for the JSON envelope. +// - Windows: the credential blob must be at most 2560 bytes +// (https://github.com/zalando/go-keyring/blob/v0.2.8/keyring_windows.go). +// +// Linux Secret Service has no such cap, hence the build constraint. This test +// pins the current behavior so a fix for #7707 has to flip it on purpose. +func TestKeyringStore_Put_RealBackendRejectsLargeToken(t *testing.T) { + store := NewKeyringStore() + account := keyringProbeAccountPrefix + uuid.New().String() + t.Cleanup(func() { + // Nothing is written when Put fails as expected; this only matters if + // the limit disappears and the token lands in the real keyring. + _ = store.Delete(account) + }) + + // The access token alone already exceeds both caps once base64-encoded, + // so the exact refresh token size is irrelevant. + tok := &oauth2.Token{ + AccessToken: strings.Repeat("a", issue7707AccessTokenBytes), + TokenType: "Bearer", + RefreshToken: strings.Repeat("r", 64), + Expiry: time.Now().Add(time.Hour), + } + + err := store.Put(account, Entry{Token: tok}) + require.ErrorIs(t, err, keyring.ErrSetDataTooBig) +} From c3b801d5d60c5377054a5d9214151305e6c99fde Mon Sep 17 00:00:00 2001 From: Jan Rose Date: Wed, 18 Nov 2026 07:13:04 +0000 Subject: [PATCH 2/3] Model go-keyring's per-OS secret caps in fakeBackend instead of the real keyring The first revision drove the real OS keyring from a unit test, which only built on the PR macOS and Windows cells, ran a delete against the keychain on every run, and would write a real secret if the cap were ever lifted. fakeBackend gains an optional size hook. The macOS rule (base64 secret on one `security -i` line, rejected over 4096 bytes) and the Windows rule (raw blob over 2560 bytes) are transcribed from go-keyring v0.2.8, so the test pins the same behavior on every OS without touching a keychain. The token shrinks to the access token alone, which exceeds both caps by itself. Co-authored-by: Isaac --- libs/auth/storage/keyring_size_limit_test.go | 125 +++++++++++++------ libs/auth/storage/keyring_test.go | 7 ++ 2 files changed, 94 insertions(+), 38 deletions(-) diff --git a/libs/auth/storage/keyring_size_limit_test.go b/libs/auth/storage/keyring_size_limit_test.go index 7871c9367fc..45bfe6dce22 100644 --- a/libs/auth/storage/keyring_size_limit_test.go +++ b/libs/auth/storage/keyring_size_limit_test.go @@ -1,52 +1,101 @@ -//go:build darwin || windows - package storage import ( + "encoding/base64" + "fmt" "strings" "testing" - "time" - "uuid" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/zalando/go-keyring" "golang.org/x/oauth2" ) -// issue7707AccessTokenBytes is the size of the U2M access tokens reported in -// https://github.com/databricks/cli/issues/7707. Tokens of 800-950 bytes from -// the same account store fine. -const issue7707AccessTokenBytes = 3250 - -// The OS backends behind zalando/go-keyring cap the secret size and return -// ErrSetDataTooBig before touching the keyring, so this test does not need an -// unlocked keychain or Credential Manager: -// - macOS: the base64-encoded `security -i add-generic-password` line must be -// at most 4096 bytes (https://github.com/zalando/go-keyring/blob/v0.2.8/keyring_darwin.go), -// which leaves ~3000 bytes for the JSON envelope. -// - Windows: the credential blob must be at most 2560 bytes -// (https://github.com/zalando/go-keyring/blob/v0.2.8/keyring_windows.go). -// -// Linux Secret Service has no such cap, hence the build constraint. This test -// pins the current behavior so a fix for #7707 has to flip it on purpose. -func TestKeyringStore_Put_RealBackendRejectsLargeToken(t *testing.T) { - store := NewKeyringStore() - account := keyringProbeAccountPrefix + uuid.New().String() - t.Cleanup(func() { - // Nothing is written when Put fails as expected; this only matters if - // the limit disappears and the token lands in the real keyring. - _ = store.Delete(account) - }) - - // The access token alone already exceeds both caps once base64-encoded, - // so the exact refresh token size is irrelevant. - tok := &oauth2.Token{ - AccessToken: strings.Repeat("a", issue7707AccessTokenBytes), - TokenType: "Bearer", - RefreshToken: strings.Repeat("r", 64), - Expiry: time.Now().Add(time.Hour), +// Access token sizes reported in https://github.com/databricks/cli/issues/7707: +// U2M tokens of about 3250 bytes fail to store on macOS, while tokens of +// 800-950 bytes from the same account store fine. +const ( + issue7707LargeAccessTokenBytes = 3250 + issue7707SmallAccessTokenBytes = 950 +) + +// darwinSecurityCommandTooBig mirrors the size check in zalando/go-keyring +// v0.2.8 keyring_darwin.go. The backend base64-encodes the secret, builds one +// `add-generic-password` line for `security -i`, and rejects the write when +// that line exceeds 4096 bytes: the tool reads at most 4095 bytes per line and +// would otherwise store a truncated secret. +func darwinSecurityCommandTooBig(service, account, secret string) bool { + password := "go-keyring-base64:" + base64.StdEncoding.EncodeToString([]byte(secret)) + command := fmt.Sprintf("add-generic-password -U -s %s -a %s -w %s\n", shellQuote(service), shellQuote(account), shellQuote(password)) + return len(command) > 4096 +} + +// windowsCredentialBlobTooBig mirrors zalando/go-keyring v0.2.8 +// keyring_windows.go, which stores the raw secret and rejects blobs over 2560 +// bytes. +func windowsCredentialBlobTooBig(_, _, secret string) bool { + return len(secret) > 2560 +} + +// shellQuote mirrors go-keyring's internal shellescape.Quote: a string made of +// [A-Za-z0-9_@%+=:,./-] passes through unchanged, anything else is +// single-quoted. +func shellQuote(s string) string { + if s == "" { + return "''" } + if strings.ContainsFunc(s, func(r rune) bool { return !shellSafe(r) }) { + return "'" + strings.ReplaceAll(s, "'", `'"'"'`) + "'" + } + return s +} - err := store.Put(account, Entry{Token: tok}) - require.ErrorIs(t, err, keyring.ErrSetDataTooBig) +func shellSafe(r rune) bool { + if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') { + return true + } + return strings.ContainsRune("_@%+=:,./-", r) +} + +// TestKeyringStore_Put_OSSecretSizeCaps pins how the CLI's keyring envelope +// fares against go-keyring's per-OS secret caps. The caps are modeled in +// fakeBackend so the test is keychain-agnostic and runs on every OS; the real +// backends gave the same results on the macOS and Windows CI cells of the +// first revision of https://github.com/databricks/cli/pull/7727. A fix for +// #7707 that changes what Put stores must update this test on purpose. +func TestKeyringStore_Put_OSSecretSizeCaps(t *testing.T) { + cases := []struct { + name string + secretTooBig func(service, account, secret string) bool + largeRejected bool + }{ + {name: "darwin security -i line", secretTooBig: darwinSecurityCommandTooBig, largeRejected: true}, + {name: "windows credential blob", secretTooBig: windowsCredentialBlobTooBig, largeRejected: true}, + {name: "linux secret service has no cap", secretTooBig: nil, largeRejected: false}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + backend := newFakeBackend() + backend.secretTooBig = tc.secretTooBig + store := newTestStore(backend) + + small := &oauth2.Token{AccessToken: strings.Repeat("a", issue7707SmallAccessTokenBytes)} + require.NoError(t, store.Put("my-profile", Entry{Token: small})) + got, err := store.Lookup("my-profile") + require.NoError(t, err) + assert.Equal(t, small.AccessToken, got.Token.AccessToken) + + // The access token alone exceeds both caps (macOS after base64, + // Windows raw), so no other token field is needed. + large := &oauth2.Token{AccessToken: strings.Repeat("a", issue7707LargeAccessTokenBytes)} + err = store.Put("my-profile", Entry{Token: large}) + if tc.largeRejected { + require.ErrorIs(t, err, keyring.ErrSetDataTooBig) + } else { + require.NoError(t, err) + } + }) + } } diff --git a/libs/auth/storage/keyring_test.go b/libs/auth/storage/keyring_test.go index 77a7ced4779..c94c6274fe3 100644 --- a/libs/auth/storage/keyring_test.go +++ b/libs/auth/storage/keyring_test.go @@ -24,6 +24,10 @@ type fakeBackend struct { setBlock bool // if true, Set blocks forever (for timeout tests) getBlock bool deleteBlock bool + + // secretTooBig, when set, mirrors an OS backend's size check: Set rejects + // the secret with keyring.ErrSetDataTooBig before storing anything. + secretTooBig func(service, account, secret string) bool } func newFakeBackend() *fakeBackend { @@ -36,6 +40,9 @@ func (f *fakeBackend) Set(service, account, secret string) error { if f.setBlock { select {} } + if f.secretTooBig != nil && f.secretTooBig(service, account, secret) { + return keyring.ErrSetDataTooBig + } if f.setErr != nil { return f.setErr } From 5df7c967f34d581766fdfcaa9180b8288dad4452 Mon Sep 17 00:00:00 2001 From: Jan Rose Date: Wed, 18 Nov 2026 08:59:51 +0000 Subject: [PATCH 3/3] Restore the real-backend keyring size test, gated to CI The fakeBackend table test models go-keyring v0.2.8's caps and cannot notice when a bumped or replaced keyring library accepts larger secrets. Bring back the darwin/windows test against the real backend as that signal, with the review feedback folded in: it runs only when CI is set so local runs never depend on a keychain, the cleanup is registered only after a successful write so the expected path never contacts the keyring, the token is just the access token, and the failure message says which caps to re-verify. Co-authored-by: Isaac --- .../storage/keyring_size_limit_os_test.go | 47 +++++++++++++++++++ libs/auth/storage/keyring_size_limit_test.go | 9 ++-- 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 libs/auth/storage/keyring_size_limit_os_test.go diff --git a/libs/auth/storage/keyring_size_limit_os_test.go b/libs/auth/storage/keyring_size_limit_os_test.go new file mode 100644 index 00000000000..ea2451c4b2d --- /dev/null +++ b/libs/auth/storage/keyring_size_limit_os_test.go @@ -0,0 +1,47 @@ +//go:build darwin || windows + +package storage + +import ( + "os" + "strings" + "testing" + "uuid" + + "github.com/stretchr/testify/require" + "github.com/zalando/go-keyring" + "golang.org/x/oauth2" +) + +// TestKeyringStore_Put_RealBackendRejectsLargeToken pins that the go-keyring +// release in go.mod rejects a 3250-byte access token on macOS and Windows, +// which is the behavior fakeBackend's caps in keyring_size_limit_test.go +// transcribe. It is the only test here that observes the real library, so it +// is what fails when a bumped or replaced keyring library accepts larger +// secrets and the modeled caps go stale. +// +// It calls the real backend and therefore runs on CI only (GitHub Actions +// sets CI=true). The library's size check fires before the OS keyring is +// contacted, so on the expected path nothing is written. Only once the cap is +// gone does the token land in the runner's keyring, and then the cleanup +// removes it. +func TestKeyringStore_Put_RealBackendRejectsLargeToken(t *testing.T) { + if os.Getenv("CI") == "" { + t.Skip("touches the real OS keyring; runs on CI only") + } + + store := NewKeyringStore() + account := keyringProbeAccountPrefix + uuid.New().String() + + // The access token alone exceeds both caps (macOS after base64, Windows + // raw), so no other token field is needed. + tok := &oauth2.Token{AccessToken: strings.Repeat("a", issue7707LargeAccessTokenBytes)} + + err := store.Put(account, Entry{Token: tok}) + if err == nil { + t.Cleanup(func() { _ = store.Delete(account) }) + } + require.ErrorIs(t, err, keyring.ErrSetDataTooBig, + "go-keyring accepted a %d-byte access token; the OS caps modeled in fakeBackend are stale, re-verify them against the new backend", + issue7707LargeAccessTokenBytes) +} diff --git a/libs/auth/storage/keyring_size_limit_test.go b/libs/auth/storage/keyring_size_limit_test.go index 45bfe6dce22..aefe3cd100d 100644 --- a/libs/auth/storage/keyring_size_limit_test.go +++ b/libs/auth/storage/keyring_size_limit_test.go @@ -60,10 +60,11 @@ func shellSafe(r rune) bool { // TestKeyringStore_Put_OSSecretSizeCaps pins how the CLI's keyring envelope // fares against go-keyring's per-OS secret caps. The caps are modeled in -// fakeBackend so the test is keychain-agnostic and runs on every OS; the real -// backends gave the same results on the macOS and Windows CI cells of the -// first revision of https://github.com/databricks/cli/pull/7727. A fix for -// #7707 that changes what Put stores must update this test on purpose. +// fakeBackend so the test is keychain-agnostic and runs on every OS; +// TestKeyringStore_Put_RealBackendRejectsLargeToken in +// keyring_size_limit_os_test.go checks the real library on CI and fails when +// the modeled caps go stale. A fix for #7707 that changes what Put stores +// must update this test on purpose. func TestKeyringStore_Put_OSSecretSizeCaps(t *testing.T) { cases := []struct { name string