shellfirm 0.3.9

`shellfirm` will intercept any risky patterns (default or defined by you) and prompt you a small challenge for double verification, kinda like a captcha for your terminal.
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
//! Platform-agnostic types and logic for the PTY proxy.

use std::{collections::HashMap, sync::OnceLock};

use regex::Regex;
use tracing::{debug, warn};

use crate::{
    audit,
    checks::{self, Check},
    config::{Config, Settings, WrappersConfig},
    env::Environment,
    prompt::{ChallengeResult, Prompter},
};

// ---------------------------------------------------------------------------
// Delimiter
// ---------------------------------------------------------------------------

/// Statement delimiter for the wrapped program.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Delimiter {
    /// A specific character (e.g. `;`).
    Char(char),
    /// Newline (`\n`).
    Newline,
}

impl Delimiter {
    /// Parse a delimiter from a config string.
    ///
    /// Falls back to `;` for multi-character strings that aren't `\n`.
    #[must_use]
    pub fn from_str_config(s: &str) -> Self {
        match s {
            "\\n" | "\n" => Self::Newline,
            _ => s
                .chars()
                .next()
                .filter(|_| s.len() == 1)
                .map_or(Self::Char(';'), Self::Char),
        }
    }

    /// The byte value that triggers statement completion.
    #[must_use]
    pub const fn trigger_byte(self) -> u8 {
        match self {
            Self::Char(c) => c as u8,
            Self::Newline => b'\n',
        }
    }
}

// ---------------------------------------------------------------------------
// WrapperConfig (resolved per-invocation)
// ---------------------------------------------------------------------------

/// Resolved configuration for a single `shellfirm wrap` invocation.
#[derive(Debug, Clone)]
pub struct WrapperConfig {
    pub program: String,
    pub delimiter: Delimiter,
    pub check_groups: Vec<String>,
    pub display_name: String,
}

/// Built-in defaults for known tools.
fn builtin_defaults() -> &'static HashMap<&'static str, (&'static str, &'static [&'static str])> {
    static DEFAULTS: OnceLock<HashMap<&str, (&str, &[&str])>> = OnceLock::new();
    DEFAULTS.get_or_init(|| {
        let mut m = HashMap::new();
        m.insert("psql", (";", &["database"] as &[&str]));
        m.insert("mysql", (";", &["database"] as &[&str]));
        m.insert("redis-cli", ("\\n", &["database"] as &[&str]));
        m.insert("mongosh", (";", &["database"] as &[&str]));
        m.insert("mongo", (";", &["database"] as &[&str]));
        m
    })
}

impl WrapperConfig {
    /// Resolve the wrapper config for a given program.
    ///
    /// Priority: CLI `--delimiter` flag > user config > built-in defaults > generic fallback.
    #[must_use]
    #[allow(clippy::option_if_let_else)]
    pub fn resolve(
        program: &str,
        cli_delimiter: Option<&str>,
        user_config: &WrappersConfig,
    ) -> Self {
        let base_name = std::path::Path::new(program)
            .file_name()
            .and_then(|s| s.to_str())
            .unwrap_or(program);

        // Look up user config, then built-in defaults
        let user_tool = user_config.tools.get(base_name);
        let builtin = builtin_defaults().get(base_name);

        // Resolve delimiter
        let delimiter = if let Some(d) = cli_delimiter {
            Delimiter::from_str_config(d)
        } else if let Some(tool) = user_tool {
            Delimiter::from_str_config(&tool.delimiter)
        } else if let Some((d, _)) = builtin {
            Delimiter::from_str_config(d)
        } else {
            Delimiter::Newline // generic fallback
        };

        // Resolve check groups
        let check_groups = if let Some(tool) = user_tool.filter(|t| !t.check_groups.is_empty()) {
            tool.check_groups.clone()
        } else if let Some((_, groups)) = builtin {
            groups.iter().map(|s| (*s).to_string()).collect()
        } else {
            vec![] // empty = use global setting
        };

        Self {
            program: program.to_string(),
            delimiter,
            check_groups,
            display_name: base_name.to_string(),
        }
    }
}

// ---------------------------------------------------------------------------
// InputBuffer — quote-aware delimiter detection
// ---------------------------------------------------------------------------

/// Tracks quote/escape state for delimiter detection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum QuoteState {
    Normal,
    SingleQuoted,
    DoubleQuoted,
    /// The next character is escaped (after `\` in Normal).
    EscapedNormal,
    /// The next character is escaped (after `\` in `DoubleQuoted`).
    EscapedDouble,
}

