Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions server/src/prompt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,15 @@ impl Prompt {
}
}

pub(crate) async fn emit_orphaned_completed(&self) -> Result<(), ServiceError> {
let signal_emitter = self.service.signal_emitter(self.path().to_owned())?;
let result = zbus::wire::Variant::new::<Vec<OwnedObjectPath>>(vec![])
.try_into_owned()
.unwrap();
Prompt::completed(&signal_emitter, true, result).await?;
Ok(())
}

async fn emit_dismissed(&self) {
if let Ok(signal_emitter) = self.service.signal_emitter(self.path().to_owned()) {
let result = zbus::wire::Variant::new::<Vec<OwnedObjectPath>>(vec![])
Expand Down
97 changes: 97 additions & 0 deletions server/src/prompt/tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use zbus::proxy::Defaults;

use crate::tests::TestServiceSetup;

#[cfg(any(feature = "gnome_native_crypto", feature = "gnome_openssl_crypto"))]
Expand Down Expand Up @@ -389,3 +391,98 @@ async fn unlock_with_cli_prompter_max_attempts() -> Result<(), Box<dyn std::erro

Ok(())
}

/// Dismissing prompts when their prompter disappears from the bus
async fn prompter_disconnect_dismisses_prompts(
prompter_name: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let setup = TestServiceSetup::plain_session(true).await?;

setup.lock_collection(&setup.collections[0]).await?;

let (_unlocked, prompt_path) = setup
.server
.unlock(vec![setup.collections[0].inner().path().to_owned().into()])
.await?;
assert!(!prompt_path.is_empty(), "Should have a prompt path");

// The proxy's receive_completed would call Prompt() on the removed
// object, subscribe to the raw signal instead
let rule = zbus::MatchRule::builder()
.msg_type(zbus::message::Type::Signal)
.interface("org.freedesktop.Secret.Prompt")
.member("Completed")
.path(prompt_path.clone())
.build()?;
let mut stream = zbus::MessageStream::for_match_rule(rule, &setup.client_conn, None).await?;

let old_owner = zbus::names::UniqueName::from_str_unchecked(":1.99");
setup
.server
.handle_owner_lost(prompter_name, &old_owner)
.await;

assert!(
setup.server.prompt(&prompt_path).await.is_none(),
"The orphaned prompt should be removed"
);

use tokio_stream::StreamExt;
let message = tokio::time::timeout(std::time::Duration::from_secs(1), stream.next())
.await
.expect("signal within timeout")
.expect("signal stream should not end")
.expect("signal should be a message, not an error");
let (dismissed, _) = message
.body()
.deserialize::<(bool, zbus::wire::OwnedVariant)>()?;
assert!(dismissed, "The completion should be a dismissal");

Ok(())
}

#[cfg(any(feature = "gnome_native_crypto", feature = "gnome_openssl_crypto"))]
#[tokio::test]
async fn gnome_prompter_disconnect_dismisses_prompts() -> Result<(), Box<dyn std::error::Error>> {
let name = crate::gnome::prompter::GNOMEPrompterProxy::DESTINATION
.as_ref()
.map(zbus::names::BusName::as_str)
.expect("GNOME prompter proxy has a destination");
prompter_disconnect_dismisses_prompts(name).await
}

#[cfg(any(feature = "plasma_native_crypto", feature = "plasma_openssl_crypto"))]
#[tokio::test]
async fn plasma_prompter_disconnect_dismisses_prompts() -> Result<(), Box<dyn std::error::Error>> {
let name = crate::plasma::prompter::PlasmaPrompterProxy::DESTINATION
.as_ref()
.map(zbus::names::BusName::as_str)
.expect("Plasma prompter proxy has a destination");
prompter_disconnect_dismisses_prompts(name).await
}

#[tokio::test]
async fn non_prompter_name_release_keeps_prompts() -> Result<(), Box<dyn std::error::Error>> {
let setup = TestServiceSetup::plain_session(true).await?;

setup.lock_collection(&setup.collections[0]).await?;

let (_unlocked, prompt_path) = setup
.server
.unlock(vec![setup.collections[0].inner().path().to_owned().into()])
.await?;
assert!(!prompt_path.is_empty(), "Should have a prompt path");

let old_owner = zbus::names::UniqueName::from_str_unchecked(":1.42");
setup
.server
.handle_owner_lost("org.example.Unrelated", &old_owner)
.await;

assert!(
setup.server.prompt(&prompt_path).await.is_some(),
"An unrelated name release should not dismiss prompts"
);

Ok(())
}
53 changes: 49 additions & 4 deletions server/src/service/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use zbus::{
use super::{Service, discovery, naming};
#[cfg(any(feature = "gnome_native_crypto", feature = "gnome_openssl_crypto"))]
use crate::gnome::internal::InternalInterface;
use crate::{collection::Collection, error::Error};
use crate::{collection::Collection, error::Error, prompt::Prompt};

impl Service {
pub async fn run(
Expand Down Expand Up @@ -329,11 +329,9 @@ impl Service {
Ok(())
}

/// A client's unique name disappeared from the bus, mark its session
/// for cleanup. A well-known name being released does not mean the
/// client disconnected.
pub(crate) async fn handle_owner_lost(&self, name: &str, old_owner: &UniqueName<'_>) {
if name != old_owner.as_str() {
self.dismiss_orphaned_prompts(name).await;
return;
}
if let Some(session) = self.session_from_sender(old_owner).await {
Expand All @@ -350,6 +348,53 @@ impl Service {
}
}

/// Dismiss the prompts waiting on a prompter that left the bus.
async fn dismiss_orphaned_prompts(&self, name: &str) {
#[cfg(any(feature = "gnome_native_crypto", feature = "gnome_openssl_crypto"))]
let is_gnome = name
== crate::gnome::prompter::GNOMEPrompterProxy::DESTINATION
.as_ref()
.map(zbus::names::BusName::as_str)
.unwrap_or_default();
#[cfg(not(any(feature = "gnome_native_crypto", feature = "gnome_openssl_crypto")))]
let is_gnome = false;
#[cfg(any(feature = "plasma_native_crypto", feature = "plasma_openssl_crypto"))]
let is_plasma = name
== crate::plasma::prompter::PlasmaPrompterProxy::DESTINATION
.as_ref()
.map(zbus::names::BusName::as_str)
.unwrap_or_default();
#[cfg(not(any(feature = "plasma_native_crypto", feature = "plasma_openssl_crypto")))]
let is_plasma = false;

if !is_gnome && !is_plasma {
return;
}

let orphaned: Vec<Prompt> = {
let prompts = self.prompts.lock().await;
prompts.values().cloned().collect()
};

for prompt in orphaned {
tracing::warn!(
"Prompter `{name}` disappeared, dismissing prompt `{}`",
prompt.path()
);
if let Err(e) = self
.object_server()
.remove::<Prompt, _>(prompt.path())
.await
{
tracing::debug!("Failed to remove prompt from object server: {e}");
}
self.remove_prompt(prompt.path()).await;
if let Err(e) = prompt.emit_orphaned_completed().await {
tracing::debug!("Failed to emit Completed on orphaned prompt: {e}");
}
}
}

pub(crate) async fn cleanup_stale_sessions(&self) {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(30));
loop {
Expand Down
Loading