Skip to content
Merged
Show file tree
Hide file tree
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
47 changes: 47 additions & 0 deletions libs/auth/storage/keyring_size_limit_os_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
102 changes: 102 additions & 0 deletions libs/auth/storage/keyring_size_limit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package storage

import (
"encoding/base64"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zalando/go-keyring"
"golang.org/x/oauth2"
)

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

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;
// 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
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)
}
})
}
}
7 changes: 7 additions & 0 deletions libs/auth/storage/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
Loading