wafrift-proxy 0.2.10

HTTP forward proxy with automatic WAF evasion and optional TLS interception support.
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
//! Keystroke dispatch for the TUI event loop.
//!
//! Two-mode model:
//! - **Normal**: keys are commands (tab switching, navigation, yank, …).
//! - **FilterEdit**: printable chars build the filter query; Esc cancels,
//!   Enter commits, Backspace deletes.

use crossterm::event::{KeyCode, KeyModifiers};
use tokio::sync::oneshot;

use super::state::{InputMode, State, Tab, ToastKind};
use super::yank::{replay_to_disk_and_optionally_exec, yank_to_disk_and_clipboard};

/// Result of one keystroke dispatch — `true` means the loop should
/// exit.
#[must_use]
pub fn handle_key(
    state: &mut State,
    code: KeyCode,
    mods: KeyModifiers,
    quit: &mut Option<oneshot::Sender<()>>,
) -> bool {
    // Filter-edit mode swallows most keys to build the query.
    if state.input_mode == InputMode::FilterEdit {
        return handle_filter_edit(state, code, mods);
    }
    handle_normal(state, code, mods, quit)
}

fn handle_filter_edit(state: &mut State, code: KeyCode, mods: KeyModifiers) -> bool {
    match code {
        KeyCode::Esc => state.cancel_filter_edit(),
        KeyCode::Enter => state.commit_filter_edit(),
        KeyCode::Backspace => state.filter_backspace(),
        KeyCode::Char(c) => {
            // Ctrl-c during edit cancels rather than quitting — operator
            // expects edit-mode to be modal.
            if mods.contains(KeyModifiers::CONTROL) && (c == 'c' || c == 'C') {
                state.cancel_filter_edit();
            } else if !mods.contains(KeyModifiers::CONTROL) {
                state.filter_push(c);
            }
        }
        _ => {}
    }
    false
}

