signer: store a permission only once the request is answered (M1)
gate() no longer writes apps.json; authorize_inner re-checks the app's identity (and its stored permission) after the last await and only then stores 'always', so a revoke or rebind during a prompt denies.
This commit is contained in:
2 files changed
+246
-19
No files matched your search
@@ -118,6 +118,16 @@ data/ desktop, metainfo, gschema (all `.in` templates), icons, re
|
||||
overrides them, which is what headless tests use. The default is
|
||||
`wss://nostr.kosmos.org` because **many public relays reject or rate-limit
|
||||
`kind:24133`** — pairing silently times out on those.
|
||||
- **A permission is stored only once the answer is produced (M1).** `gate()`
|
||||
returns the user's verdict but does **not** write `apps.json`; `authorize_inner`
|
||||
re-checks `identity_for` (and, for a silent answer, `permits`) after the last
|
||||
`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.
|
||||
- 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
|
||||
|
||||
+236
-19
@@ -990,9 +990,9 @@ async fn gate(
|
||||
client: &ClientInfo,
|
||||
identity: PublicKey,
|
||||
request: Request,
|
||||
permission: Option<Permission>,
|
||||
permission: &Option<Permission>,
|
||||
) -> Verdict {
|
||||
if let Some(permission) = &permission {
|
||||
if let Some(permission) = permission {
|
||||
let covered = config
|
||||
.registry
|
||||
.lock()
|
||||
@@ -1022,22 +1022,11 @@ async fn gate(
|
||||
match decision {
|
||||
Decision::Deny => Verdict::Deny,
|
||||
Decision::Once => Verdict::AllowOnce,
|
||||
Decision::Always => {
|
||||
if let Some(permission) = permission {
|
||||
if let Err(error) = config
|
||||
.registry
|
||||
.lock()
|
||||
.unwrap()
|
||||
.allow(&client.public_key, permission)
|
||||
{
|
||||
// The user allowed this once, but it could not be stored —
|
||||
// record it honestly, because the next request will ask.
|
||||
eprintln!("regent: could not remember the permission: {error}");
|
||||
return Verdict::AllowOnce;
|
||||
}
|
||||
}
|
||||
Verdict::AllowAlways
|
||||
}
|
||||
// Persisting "always" is deliberately **not** done here. The request is
|
||||
// not answered yet — the vault may still refuse, and the user may revoke
|
||||
// or rebind the app while the unlock prompt is open. `authorize_inner`
|
||||
// stores it only once the answer is actually going to be produced.
|
||||
Decision::Always => Verdict::AllowAlways,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1079,7 +1068,7 @@ async fn authorize_inner(
|
||||
kind: Option<u16>,
|
||||
declined: &str,
|
||||
) -> std::result::Result<Keys, String> {
|
||||
let verdict = gate(config, client, identity, request, permission).await;
|
||||
let verdict = gate(config, client, identity, request, &permission).await;
|
||||
if verdict == Verdict::Deny {
|
||||
record(config, client, method, kind, Some(&identity), verdict.outcome());
|
||||
return Err(declined.to_string());
|
||||
@@ -1100,6 +1089,54 @@ async fn authorize_inner(
|
||||
return Err("the key vault is locked".to_string());
|
||||
}
|
||||
|
||||
// Re-check now that every `await` is behind us. Both the approval dialog and
|
||||
// the unlock prompt give the user time to revoke the app or rebind it to a
|
||||
// different identity — and the answer must not survive that. This runs on
|
||||
// **every** verdict path, not only after an unlock: the unlocked path has the
|
||||
// same dialog window and the same race.
|
||||
{
|
||||
let registry = config.registry.lock().unwrap();
|
||||
let still_bound = registry.identity_for(&client.public_key) == Some(identity);
|
||||
let still_permitted = match (&verdict, &permission) {
|
||||
(Verdict::Silent, Some(permission)) => {
|
||||
registry.permits(&client.public_key, permission)
|
||||
}
|
||||
_ => true,
|
||||
};
|
||||
if !still_bound || !still_permitted {
|
||||
drop(registry);
|
||||
record(
|
||||
config,
|
||||
client,
|
||||
method,
|
||||
kind,
|
||||
Some(&identity),
|
||||
audit::outcome::DENIED,
|
||||
);
|
||||
return Err(declined.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
// Only now, with the answer actually going to be produced, remember
|
||||
// "always". If it cannot be stored, fall back to "once" honestly, because
|
||||
// the next request will ask again.
|
||||
let verdict = match (verdict, &permission) {
|
||||
(Verdict::AllowAlways, Some(permission)) => {
|
||||
if let Err(error) = config
|
||||
.registry
|
||||
.lock()
|
||||
.unwrap()
|
||||
.allow(&client.public_key, permission.clone())
|
||||
{
|
||||
eprintln!("regent: could not remember the permission: {error}");
|
||||
Verdict::AllowOnce
|
||||
} else {
|
||||
Verdict::AllowAlways
|
||||
}
|
||||
}
|
||||
(verdict, _) => verdict,
|
||||
};
|
||||
|
||||
let keys = match config.keys.get(&identity).await {
|
||||
Ok(keys) => keys,
|
||||
Err(error) => {
|
||||
@@ -2053,6 +2090,186 @@ mod tests {
|
||||
let _ = std::fs::remove_file(&log_path);
|
||||
}
|
||||
|
||||
/// A revocation while the unlock prompt is open must deny: the answer cannot
|
||||
/// survive the window the dialog gives the user (M1).
|
||||
#[tokio::test]
|
||||
async fn a_revocation_during_the_unlock_prompt_denies() {
|
||||
let (dir, store) = vault_with_key("hunter2").await;
|
||||
let store = Arc::new(store);
|
||||
let identity = store.list_user_keys()[0].public_key;
|
||||
let client = Keys::generate();
|
||||
let client_key = client.public_key();
|
||||
|
||||
let (approver, prompts, _notices) = Approver::new();
|
||||
let (log_path, log) = temp_log();
|
||||
let config = vault_config(
|
||||
"ws://127.0.0.1:1",
|
||||
approver,
|
||||
Arc::clone(&store),
|
||||
identity,
|
||||
log.clone(),
|
||||
);
|
||||
config
|
||||
.registry
|
||||
.lock()
|
||||
.unwrap()
|
||||
.approve(&bare_client(&client_key), identity, None)
|
||||
.unwrap();
|
||||
|
||||
let registry = Arc::clone(&config.registry);
|
||||
let unlocking = Arc::clone(&store);
|
||||
let drain = tokio::spawn(async move {
|
||||
while let Ok(prompt) = prompts.recv().await {
|
||||
match prompt {
|
||||
Prompt::Approve { reply, .. } => {
|
||||
let _ = reply.send(Decision::Once.into()).await;
|
||||
}
|
||||
Prompt::Unlock { reply, .. } => {
|
||||
let opened = unlocking.unlock("hunter2").await.is_ok();
|
||||
// The user revokes the app while the unlock is pending.
|
||||
registry.lock().unwrap().revoke(&client_key).unwrap();
|
||||
let _ = reply.send(opened).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let device = config.device_keys.public_key();
|
||||
let answer = sign_once(&client, &device, &config, "hello").await;
|
||||
assert!(
|
||||
answer["result"].is_null(),
|
||||
"a revoked app must not be signed for: {answer}"
|
||||
);
|
||||
assert!(answer["error"].is_string());
|
||||
|
||||
drain.abort();
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
let _ = std::fs::remove_file(&log_path);
|
||||
}
|
||||
|
||||
/// A cancelled unlock must not leave a stored "Allow always" behind: the
|
||||
/// permission is written only once the answer is actually produced (M1).
|
||||
#[tokio::test]
|
||||
async fn a_cancelled_unlock_does_not_remember_allow_always() {
|
||||
let (dir, store) = vault_with_key("hunter2").await;
|
||||
let store = Arc::new(store);
|
||||
let identity = store.list_user_keys()[0].public_key;
|
||||
let client = Keys::generate();
|
||||
let client_key = client.public_key();
|
||||
|
||||
let (approver, prompts, _notices) = Approver::new();
|
||||
let drain = tokio::spawn(async move {
|
||||
while let Ok(prompt) = prompts.recv().await {
|
||||
match prompt {
|
||||
Prompt::Approve { reply, .. } => {
|
||||
let _ = reply.send(Decision::Always.into()).await;
|
||||
}
|
||||
// The user closes the unlock dialog without opening it.
|
||||
Prompt::Unlock { reply, .. } => {
|
||||
let _ = reply.send(false).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let (log_path, log) = temp_log();
|
||||
let config = vault_config(
|
||||
"ws://127.0.0.1:1",
|
||||
approver,
|
||||
Arc::clone(&store),
|
||||
identity,
|
||||
log.clone(),
|
||||
);
|
||||
config
|
||||
.registry
|
||||
.lock()
|
||||
.unwrap()
|
||||
.approve(&bare_client(&client_key), identity, None)
|
||||
.unwrap();
|
||||
|
||||
let device = config.device_keys.public_key();
|
||||
let answer = sign_once(&client, &device, &config, "hello").await;
|
||||
assert!(answer["result"].is_null(), "the unlock was cancelled: {answer}");
|
||||
|
||||
let policy = config.registry.lock().unwrap().permissions(&client_key);
|
||||
assert!(
|
||||
policy.kinds.is_empty(),
|
||||
"a cancelled unlock must not store a permission: {policy:?}"
|
||||
);
|
||||
|
||||
drain.abort();
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
let _ = std::fs::remove_file(&log_path);
|
||||
}
|
||||
|
||||
/// A rebind to another identity while the (already-unlocked) dialog is open
|
||||
/// must deny, and must not write the old identity's permission (M1).
|
||||
#[tokio::test]
|
||||
async fn a_rebind_during_the_dialog_denies_and_stores_nothing() {
|
||||
let (dir, store) = vault_with_key("hunter2").await;
|
||||
let store = Arc::new(store);
|
||||
// Already unlocked, so the only prompt is the approval dialog.
|
||||
store.unlock("hunter2").await.expect("unlock");
|
||||
let identity = store.list_user_keys()[0].public_key;
|
||||
let client = Keys::generate();
|
||||
let client_key = client.public_key();
|
||||
let other = Keys::generate().public_key();
|
||||
|
||||
let (approver, prompts, _notices) = Approver::new();
|
||||
let (log_path, log) = temp_log();
|
||||
let config = vault_config(
|
||||
"ws://127.0.0.1:1",
|
||||
approver,
|
||||
Arc::clone(&store),
|
||||
identity,
|
||||
log.clone(),
|
||||
);
|
||||
let registry = Arc::clone(&config.registry);
|
||||
registry
|
||||
.lock()
|
||||
.unwrap()
|
||||
.approve(&bare_client(&client_key), identity, None)
|
||||
.unwrap();
|
||||
|
||||
let drain = tokio::spawn(async move {
|
||||
while let Ok(prompt) = prompts.recv().await {
|
||||
match prompt {
|
||||
Prompt::Approve { reply, .. } => {
|
||||
// The app is rebound to another identity while the
|
||||
// dialog is open. (This mirrors `ConnectDialog::
|
||||
// begin_pairing` bypassing the serialized Approver
|
||||
// queue — the L4 residual.)
|
||||
registry
|
||||
.lock()
|
||||
.unwrap()
|
||||
.approve(&bare_client(&client_key), other, None)
|
||||
.unwrap();
|
||||
let _ = reply.send(Decision::Always.into()).await;
|
||||
}
|
||||
Prompt::Unlock { reply, .. } => {
|
||||
let _ = reply.send(true).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let device = config.device_keys.public_key();
|
||||
let answer = sign_once(&client, &device, &config, "hello").await;
|
||||
assert!(
|
||||
answer["result"].is_null(),
|
||||
"a rebound client must not be signed for: {answer}"
|
||||
);
|
||||
let policy = config.registry.lock().unwrap().permissions(&client_key);
|
||||
assert!(
|
||||
policy.kinds.is_empty(),
|
||||
"no permission may be stored across a rebind: {policy:?}"
|
||||
);
|
||||
|
||||
drain.abort();
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
let _ = std::fs::remove_file(&log_path);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn signing_asks_and_a_denial_reaches_the_client() {
|
||||
let client = Keys::generate();
|
||||
|
||||
Reference in new issue
Block a user