Re: [PATCH v2] credential/libsecret: load secrets explicitly
Hi Daniel
Here are some comments from someone who uses the libsecret credential helper and has just spent some time reading the api docs (i.e. I don't have any experience writing code that uses libsecret).
On 27/09/2026 23:46, Daniel Martí via GitGitGadget wrote:
> From: =?UTF-8?q?Daniel=20Mart=C3=AD?= <mvdan@mvdan.cc>
> > keyring_get() searches with SECRET_SEARCH_LOAD_SECRETS, then passes
> secret_item_get_secret() of the first match unchecked to
> secret_value_get_text() and secret_value_unref().
This sentence is incredibly dense - I had to go and read the code to understand what it was saying.
> As libsecret
> documents, that secret can be NULL: the search does not load secrets
> of locked items, such as when SECRET_SEARCH_UNLOCK fails to unlock
> them, and it ignores errors from loading secrets. The GNOME keyring> daemon also silently leaves out of its reply any item which is locked
Is that true? My reading of the documentation is that it includes the SecretItem in the list we get back from the search, but we cannot load the secret because it is locked.
Show 9 quoted lines
> or which was deleted after the search matched it, e.g. by a concurrent
> "credential erase" from another git process. We then print
> > secret_value_get_text: assertion 'value' failed
> secret_value_unref: assertion 'value != NULL' failed
> > before git falls back to prompting for the password.
> > We could keep the flag and load the secret explicitly only when it is
> NULL, but SECRET_SEARCH_LOAD_SECRETS is not part of the search call:
> libsecret implements it as a separate GetSecrets D-Bus call after
> SearchItems.
I don't see how this implementation detail is relevant. Surely what matters is the lack of error information, not how SECRET_SEARCH_LOAD_SECRETS is implemented.
> Drop the flag and instead always load the one secret we
> use with secret_item_load_secret_sync(), which reports errors.
If SECRET_SEARCH_UNLOCK fails and so we cannot load a secret because it is still locked, calling secret_item_load_secret_sync() does not tell us why the unlock failed. If we want to provide a better error message in that case I think we need to unlock the secret ourselves.
> This takes as many D-Bus calls as before,
Are we really that worried about the number of D-Bus calls? If we are then we should remove SECRET_SEARCH_UNLOCK and call secret_item_get_locked() to see whether we need to unlock it or not.
> and leaves a single code path
> that runs every time, rather than a fallback that only runs in a rare
> race. libsecret's own secret-tool also loads each secret explicitly
> after searching.
secret-tool appears to use a completely different api to the code here
> An inaccessible item now produces a useful error message The error message is printed with g_critical() which is intended for logging failed assertions[1], not printing runtime error messages. We should use
g_printerr("error: could not load secret: %s", error->message);instead. If we cannot load a secret because it has been erased by another process should we consider that an error as this patch does, or should we treat it like a missing credential behave as if the initial lookup had failed?
> instead of
> the assertion failures, and git still falls back to prompting. The
> race needs a concurrent process or a locked keyring to trigger, so
> there is no test.
I don't understand why needing a locked keyring means we cannot have a test. "secret-tool lock" will lock the keyring. There maybe other reasons that make it hard to test but it would be helpful to give a bit more detail.
> + if (!secret_item_load_secret_sync(item, NULL, &error)) {
> + g_critical("could not load secret: %s", error->message);While this matches the existing code, we should be using g_printerr().
> + g_error_free(error);
> + g_list_free_full(items, g_object_unref);
It is a bit of a pain to have to free this in two paths. We could avoid that by doing
loaded = secret_item_load_secret_sync(item, NULL, &error);
g_list_free_full(items, g_object_unref);
if (!loaded) {
/* report error and return */
}and removing the call to g_list_free_full() at the end of the function. That would match how we handle freeing the hash table of attributes after the call to secret_service_search_sync().
> + return EXIT_FAILURE;
> + }
> secret = secret_item_get_secret(item);
Are we sure this can never fail now, or should we check secret is non-NULL to avoid future problems?
Thanks
Phillip
[1] https://docs.gtk.org/glib/func.critical.html
> attributes = secret_item_get_attributes(item);
> > > base-commit: 34f06850c16c7f7ac822b1adc71354f11b0f2ca3