podbox-protocol 0.9.9

Length-prefixed socket protocol and message types shared between the podbox host CLI and its guest daemon.
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
mod capabilities;
mod framing;
mod messages;

pub use capabilities::{
    ALL_CAPABILITIES, CAP_CLIPBOARD, CAP_HOST_EXEC, CAP_HOST_EXEC_FDS, CAP_HOST_EXEC_STREAM,
    CAP_NOTIFY, CAP_XDG_OPEN, INTERCEPTORS, TERMINAL_ENV_KEYS, is_host_exec_env,
};
pub use framing::{FrameRead, FrameReader, read_frame, write_frame};
pub use messages::{ExecStream, GuestMessage, HostMessage, NotifyAction};

/// Increment on breaking wire-format changes. Backwards-compatible
/// additions (new optional message types) do NOT increment this.
pub const PROTOCOL_VERSION: u32 = 1;

/// Largest clipboard payload either side will send or accept (8 MiB).
/// Beyond this the host answers with an `Error` frame and the guest
/// refuses locally with a clear message, instead of failing the frame.
/// A fixed constant (not a config key): raising it needs the matching
/// host/guest rollout, and the frame cap bounds it anyway.
pub const MAX_CLIPBOARD_BYTES: usize = 8 * 1024 * 1024;

/// Unix-only helpers shared by the host CLI and the guest daemon.
#[cfg(unix)]
pub mod sysutil;

#[cfg(test)]
mod tests {
    use super::*;
    use serde::Deserialize;
    use std::io::{self, Read};

    #[test]
    fn hello_serializes_with_type_tag() {
        let msg = GuestMessage::Hello {
            protocol_version: 1,
            guest_version: "0.2.0".into(),
            container: "myenv".into(),
            capabilities: vec!["notify".into()],
        };
        let json = serde_json::to_string(&msg).unwrap();
        assert!(json.contains("\"type\":\"hello\""));
    }

    #[test]
    fn frame_length_prefix_matches_payload() {
        let msg = GuestMessage::ClipboardGet;
        let mut buf = Vec::new();
        write_frame(&mut buf, &msg).unwrap();
        let len = u32::from_be_bytes(buf[..4].try_into().unwrap()) as usize;
        assert_eq!(len, buf[4..].len());
    }

    #[test]
    fn interceptors_cover_known_binaries_and_capabilities() {
        use std::collections::HashSet;
        let binaries: HashSet<&str> = INTERCEPTORS.iter().map(|(bin, _)| *bin).collect();
        assert_eq!(binaries.len(), INTERCEPTORS.len(), "duplicate binary");
        for (_, cap) in INTERCEPTORS {
            assert!(ALL_CAPABILITIES.contains(cap), "unknown capability {cap}");
        }
    }

    #[test]
    fn roundtrip_notify_message() {
        let msg = GuestMessage::Notify {
            summary: "hello".into(),
            body: "world".into(),
            urgency: "normal".into(),
            actions: vec![],
            app_name: String::new(),
        };
        let mut buf = Vec::new();
        write_frame(&mut buf, &msg).unwrap();

        let payload = read_frame(&mut &buf[..]).unwrap().unwrap();
        let decoded: GuestMessage = serde_json::from_slice(&payload).unwrap();
        match decoded {
            GuestMessage::Notify {
                summary,
                body,
                urgency,
                actions,
                app_name: _,
            } => {
                assert_eq!(summary, "hello");
                assert_eq!(body, "world");
                assert_eq!(urgency, "normal");
                assert!(actions.is_empty());
            }
            _ => panic!("wrong message type"),
        }
    }

    #[test]
    fn roundtrip_clipboard_set() {
        let msg = GuestMessage::ClipboardSet {
            text: "clipboard content".into(),
        };
        let mut buf = Vec::new();
        write_frame(&mut buf, &msg).unwrap();

        let payload = read_frame(&mut &buf[..]).unwrap().unwrap();
        let decoded: GuestMessage = serde_json::from_slice(&payload).unwrap();
        match decoded {
            GuestMessage::ClipboardSet { text } => {
                assert_eq!(text, "clipboard content");
            }
            _ => panic!("wrong message type"),
        }
    }

