Repository navigation
Split keyring unlock from the unlocked check and stop double prompts - #2627
Merged
Merged
Conversation
…mpts `is_unlocked` used to unlock the keyring as a side effect on KWallet and SecretStorage, so `BorgJob.prepare` could prompt twice (once via `get_password`, once via `is_unlocked`). If the first prompt timed out and the second succeeded, the job failed with the misleading "no longer available" message. The add-repo dialog also read the keyring on every keystroke in the URL field, prompting each time while the keyring was locked. - `is_unlocked` is now a pure check that never prompts. A new `try_unlock()` unlocks if needed (may prompt) and returns whether the keyring is unlocked. KWallet queries `isOpen` by wallet name; Darwin calls `SecKeychainUnlock` to show the system unlock dialog. - `BorgJob.prepare` unlocks at most once, before reading. If that fails, it reports "Please unlock your system password manager…" without consulting the DB fallback. Unencrypted repos no longer prompt; the stored password is only read if the keyring is already unlocked. - `BorgInfoRepoJob.prepare` uses `try_unlock()` when a password is given. - Add-repo autofill only reads the keyring for URLs that look like a repo and only when the keyring is already unlocked. Backend caching and the related locking changes follow separately. Part of #2606
keyring_password() was defined on AddRepoWindow, so ExistingRepoWindow.set_password raised AttributeError on every change of the URL field. Move it to RepoWindow and cover the existing-repo autofill and BorgInfoRepoJob.prepare with tests.
With the wallet closed and an empty passphrase field, nothing opened KWallet any more, so process_result() wrote the passphrase of an encrypted repo with an invalid handle and it was silently lost. Unlock unconditionally in BorgInfoRepoJob.prepare, as the base prepare did before, but still only fail on a locked keyring when a passphrase was entered. Also word the Darwin unlock comment as intent; the dialog is not verified yet.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Separates "is the keyring unlocked" from "unlock the keyring", and uses that to stop repeated unlock prompts.
What was wrong
is_unlockedunlocked the keyring as a side effect, andget_passwordwent through it.BorgJob.preparecalledget_passwordand thenis_unlocked, so it made two unlock attempts. If the first one failed (for example the D-Bus reply timed out while the user was still typing the wallet password) and the second succeeded, the passphrase wasNoneon an unlocked keyring and the job failed with "Your repo passphrase was stored in a password manager which is no longer available. Try unlinking and re-adding your repo." The next run worked.preparealso prompted for repos without encryption.What changed
VortaKeyring.is_unlockedis now a pure check that must not prompt. A newtry_unlock()unlocks if needed, may prompt, and returns whether the keyring is unlocked. The DB keyring uses the default (bool(self.is_unlocked)).is_unlockedreturns True for an open handle, otherwise asksisOpenwith the wallet name. It never callsopen, never shows the "Create Wallet" dialog and never changes the handle.try_unlock()returns a bool on every path and reuses an open handle.get_passwordcallstry_unlock().is_unlockedisnot collection.is_locked().try_unlock()callscollection.unlock()when locked and then re-readsis_locked(), becauseunlock()only reports whether a prompt was dismissed.get_password/set_password/delete_passwordcalltry_unlock().try_unlock()callsSecKeychainUnlockwithout a password when the login keychain is locked. Before, a locked keychain was prompted for implicitly by the read;preparenow unlocks before it reads, so the prompt has to be explicit.BorgJob.preparecallstry_unlock()once, before reading, and only for repos that need a passphrase. For unencrypted repos it reads the stored passphrase only if the keyring is already unlocked. The DB fallback and the "no longer available" check below it are unchanged.BorgInfoRepoJob.prepare(add existing repo) callstry_unlock()once, whether or not a passphrase was entered, and fails with "Please unlock your password manager." only if a passphrase was entered and the keyring stays locked. The unconditional unlock is needed becauseprocess_resultstores the passphrase when the repo turns out to be encrypted, and KWallet needs an open handle for that write.RepoWindow.keyring_password) only looks up URLs that look like a repo (.+:.+, as invalidate(), or an absolute path) and only whenis_unlockedis already True. It never prompts.Related Issue
Part of #2606
This is the first of two changes for that issue. Deferred to the follow-up:
get_keyring()instead of building one per call (the per-connection authorization prompts with KeePassXC), including cache reset for tests, stale KWallet handle recovery and a provider that starts after Vorta;keyring_lockinto the keyring package and covering every backend call with it;borg_job.py;use_system_keyringrow is missing;use_system_keyringchanges (needs a maintainer decision, see the issue).keyring_lockandget_keyring()are untouched here.Behaviour changes
"Locked" refers to a system keyring. With the DB keyring, and with an unlocked system keyring, the outcome of
prepareis the same as before. A repo whoseencryptionis NULL still counts as encrypted.opencalls)BORG_PASSPHRASEif nothing is found)prepare, second attempt ininfo_repo.pyinfo_repo.py; same "Please unlock your password manager." if it stays lockedprepare; continues either wayinfo_repo.py; continues either waySecKeychainUnlockCreating a repo with encryption and changing a passphrase behave as before apart from the single unlock attempt.
Known residual, not addressed here: on KWallet, if another application has the wallet open and Vorta is not yet authorised for it,
is_unlockedis True and the read that follows can show kwalletd's per-application authorisation dialog, also from the autofill.How Has This Been Tested?
All on macOS with Borg 1.4.3;
tests/unit/test_keyring_darwin.py::test_keyring_non_ascii_repodeselected locally because it writes to the real login keychain.tests/unit/test_keyring.py:prepareunlocks once before reading and does not read when the unlock fails; unencrypted repos never unlock and only read from an unlocked keyring;BorgInfoRepoJob.prepareunlocks once with and without a passphrase and only fails for an entered passphrase; with a closed KWallet (mocked D-Bus) and an empty passphrase field,process_resultwrites with the handle fromopen, never with-1.tests/unit/test_kwallet.py(mocked D-Bus): the six existing tests are unchanged; new tests foris_unlocked(noopen, no dialog, handle unchanged, wallet opened elsewhere, open handle needs no D-Bus call) and for every return path oftry_unlock().tests/unit/test_keyring_secretstorage.py(mocked collection; skipped whensecretstorageis not installed): 5 passed withuv run --with secretstorage pytest tests/unit/test_keyring_secretstorage.py.tests/unit/test_keyring_darwin.py:try_unlock()withSecKeychainUnlockand the keychain status mocked.tests/unit/test_repo.py: no keyring read while typing with a locked keyring or for input that is not a repo URL; autofill in the existing-repo dialog. The last one was added becausekeyring_passwordhad first been put onAddRepoWindowonly, which made every URL change in the existing-repo dialog raiseAttributeError; no test covered that dialog's autofill before.uv run nox -- tests/unit --deselect tests/unit/test_keyring_darwin.py::test_keyring_non_ascii_repo: 559 passed, 8 skipped when it completes. It does not always complete, see the next point.tests/unit/test_scheduler.py::test_create_backup_records_a_running_jobin a large share of local runs: 4 of 5 on an otherwise idle machine, against 0 of 4 runs of master interleaved with them. Removing eithertests/unit/test_keyring.pyor the three newtest_repo.pytests from the run made it pass 4 of 4, and that test file alone, or together with the keyring and repo tests, never crashed. The crash is not in code this change touches: a native backtrace shows the main thread delivering a queued call to a bound signal'semitwhose owning Python object has been freed and its memory reused (pyqtBoundSignal_emit->sip_api_py_type_nameon a pointer that decodes to JSON text), while a job worker thread is running. Unmodified master crashes in the same test with the same thread states once the cyclic garbage collector's timing is shifted: withgc.set_threshold(660, 10, 10)set from a pytest plugin, 2 of 3 full runs of master segfaulted there (thresholds 690, 710 and 740: 1 run each, no crash). So this looks like an existing use-after-free that depends on when a collection happens, and the extra tests move that moment; it is not fixed here. The only place that passes a signal'semitto a queued call isQTimer.singleShot(0, self.scheduler.jobs_changed.emit)insrc/vorta/scheduler/state.py, but which object is freed was not established. Whether Linux CI is affected is unknown.tests/integration/test_init.py(create repo, add existing repo): 2 passed.make linthooks (ruff, ruff-format) pass on the changed files.Not tested. The KWallet and SecretStorage backends are covered by mocked tests only; nothing here ran against a real kwalletd, GNOME Keyring or KeePassXC. The macOS
SecKeychainUnlockcall has never been executed: it is only known to load with the given type encoding, and the test mocks it. If it shows no dialog on a locked login keychain, encrypted repos would get "Please unlock your system password manager…" without any prompt, where the read used to prompt before.Manual checks required before merge
security lock-keychain), then start a backup of an encrypted repo: exactly one system unlock dialog, then the backup runs. If no dialog appears, the Darwintry_unlock()needs rework before this is merged. Dismissing the dialog gives "Please unlock your system password manager…".Types of changes
Checklist:
I provide my contribution under the terms of the license of this repository and I affirm the Developer Certificate of Origin.