Skip to content

Commit

Permalink
Merge pull request #83 from leejw51crypto/gnome
Browse files Browse the repository at this point in the history
dashes in secret service backend break roundtrip (fix #82)
  • Loading branch information
mtibben authored Oct 11, 2021
2 parents b50c01a + 6bc6f5a commit 74483f7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
27 changes: 26 additions & 1 deletion libsecret.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
package keyring

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

"strings"

"github.com/godbus/dbus"
"github.com/gsterjov/go-libsecret"
)
Expand Down Expand Up @@ -57,6 +60,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 {

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 @@ -73,7 +98,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
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,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")
}
}

0 comments on commit 74483f7

Please sign in to comment.