/// Result of feeding a byte to the input buffer.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BufferResult {
    /// Byte was buffered, no statement complete yet.
    Buffered,
    /// A complete statement was detected (delimiter found outside quotes).
    Statement(String),
}

/// Accumulates input bytes and detects statement boundaries,
/// respecting single/double quotes and backslash escapes.
#[derive(Debug)]
pub struct InputBuffer {
    buf: Vec<u8>,
    state: QuoteState,
    delimiter: Delimiter,
}

impl InputBuffer {
    /// Create a new buffer with the given delimiter.
    #[must_use]
    pub fn new(delimiter: Delimiter) -> Self {
        Self {
            buf: Vec::with_capacity(256),
            state: QuoteState::Normal,
            delimiter,
        }
    }

    /// Feed a single byte. Returns `Statement(text)` when a delimiter is
    /// found outside of quotes, consuming the buffer up to (but not
    /// including) the delimiter byte.
    pub fn feed(&mut self, byte: u8) -> BufferResult {
        match self.state {
            QuoteState::EscapedNormal => {
                self.buf.push(byte);
                self.state = QuoteState::Normal;
                BufferResult::Buffered
            }
            QuoteState::EscapedDouble => {
                self.buf.push(byte);
                self.state = QuoteState::DoubleQuoted;
                BufferResult::Buffered
            }
            QuoteState::SingleQuoted => {
                self.buf.push(byte);
                if byte == b'\'' {
                    self.state = QuoteState::Normal;
                }
                BufferResult::Buffered
            }
            QuoteState::DoubleQuoted => {
                self.buf.push(byte);
                if byte == b'"' {
                    self.state = QuoteState::Normal;
                } else if byte == b'\\' {
                    self.state = QuoteState::EscapedDouble;
                }
                BufferResult::Buffered
            }
            QuoteState::Normal => {
                if byte == b'\\' {
                    self.buf.push(byte);
                    self.state = QuoteState::EscapedNormal;
                    return BufferResult::Buffered;
                }
                if byte == b'\'' {
                    self.buf.push(byte);
                    self.state = QuoteState::SingleQuoted;
                    return BufferResult::Buffered;
                }
                if byte == b'"' {
                    self.buf.push(byte);
                    self.state = QuoteState::DoubleQuoted;
                    return BufferResult::Buffered;
                }
                // Check delimiter
                if byte == self.delimiter.trigger_byte() {
                    let stmt = String::from_utf8_lossy(&self.buf).to_string();
                    self.buf.clear();
                    self.state = QuoteState::Normal;
                    return BufferResult::Statement(stmt);
                }
                self.buf.push(byte);
                BufferResult::Buffered
            }
        }
    }

    /// Reset the buffer and quote state.
    pub fn reset(&mut self) {
        self.buf.clear();
        self.state = QuoteState::Normal;
    }
}

// ---------------------------------------------------------------------------
// Statement handling (reuses the existing analysis pipeline)
// ---------------------------------------------------------------------------

/// Outcome after analyzing a statement.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StatementAction {
    /// Statement is safe or user passed the challenge — forward the delimiter.
    Forward,
    /// Statement was blocked — send Ctrl-C to cancel.
    Block,
}

fn strip_quotes_regex() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| Regex::new(r#"'[^']*'|"[^"]*""#).unwrap())
}