    #[test]
    fn hello_ack_with_shims_round_trips() {
        let msg = HostMessage::HelloAck {
            accepted: vec!["host_exec".into()],
            rejected: vec![],
            idle_timeout_secs: 10,
            host_exec_shims: vec!["git".into(), "code".into()],
            host_version: "0.9.5".into(),
        };
        let mut buf = Vec::new();
        write_frame(&mut buf, &msg).unwrap();
        let payload = read_frame(&mut &buf[..]).unwrap().unwrap();
        let decoded: HostMessage = serde_json::from_slice(&payload).unwrap();
        match decoded {
            HostMessage::HelloAck {
                accepted,
                host_exec_shims,
                host_version,
                ..
            } => {
                assert_eq!(accepted, vec!["host_exec"]);
                assert_eq!(host_exec_shims, vec!["git", "code"]);
                assert_eq!(host_version, "0.9.5");
            }
            _ => panic!("wrong message type"),
        }
    }

    #[test]
    fn hello_ack_backward_compat_missing_shims() {
        let json = r#"{"type":"hello_ack","accepted":[],"rejected":[],"idle_timeout_secs":0}"#;
        let msg: HostMessage = serde_json::from_slice(json.as_bytes()).unwrap();
        match msg {
            HostMessage::HelloAck {
                host_exec_shims,
                host_version,
                ..
            } => {
                assert!(host_exec_shims.is_empty());
                assert!(host_version.is_empty());
            }
            _ => panic!("wrong message type"),
        }
    }

    /// One scripted step of a fake socket: bytes, or a read timeout.
    enum Step {
        Bytes(Vec<u8>),
        Timeout,
    }

    struct Scripted(std::collections::VecDeque<Step>);

