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

dashes in secret service backend break roundtrip (fix #82) #83

Merged
merged 1 commit into from Oct 11, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion libsecret.go
Expand Up @@ -3,9 +3,12 @@
package keyring

import (
"encoding/hex"
"encoding/json"
"errors"

"strings"

"github.com/godbus/dbus"
"github.com/gsterjov/go-libsecret"
)
Expand Down Expand Up @@ -56,6 +59,28 @@ func (e *secretsError) Error() string {

var errCollectionNotFound = errors.New("The collection does not exist. Please add a key first")

func decodeKeyringString(src string) string {

Choose a reason for hiding this comment

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

Seems like a good function to have some unit tests for ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, i'll add unit test


var dst strings.Builder
for i := 0; i < len(src); i++ {
if src[i] != '_' {
dst.WriteString(string(src[i]))
} else {
if i+3 > len(src) {
return src
}
hexstring := src[i+1 : i+3]
decoded, err := hex.DecodeString(hexstring)
if err != nil {
return src
}
dst.Write(decoded)
i += 2
}
}
return dst.String()
}

func (k *secretsKeyring) openSecrets() error {
session, err := k.service.Open()
if err != nil {
Expand All @@ -72,7 +97,7 @@ func (k *secretsKeyring) openSecrets() error {
path := libsecret.DBusPath + "/collection/" + k.name

for _, collection := range collections {
if string(collection.Path()) == path {
if decodeKeyringString(string(collection.Path())) == path {
k.collection = &collection
return nil
}
Expand Down
23 changes: 23 additions & 0 deletions libsecret_test.go
Expand Up @@ -141,3 +141,26 @@ func TestLibSecretRemoveWhenNotEmpty(t *testing.T) {
t.Fatal(err)
}
}

func TestLibSpecialCharacters(t *testing.T) {

decoded := decodeKeyringString("keyring_2dtest")
if decoded != "keyring-test" {
t.Fatal("incorrect decodeKeyringString")
}

decoded = decodeKeyringString("keyring_2d_2dtest")
if decoded != "keyring--test" {
t.Fatal("incorrect decodeKeyringString")
}

decoded = decodeKeyringString("keyring_2dtest_2d_2d")
if decoded != "keyring-test--" {
t.Fatal("incorrect decodeKeyringString")
}

decoded = decodeKeyringString("_2d_2dkeyring_2dtest_2d_2d")
if decoded != "--keyring-test--" {
t.Fatal("incorrect decodeKeyringString")
}
}