/// Analyze a statement and optionally challenge the user.
///
/// Fail-open: if analysis errors out, we return `Forward` to avoid
/// breaking the user's interactive session.
#[allow(clippy::too_many_arguments)]
pub fn handle_statement(
    statement: &str,
    settings: &Settings,
    checks: &[Check],
    env: &dyn Environment,
    prompter: &dyn Prompter,
    config: &Config,
    tool_name: &str,
) -> StatementAction {
    let trimmed = statement.trim();
    if trimmed.is_empty() {
        return StatementAction::Forward;
    }

    debug!("[wrap:{tool_name}] analyzing: {trimmed:?}");

    let pipeline =
        match checks::analyze_command(trimmed, settings, checks, env, strip_quotes_regex()) {
            Ok(p) => p,
            Err(e) => {
                warn!("[wrap:{tool_name}] analysis failed (fail-open): {e}");
                return StatementAction::Forward;
            }
        };

    if pipeline.active_matches.is_empty() {
        return StatementAction::Forward;
    }

    let active_refs: Vec<&Check> = pipeline.active_matches.iter().collect();

    // Audit: pre-challenge entry
    let event_id = uuid::Uuid::new_v4().to_string();
    if settings.audit_enabled {
        let event = audit::AuditEvent {
            event_id: event_id.clone(),
            timestamp: audit::now_timestamp(),
            command: format!("[wrap:{tool_name}] {trimmed}"),
            matched_ids: pipeline
                .active_matches
                .iter()
                .map(|c| c.id.clone())
                .collect(),
            challenge_type: format!("{}", settings.challenge),
            outcome: audit::AuditOutcome::Cancelled,
            context_labels: pipeline.context.labels.clone(),
            severity: pipeline.max_severity,
            agent_name: None,
            agent_session_id: None,
            blast_radius_scope: None,
            blast_radius_detail: None,
        };
        if let Err(e) = audit::log_event(&config.audit_log_path(), &event) {
            warn!("Failed to write audit log: {e}");
        }
    }

    // Run challenge
    let result = match checks::challenge_with_context(
        settings,
        &active_refs,
        &pipeline.context,
        &pipeline.merged_policy,
        prompter,
        &pipeline.blast_radii,
    ) {
        Ok(r) => r,
        Err(e) => {
            warn!("[wrap:{tool_name}] challenge failed (fail-open): {e}");
            return StatementAction::Forward;
        }
    };

    // Audit: post-challenge entry
    if settings.audit_enabled {
        let outcome = match result {
            ChallengeResult::Passed => audit::AuditOutcome::Allowed,
            ChallengeResult::Denied => audit::AuditOutcome::Denied,
        };
        let event = audit::AuditEvent {
            event_id,
            timestamp: audit::now_timestamp(),
            command: format!("[wrap:{tool_name}] {trimmed}"),
            matched_ids: pipeline
                .active_matches
                .iter()
                .map(|c| c.id.clone())
                .collect(),
            challenge_type: format!("{}", settings.challenge),
            outcome,
            context_labels: pipeline.context.labels,
            severity: pipeline.max_severity,
            agent_name: None,
            agent_session_id: None,
            blast_radius_scope: None,
            blast_radius_detail: None,
        };
        if let Err(e) = audit::log_event(&config.audit_log_path(), &event) {
            warn!("Failed to write audit log: {e}");
        }
    }

    match result {
        ChallengeResult::Passed => StatementAction::Forward,
        ChallengeResult::Denied => StatementAction::Block,
    }
}

