Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] WinCred kr.Keys() return empty when kr.Set() doesn't call before #97

Open
Halimao opened this issue Dec 9, 2021 · 1 comment
Open

Comments

@Halimao
Copy link

Halimao commented Dec 9, 2021

I have set keyring:default:key1, keyring:default:key2 already, and can be found in windows Credential Manager.
then test following code:

kr, err := keyring.Open(keyring.Config{
    AllowedBackends: []keyring.BackendType{keyring.WinCredBackend},
})
if err != nil {
    t.Fatal(err)
}
keys, err := kr.Keys()
if err != nil {
    t.Fatal(err)
}

it would return empty keys. but if I call kr.Set() before kr.Keys(), it would return keyring:default:key1 and keyring:default:key2

kr, err := keyring.Open(keyring.Config{
    AllowedBackends: []keyring.BackendType{keyring.WinCredBackend},
})
if err != nil {
    t.Fatal(err)
}
item1 := keyring.Item{
    Key:  "test3",
    Data: []byte("loose lips sink ships"),
}
err = kr.Set(item1)
if err != nil {
    t.Fatal(err)
}
keys, err := kr.Keys()
if err != nil {
    t.Fatal(err)
}
@Halimao
Copy link
Author

Halimao commented Dec 9, 2021

kr, err := keyring.Open(keyring.Config{
    AllowedBackends: []keyring.BackendType{keyring.WinCredBackend},
})
if err != nil {
    t.Fatal(err)
}
key, err := kr.Get("key1")
if err != nil {
    t.Fatal(err)
}
t.Log(key)

will cause panic, it's because calling cred, err := wincred.GetGenericCredential(k.credentialName(key)) returns nil, nil without error, and cred is used for cred.CredentialBlob caused panic

func (k *windowsKeyring) Get(key string) (Item, error) {
	cred, err := wincred.GetGenericCredential(k.credentialName(key))
	if err != nil {
		if err == elementNotFoundError {
			return Item{}, ErrKeyNotFound
		}
		return Item{}, err
	}

	item := Item{
		Key:  key,
		Data: cred.CredentialBlob,
	}

	return item, nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant