Compare commits

..
Author SHA1 Message Date
nvrl 64864562ef fix(core): save the session by replacing its keyring item (#78, B291)
Fedora 45 and 46 ship oo7-daemon 0.7.0~beta^1.e91b98a, which applies
SetSecret only in memory (fixed upstream in 7fb11dd, after that
snapshot). Every token rotation saved there was lost at the next
reboot, and the next start found a spent refresh token.

An existing entry is now written with CreateItem and replace, in the
old item's collection with its label and attributes, which that build
persists. A duplicate left behind is removed; the first save, a locked
item, ambiguity or any failure fall back to SetSecret. No prompt is
ever shown on this path.

The daemon also logs a short fingerprint of the refresh token at each
resume and each save, so a log across a reboot shows whether the
keyring returned the last token saved.
2026-11-15 20:31:43 +01:00
nvrl ce3bfa217d Merge pull request #97 from narrrl/fix/db-lock-fork-race
fix(core): retry the database lock briefly before calling it another daemon (B290)
2026-11-15 19:44:24 +01:00
nvrl 593df5c401 fix(core): retry the database lock briefly before calling it another daemon (B290)
A child spawned by another thread holds a copy of the lock descriptor
between its fork and its exec, so a reopen right after a drop could be
refused. Seen once in CI in
what_a_create_sent_outlives_a_newer_blob_its_trash_and_a_restart.
2026-11-15 19:42:22 +01:00
nvrl fd6b2d3e85 Merge pull request #96 from narrrl/chore/screenshots
chore: retake the screenshots from a demo account, by script
2026-11-15 19:20:45 +01:00
10 changed files with 371 additions and 14 deletions

No files matched your search

Generated
+1
View File
@@ -3604,6 +3604,7 @@ dependencies = [
"async-trait",
"base64 0.23.1",
"chrono",
"dbus-secret-service",
"dbus-secret-service-keyring-store",
"directories",
"ignore",
+3
View File
@@ -60,6 +60,9 @@ rusqlite = { version = "0.41", features = ["bundled"] }
# with no `target` it matches items v3 wrote, so existing logins survive.
keyring-core = "1"
dbus-secret-service-keyring-store = { version = "1", features = ["crypto-rust"] }
# The same Secret Service client the store wraps, used directly to replace an
# item instead of editing it, which oo7 before 0.7.0 kept only in memory (B291).
dbus-secret-service = { version = "4.1", features = ["crypto-rust"] }
clap = { version = "4", features = ["derive"] }
# `unstable-dynamic`: Tab calls `pdfs` back, so arguments complete from the
# running service. Only `pdfs-cli/src/complete.rs` uses it.
+1
View File
@@ -26,6 +26,7 @@ zip.workspace = true
rusqlite.workspace = true
keyring-core.workspace = true
dbus-secret-service-keyring-store.workspace = true
dbus-secret-service.workspace = true
parking_lot.workspace = true
async-trait.workspace = true
libc.workspace = true
+87 -1
View File
@@ -490,11 +490,48 @@ fn classify_verification_gate(e: ProtonError) -> Error {
}
}
/// Save `stored` to the keyring.
///
/// An existing entry is replaced by a new item rather than edited, because
/// oo7 before 0.7.0 keeps an edit only in memory and a reboot brings the
/// spent refresh token back (`docs/BUGS.md` B291). The usual write is the
/// fallback: for the first save, and whenever replacing is not possible.
fn write_stored(stored: &StoredSession) -> Result<()> {
let json = Zeroizing::new(serde_json::to_string(stored)?);
if replace_items() {
match crate::secretservice::replace(KEYRING_SERVICE, KEYRING_USER, json.as_bytes()) {
crate::secretservice::Replaced::Saved => return Ok(()),
crate::secretservice::Replaced::Skipped(why) => {
tracing::debug!(%why, "not replacing the keyring item; writing it in place");
}
}
}
with_keyring(|entry| entry.set_password(&json))
}
/// Whether [`write_stored`] talks to the Secret Service directly: always,
/// except in tests that use the in-memory store.
#[cfg(not(test))]
fn replace_items() -> bool {
true
}
#[cfg(test)]
fn replace_items() -> bool {
tests::MOCK_STORE.lock().is_none()
}
/// A short, one-way tag for a refresh token, for the log: enough to tell
/// whether the token a start finds is the one the last save wrote, and
/// nothing that helps anyone use it.
fn fingerprint(token: &str) -> String {
use sha2::{Digest, Sha256};
Sha256::digest(token.as_bytes())[..4]
.iter()
.map(|b| format!("{b:02x}"))
.collect()
}
/// Load the persisted session blob, or `Error::NotLoggedIn` if absent.
pub fn load() -> Result<StoredSession> {
match with_keyring(|entry| entry.get_password()) {
@@ -703,6 +740,10 @@ fn resume_stored(stored: &StoredSession) -> Result<ProtonApiSession> {
resumed.access_token = tokens.access_token.clone();
resumed.refresh_token = tokens.refresh_token.clone();
}
tracing::info!(
refresh = %fingerprint(&resumed.refresh_token),
"resuming the stored session"
);
let session = ProtonApiSession::resume(client_config(), resumed.to_params())?;
let sink = register_refresh_handler(&session, stored.refresh_token.clone());
if let Some(tokens) = unsaved {
@@ -1082,8 +1123,11 @@ impl TokenSink {
) {
Rotation::Taken => {
write_stored(&stored)?;
tracing::info!(
refresh = %fingerprint(&tokens.refresh_token),
"successfully auto-persisted refreshed tokens in keyring"
);
state.stored_refresh = tokens.refresh_token;
tracing::info!("successfully auto-persisted refreshed tokens in keyring");
}
Rotation::MovedOn => {
tracing::info!(
@@ -1408,6 +1452,48 @@ mod tests {
keyring_core::unset_default_store();
}
/// B291: every save after the first replaces the keyring item, so a
/// provider that keeps an edited secret only in memory (oo7 before 0.7.0)
/// still has the last one after a restart, and only one item is left.
///
/// Run like [`real_secret_service_survives_a_provider_restart`], in a
/// throwaway session bus only.
#[test]
#[ignore = "needs a throwaway Secret Service; see real_secret_service_survives_a_provider_restart"]
fn real_secret_service_keeps_the_last_save_across_a_provider_restart() {
if std::env::var_os("PDFS_SECRET_SERVICE_RESTART_TEST").is_none() {
return;
}
let restart = std::env::var("PDFS_RESTART_PROVIDER").expect("PDFS_RESTART_PROVIDER");
let _turn = KEYRING.lock();
*MOCK_STORE.lock() = None;
keyring_core::unset_default_store();
for refresh in ["r1", "r2", "r3"] {
write_stored(&stored_with(refresh)).unwrap();
}
let status = std::process::Command::new("sh")
.args(["-c", &restart])
.status()
.unwrap();
assert!(status.success(), "restarting the provider failed");
std::thread::sleep(Duration::from_secs(1));
assert_eq!(load().unwrap().refresh_token, "r3");
let ss =
dbus_secret_service::SecretService::connect(dbus_secret_service::EncryptionType::Plain)
.unwrap();
let found = ss
.search_items(std::collections::HashMap::from([
("service", KEYRING_SERVICE),
("username", KEYRING_USER),
]))
.unwrap();
assert_eq!(found.unlocked.len() + found.locked.len(), 1);
with_keyring(|e| e.delete_credential()).unwrap();
keyring_core::unset_default_store();
}
fn stored_with(refresh: &str) -> StoredSession {
serde_json::from_str(&blob("").replace(r#""r""#, &format!("{refresh:?}"))).unwrap()
}
+23 -6
View File
@@ -445,6 +445,13 @@ fn is_in_memory(path: &Path) -> bool {
/// SQLite uses POSIX record locks, so the two never interact, and a lock file
/// left behind carries no state — the lock lives in the kernel and dies with
/// the process, however it exits.
///
/// A refusal is retried for [`LOCK_RETRY`] before it counts as another
/// daemon. A child this process spawns holds a copy of every descriptor from
/// its fork until its exec closes them, and `flock` belongs to the open file,
/// not the descriptor. So a lock the process has just let go of can stay held
/// for that moment, and a reopen right after a drop is refused
/// (`docs/BUGS.md` B290).
fn acquire_single_writer_lock(path: &Path) -> Result<Option<File>> {
if is_in_memory(path) {
return Ok(None);
@@ -456,22 +463,32 @@ fn acquire_single_writer_lock(path: &Path) -> Result<Option<File>> {
.truncate(false)
.write(true)
.open(&lock_path)?;
// SAFETY: `file` owns the descriptor and outlives the call.
let rc = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) };
if rc != 0 {
let deadline = std::time::Instant::now() + LOCK_RETRY;
loop {
// SAFETY: `file` owns the descriptor and outlives the call.
let rc = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) };
if rc == 0 {
return Ok(Some(file));
}
let e = std::io::Error::last_os_error();
if e.raw_os_error() == Some(libc::EWOULDBLOCK) {
if e.raw_os_error() != Some(libc::EWOULDBLOCK) {
return Err(Error::Io(e));
}
if std::time::Instant::now() >= deadline {
return Err(Error::Other(format!(
"another Proton Drive daemon is already using {} — \
stop it first (`systemctl --user stop proton-drive.service`)",
path.display()
)));
}
return Err(Error::Io(e));
std::thread::sleep(std::time::Duration::from_millis(20));
}
Ok(Some(file))
}
/// How long a refused single-writer lock is retried; far longer than a fork
/// takes to reach its exec, far shorter than anyone notices.
const LOCK_RETRY: std::time::Duration = std::time::Duration::from_millis(500);
fn lock_path_for(path: &Path) -> PathBuf {
let mut name = path.as_os_str().to_owned();
name.push(".lock");
+1
View File
@@ -23,6 +23,7 @@ pub mod places;
pub mod profile;
pub mod sdkcache;
pub mod search;
mod secretservice;
pub mod service;
pub mod shell;
pub mod similar;
+135
View File
@@ -0,0 +1,135 @@
//! Saving a new secret into an existing Secret Service item by replacing the
//! item, not by editing its secret (`docs/BUGS.md` B291).
//!
//! oo7-daemon before 0.7.0 applies `SetSecret` only in memory: the keyring
//! file keeps the old secret, and it comes back once the daemon restarts.
//! Fedora 45 ships such a build (`0.7.0~beta^1.e91b98a`). Every refreshed
//! sign-in pdfs saved there was gone after a reboot, and the next start found
//! a refresh token Proton had already spent.
//!
//! `CreateItem` with `replace` is written to the file there, and every
//! provider implements it. So a secret that changes is written as a new item
//! that replaces the old one: in the old one's collection, with its label and
//! all of its attributes, so the provider recognises it as the same entry.
use dbus_secret_service::{Collection, EncryptionType, Path, SecretService};
use std::collections::HashMap;
/// The content type the keyring store writes its secrets with.
const CONTENT_TYPE: &str = "application/octet-stream";
/// What [`replace`] did.
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Replaced {
/// The secret is saved.
Saved,
/// Nothing was changed; save it the usual way. Says why, for the log.
Skipped(String),
}
/// Save `secret` into the one item for `service`/`user` by replacing it.
///
/// Skips, changing nothing, when there is no such item yet (the store creates
/// it), when there is more than one, when it is locked, or when the Secret
/// Service fails before the new item exists. Never shows a prompt: one would
/// block the caller, so it is dismissed and the write is skipped.
pub(crate) fn replace(service: &str, user: &str, secret: &[u8]) -> Replaced {
match try_replace(service, user, secret) {
Ok(replaced) => replaced,
Err(e) => Replaced::Skipped(e.to_string()),
}
}
fn try_replace(
service: &str,
user: &str,
secret: &[u8],
) -> Result<Replaced, dbus_secret_service::Error> {
let ss = SecretService::connect_with_max_prompt_timeout(EncryptionType::Dh, 0)?;
let lookup = HashMap::from([("service", service), ("username", user)]);
let found = ss.search_items(lookup.clone())?;
if !found.locked.is_empty() {
return Ok(Replaced::Skipped("the item is locked".into()));
}
let [old] = found.unlocked.as_slice() else {
return Ok(Replaced::Skipped(format!(
"{} items match, not one",
found.unlocked.len()
)));
};
let Some(collection) = collection_of(&old.path) else {
return Ok(Replaced::Skipped(format!(
"no collection in the item path {}",
old.path
)));
};
let label = old.get_label()?;
let attributes = old.get_attributes()?;
let attributes: HashMap<&str, &str> = attributes
.iter()
.map(|(k, v)| (k.as_str(), v.as_str()))
.collect();
let collection = Collection::new(&ss, collection);
let new = collection.create_item(&label, attributes, secret, true, CONTENT_TYPE)?;
// A provider that did not match the old item leaves it next to the new
// one, and two items make every read ambiguous.
let after = ss.search_items(lookup)?;
for item in after.unlocked.iter().chain(&after.locked) {
if item.path == new.path {
continue;
}
if let Err(e) = item.delete() {
// The old item still holds the previous secret, so taking the new
// one out leaves things as they were, for the usual write.
if let Err(undo) = new.delete() {
tracing::error!(
error = %undo,
"could not remove a duplicate keyring item; remove one of the two \
`proton-drive-linux` entries in your keyring and sign in again"
);
}
return Ok(Replaced::Skipped(format!(
"could not remove the replaced item: {e}"
)));
}
}
Ok(Replaced::Saved)
}
/// The collection an item path belongs to: the path without its last part,
/// as long as it is a Secret Service object path.
fn collection_of(item: &Path<'_>) -> Option<Path<'static>> {
let (collection, _) = item.rsplit_once('/')?;
if !collection.starts_with("/org/freedesktop/secrets/") || collection.ends_with('/') {
return None;
}
Path::new(collection.to_owned()).ok()
}
#[cfg(test)]
mod tests {
use super::*;
fn collection(item: &str) -> Option<String> {
collection_of(&Path::new(item).unwrap()).map(|p| p.to_string())
}
#[test]
fn an_item_belongs_to_the_collection_its_path_names() {
assert_eq!(
collection("/org/freedesktop/secrets/collection/login/14").as_deref(),
Some("/org/freedesktop/secrets/collection/login")
);
assert_eq!(
collection("/org/freedesktop/secrets/aliases/default/3").as_deref(),
Some("/org/freedesktop/secrets/aliases/default")
);
}
#[test]
fn a_path_outside_the_secret_service_names_no_collection() {
assert_eq!(collection("/org/example/item/1"), None);
assert_eq!(collection("/item"), None);
}
}
+67
View File
@@ -109,6 +109,7 @@ a live account since.
| Entry | Title |
|---|---|
| [B291](#b291--on-fedora-45s-oo7-every-saved-sign-in-is-lost-at-the-next-reboot) | On Fedora 45's oo7, every saved sign-in is lost at the next reboot |
| [B289](#b289--the-takeout-import-mixes-in-other-services-and-drops-location-and-live-photos) | The Takeout import mixes in other services and drops location and live photos |
| [B288](#b288--an-interrupted-takeout-import-reads-as-finished-and-one-error-ends-it) | An interrupted Takeout import reads as finished, and one error ends it |
| [B287](#b287--a-photo-draft-left-by-an-interrupted-upload-blocks-that-name) | A photo draft left by an interrupted upload blocks that name |
@@ -305,6 +306,7 @@ a live account since.
| Entry | Title |
|---|---|
| [B290](#b290--reopening-the-database-right-after-closing-it-can-be-refused-as-another-daemon) | Reopening the database right after closing it can be refused as another daemon |
| [B276](#b276--a-stop-can-end-with-a-panic-in-a-thread-the-stop-never-waited-for) | A stop can end with a panic in a thread the stop never waited for |
| [B258](#b258--journalctl--p-warning-shows-nothing-whatever-the-daemon-logged) | `journalctl -p warning` shows nothing, whatever the daemon logged |
| [B256](#b256--pdfs-copy-right-after-a-save-copies-the-old-version) | `pdfs copy` right after a save copies the old version |
@@ -392,6 +394,71 @@ a live account since.
Newest first.
### B291 — On Fedora 45's oo7, every saved sign-in is lost at the next reboot
**Status:** Fixed, not verified live
**Found:** 2026-11-15, from the reporter's package list in
[issue #78](https://github.com/narrrl/proton-drive-linux/issues/78): `oo7-daemon-0.7.0~beta^1.e91b98a`,
the build Fedora 45 and 46 ship as stable, from before oo7's
[7fb11dd](https://github.com/linux-credentials/oo7/commit/7fb11dd111c4be8fec01996e3caa31c44fa962b3)
("server: Write item changes to the keyring file"). See
[the investigation](ISSUE-78.md#setsecret-is-kept-only-in-memory-in-fedoras-oo7).
**Where:** `crates/pdfs-core/src/auth.rs`, `write_stored`; `crates/pdfs-core/src/secretservice.rs`
**Repro:** With oo7-daemon at e91b98a, save the session blob, save it again with a new refresh
token, and restart oo7-daemon. The read after the restart returns the first blob. The keyring
store writes an existing item with `SetSecret`, and that oo7 build applies `SetSecret` only to the
item in memory; the keyring file keeps what `CreateItem` wrote. Within a boot every read returns
the new tokens, so nothing looks wrong. After a reboot the daemon resumes with the refresh token
of the last `pdfs login`, which Proton spent long ago: `InvalidRefreshToken`, and the user has to
sign in again. Typing the password at login, or the fixes for B284 and B285, change nothing.
**Cause:** A bug in that oo7 build, fixed in oo7 0.7.0 but not packaged by Fedora. Every token
refresh pdfs saved went through the one call it does not persist.
**Fix:** An existing entry is saved by `CreateItem` with `replace`, which that build writes to the
file: a new item in the old one's collection, with its label and all its attributes, so the
provider replaces the old one. A duplicate that a provider leaves behind is removed; if it cannot
be, the new item is taken out again and the save falls back to the usual `SetSecret`. The first
save, a locked item, two matching items, or any failure before the new item exists also fall back
to it. The connection used for this never shows a prompt. The daemon log now has a short
fingerprint of the refresh token at each start and each save, so a log across a reboot shows
whether the keyring returned the last token saved.
**Verified:** 2026-11-15, `real_secret_service_keeps_the_last_save_across_a_provider_restart`
(ignored; needs a throwaway Secret Service) against oo7-daemon built at e91b98a: with the
in-place write it reads `r1` after the restart, with this change `r3` and one item. The same test
and `real_secret_service_survives_a_provider_restart` pass against gnome-keyring. Not yet run on
a Fedora 45 desktop with a real account.
---
### B290 — Reopening the database right after closing it can be refused as another daemon
**Status:** Fixed, 2026-11-15
**Found:** 2026-11-15, CI on `chore/release-3.8.0` (fd6b2d3):
`db::tests::what_a_create_sent_outlives_a_newer_blob_its_trash_and_a_restart` failed once with
"another Proton Drive daemon is already using /tmp/pdfs-create-sent-….db". It passes on its own.
**Where:** `crates/pdfs-core/src/db/mod.rs`, `acquire_single_writer_lock`
**Repro:** In one process, drop a `Db` and open the same file again while another thread spawns a
child process. Rare; seen once in about 500 tests that run in parallel, several of which spawn
processes.
**Cause:** The single-writer lock is a `flock` on `<db>.lock`, and a `flock` belongs to the open
file, not to one descriptor. A child holds a copy of every descriptor from its fork until its exec
closes the `O_CLOEXEC` ones. A lock dropped by the parent in that moment stays held by the child,
and an immediate reopen gets `EWOULDBLOCK`, which reads as a second daemon.
**Fix:** A refused lock is retried every 20 ms for up to 500 ms before it counts as another
daemon. That covers any fork-to-exec window. A real second daemon is still refused, half a
second later.
**Verified:** `cargo test -p pdfs-core` on 2026-11-15, including
`a_second_writer_is_refused_while_the_first_holds_the_database`.
---
### B289 — The Takeout import mixes in other services and drops location and live photos
**Status:** Fixed, not verified live
+7 -7
View File
@@ -162,13 +162,13 @@ Existing folders stay device backups. Older builds cannot open the upgraded data
screen there, and with nothing to scroll no more were loaded. More now load whenever the photos
don't fill the window, also after a zoom or a resize.
- **Signed out after a cold boot on Fedora 45** (issue #78, B284, B285). Fedora 45 replaced the
GNOME keyring with oo7, and two things there could keep a refreshed sign-in from being saved:
oo7 reports the entries of a locked keyring as missing, which the client took for a sign-out,
and a restarted oo7 no longer knew the client's connection, which then failed every save until
the service restarted. The refreshed sign-in lived only in memory, and the next boot found the
spent one. The client now keeps such a sign-in and saves it once the keyring can be read, and
reconnects to a keyring service that restarted.
- **Signed out after every reboot on Fedora 45** (issue #78, B284, B285, B291). Fedora 45 replaced
the GNOME keyring with oo7, and the oo7 build it ships keeps a changed keyring entry only in
memory: every refreshed sign-in the client saved was gone after a reboot, and the next start
found one Proton had already retired. The client now saves the sign-in as a new entry that
replaces the old one, which that oo7 does write to disk. It also no longer drops a refreshed
sign-in when oo7 reports the entries of a locked keyring as missing, and it reconnects to a
keyring service that restarted.
- **Thumbnails appear in the folder you are looking at as soon as they are made.** During
**Build thumbnails**, the folder on screen kept its file icons until you left it and opened it
+46
View File
@@ -58,6 +58,52 @@ can resolve the default alias to a locked collection. Both have fixes in
after the reporter's November 7 retest. Neither can be assigned as this report's cause without
matching runtime evidence.
## SetSecret is kept only in memory in Fedora's oo7
Follow-up on 2026-11-15. The reporter's `rpm -qa 'oo7*'` shows
`oo7-daemon-0.7.0~beta^1.e91b98a-1.fc45`, not the 0.7.0 release assumed above. Bodhi lists that
build as the stable oo7 for Fedora 45 and 46; neither has 0.7.0 or 0.7.1. Commit e91b98a
(October 24) predates
[7fb11dd](https://github.com/linux-credentials/oo7/commit/7fb11dd111c4be8fec01996e3caa31c44fa962b3)
(October 28): "Setting the Label or Attributes of an item, or calling SetSecret, only changed the
item in memory, so the old values came back once the daemon restarted." 0.7.0 carries it as
37e3334.
pdfs writes an existing entry with `SetSecret`, so on that build every saved token rotation was
lost at the next reboot. This explains the reporter's log: the keyring was unlocked one second
before pdfs started (they type their password at login) and still held a spent refresh token.
It does not need a locked keyring or a restarted provider.
pdfs now saves an existing entry with `CreateItem` and `replace` instead
(`crates/pdfs-core/src/secretservice.rs`, B291), which e91b98a writes to the keyring file.
Reproduced and verified with oo7-daemon built at e91b98a in a throwaway session bus:
```sh
git clone https://github.com/linux-credentials/oo7.git /tmp/oo7 && git -C /tmp/oo7 checkout e91b98a
cargo build --manifest-path /tmp/oo7/Cargo.toml -p oo7-daemon --bin oo7-daemon
OO7=/tmp/oo7/target/debug/oo7-daemon
T=$(mktemp -d /tmp/ss.XXXX); mkdir -p $T/data $T/run; chmod 700 $T/run
XDG_DATA_HOME=$T/data XDG_RUNTIME_DIR=$T/run OO7=$OO7 PDFS_SECRET_SERVICE_RESTART_TEST=1 \
PDFS_RESTART_PROVIDER='pkill -x oo7-daemon; sleep 1; (printf pw | setsid "$OO7" --login >/dev/null 2>&1 &); sleep 2' \
dbus-run-session -- sh -c 'printf pw | setsid "$OO7" --login >/dev/null 2>&1 & sleep 2;
cargo test -p pdfs-core real_secret_service_keeps -- --ignored'
```
With the in-place write, the read after the restart returns the first of three saves (`r1`);
with the replacing write it returns the last (`r3`), and one item is left.
The daemon also logs a short fingerprint of the refresh token when it resumes the stored session
and each time it saves a rotation (`refresh=1a2b3c4d`). If a start is refused, compare the
fingerprint it resumed with against the last one saved in the boot before:
```sh
journalctl --user -b -1 -u proton-drive.service | grep -E 'refresh=' | tail -2
journalctl --user -b 0 -u proton-drive.service | grep -E 'refresh=' | head -2
```
The same fingerprint means the keyring kept the last save and Proton refused it; a different one
means the keyring lost it.
## A restarted Secret Service
A second path to the same symptom, independent of locking (B285). The keyring store pdfs used