rmux-server 0.10.0

Tokio daemon and request dispatcher for the RMUX terminal multiplexer.
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
use super::*;
use crate::pane_io::AttachControl;

async fn register_delegated_attach(
    handler: &RequestHandler,
    requester_pid: u32,
    session: &rmux_proto::SessionName,
) -> mpsc::UnboundedReceiver<AttachControl> {
    create_send_keys_test_session(handler, session).await;
    register_existing_delegated_attach(handler, requester_pid, session).await
}

async fn register_existing_delegated_attach(
    handler: &RequestHandler,
    requester_pid: u32,
    session: &rmux_proto::SessionName,
) -> mpsc::UnboundedReceiver<AttachControl> {
    let (control_tx, control_rx) = mpsc::unbounded_channel();
    handler
        .register_attach(requester_pid, session.clone(), control_tx)
        .await;
    let mut active_attach = handler.active_attach.lock().await;
    let active = active_attach
        .by_pid
        .get_mut(&requester_pid)
        .expect("delegated attach is active");
    active.can_write = false;
    active.flags = active.flags.with_read_only();
    control_rx
}

async fn bind_read_only_key(
    handler: &RequestHandler,
    table_name: &str,
    key: &str,
    command: &[&str],
) {
    let response = handler
        .handle(Request::BindKey(Box::new(BindKeyRequest {
            table_name: table_name.to_owned(),
            key: key.to_owned(),
            note: Some("read-only client action test".to_owned()),
            repeat: false,
            command: Some(
                command
                    .iter()
                    .map(|argument| (*argument).to_owned())
                    .collect(),
            ),
        })))
        .await;
    assert!(matches!(response, Response::BindKey(_)), "{response:?}");
}

async fn active_session_name(
    handler: &RequestHandler,
    requester_pid: u32,
) -> rmux_proto::SessionName {
    handler
        .active_attach
        .lock()
        .await
        .by_pid
        .get(&requester_pid)
        .expect("delegated attach remains active")
        .session_name
        .clone()
}

async fn active_key_table(handler: &RequestHandler, requester_pid: u32) -> Option<String> {
    handler
        .active_attach
        .lock()
        .await
        .by_pid
        .get(&requester_pid)
        .expect("delegated attach remains active")
        .key_table_name
        .clone()
}

async fn recv_detach(control_rx: &mut mpsc::UnboundedReceiver<AttachControl>) {
    tokio::time::timeout(Duration::from_secs(5), async {
        while let Some(control) = control_rx.recv().await {
            if matches!(control, AttachControl::Detach) {
                return;
            }
        }
        panic!("attach control channel closed before detach");
    })
    .await
    .expect("delegated detach arrives");
}

fn assert_no_detach(control_rx: &mut mpsc::UnboundedReceiver<AttachControl>) {
    while let Ok(control) = control_rx.try_recv() {
        assert!(
            !matches!(control, AttachControl::Detach),
            "unauthorized input must not detach the client"
        );
    }
}

fn assert_no_detach_variant(control_rx: &mut mpsc::UnboundedReceiver<AttachControl>) {
    while let Ok(control) = control_rx.try_recv() {
        assert!(
            !matches!(
                control,
                AttachControl::Detach
                    | AttachControl::DetachKill
                    | AttachControl::DetachExecShellCommand(_)
            ),
            "unauthorized input must not terminate or replace a client"
        );
    }
}

#[tokio::test]
async fn delegated_read_only_attach_can_switch_its_own_session_from_prefix_binding() {
    let handler = RequestHandler::new();
    let alpha = session_name("delegated-read-only-switch-prefix-alpha");
    let beta = session_name("delegated-read-only-switch-prefix-beta");
    let requester_pid = std::process::id();
    let mut control_rx = register_delegated_attach(&handler, requester_pid, &alpha).await;
    create_send_keys_test_session(&handler, &beta).await;
    {
        let mut state = handler.state.lock().await;
        state.environment.set(
            ScopeSelector::Session(beta.clone()),
            "DISPLAY".to_owned(),
            "read-only-target-sentinel".to_owned(),
        );
    }
    bind_read_only_key(
        &handler,
        "prefix",
        "s",
        &["switch-client", "-t", beta.as_str()],
    )
    .await;

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x02s")
        .await
        .expect("read-only prefix switch input");

    assert_eq!(active_session_name(&handler, requester_pid).await, beta);
    assert_eq!(
        handler
            .state
            .lock()
            .await
            .environment
            .session_value(&beta, "DISPLAY"),
        Some("read-only-target-sentinel"),
        "read-only navigation must force the no-environment-update form"
    );
    assert_no_detach_variant(&mut control_rx);
}

