Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions libs/auth/storage/keyring_size_limit_test.go
Original file line number Diff line number Diff line change
@@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[nit] .agents/rules/comments.md asks for Go doc comments that start with the function name, but this one starts with "The OS backends behind zalando/go-keyring...". Start it with TestKeyringStore_Put_RealBackendRejectsLargeToken ....

// 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.
Comment on lines +30 to +31

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[nit] Only fixes that change what keyringStore.Put stores flip this test. A fix in the login path, for example falling back to plaintext on keyring.ErrSetDataTooBig in storeLoginToken (cmd/auth/login.go), would leave it green while users no longer see the error. Soften the comment to say it pins the storage-layer limit of go-keyring.

func TestKeyringStore_Put_RealBackendRejectsLargeToken(t *testing.T) {
store := NewKeyringStore()
account := keyringProbeAccountPrefix + uuid.New().String()
t.Cleanup(func() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[nit] The t.Cleanup always runs store.Delete, which runs security delete-generic-password on macOS and a Credential Manager lookup on Windows. So every run contacts the real keyring, which contradicts the comment that the test does not need an unlocked keychain. On a locked keychain the Delete can wait up to the 3s timeout. Register the cleanup only when the write went through: err := store.Put(...); if err == nil { t.Cleanup(func() { _ = store.Delete(account) }) }. That also makes the 'only matters if the limit disappears' comment unnecessary.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[nit] t.Cleanup always runs store.Delete. On the expected path, that still runs security delete-generic-password on macOS or CredRead on Windows against the real store, even though the doc comment says the test does not need the keychain. On a locked or headless keychain, the cleanup can wait up to the 3s keyring timeout, and its error is thrown away. Register the cleanup only when Put returns nil (for example if err == nil { t.Cleanup(...) } before the assertion). That way the keyring is touched only in the case your comment describes.

// 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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[nit] go-keyring base64-encodes the secret only on macOS. keyring_windows.go checks the raw JSON with len(password) > 2560. On Windows, the 3250-byte token goes over the cap because of its raw size, not because of base64. A reader who changes the token size based on this comment would get the Windows cap wrong. Suggested wording: "The access token alone exceeds both caps (macOS after base64, Windows raw), so the exact refresh token size is irrelevant."

// so the exact refresh token size is irrelevant.
Comment on lines +41 to +42

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[nit] go-keyring does not base64-encode on Windows. The 2560-byte cap applies to the raw JSON envelope, so access tokens from about 2.4 KB already fail there. Reword to something like "The access token alone exceeds both caps", and consider noting the lower Windows threshold.

tok := &oauth2.Token{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[nit] TokenType, RefreshToken and Expiry don't affect the result. The 3250-byte access token alone makes the JSON larger than the roughly 2990-byte macOS budget and the 2560-byte Windows cap. Using &oauth2.Token{AccessToken: strings.Repeat("a", issue7707AccessTokenBytes)} matches how probeWithBackend builds its token, removes the time.Now() call, and lets you drop the comment on lines 41-42.

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)
}
Loading