Skip to content

Commit

Permalink
Merge pull request #621 from inetic/linux-cold-keyring
Browse files Browse the repository at this point in the history
Linux fix: Search with schemas fails in cold keyrings
  • Loading branch information
juliansteenbakker authored Oct 3, 2023
2 parents 0b5083e + 5e2d681 commit a9a7100
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion flutter_secure_storage/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ platforms:
linux:
macos:
web:
windows:
windows:
33 changes: 33 additions & 0 deletions flutter_secure_storage_linux/linux/include/Secret.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class SecretStorage {

void deleteItem(const char *key) {
nlohmann::json root = readFromKeyring();
if (root.is_null()) {
return;
}
root.erase(key);
storeToKeyring(root);
}
Expand All @@ -70,6 +73,8 @@ class SecretStorage {
nlohmann::json value;
g_autoptr(GError) err = nullptr;

warmupKeyring();

secret_autofree gchar *result = secret_password_lookupv_sync(
&the_schema, m_attributes.getGHashTable(), nullptr, &err);

Expand All @@ -81,4 +86,32 @@ class SecretStorage {
}
return value;
}

private:
// Search with schemas fails in cold keyrings.
// https://gitlab.gnome.org/GNOME/gnome-keyring/-/issues/89
//
// Note that we're not using the workaround mentioned in the above issue. Instead, we're using
// a workaround as implemented in http://crbug.com/660005. Reason being that with the lookup
// approach we can't distinguish whether the keyring was actually unlocked or whether the user
// cancelled the password prompt.
void warmupKeyring() {
g_autoptr(GError) err = nullptr;

FHashTable attributes;
attributes.insert("explanation", "Because of quirks in the gnome libsecret API, "
"flutter_secret_storage needs to store a dummy entry to guarantee that "
"this keyring was properly unlocked. More details at http://crbug.com/660005.");

const gchar* dummy_label = "FlutterSecureStorage Control";

// Store a dummy entry without `the_schema`.
bool success = secret_password_storev_sync(
NULL, attributes.getGHashTable(), nullptr, dummy_label,
"The meaning of life", nullptr, &err);

if (!success) {
throw "Failed to unlock the keyring";
}
}
};

0 comments on commit a9a7100

Please sign in to comment.