#[tokio::test]
async fn delegated_read_only_attach_can_switch_its_own_session_from_root_binding() {
    let handler = RequestHandler::new();
    let alpha = session_name("delegated-read-only-switch-root-alpha");
    let beta = session_name("delegated-read-only-switch-root-beta");
    let requester_pid = std::process::id();
    let mut control_rx = register_delegated_attach(&handler, requester_pid, &alpha).await;
    create_send_keys_test_session(&handler, &beta).await;
    bind_read_only_key(
        &handler,
        "root",
        "s",
        &["switch-client", "-E", "-t", beta.as_str()],
    )
    .await;

    handler
        .handle_attached_live_input_for_test(requester_pid, b"s")
        .await
        .expect("read-only root switch input");

    assert_eq!(active_session_name(&handler, requester_pid).await, beta);
    assert_no_detach_variant(&mut control_rx);
}

#[tokio::test]
async fn delegated_read_only_attach_can_enter_and_use_a_custom_safe_table() {
    let handler = RequestHandler::new();
    let alpha = session_name("delegated-read-only-switch-table-alpha");
    let beta = session_name("delegated-read-only-switch-table-beta");
    let requester_pid = std::process::id();
    let mut control_rx = register_delegated_attach(&handler, requester_pid, &alpha).await;
    create_send_keys_test_session(&handler, &beta).await;
    bind_read_only_key(
        &handler,
        "root",
        "q",
        &["switch-client", "-T", "readonly-custom"],
    )
    .await;
    bind_read_only_key(
        &handler,
        "readonly-custom",
        "s",
        &["switch-client", "-t", beta.as_str()],
    )
    .await;

    handler
        .handle_attached_live_input_for_test(requester_pid, b"q")
        .await
        .expect("read-only key-table switch input");
    assert_eq!(
        active_key_table(&handler, requester_pid).await.as_deref(),
        Some("readonly-custom")
    );
    assert_eq!(active_session_name(&handler, requester_pid).await, alpha);

    handler
        .handle_attached_live_input_for_test(requester_pid, b"s")
        .await
        .expect("read-only custom-table navigation input");
    assert_eq!(active_session_name(&handler, requester_pid).await, beta);
    assert_eq!(active_key_table(&handler, requester_pid).await, None);
    assert_no_detach_variant(&mut control_rx);
}

#[tokio::test]
async fn delegated_read_only_attach_allows_safe_session_order_navigation_repeatedly() {
    for (label, command) in [
        ("last", vec!["switch-client", "-l"]),
        ("next", vec!["switch-client", "-n"]),
        ("previous", vec!["switch-client", "-p"]),
        ("next-order", vec!["switch-client", "-En", "-O", "name"]),
    ] {
        let handler = RequestHandler::new();
        let alpha = session_name(&format!("delegated-read-only-{label}-alpha"));
        let beta = session_name(&format!("delegated-read-only-{label}-beta"));
        let requester_pid = std::process::id();
        let mut control_rx = register_delegated_attach(&handler, requester_pid, &alpha).await;
        let forwarder_identity = handler.active_attach_identity_for_test(requester_pid).await;
        let mut pending_input = Vec::new();
        create_send_keys_test_session(&handler, &beta).await;
        if label == "last" {
            let beta_id = handler
                .state
                .lock()
                .await
                .sessions
                .session(&beta)
                .expect("beta exists")
                .id();
            let mut active_attach = handler.active_attach.lock().await;
            let active = active_attach
                .by_pid
                .get_mut(&requester_pid)
                .expect("delegated attach remains active");
            active.last_session = Some(beta.clone());
            active.last_session_id = Some(beta_id);
        }
        bind_read_only_key(&handler, "root", "h", &command).await;

        handler
            .handle_attached_live_input_inner_for_identity(
                forwarder_identity,
                &mut pending_input,
                b"h",
            )
            .await
            .expect("first read-only ordered switch");
        assert_eq!(
            active_session_name(&handler, requester_pid).await,
            beta,
            "{label} first application"
        );
        handler
            .handle_attached_live_input_inner_for_identity(
                forwarder_identity,
                &mut pending_input,
                b"h",
            )
            .await
            .expect("repeated read-only ordered switch");
        assert_eq!(
            active_session_name(&handler, requester_pid).await,
            alpha,
            "{label} repeated application"
        );
        assert_no_detach_variant(&mut control_rx);
    }
}