/// Returns true for control bytes that should be forwarded immediately
/// without being fed to the input buffer.
#[must_use]
pub const fn is_control_passthrough(byte: u8) -> bool {
    matches!(
        byte,
        0x01..=0x02 // Ctrl-A, Ctrl-B
        | 0x03      // Ctrl-C
        | 0x04      // Ctrl-D
        | 0x05..=0x06 // Ctrl-E, Ctrl-F
        | 0x09      // Tab
        | 0x0D      // CR (Enter in raw mode) — forward to child, don't buffer
        | 0x0E..=0x10 // Ctrl-N, Ctrl-O, Ctrl-P
        | 0x12..=0x14 // Ctrl-R, Ctrl-S, Ctrl-T
        | 0x15..=0x17 // Ctrl-U, Ctrl-V, Ctrl-W
        | 0x1A      // Ctrl-Z
        | 0x1B      // ESC
        | 0x7F      // DEL (backspace)
    )
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::WrapperToolConfig;

    // -- Delimiter tests --

    #[test]
    fn delimiter_from_semicolon() {
        let d = Delimiter::from_str_config(";");
        assert_eq!(d, Delimiter::Char(';'));
        assert_eq!(d.trigger_byte(), b';');
    }

    #[test]
    fn delimiter_from_newline_escape() {
        let d = Delimiter::from_str_config("\\n");
        assert_eq!(d, Delimiter::Newline);
        assert_eq!(d.trigger_byte(), b'\n');
    }

    #[test]
    fn delimiter_from_literal_newline() {
        let d = Delimiter::from_str_config("\n");
        assert_eq!(d, Delimiter::Newline);
    }

    // -- InputBuffer tests --

    #[test]
    fn basic_semicolon_delimiter() {
        let mut buf = InputBuffer::new(Delimiter::Char(';'));
        assert_eq!(buf.feed(b'S'), BufferResult::Buffered);
        assert_eq!(buf.feed(b'E'), BufferResult::Buffered);
        assert_eq!(buf.feed(b'L'), BufferResult::Buffered);
        assert_eq!(buf.feed(b';'), BufferResult::Statement("SEL".to_string()));
    }

    #[test]
    fn delimiter_inside_single_quotes_not_split() {
        let mut buf = InputBuffer::new(Delimiter::Char(';'));
        for &b in b"INSERT INTO t VALUES('" {
            buf.feed(b);
        }
        // Semicolon inside single quotes — should NOT trigger
        assert_eq!(buf.feed(b';'), BufferResult::Buffered);
        buf.feed(b'\''); // close quote
        buf.feed(b')');
        assert_eq!(
            buf.feed(b';'),
            BufferResult::Statement("INSERT INTO t VALUES(';')".to_string())
        );
    }

    #[test]
    fn delimiter_inside_double_quotes_not_split() {
        let mut buf = InputBuffer::new(Delimiter::Char(';'));
        for &b in b"SELECT \"col;name\" FROM t" {
            buf.feed(b);
        }
        assert_eq!(
            buf.feed(b';'),
            BufferResult::Statement("SELECT \"col;name\" FROM t".to_string())
        );
    }

    #[test]
    fn escaped_quote_handling() {
        let mut buf = InputBuffer::new(Delimiter::Char(';'));
        // SELECT "col\"name" FROM t;
        for &b in b"SELECT \"col\\" {
            buf.feed(b);
        }
        // The \" should not end the double-quote
        buf.feed(b'"'); // this is escaped, stays in DoubleQuoted
        for &b in b"name\" FROM t" {
            buf.feed(b);
        }
        assert_eq!(
            buf.feed(b';'),
            BufferResult::Statement("SELECT \"col\\\"name\" FROM t".to_string())
        );
    }

    #[test]
    fn multiple_statements() {
        let mut buf = InputBuffer::new(Delimiter::Char(';'));
        for &b in b"SELECT 1" {
            buf.feed(b);
        }
        assert_eq!(
            buf.feed(b';'),
            BufferResult::Statement("SELECT 1".to_string())
        );
        for &b in b" DROP TABLE x" {
            buf.feed(b);
        }
        assert_eq!(
            buf.feed(b';'),
            BufferResult::Statement(" DROP TABLE x".to_string())
        );
    }

    #[test]
    fn newline_delimiter() {
        let mut buf = InputBuffer::new(Delimiter::Newline);
        for &b in b"FLUSHALL" {
            buf.feed(b);
        }
        assert_eq!(
            buf.feed(b'\n'),
            BufferResult::Statement("FLUSHALL".to_string())
        );
    }

    #[test]
    fn empty_statement() {
        let mut buf = InputBuffer::new(Delimiter::Char(';'));
        assert_eq!(buf.feed(b';'), BufferResult::Statement(String::new()));
    }

    #[test]
    fn whitespace_only_statement() {
        let mut buf = InputBuffer::new(Delimiter::Char(';'));
        buf.feed(b' ');
        buf.feed(b' ');
        assert_eq!(buf.feed(b';'), BufferResult::Statement("  ".to_string()));
    }

    #[test]
    fn multi_line_sql() {
        let mut buf = InputBuffer::new(Delimiter::Char(';'));
        let input = b"SELECT *\nFROM users\nWHERE id = 1";
        for &b in input {
            buf.feed(b);
        }
        assert_eq!(
            buf.feed(b';'),
            BufferResult::Statement("SELECT *\nFROM users\nWHERE id = 1".to_string())
        );
    }

    #[test]
    fn reset_clears_buffer() {
        let mut buf = InputBuffer::new(Delimiter::Char(';'));
        buf.feed(b'A');
        buf.feed(b'B');
        buf.reset();
        buf.feed(b'C');
        assert_eq!(buf.feed(b';'), BufferResult::Statement("C".to_string()));
    }

    // -- WrapperConfig resolution tests --

    #[test]
    fn known_tool_gets_builtin_defaults() {
        let cfg = WrapperConfig::resolve("psql", None, &WrappersConfig::default());
        assert_eq!(cfg.delimiter, Delimiter::Char(';'));
        assert_eq!(cfg.check_groups, vec!["database"]);
        assert_eq!(cfg.display_name, "psql");
    }

    #[test]
    fn redis_cli_gets_newline_delimiter() {
        let cfg = WrapperConfig::resolve("redis-cli", None, &WrappersConfig::default());
        assert_eq!(cfg.delimiter, Delimiter::Newline);
        assert_eq!(cfg.check_groups, vec!["database"]);
    }

    #[test]
    fn user_override_takes_precedence() {
        let mut tools = HashMap::new();
        tools.insert(
            "psql".to_string(),
            WrapperToolConfig {
                delimiter: "\\n".to_string(),
                check_groups: vec!["custom".to_string()],
            },
        );
        let user_cfg = WrappersConfig { tools };

        let cfg = WrapperConfig::resolve("psql", None, &user_cfg);
        assert_eq!(cfg.delimiter, Delimiter::Newline);
        assert_eq!(cfg.check_groups, vec!["custom"]);
    }

    #[test]
    fn cli_delimiter_overrides_all() {
        let cfg = WrapperConfig::resolve("psql", Some("\\n"), &WrappersConfig::default());
        assert_eq!(cfg.delimiter, Delimiter::Newline);
    }

    #[test]
    fn unknown_tool_gets_generic_fallback() {
        let cfg = WrapperConfig::resolve("some-tool", None, &WrappersConfig::default());
        assert_eq!(cfg.delimiter, Delimiter::Newline);
        assert!(cfg.check_groups.is_empty());
    }

    #[test]
    fn path_in_program_name_uses_basename() {
        let cfg = WrapperConfig::resolve("/usr/bin/psql", None, &WrappersConfig::default());
        assert_eq!(cfg.display_name, "psql");
        assert_eq!(cfg.delimiter, Delimiter::Char(';'));
    }

    // -- handle_statement tests --

    #[test]
    fn safe_statement_forwards() {
        let settings = Settings::default();
        let checks = settings.get_active_checks().unwrap();
        let env = crate::env::MockEnvironment {
            cwd: "/tmp".into(),
            ..Default::default()
        };
        let prompter = crate::prompt::MockPrompter::passing();
        let temp = tree_fs::TreeBuilder::default()
            .create()
            .expect("create tree");
        let config = Config::new(Some(&temp.root.join("app").display().to_string())).unwrap();

        let action = handle_statement(
            "SELECT 1", &settings, &checks, &env, &prompter, &config, "psql",
        );
        assert_eq!(action, StatementAction::Forward);
    }

    #[test]
    fn drop_table_triggers_challenge() {
        let settings = Settings::default();
        let checks = settings.get_active_checks().unwrap();
        let env = crate::env::MockEnvironment {
            cwd: "/tmp".into(),
            ..Default::default()
        };
        let prompter = crate::prompt::MockPrompter::passing();
        let temp = tree_fs::TreeBuilder::default()
            .create()
            .expect("create tree");
        let config = Config::new(Some(&temp.root.join("app").display().to_string())).unwrap();

        let action = handle_statement(
            "DROP TABLE users",
            &settings,
            &checks,
            &env,
            &prompter,
            &config,
            "psql",
        );
        // MockPrompter::passing() passes the challenge
        assert_eq!(action, StatementAction::Forward);

        // Verify the prompter was invoked (challenge was shown)
        let displays = prompter.captured_displays.borrow();
        assert_eq!(displays.len(), 1);
        assert!(displays[0]
            .descriptions
            .iter()
            .any(|d| d.contains("Dropping a table")));
    }

    #[test]
    fn empty_statement_forwards() {
        let settings = Settings::default();
        let checks = settings.get_active_checks().unwrap();
        let env = crate::env::MockEnvironment::default();
        let prompter = crate::prompt::MockPrompter::passing();
        let temp = tree_fs::TreeBuilder::default()
            .create()
            .expect("create tree");
        let config = Config::new(Some(&temp.root.join("app").display().to_string())).unwrap();

        assert_eq!(
            handle_statement("", &settings, &checks, &env, &prompter, &config, "test"),
            StatementAction::Forward
        );
        assert_eq!(
            handle_statement("   ", &settings, &checks, &env, &prompter, &config, "test"),
            StatementAction::Forward
        );
    }

    #[test]
    fn cr_is_control_passthrough() {
        // 0x0D (CR / Enter in raw mode) must be a control passthrough so it
        // never pollutes the input buffer with stray \r bytes.
        assert!(is_control_passthrough(0x0D));
    }

    #[test]
    fn interactive_flushall_triggers_challenge() {
        let settings = Settings::default();
        let checks = settings.get_active_checks().unwrap();
        let env = crate::env::MockEnvironment {
            cwd: "/tmp".into(),
            ..Default::default()
        };
        let prompter = crate::prompt::MockPrompter::passing();
        let temp = tree_fs::TreeBuilder::default()
            .create()
            .expect("create tree");
        let config = Config::new(Some(&temp.root.join("app").display().to_string())).unwrap();

        let action = handle_statement(
            "FLUSHALL",
            &settings,
            &checks,
            &env,
            &prompter,
            &config,
            "redis-cli",
        );
        assert_eq!(action, StatementAction::Forward);

        let displays = prompter.captured_displays.borrow();
        assert_eq!(displays.len(), 1);
        assert!(displays[0]
            .descriptions
            .iter()
            .any(|d| d.contains("FLUSHALL")));
    }
}