signer: serialize the pairing dialog through the approval queue (L4)

This commit is contained in:
raucao committed 2026-11-30 10:31:00 +04:00
1 parent 6e1f35ef62
commit e67963d86b
8 files changed
+162 -27

No files matched your search

+7 -5
View File
@@ -139,10 +139,11 @@ data/ desktop, metainfo, gschema (all `.in` templates), icons, re
`await` — the approval dialog and the unlock prompt both give the user time to
revoke or rebind the app — and only then stores "always" and fetches the key. A
revocation or rebind during either prompt therefore denies, and a cancelled
unlock leaves no permission behind. **Known residual:** pairing dialogs are not
yet serialized (`ui/connect.rs` bypasses the `Approver` queue — L4); the
re-check makes that race fail closed rather than exploitable, and serializing
the dialogs is Phase-3 work.
unlock leaves no permission behind. **Serialized now (L4):**
the in-app and `nostrconnect://`-link pairing dialogs go through the same
`Approver` queue (`Approver::ask_pairing` → the `Prompt::Pair` arm in
`ui/approval.rs::install`), so a pairing prompt can never stack over a request
prompt; the re-check above still makes the race fail closed.
- Two traps that make a signer look "hung" against a real relay. Both are fixed;
do not reintroduce them:
- **Subscriptions must target relays explicitly.** With gossip enabled (the
@@ -743,7 +744,8 @@ data/ desktop, metainfo, gschema (all `.in` templates), icons, re
secret and works while locked.
- **In-app pairing** (Apps page → "Connect an app…" or the primary menu): paste the
`nostrconnect://` string, then the *same* approval dialog used for responder
questions (`ui::approval::present_question`). On allow, the client's relays are
questions, through the same serialized `Approver` queue
(`Approver::ask_pairing`; L4). On allow, the client's relays are
added to the running signer and `signer::pair_client` registers + answers. If
the signer is stopped, it is started with the client's relays first. The
`SignerRunner` has a `Stopped | Starting | Running` state so a second start
+6
View File
@@ -128,6 +128,12 @@ async fn serve(
Prompt::Approve { reply, .. } => {
let _ = reply.send(Answer::denied()).await;
}
// Headless never sends a pairing prompt (`ask_pairing` is
// GUI-only; `--pair` consents via `pair_client`), but the drain
// fails closed regardless.
Prompt::Pair { reply, .. } => {
let _ = reply.send(Answer::denied()).await;
}
Prompt::Unlock { reply, .. } => {
let _ = reply.send(false).await;
}
+6
View File
@@ -132,6 +132,12 @@ impl SignerRunner {
}
}
/// The approval queue, so a user-initiated pairing can go through the same
/// serialized drain as responder prompts (L4).
pub fn approver(&self) -> Approver {
self.approver.clone()
}
/// Register a callback for lifecycle changes. It runs immediately, so the
/// caller does not need a separate initial refresh.
pub fn listen(self: &Rc<Self>, listener: impl Fn() + 'static) {
+82
View File
@@ -181,6 +181,15 @@ pub enum Prompt {
/// backstop, leaving the dialog with buttons that no longer do anything.
deadline: Instant,
},
/// A **user-initiated** pairing question (`nostrconnect://` paste or link).
///
/// Sent through the same queue as every other prompt, so a pairing dialog
/// can never stack over a request prompt (L4). No deadline: the user opened
/// it deliberately, so they may take their time.
Pair {
question: Question,
reply: Sender<Answer>,
},
/// Open the vault. `true` means it is now unlocked.
Unlock {
reply: Sender<bool>,
@@ -237,6 +246,24 @@ impl Approver {
self.ask_within(ASK_TIMEOUT, ASK_BACKSTOP, question).await
}
/// Ask a **user-initiated** pairing question, serialized with every other
/// prompt.
///
/// No timeout: the user pasted the code or opened the link themselves, so
/// they may take their time. A missing UI still denies.
pub async fn ask_pairing(&self, question: Question) -> Answer {
let (reply, answer) = bounded(1);
if self
.inbox
.send(Prompt::Pair { question, reply })
.await
.is_err()
{
return Answer::denied();
}
answer.recv().await.unwrap_or_else(|_| Answer::denied())
}
/// The same, with explicit deadlines so tests can exercise both without
/// waiting a minute: `deadline` is what the UI is told, `within` is this
/// side's own backstop. Both are anchored **now**, so a prompt that waits in
@@ -363,6 +390,61 @@ mod tests {
assert!(ASK_BACKSTOP > ASK_TIMEOUT);
}
/// A pairing prompt goes through the same queue as a request prompt, so the
/// two are serialized (L4); with no UI at all it denies.
#[tokio::test]
async fn a_pairing_prompt_shares_the_queue_and_a_gone_queue_denies() {
let (approver, prompts, _notices) = Approver::new();
let drain = tokio::spawn(async move {
let mut order = Vec::new();
while let Ok(prompt) = prompts.recv().await {
match prompt {
Prompt::Approve { reply, .. } => {
order.push("approve");
let _ = reply.send(Answer::denied()).await;
}
Prompt::Pair { reply, .. } => {
order.push("pair");
let _ = reply.send(Answer::denied()).await;
}
Prompt::Unlock { .. } => {}
}
if order.len() == 2 {
break;
}
}
order
});
// `ask` (a responder request) and `ask_pairing` (user-initiated) share
// the one inbox.
let request = approver.ask(question(Request::Pair {
requested: None,
relays: Vec::new(),
}));
let pairing = approver.ask_pairing(question(Request::Pair {
requested: None,
relays: Vec::new(),
}));
let (first, second) = tokio::join!(request, pairing);
assert_eq!(first, Answer::denied());
assert_eq!(second, Answer::denied());
assert_eq!(drain.await.unwrap(), ["approve", "pair"]);
// With the queue gone, a pairing ask denies.
let (approver, prompts, _notices) = Approver::new();
drop(prompts);
assert_eq!(
approver
.ask_pairing(question(Request::Pair {
requested: None,
relays: Vec::new(),
}))
.await,
Answer::denied()
);
}
/// A prompt can wait in the queue behind another one, so the deadline the
/// UI is handed has to be anchored **when the ask starts**, not when the
/// dialog finally opens: otherwise a queued prompt's dialog would outlive
+29
View File
@@ -1695,6 +1695,9 @@ mod tests {
Prompt::Approve { reply, .. } => {
let _ = reply.send(decision.into()).await;
}
Prompt::Pair { reply, .. } => {
let _ = reply.send(decision.into()).await;
}
Prompt::Unlock { reply, .. } => {
let _ = reply.send(false).await;
}
@@ -1726,6 +1729,11 @@ mod tests {
log.lock().unwrap().push(question.request.clone());
let _ = reply.send(decision.into()).await;
}
Prompt::Pair { question, reply } => {
let decision = decide(&question.request);
log.lock().unwrap().push(question.request.clone());
let _ = reply.send(decision.into()).await;
}
Prompt::Unlock { reply, .. } => {
let _ = reply.send(false).await;
}
@@ -2221,6 +2229,9 @@ mod tests {
Prompt::Approve { reply, .. } => {
let _ = reply.send(Decision::Once.into()).await;
}
Prompt::Pair { reply, .. } => {
let _ = reply.send(Decision::Deny.into()).await;
}
Prompt::Unlock { reply, .. } => {
let opened = unlocking.unlock("hunter2").await.is_ok();
let _ = reply.send(opened).await;
@@ -2277,6 +2288,9 @@ mod tests {
Prompt::Approve { reply, .. } => {
let _ = reply.send(Decision::Once.into()).await;
}
Prompt::Pair { reply, .. } => {
let _ = reply.send(Decision::Deny.into()).await;
}
// The user closes the unlock dialog without opening it.
Prompt::Unlock { reply, .. } => {
let _ = reply.send(false).await;
@@ -2371,6 +2385,9 @@ mod tests {
Prompt::Approve { reply, .. } => {
let _ = reply.send(Decision::Once.into()).await;
}
Prompt::Pair { reply, .. } => {
let _ = reply.send(Decision::Deny.into()).await;
}
Prompt::Unlock { reply, .. } => {
let opened = unlocking.unlock("hunter2").await.is_ok();
// The user revokes the app while the unlock is pending.
@@ -2411,6 +2428,9 @@ mod tests {
Prompt::Approve { reply, .. } => {
let _ = reply.send(Decision::Always.into()).await;
}
Prompt::Pair { reply, .. } => {
let _ = reply.send(Decision::Deny.into()).await;
}
// The user closes the unlock dialog without opening it.
Prompt::Unlock { reply, .. } => {
let _ = reply.send(false).await;
@@ -2493,6 +2513,9 @@ mod tests {
.unwrap();
let _ = reply.send(Decision::Always.into()).await;
}
Prompt::Pair { reply, .. } => {
let _ = reply.send(Decision::Deny.into()).await;
}
Prompt::Unlock { reply, .. } => {
let _ = reply.send(true).await;
}
@@ -2996,6 +3019,9 @@ mod tests {
Prompt::Approve { reply, .. } => {
let _ = reply.send(Decision::Once.into()).await;
}
Prompt::Pair { reply, .. } => {
let _ = reply.send(Decision::Deny.into()).await;
}
Prompt::Unlock { reply, .. } => {
let _ = reply.send(false).await;
}
@@ -3040,6 +3066,9 @@ mod tests {
Prompt::Approve { reply, .. } => {
let _ = reply.send(Decision::Always.into()).await;
}
Prompt::Pair { reply, .. } => {
let _ = reply.send(Decision::Deny.into()).await;
}
Prompt::Unlock { reply, .. } => {
let _ = reply.send(false).await;
}
+15
View File
@@ -83,6 +83,21 @@ pub fn install(
}
let _ = reply.send(answer).await;
}
Prompt::Pair { question, reply } => {
// A user-initiated pairing (L4): same serialized drain, but
// no deadline. It can arrive while the window is hidden
// (background/autostart), so mirror the `Approve` arm —
// notify when away and present the window for the dialog.
let notified = notify_pending(&widget, notification_body(&question.request));
primary.present_for_prompt();
runner.set_pending(true);
let answer = present_question(&widget, &question, None, &state).await;
runner.set_pending(false);
if notified {
withdraw_pending(&widget);
}
let _ = reply.send(answer).await;
}
Prompt::Unlock { reply, deadline } => {
if expired(deadline) {
let _ = reply.send(false).await;
+13 -11
View File
@@ -26,7 +26,7 @@ use crate::runtime;
use crate::signer::approval::{Decision, Question, Request, SigningKey};
use crate::signer::{pair_client, parse_client_uri, ClientPairing, Signer};
use crate::state::AppState;
use crate::ui::{approval, plain, qr};
use crate::ui::{plain, qr};
use crate::window::Windows;
pub struct ConnectDialog {
@@ -356,13 +356,15 @@ impl ConnectDialog {
if text.trim().is_empty() {
return;
}
self.begin_pairing(text.trim(), &self.dialog.clone().upcast::<gtk::Widget>());
self.begin_pairing(text.trim());
}
/// Pair from a `nostrconnect://` URI, asked over `parent`: the "Connect an
/// app" dialog passes itself, an opened link passes the main window (that
/// dialog is not open then).
pub(crate) fn begin_pairing(self: &Rc<Self>, uri: &str, parent: &gtk::Widget) {
/// Pair from a `nostrconnect://` URI.
///
/// The question goes through the responder's serialized `Approver` queue
/// (L4), so it can never stack over another prompt; it is presented over the
/// primary window by the drain.
pub(crate) fn begin_pairing(self: &Rc<Self>, uri: &str) {
let pairing = match parse_client_uri(uri) {
Ok(pairing) => pairing,
Err(error) => {
@@ -411,13 +413,13 @@ impl ConnectDialog {
rebind_from,
};
let parent = parent.clone();
let approver = self.runner.approver();
let connect = Rc::clone(self);
glib::MainContext::default().spawn_local(async move {
// The same approval dialog as a responder question: the app is
// registered *before* the response goes out, and the user is the
// one who decides.
let answer = approval::present_question(&parent, &question, None, &connect.state).await;
// The same approval dialog as a responder question, through the same
// serialized queue: the app is registered *before* the response goes
// out, and the user is the one who decides.
let answer = approver.ask_pairing(question).await;
if answer.decision == Decision::Deny {
return;
}
+4 -11
View File
@@ -332,22 +332,15 @@ impl Windows {
}
}
/// Pair from a `nostrconnect://` URI — what opening a link does. Goes
/// straight to the approval dialog, over the window the app is.
/// Pair from a `nostrconnect://` URI — what opening a link does. The
/// question goes through the serialized approval queue, which presents the
/// window itself if it is hidden.
pub fn pair_uri(self: &Rc<Self>, uri: &str) {
let Some(connect) = self.connect_dialog() else {
return;
};
// A link can arrive while the window is hidden in the background, and a
// dialog parented to a hidden window is invisible.
self.present_primary();
let Some(parent) = self
.visible_parent()
.map(|window| window.upcast::<gtk::Widget>())
else {
return;
};
connect.begin_pairing(uri, &parent);
connect.begin_pairing(uri);
}
/// Bring the Apps page to the front. A pairing closes the "Connect an app"