#[tokio::test]
async fn delegated_read_only_attach_safe_noop_and_invalid_target_stay_local() {
    let handler = RequestHandler::new();
    let alpha = session_name("delegated-read-only-switch-noop");
    let requester_pid = std::process::id();
    let mut control_rx = register_delegated_attach(&handler, requester_pid, &alpha).await;
    bind_read_only_key(
        &handler,
        "root",
        "n",
        &["switch-client", "-t", alpha.as_str()],
    )
    .await;
    bind_read_only_key(
        &handler,
        "root",
        "i",
        &["switch-client", "-t", "does-not-exist"],
    )
    .await;

    handler
        .handle_attached_live_input_for_test(requester_pid, b"n")
        .await
        .expect("read-only no-op switch");
    assert_eq!(active_session_name(&handler, requester_pid).await, alpha);
    handler
        .handle_attached_live_input_for_test(requester_pid, b"i")
        .await
        .expect("read-only invalid-target switch");
    assert_eq!(active_session_name(&handler, requester_pid).await, alpha);
    assert!(
        handler
            .state
            .lock()
            .await
            .message_log
            .back()
            .is_some_and(|entry| entry.msg.contains("does-not-exist")),
        "an admitted invalid target must report its local command error"
    );
    assert_no_detach_variant(&mut control_rx);
}

#[tokio::test]
async fn delegated_read_only_attach_can_detach_with_split_prefix_binding() {
    let handler = RequestHandler::new();
    let alpha = session_name("delegated-read-only-detach");
    let requester_pid = std::process::id();
    let mut control_rx = register_delegated_attach(&handler, requester_pid, &alpha).await;

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x02")
        .await
        .expect("read-only prefix input");
    assert_no_detach(&mut control_rx);
    handler
        .handle_attached_live_input_for_test(requester_pid, b"d")
        .await
        .expect("read-only detach input");

    recv_detach(&mut control_rx).await;
}

#[tokio::test]
async fn delegated_read_only_attach_still_rejects_content_and_mutating_bindings() {
    let handler = RequestHandler::new();
    let alpha = session_name("delegated-read-only-denied");
    let requester_pid = std::process::id();
    let mut control_rx = register_delegated_attach(&handler, requester_pid, &alpha).await;
    let capture =
        RawPaneInputProbe::start(&handler, &alpha, "delegated-read-only-content", 0).await;

    handler
        .handle_attached_live_input_for_test(requester_pid, b"content")
        .await
        .expect("read-only content input");
    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x02")
        .await
        .expect("read-only prefix input");
    handler
        .handle_attached_live_input_for_test(requester_pid, b"c")
        .await
        .expect("read-only new-window binding");

    capture.finish(&handler, &alpha).await;
    capture.assert_contents(&handler, b"").await;
    assert_eq!(
        handler
            .state
            .lock()
            .await
            .sessions
            .session(&alpha)
            .expect("session remains")
            .windows()
            .len(),
        1,
        "read-only prefix c must not create a window"
    );
    assert_no_detach(&mut control_rx);
}

// tmux 3.7b executes both commands in this binding for an `attach-session -r`
// client. RMUX delegated access deliberately grants only the local detach
// capability, never the mutation chained after it.
#[tokio::test]
async fn delegated_read_only_attach_rejects_chained_detach_binding_product_divergence() {
    let handler = RequestHandler::new();
    let alpha = session_name("delegated-read-only-chained");
    let requester_pid = std::process::id();
    let mut control_rx = register_delegated_attach(&handler, requester_pid, &alpha).await;
    let rebound = handler
        .handle(Request::BindKey(Box::new(BindKeyRequest {
            table_name: "prefix".to_owned(),
            key: "d".to_owned(),
            note: Some("delegated detach must remain local".to_owned()),
            repeat: false,
            command: Some(vec![
                "detach-client".to_owned(),
                ";".to_owned(),
                "new-window".to_owned(),
            ]),
        })))
        .await;
    assert!(matches!(rebound, Response::BindKey(_)), "{rebound:?}");

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x02d")
        .await
        .expect("chained read-only binding input");

    assert_eq!(
        handler
            .state
            .lock()
            .await
            .sessions
            .session(&alpha)
            .expect("session remains")
            .windows()
            .len(),
        1,
        "the chained new-window command must not execute"
    );
    assert_no_detach(&mut control_rx);
}

#[tokio::test]
async fn delegated_read_only_attach_does_not_interpret_detach_keys_inside_fragmented_paste() {
    let handler = RequestHandler::new();
    let alpha = session_name("delegated-read-only-paste");
    let requester_pid = std::process::id();
    let mut control_rx = register_delegated_attach(&handler, requester_pid, &alpha).await;
    let mut pending_input = Vec::new();

    for chunk in [
        b"\x1b[20".as_slice(),
        b"0~\x02".as_slice(),
        b"d\x1b[201~".as_slice(),
    ] {
        handler
            .handle_attached_live_input(requester_pid, &mut pending_input, chunk)
            .await
            .expect("read-only fragmented paste input");
    }

    assert!(pending_input.is_empty(), "complete paste must be consumed");
    assert_no_detach(&mut control_rx);

    handler
        .handle_attached_live_input_for_test(requester_pid, b"\x02d")
        .await
        .expect("explicit detach after paste");
    recv_detach(&mut control_rx).await;
}