    impl Read for Scripted {
        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
            match self.0.pop_front() {
                None => Ok(0),
                Some(Step::Timeout) => Err(io::Error::from(io::ErrorKind::WouldBlock)),
                Some(Step::Bytes(mut b)) => {
                    let n = b.len().min(buf.len());
                    buf[..n].copy_from_slice(&b[..n]);
                    if n < b.len() {
                        b.drain(..n);
                        self.0.push_front(Step::Bytes(b));
                    }
                    Ok(n)
                }
            }
        }
    }

    fn encoded(msg: &str) -> Vec<u8> {
        let mut buf = Vec::new();
        write_frame(&mut buf, &msg).unwrap();
        buf
    }

    /// A timeout after the first two length bytes must not lose them: the
    /// frame still comes out whole, with no misparse of the payload.
    #[test]
    fn frame_survives_a_timeout_in_the_length_prefix() {
        let bytes = encoded("hello there");
        let mut r = Scripted(
            [
                Step::Bytes(bytes[..2].to_vec()),
                Step::Timeout,
                Step::Bytes(bytes[2..].to_vec()),
            ]
            .into(),
        );
        let mut fr = FrameReader::new();
        assert!(fr.read(&mut r).is_err());
        assert!(fr.in_progress());
        let got = fr.read(&mut r).unwrap();
        assert_eq!(
            got,
            FrameRead::Frame(serde_json::to_vec("hello there").unwrap())
        );
        assert!(!fr.in_progress());
    }

    #[test]
    fn frame_survives_a_timeout_in_the_payload() {
        let bytes = encoded("a longer payload to split");
        let cut = 4 + 7;
        let mut r = Scripted(
            [
                Step::Bytes(bytes[..cut].to_vec()),
                Step::Timeout,
                Step::Bytes(bytes[cut..].to_vec()),
            ]
            .into(),
        );
        let mut fr = FrameReader::new();
        assert!(fr.read(&mut r).is_err());
        assert_eq!(
            fr.read(&mut r).unwrap(),
            FrameRead::Frame(serde_json::to_vec("a longer payload to split").unwrap())
        );
    }

    #[test]
    fn clean_eof_between_frames_is_eof_not_an_error() {
        let mut r = Scripted(std::collections::VecDeque::new());
        assert_eq!(FrameReader::new().read(&mut r).unwrap(), FrameRead::Eof);
    }

    #[test]
    fn eof_inside_a_frame_is_an_error() {
        let bytes = encoded("cut short");
        let mut r = Scripted([Step::Bytes(bytes[..6].to_vec())].into());
        let err = FrameReader::new().read(&mut r).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::UnexpectedEof);
    }

    #[test]
    fn host_exec_output_chunk_round_trips_raw_bytes() {
        // Invalid UTF-8 must survive the frame exactly: chunks carry bytes,
        // not lossy strings.
        let msg = HostMessage::HostExecOutput {
            stream: ExecStream::Stdout,
            data: vec![0xff, 0xfe, b'A'],
        };
        let mut buf = Vec::new();
        write_frame(&mut buf, &msg).unwrap();
        let payload = read_frame(&mut &buf[..]).unwrap().unwrap();
        let decoded: HostMessage = serde_json::from_slice(&payload).unwrap();
        match decoded {
            HostMessage::HostExecOutput { stream, data } => {
                assert_eq!(stream, ExecStream::Stdout);
                assert_eq!(data, vec![0xff, 0xfe, b'A']);
            }
            _ => panic!("wrong message type"),
        }
    }

    #[test]
    fn host_exec_v2_optional_fields_are_serde_defaulted() {
        // A minimal V2 frame (old guest shims that only know cmd/args,
        // mostly future guests) must decode with every new field defaulted:
        // the wire format is frozen without bumping PROTOCOL_VERSION.
        let json = br#"{"type":"host_exec_v2","cmd":"cat","args":["-s"]}"#;
        let msg: GuestMessage = serde_json::from_slice(json).unwrap();
        match msg {
            GuestMessage::HostExecV2 {
                cmd,
                args,
                env,
                cwd,
                tty,
            } => {
                assert_eq!(cmd, "cat");
                assert_eq!(args, vec!["-s"]);
                assert!(env.is_empty());
                assert!(cwd.is_none());
                assert!(!tty);
            }
            other => panic!("expected HostExecV2, got {other:?}"),
        }
    }

    #[test]
    fn host_exec_v2_round_trips_the_full_field_set() {
        let msg = GuestMessage::HostExecV2 {
            cmd: "git".into(),
            args: vec!["log".into()],
            env: vec![("TERM".into(), "xterm-256color".into())],
            cwd: Some("/home/user/project".into()),
            tty: true,
        };
        let mut buf = Vec::new();
        write_frame(&mut buf, &msg).unwrap();
        let payload = read_frame(&mut &buf[..]).unwrap().unwrap();
        let decoded: GuestMessage = serde_json::from_slice(&payload).unwrap();
        match decoded {
            GuestMessage::HostExecV2 {
                cmd,
                args,
                env,
                cwd,
                tty,
            } => {
                assert_eq!(cmd, "git");
                assert_eq!(args, vec!["log"]);
                assert_eq!(
                    env,
                    vec![("TERM".to_string(), "xterm-256color".to_string())]
                );
                assert_eq!(cwd.as_deref(), Some("/home/user/project"));
                assert!(tty);
            }
            other => panic!("expected HostExecV2, got {other:?}"),
        }
    }

    #[test]
    fn terminal_env_allowlist_carries_identity_but_not_host_sockets() {
        for key in [
            "TERM",
            "COLORTERM",
            "TERM_PROGRAM",
            "TERM_PROGRAM_VERSION",
            "VTE_VERSION",
            "KITTY_WINDOW_ID",
            "LC_TERMINAL",
        ] {
            assert!(TERMINAL_ENV_KEYS.contains(&key), "{key} must be forwarded");
            assert!(is_host_exec_env(key), "{key} must reach host-exec");
        }
        assert!(is_host_exec_env("LANG"));
        for key in [
            "PATH",
            "LD_PRELOAD",
            "KITTY_LISTEN_ON",
            "WEZTERM_UNIX_SOCKET",
            "TMUX",
            "STY",
        ] {
            assert!(!is_host_exec_env(key), "{key} must stay inside");
            assert!(
                !TERMINAL_ENV_KEYS.contains(&key),
                "{key} is not terminal identity"
            );
        }
    }

    #[test]
    fn host_exec_signal_round_trips() {
        let msg = GuestMessage::HostExecSignal { sig: 2 };
        let mut buf = Vec::new();
        write_frame(&mut buf, &msg).unwrap();
        let payload = read_frame(&mut &buf[..]).unwrap().unwrap();
        let decoded: GuestMessage = serde_json::from_slice(&payload).unwrap();
        match decoded {
            GuestMessage::HostExecSignal { sig } => assert_eq!(sig, 2),
            other => panic!("expected HostExecSignal, got {other:?}"),
        }
    }

    /// Wire compatibility in the other direction: a host built before
    /// `host_exec_fds` can still parse a `Hello` that requests it — the
    /// unknown capability lands in `capabilities` and negotiation rejects
    /// it, which is how old hosts degrade the guest to plain `HostExec`.
    #[test]
    fn hello_with_an_unknown_capability_parses_on_the_old_schema() {
        // Serde deserializes every declared field, so the enum's own test
        // reads are the negotiate path, not these bindings.
        #[allow(dead_code)]
        #[derive(Deserialize)]
        #[serde(tag = "type", rename_all = "snake_case")]
        enum OldGuestMessage {
            Hello {
                protocol_version: u32,
                guest_version: String,
                container: String,
                capabilities: Vec<String>,
            },
        }

        let json = r#"{"type":"hello","protocol_version":1,"guest_version":"0.2.0",
            "container":"dev","capabilities":["host_exec","host_exec_fds"]}"#;
        let msg: OldGuestMessage = serde_json::from_slice(json.as_bytes()).unwrap();
        let OldGuestMessage::Hello { capabilities, .. } = msg;
        assert_eq!(capabilities, vec!["host_exec", "host_exec_fds"]);
    }

    #[test]
    fn host_exec_done_carries_an_optional_signal() {
        let msg = HostMessage::HostExecDone {
            exit_code: 143,
            signal: Some(15),
        };
        let mut buf = Vec::new();
        write_frame(&mut buf, &msg).unwrap();
        let payload = read_frame(&mut &buf[..]).unwrap().unwrap();
        let decoded: HostMessage = serde_json::from_slice(&payload).unwrap();
        match decoded {
            HostMessage::HostExecDone { exit_code, signal } => {
                assert_eq!(exit_code, 143);
                assert_eq!(signal, Some(15));
            }
            _ => panic!("wrong message type"),
        }
    }

    /// Wire compatibility: a guest built before `signal` existed must
    /// still parse the new `HostExecDone`. Serde ignores the unknown
    /// field, so old guests keep working with no version bump.
    #[test]
    fn host_exec_done_with_signal_parses_under_the_old_schema() {
        #[derive(Debug, Deserialize, PartialEq, Eq)]
        #[serde(tag = "type", rename_all = "snake_case")]
        enum OldHostMessage {
            HostExecDone { exit_code: i32 },
        }

        let json = r#"{"type":"host_exec_done","exit_code":143,"signal":15}"#;
        let msg: OldHostMessage = serde_json::from_slice(json.as_bytes()).unwrap();
        assert_eq!(msg, OldHostMessage::HostExecDone { exit_code: 143 });
    }
}