fn handle_normal(
    state: &mut State,
    code: KeyCode,
    mods: KeyModifiers,
    quit: &mut Option<oneshot::Sender<()>>,
) -> bool {
    match code {
        KeyCode::Char('q') | KeyCode::Esc => {
            send_quit(quit);
            return true;
        }
        KeyCode::Char('c') if mods.contains(KeyModifiers::CONTROL) => {
            send_quit(quit);
            return true;
        }

        // Tab switching
        KeyCode::Tab => state.tab = state.tab.next(),
        KeyCode::Char('1') | KeyCode::Char('f') | KeyCode::Char('F') => state.tab = Tab::Flow,
        KeyCode::Char('2') | KeyCode::Char('o') | KeyCode::Char('O')
            if !is_flow_outcome_cycle(state, code) =>
        {
            // 'o' / 'O' is bound to outcome filter on Flow; on other tabs
            // it switches to Overview (kept for backward compat).
            state.tab = Tab::Overview;
        }
        KeyCode::Char('2') => state.tab = Tab::Overview,
        KeyCode::Char('3') | KeyCode::Char('H') => state.tab = Tab::Hosts,
        KeyCode::Char('4') | KeyCode::Char('t') | KeyCode::Char('T') => {
            state.tab = Tab::Techniques;
        }
        KeyCode::Char('5') => state.tab = Tab::Intercept,

        // Outcome filter cycle (Flow only) — 'o' lowercase
        KeyCode::Char('o') if state.tab == Tab::Flow => {
            state.cycle_outcome_filter();
            let label = state.outcome_filter.label();
            state.set_toast(format!("outcome filter → {label}"), ToastKind::Info);
        }

        // Filter-edit entry
        KeyCode::Char('/') => {
            state.enter_filter_edit();
            state.set_toast(
                "filter: type to narrow, Enter to commit, Esc to cancel",
                ToastKind::Info,
            );
        }

        // Pause/follow toggle
        KeyCode::Char('p') | KeyCode::Char('P') => {
            state.toggle_follow();
            let msg = if state.follow {
                "follow → ON"
            } else {
                "follow → PAUSED"
            };
            state.set_toast(msg, ToastKind::Info);
        }

        KeyCode::Char('r') if state.tab != Tab::Intercept => {
            state.record(&super::state::Event::ResetCounters);
            state.set_toast("counters reset", ToastKind::Ok);
        }
        KeyCode::Char('c') if state.tab != Tab::Intercept => {
            state.recent.clear();
            state.selected = None;
            state.set_toast("request list cleared", ToastKind::Ok);
        }

        // Navigation — when inspect pane is open, j/k scroll the detail
        // pane; otherwise they navigate the list.
        KeyCode::Char('j') | KeyCode::Down if state.tab == Tab::Flow => {
            if state.inspect {
                state.detail_scroll = state.detail_scroll.saturating_add(1);
            } else {
                state.select_offset(1);
            }
        }
        KeyCode::Char('k') | KeyCode::Up if state.tab == Tab::Flow => {
            if state.inspect {
                state.detail_scroll = state.detail_scroll.saturating_sub(1);
            } else {
                state.select_offset(-1);
            }
        }
        KeyCode::PageDown if state.tab == Tab::Flow => {
            if state.inspect {
                state.detail_scroll = state.detail_scroll.saturating_add(10);
            } else {
                state.select_offset(10);
            }
        }
        KeyCode::PageUp if state.tab == Tab::Flow => {
            if state.inspect {
                state.detail_scroll = state.detail_scroll.saturating_sub(10);
            } else {
                state.select_offset(-10);
            }
        }
        KeyCode::Home if state.tab == Tab::Flow => {
            if state.inspect {
                state.detail_scroll = 0;
            } else {
                state.select_first();
            }
        }
        KeyCode::End if state.tab == Tab::Flow => {
            if state.inspect {
                state.detail_scroll = u16::MAX;
            } else {
                state.select_last();
                state.follow = true;
            }
        }
        KeyCode::Char('g') if state.tab == Tab::Flow => state.select_first(),
        KeyCode::Char('G') if state.tab == Tab::Flow => state.select_last(),
        KeyCode::Enter if state.tab == Tab::Flow => {
            state.inspect = !state.inspect;
            state.detail_scroll = 0;
            if state.inspect && state.selected.is_none() {
                state.select_last();
            }
        }

        // Yank curl (Flow with selection only)
        KeyCode::Char('y') | KeyCode::Char('Y') if state.tab == Tab::Flow => {
            do_yank(state);
        }

        // Replay — write a /tmp/wafrift-replay-N.curl reproducer and
        // (when WAFRIFT_REPLAY_AUTOEXEC=1) re-fire it via bash. This
        // does NOT route through the proxy's evade pipeline — the
        // captured request is already evaded.
        KeyCode::Char('R') if state.tab == Tab::Flow => {
            do_replay(state);
        }

        // Intercept-mode toggle — works from any tab so the operator
        // doesn't have to navigate to enable.
        KeyCode::Char('i') | KeyCode::Char('I') => {
            let now_on = crate::intercept::toggle_intercept_mode();
            state.set_toast(
                format!("intercept mode → {}", if now_on { "ON" } else { "OFF" }),
                if now_on {
                    ToastKind::Warn
                } else {
                    ToastKind::Info
                },
            );
            if now_on {
                state.tab = Tab::Intercept;
            }
        }

        // Intercept tab actions: r releases the oldest pending,
        // k kills the oldest pending. Operator-friendly default
        // (act on the head of the queue) so no per-row selection
        // is needed for the v1 surface.
        KeyCode::Char('r') if state.tab == Tab::Intercept => {
            let store = crate::intercept::global_store();
            if let Some(pending) = store.snapshot().into_iter().next() {
                store.resolve(pending.id, crate::intercept::InterceptDecision::Release);
                state.set_toast(
                    format!("released #{} → upstream", pending.id),
                    ToastKind::Ok,
                );
            } else {
                state.set_toast("intercept: no pending request", ToastKind::Warn);
            }
        }
        KeyCode::Char('k') if state.tab == Tab::Intercept => {
            let store = crate::intercept::global_store();
            if let Some(pending) = store.snapshot().into_iter().next() {
                store.resolve(pending.id, crate::intercept::InterceptDecision::Kill);
                state.set_toast(format!("killed #{} → 403", pending.id), ToastKind::Err);
            } else {
                state.set_toast("intercept: no pending request", ToastKind::Warn);
            }
        }

        _ => {}
    }
    false
}

/// Bypass the `2`/`o` overlap: when we're already on Flow, lowercase
/// `o` is the outcome filter command, not "switch to Overview". This
/// helper detects that exact case so the broader keymap match doesn't
/// fire the wrong arm.
fn is_flow_outcome_cycle(state: &State, code: KeyCode) -> bool {
    matches!(code, KeyCode::Char('o')) && state.tab == Tab::Flow
}

fn send_quit(quit: &mut Option<oneshot::Sender<()>>) {
    if let Some(tx) = quit.take() {
        let _ = tx.send(());
    }
}

