raffi 0.25.0

A simple fuzzel launcher based on YAML file
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//! Secret operations of the password manager addons: which operation is
//! running, the master password or passphrase prompt, and the job that
//! acts on a fetched secret once Raffi has closed.
//!
//! Workers have no Raffi-side effects: they hand an outcome back to the UI
//! thread, which drops it unless its generation is still current, so
//! nothing is copied, typed or prompted for after Escape or Lock.

use std::collections::HashMap;
use std::path::PathBuf;
use std::time::Duration;

use crate::secret::Secret;

use super::bitwarden;
use super::command_list::{CommandItem, CommandListAddon};
use super::pass::{self, PassContext, Pinentry};
use super::secret_io::{
    copy_secret, type_secret, Cancel, ClipboardTools, Runner, SecretError, SystemRunner, TypeTool,
};
use super::support::MruEntry;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Field {
    Password,
    Username,
    Otp,
}

/// What to do with an entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum EntryAction {
    Copy(Field),
    Type,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum VaultAddon {
    Pass,
    Bitwarden,
}

impl VaultAddon {
    pub fn of(addon: CommandListAddon) -> Option<Self> {
        match addon {
            CommandListAddon::Pass => Some(Self::Pass),
            CommandListAddon::Bitwarden => Some(Self::Bitwarden),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum OpKind {
    Load,
    /// An action on the entry (pass) or item id (Bitwarden) named here.
    Entry(EntryAction, String),
    Lock,
    Sync,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Op {
    pub addon: VaultAddon,
    pub kind: OpKind,
}

impl Op {
    /// The operation without the entry name or item id, for logs.
    pub fn log_name(&self) -> String {
        match &self.kind {
            OpKind::Entry(action, _) => format!("{:?} {action:?}", self.addon),
            kind => format!("{:?} {kind:?}", self.addon),
        }
    }

    /// The row the operation acts on, for entry actions.
    pub fn identity(&self) -> Option<String> {
        let OpKind::Entry(_, name) = &self.kind else {
            return None;
        };
        Some(match self.addon {
            VaultAddon::Pass => pass::entry_identity(name),
            VaultAddon::Bitwarden => bitwarden::item_identity(name),
        })
    }

    /// The row text shown while the operation runs, if any.
    pub fn progress(&self) -> Option<&'static str> {
        match (&self.kind, self.addon) {
            (OpKind::Lock, _) => Some("Locking..."),
            (OpKind::Sync, _) => Some("Syncing..."),
            (OpKind::Entry(..), VaultAddon::Bitwarden) => Some("Reading from Bitwarden..."),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Purpose {
    GpgPassphrase,
}

impl Purpose {
    pub fn placeholder(self) -> &'static str {
        match self {
            Self::GpgPassphrase => "GPG passphrase",
        }
    }
}

/// Settings of the addon the operation belongs to.
#[derive(Debug, Clone)]
pub(super) enum Backend {
    Pass(PassContext),
    Bitwarden,
}

#[derive(Debug, Clone, Copy)]
pub(super) struct JobSettings {
    pub clipboard_timeout: u64,
    pub type_delay: Duration,
}

/// Everything a worker needs; secrets inside are redacted from `Debug`.
#[derive(Debug, Clone)]
pub(super) struct Request {
    pub op: Op,
    pub backend: Backend,
    /// What the user typed in the prompt, if this run follows one.
    pub credential: Option<Secret>,
    /// Run `rbw unlock` (rbw's pinentry) before the operation.
    pub unlock_first: bool,
    pub settings: JobSettings,
}

#[derive(Debug, Clone)]
pub(super) enum Outcome {
    Loaded(Vec<CommandItem>),
    /// Close Raffi and run this.
    Ready(SecretJob),
    /// Lock or Sync finished; reload the list.
    Done,
    /// Ask for the passphrase, with why the last one failed.
    Prompt(Purpose, Option<SecretError>),
    /// The Bitwarden vault is locked: run again with `unlock_first`.
    NeedsUnlock,
    Failed(SecretError),
}

#[derive(Debug, Clone)]
pub(super) struct Report {
    pub outcome: Outcome,
    /// Something the user should know even though the operation worked.
    pub notice: Option<String>,
}

impl Report {
    fn of(outcome: Outcome) -> Self {
        Self {
            outcome,
            notice: None,
        }
    }

    pub fn failed(error: SecretError) -> Self {
        Self::of(Outcome::Failed(error))
    }
}

/// Run `request` to completion. Blocking: call it off the UI thread.
pub(super) fn run(runner: &dyn Runner, request: Request) -> Report {
    let report = match &request.backend {
        Backend::Pass(ctx) => Report::of(run_pass(runner, ctx, &request)),
        Backend::Bitwarden => Report::of(run_bitwarden(runner, &request)),
    };
    if runner.cancelled() {
        return Report::of(Outcome::Failed(SecretError::Cancelled));
    }
    report
}

fn run_pass(runner: &dyn Runner, ctx: &PassContext, request: &Request) -> Outcome {
    let entry_action = match &request.op.kind {
        OpKind::Load => {
            return match pass::load_entries(ctx) {
                Ok(items) => Outcome::Loaded(items),
                Err(error) => Outcome::Failed(SecretError::Tool(error)),
            }
        }
        OpKind::Lock => return done(pass::lock(runner)),
        OpKind::Sync => return done(pass::sync(runner, ctx)),
        OpKind::Entry(action, entry) => (*action, entry),
    };
    let (action, entry) = entry_action;
    let pinentry = match &request.credential {
        Some(passphrase) => Pinentry::Loopback(passphrase),
        None => Pinentry::Cached,
    };
    match pass::fetch(runner, ctx, entry, action, pinentry) {
        Ok(payload) => Outcome::Ready(SecretJob::fetched(action, payload, request.settings)),
        Err(SecretError::NeedPassphrase) if ctx.pinentry == crate::PassPinentry::Agent => {
            Outcome::Ready(SecretJob {
                action,
                source: JobSource::PassAgent {
                    ctx: ctx.clone(),
                    entry: entry.clone(),
                },
                settings: request.settings,
            })
        }
        Err(SecretError::NeedPassphrase) => Outcome::Prompt(Purpose::GpgPassphrase, None),
        Err(SecretError::BadPassphrase) if request.credential.is_some() => {
            Outcome::Prompt(Purpose::GpgPassphrase, Some(SecretError::BadPassphrase))
        }
        Err(error) => Outcome::Failed(error),
    }
}

fn done(result: Result<(), SecretError>) -> Outcome {
    match result {
        Ok(()) => Outcome::Done,
        Err(error) => Outcome::Failed(error),
    }
}

fn run_bitwarden(runner: &dyn Runner, request: &Request) -> Outcome {
    if request.op.kind == OpKind::Lock {
        return done(bitwarden::lock(runner));
    }
    // Every other rbw command unlocks on its own, popping rbw's pinentry
    // under a misleading progress row; check first and say so instead.
    if request.unlock_first {
        if let Err(error) = bitwarden::unlock(runner) {
            return Outcome::Failed(error);
        }
    } else {
        match bitwarden::is_unlocked(runner) {
            Ok(true) => {}
            Ok(false) => return Outcome::NeedsUnlock,
            Err(error) => return Outcome::Failed(error),
        }
    }
    if runner.cancelled() {
        return Outcome::Failed(SecretError::Cancelled);
    }
    let result = match &request.op.kind {
        OpKind::Load => bitwarden::load_items(runner).map(Outcome::Loaded),
        OpKind::Sync => bitwarden::sync(runner).map(|()| Outcome::Done),
        OpKind::Entry(action, id) => bitwarden::fetch(runner, id, *action)
            .map(|payload| Outcome::Ready(SecretJob::fetched(*action, payload, request.settings))),
        OpKind::Lock => unreachable!("handled above"),
    };
    result.unwrap_or_else(Outcome::Failed)
}

#[derive(Debug, Clone)]
enum JobSource {
    Fetched(Secret),
    /// Decrypt after Raffi closed, with gpg-agent's own pinentry.
    PassAgent {
        ctx: PassContext,
        entry: String,
    },
}

/// A secret action run after Raffi closed. Its `Debug` shows no secret.
#[derive(Debug, Clone)]
pub(super) struct SecretJob {
    action: EntryAction,
    source: JobSource,
    settings: JobSettings,
}

impl SecretJob {
    pub fn fetched(action: EntryAction, secret: Secret, settings: JobSettings) -> Self {
        Self {
            action,
            source: JobSource::Fetched(secret),
            settings,
        }
    }

    pub fn run(&self) -> Result<(), String> {
        self.run_with(&SystemRunner {
            cancel: Cancel::default(),
        })
        .map_err(|error| error.message())
    }

    fn run_with(&self, runner: &dyn Runner) -> Result<(), SecretError> {
        let secret = match &self.source {
            JobSource::Fetched(secret) => secret.clone(),
            JobSource::PassAgent { ctx, entry } => {
                pass::fetch(runner, ctx, entry, self.action, Pinentry::Agent)?
            }
        };
        match self.action {
            EntryAction::Copy(_) => copy_secret(
                &ClipboardTools::detect()?,
                &secret,
                self.settings.clipboard_timeout,
            ),
            EntryAction::Type => {
                let tool = TypeTool::detect()?;
                std::thread::sleep(self.settings.type_delay);
                type_secret(runner, tool, &secret)
            }
        }
    }
}

/// The masked prompt for the GPG passphrase.
#[derive(Debug)]
pub(super) struct PromptState {
    pub purpose: Purpose,
    /// The operation to resume once unlocked.
    pub op: Op,
    pub buffer: Secret,
    pub error: Option<String>,
}

/// Secret operations of one Raffi run. At most one runs at a time; its
/// children are killed when it is dropped, and when Raffi exits.
#[derive(Debug, Default)]
pub(super) struct VaultState {
    /// Bumped by every start and abort, so stale results are dropped.
    pub generation: u64,
    pub cancel: Cancel,
    pub running: Option<Op>,
    pub prompt: Option<PromptState>,
    /// The running operation first waits for rbw's pinentry.
    pub unlocking: bool,
    /// The Bitwarden notes search in flight, apart from the operation:
    /// its generation, its children, and the filter it is for.
    pub search_generation: u64,
    pub search_cancel: Cancel,
    pub search_query: Option<String>,
    /// When each entry was last copied or typed, by row identity, and where
    /// that is saved; `None` keeps it in memory only.
    pub usage: HashMap<String, MruEntry>,
    pub usage_path: Option<PathBuf>,
}

impl VaultState {
    pub fn busy(&self) -> bool {
        self.running.is_some()
    }

    /// Start `op`; its result must carry the returned generation.
    pub fn begin(&mut self, op: Op) -> (u64, Cancel) {
        self.cancel.cancel();
        self.cancel = Cancel::default();
        self.generation = self.generation.wrapping_add(1);
        self.running = Some(op);
        self.unlocking = false;
        (self.generation, self.cancel.clone())
    }

    /// Drop whatever runs, killing its children, and close the prompt.
    pub fn abort(&mut self) {
        self.cancel.cancel();
        self.generation = self.generation.wrapping_add(1);
        self.running = None;
        self.prompt = None;
        self.unlocking = false;
    }

    /// Take the operation a result belongs to, if it is still wanted.
    pub fn finish(&mut self, generation: u64) -> Option<Op> {
        if generation != self.generation {
            return None;
        }
        self.running.take()
    }

    /// Start a notes search for `query`, dropping the one in flight; its
    /// result must carry the returned generation.
    pub fn begin_search(&mut self, query: String) -> (u64, Cancel) {
        self.stop_search();
        self.search_query = Some(query);
        (self.search_generation, self.search_cancel.clone())
    }

    /// Drop the notes search in flight, killing its children.
    pub fn stop_search(&mut self) {
        if self.search_query.take().is_some() {
            self.search_cancel.cancel();
            self.search_cancel = Cancel::default();
            self.search_generation = self.search_generation.wrapping_add(1);
        }
    }

    /// The filter a search result is for, if it is still wanted.
    pub fn finish_search(&mut self, generation: u64) -> Option<String> {
        if generation != self.search_generation {
            return None;
        }
        let query = self.search_query.take();
        self.search_generation = self.search_generation.wrapping_add(1);
        query
    }

    /// The text of the row replacing the list while prompting or working.
    pub fn overlay(&self) -> Option<String> {
        if let Some(prompt) = &self.prompt {
            if self.busy() {
                return Some(match prompt.purpose {
                    Purpose::GpgPassphrase => "Decrypting...".to_string(),
                });
            }
            return Some(match &prompt.error {
                Some(error) => format!("{error}, try again (Escape cancels)"),
                None => format!(
                    "Enter the {} and press Enter (Escape cancels)",
                    prompt.purpose.placeholder()
                ),
            });
        }
        if self.unlocking && self.busy() {
            return Some("Unlocking with rbw: answer the pinentry window (Escape cancels)".into());
        }
        self.running.as_ref()?.progress().map(str::to_string)
    }
}

impl Drop for VaultState {
    fn drop(&mut self) {
        self.cancel.cancel();
        self.search_cancel.cancel();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::secret::SecretBytes;
    use crate::PassPinentry;
    use std::path::PathBuf;
    use std::sync::Mutex;

    use super::super::secret_io::{Cmd, Output};

    struct FakeRunner {
        outputs: Mutex<Vec<(i32, &'static str, &'static str)>>,
        seen: Mutex<Vec<Cmd>>,
        cancel: Cancel,
    }

    impl FakeRunner {
        fn new(outputs: &[(i32, &'static str, &'static str)]) -> Self {
            Self {
                outputs: Mutex::new(outputs.iter().rev().copied().collect()),
                seen: Mutex::new(Vec::new()),
                cancel: Cancel::default(),
            }
        }
    }

    impl Runner for FakeRunner {
        fn run(&self, cmd: &Cmd) -> Result<Output, SecretError> {
            self.seen.lock().unwrap().push(cmd.clone());
            let (code, out, err) = self
                .outputs
                .lock()
                .unwrap()
                .pop()
                .ok_or(SecretError::Tool("unexpected command".into()))?;
            Ok(Output {
                code: Some(code),
                stdout: SecretBytes::new(out.as_bytes().to_vec()),
                stderr: SecretBytes::new(err.as_bytes().to_vec()),
            })
        }

        fn cancelled(&self) -> bool {
            self.cancel.is_cancelled()
        }
    }

    const NEED: &str = "[GNUPG:] ERROR pkdecrypt_failed 67108949\n";
    const BAD: &str = "[GNUPG:] ERROR pkdecrypt_failed 67108875\n";
    const OKAY: &str = "[GNUPG:] DECRYPTION_OKAY\n";

    fn pass_request(
        dir: &std::path::Path,
        pinentry: PassPinentry,
        credential: Option<&str>,
    ) -> Request {
        Request {
            op: Op {
                addon: VaultAddon::Pass,
                kind: OpKind::Entry(EntryAction::Copy(Field::Password), "site".into()),
            },
            backend: Backend::Pass(PassContext {
                store_dir: dir.to_path_buf(),
                pinentry,
                username_fields: vec!["login".into()],
                gpg: "gpg".into(),
            }),
            credential: credential.map(|text| Secret::new(text.into())),
            unlock_first: false,
            settings: JobSettings {
                clipboard_timeout: 45,
                type_delay: Duration::ZERO,
            },
        }
    }

    fn store() -> PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "raffi-vault-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join("site.gpg"), b"").unwrap();
        dir
    }

    #[test]
    fn test_log_name_leaves_out_the_entry() {
        let op = Op {
            addon: VaultAddon::Pass,
            kind: OpKind::Entry(EntryAction::Copy(Field::Otp), "bank/alice".into()),
        };
        assert_eq!(op.log_name(), "Pass Copy(Otp)");
    }

    #[test]
    fn test_pass_prompts_retries_then_fetches() {
        let dir = store();
        let runner = FakeRunner::new(&[(2, "", NEED)]);
        let report = run(&runner, pass_request(&dir, PassPinentry::Raffi, None));
        assert!(matches!(
            report.outcome,
            Outcome::Prompt(Purpose::GpgPassphrase, None)
        ));

        let runner = FakeRunner::new(&[(2, "", BAD)]);
        let report = run(
            &runner,
            pass_request(&dir, PassPinentry::Raffi, Some("wrong")),
        );
        assert!(matches!(
            report.outcome,
            Outcome::Prompt(Purpose::GpgPassphrase, Some(SecretError::BadPassphrase))
        ));

        let runner = FakeRunner::new(&[(0, "pw\n", OKAY)]);
        let report = run(
            &runner,
            pass_request(&dir, PassPinentry::Raffi, Some("right")),
        );
        let Outcome::Ready(job) = report.outcome else {
            panic!("expected a job");
        };
        assert!(!format!("{job:?}").contains("pw"));
        match job.source {
            JobSource::Fetched(secret) => assert_eq!(secret.expose(), "pw"),
            _ => panic!("expected a fetched secret"),
        }
        std::fs::remove_dir_all(dir).unwrap();
    }

    #[test]
    fn test_pass_agent_mode_defers_the_decrypt() {
        let dir = store();
        let runner = FakeRunner::new(&[(2, "", NEED)]);
        let report = run(&runner, pass_request(&dir, PassPinentry::Agent, None));
        let Outcome::Ready(job) = report.outcome else {
            panic!("expected a job");
        };
        assert!(matches!(job.source, JobSource::PassAgent { .. }));

        // After close, the decrypt asks gpg-agent; a failure types nothing.
        let runner = FakeRunner::new(&[(2, "", "[GNUPG:] ERROR pkdecrypt_failed 83886179\n")]);
        assert_eq!(job.run_with(&runner).unwrap_err(), SecretError::Cancelled);
        let seen = runner.seen.lock().unwrap();
        assert_eq!(seen.len(), 1);
        assert!(seen[0]
            .args
            .windows(2)
            .any(|pair| pair == ["--pinentry-mode", "ask"]));
        std::fs::remove_dir_all(dir).unwrap();
    }

    #[test]
    fn test_cancelled_run_reports_cancelled() {
        let dir = store();
        let runner = FakeRunner::new(&[(0, "pw\n", OKAY)]);
        runner.cancel.cancel();
        let report = run(&runner, pass_request(&dir, PassPinentry::Raffi, None));
        assert!(matches!(
            report.outcome,
            Outcome::Failed(SecretError::Cancelled)
        ));
        std::fs::remove_dir_all(dir).unwrap();
    }

    #[test]
    fn test_bitwarden_locked_asks_for_unlock_then_resumes() {
        let request = |unlock_first| Request {
            op: Op {
                addon: VaultAddon::Bitwarden,
                kind: OpKind::Entry(EntryAction::Copy(Field::Username), "id".into()),
            },
            backend: Backend::Bitwarden,
            credential: None,
            unlock_first,
            settings: JobSettings {
                clipboard_timeout: 0,
                type_delay: Duration::ZERO,
            },
        };
        // Locked: nothing else runs, so rbw's pinentry does not pop up
        // under the wrong progress row.
        let runner = FakeRunner::new(&[(1, "", "agent is locked")]);
        assert!(matches!(
            run(&runner, request(false)).outcome,
            Outcome::NeedsUnlock
        ));
        assert_eq!(runner.seen.lock().unwrap().len(), 1);

        // The second run unlocks, then resumes the original fetch.
        let item = r#"{"id":"id","data":{"username":"me","password":null,"totp":null}}"#;
        let runner = FakeRunner::new(&[(0, "", ""), (0, item, "")]);
        assert!(matches!(
            run(&runner, request(true)).outcome,
            Outcome::Ready(_)
        ));
        let seen = runner.seen.lock().unwrap();
        assert_eq!(seen[0].args, ["unlock"]);
        assert_eq!(seen[1].args, ["get", "--raw", "id"]);
        drop(seen);

        // A cancelled pinentry stops there.
        let runner = FakeRunner::new(&[(1, "", "pinentry cancelled")]);
        assert!(matches!(
            run(&runner, request(true)).outcome,
            Outcome::Failed(SecretError::Tool(_))
        ));
        assert_eq!(runner.seen.lock().unwrap().len(), 1);
    }

    #[test]
    fn test_state_drops_stale_results_and_kills_on_abort() {
        let mut state = VaultState::default();
        let op = Op {
            addon: VaultAddon::Pass,
            kind: OpKind::Load,
        };
        let (generation, cancel) = state.begin(op.clone());
        state.abort();
        assert!(cancel.is_cancelled());
        assert_eq!(state.finish(generation), None);

        let (generation, _) = state.begin(op.clone());
        assert_eq!(state.finish(generation), Some(op));
        assert!(!state.busy());
    }
}