fn do_replay(state: &mut State) {
    let Some(idx) = state.selected else {
        state.set_toast("replay: no request selected", ToastKind::Warn);
        return;
    };
    let Some(rec) = state.recent.get(idx).cloned() else {
        state.set_toast("replay: stale selection", ToastKind::Warn);
        return;
    };
    state.yank_seq = state.yank_seq.wrapping_add(1);
    let seq = state.yank_seq;
    match replay_to_disk_and_optionally_exec(&rec, seq) {
        Ok(report) => {
            let exec_label = match report.upstream_status {
                Some(code) => format!("autoexec exit={code}"),
                None => "no autoexec (set WAFRIFT_REPLAY_AUTOEXEC=1 to fire on every R)".into(),
            };
            state.set_toast(
                format!(
                    "replay → {} ({} bytes, {})",
                    report.path.display(),
                    report.bytes,
                    exec_label
                ),
                if report.upstream_status.unwrap_or(0) == 0 {
                    ToastKind::Ok
                } else {
                    ToastKind::Info
                },
            );
        }
        Err(e) => {
            state.set_toast(format!("replay failed: {e}"), ToastKind::Err);
        }
    }
}

fn do_yank(state: &mut State) {
    let Some(idx) = state.selected else {
        state.set_toast("yank: no request selected", ToastKind::Warn);
        return;
    };
    let Some(rec) = state.recent.get(idx).cloned() else {
        state.set_toast("yank: stale selection", ToastKind::Warn);
        return;
    };
    state.yank_seq = state.yank_seq.wrapping_add(1);
    let seq = state.yank_seq;
    match yank_to_disk_and_clipboard(&rec, seq) {
        Ok(report) => {
            let clip_label = if report.clipboard_ok {
                "clipboard ✓"
            } else {
                "clipboard ✗"
            };
            state.set_toast(
                format!(
                    "yanked → {} ({} bytes, {})",
                    report.path.display(),
                    report.bytes,
                    clip_label
                ),
                if report.clipboard_ok {
                    ToastKind::Ok
                } else {
                    ToastKind::Warn
                },
            );
        }
        Err(e) => {
            state.set_toast(format!("yank failed: {e}"), ToastKind::Err);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tui::state::{Event, OutcomeFilter, State, Tab};

    fn req(host: &str, status: u16, bypassed: bool) -> Event {
        Event::Request {
            host: host.into(),
            method: "GET".into(),
            path: "/".into(),
            status,
            bypassed,
            blocked: !bypassed && status == 403,
            techniques: String::new(),
            tls_profile: None,
            body_padded: false,
            upstream_latency_ms: 1,
            waf_name: None,
            req_headers: vec![],
            req_body_excerpt: vec![],
            req_headers_pre: vec![],
            req_body_pre_excerpt: vec![],
            resp_headers: vec![],
            resp_body_excerpt: vec![],
            resp_body_total: 0,
            attempts: 0,
        }
    }

    fn key(c: char) -> (KeyCode, KeyModifiers) {
        (KeyCode::Char(c), KeyModifiers::NONE)
    }

    fn press(s: &mut State, code: KeyCode, mods: KeyModifiers) {
        let mut q: Option<oneshot::Sender<()>> = None;
        let _ = handle_key(s, code, mods, &mut q);
    }

    #[test]
    fn slash_enters_filter_edit_mode() {
        let mut s = State::new();
        let (c, m) = key('/');
        press(&mut s, c, m);
        assert_eq!(s.input_mode, InputMode::FilterEdit);
    }

    #[test]
    fn typing_in_filter_edit_builds_query() {
        let mut s = State::new();
        s.enter_filter_edit();
        press(&mut s, KeyCode::Char('a'), KeyModifiers::NONE);
        press(&mut s, KeyCode::Char('p'), KeyModifiers::NONE);
        press(&mut s, KeyCode::Char('i'), KeyModifiers::NONE);
        assert_eq!(s.filter_query, "api");
        // Esc cancels, clearing the query
        press(&mut s, KeyCode::Esc, KeyModifiers::NONE);
        assert_eq!(s.input_mode, InputMode::Normal);
        assert_eq!(s.filter_query, "");
    }

    #[test]
    fn enter_commits_filter_edit() {
        let mut s = State::new();
        s.enter_filter_edit();
        press(&mut s, KeyCode::Char('a'), KeyModifiers::NONE);
        press(&mut s, KeyCode::Enter, KeyModifiers::NONE);
        assert_eq!(s.input_mode, InputMode::Normal);
        assert_eq!(s.filter_query, "a");
    }

    #[test]
    fn p_toggles_follow() {
        let mut s = State::new();
        assert!(s.follow);
        press(&mut s, KeyCode::Char('p'), KeyModifiers::NONE);
        assert!(!s.follow);
        press(&mut s, KeyCode::Char('p'), KeyModifiers::NONE);
        assert!(s.follow);
    }

    #[test]
    fn o_on_flow_cycles_outcome_filter() {
        let mut s = State::new();
        assert_eq!(s.tab, Tab::Flow);
        press(&mut s, KeyCode::Char('o'), KeyModifiers::NONE);
        assert_eq!(s.outcome_filter, OutcomeFilter::BypassOnly);
        // tab unchanged — 'o' on Flow MUST NOT also jump to Overview
        assert_eq!(s.tab, Tab::Flow);
    }

    #[test]
    fn o_on_overview_does_not_cycle_outcome_filter() {
        let mut s = State::new();
        s.tab = Tab::Overview;
        let before = s.outcome_filter;
        press(&mut s, KeyCode::Char('o'), KeyModifiers::NONE);
        // we're on Overview already — pressing 'o' is a no-op (was a
        // jump-to-Overview command, but we're already there)
        assert_eq!(s.outcome_filter, before);
    }

    #[test]
    fn t_switches_to_techniques_tab() {
        let mut s = State::new();
        press(&mut s, KeyCode::Char('t'), KeyModifiers::NONE);
        assert_eq!(s.tab, Tab::Techniques);
    }

    #[test]
    fn h_capital_switches_to_hosts() {
        let mut s = State::new();
        press(&mut s, KeyCode::Char('H'), KeyModifiers::NONE);
        assert_eq!(s.tab, Tab::Hosts);
    }

    #[test]
    fn j_in_inspect_scrolls_detail_not_list() {
        let mut s = State::new();
        s.record(&req("a.com", 200, true));
        s.record(&req("b.com", 200, true));
        s.select_last();
        s.inspect = true;
        let before_sel = s.selected;
        press(&mut s, KeyCode::Char('j'), KeyModifiers::NONE);
        assert_eq!(s.detail_scroll, 1);
        assert_eq!(
            s.selected, before_sel,
            "selection must NOT move while inspecting"
        );
    }

    #[test]
    fn pgdn_scrolls_detail_when_inspecting() {
        let mut s = State::new();
        s.record(&req("a.com", 200, true));
        s.select_last();
        s.inspect = true;
        press(&mut s, KeyCode::PageDown, KeyModifiers::NONE);
        assert_eq!(s.detail_scroll, 10);
    }

    #[test]
    fn end_outside_inspect_engages_follow() {
        let mut s = State::new();
        s.record(&req("a.com", 200, true));
        s.follow = false;
        press(&mut s, KeyCode::End, KeyModifiers::NONE);
        assert!(s.follow);
    }

    #[test]
    fn five_switches_to_intercept_tab() {
        let mut s = State::new();
        press(&mut s, KeyCode::Char('5'), KeyModifiers::NONE);
        assert_eq!(s.tab, Tab::Intercept);
    }

    #[test]
    fn i_toggles_intercept_mode_and_jumps_to_tab_when_enabling() {
        // Reset known state so the toggle direction is deterministic.
        crate::intercept::set_intercept_mode(false);
        let mut s = State::new();
        assert_eq!(s.tab, Tab::Flow);
        press(&mut s, KeyCode::Char('i'), KeyModifiers::NONE);
        assert!(crate::intercept::intercept_mode_enabled());
        assert_eq!(s.tab, Tab::Intercept, "enabling intercept jumps to the tab");
        press(&mut s, KeyCode::Char('i'), KeyModifiers::NONE);
        assert!(!crate::intercept::intercept_mode_enabled());
        // Reset for any later test.
        crate::intercept::set_intercept_mode(false);
    }

    #[test]
    fn r_on_intercept_tab_does_not_reset_counters() {
        let mut s = State::new();
        // Force the unguarded reset arm NOT to fire from the
        // Intercept tab — total must stay 0 after r.
        s.tab = Tab::Intercept;
        s.total = 7;
        press(&mut s, KeyCode::Char('r'), KeyModifiers::NONE);
        assert_eq!(s.total, 7, "r on Intercept must not run reset_counters");
    }

    #[test]
    fn ctrl_c_in_filter_edit_cancels_not_quits() {
        let mut s = State::new();
        s.enter_filter_edit();
        s.filter_push('a');
        let mut q: Option<oneshot::Sender<()>> = None;
        let exit = handle_key(&mut s, KeyCode::Char('c'), KeyModifiers::CONTROL, &mut q);
        assert!(!exit);
        assert_eq!(s.input_mode, InputMode::Normal);
    }
}