mxc-sdk 1.2.0

Importable library for starting MXC sandboxes in-process, with streaming stdio.
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Process-creation options for the IsolationSession backend: MXC-internal
//! `ProcessOptions` built from an `ExecutionRequest`, then translated to the
//! WinRT `IsoSessionProcessOptions` consumed by `RunProcessWithOptionsAsync`.

use crate::mxc_common::models::ExecutionRequest;

use crate::isolation_session_bindings::bindings::IsoSessionProcessOptions;
use windows_core::HSTRING;

use super::error::{op, transport_err, IsolationSessionError};

const REDIRECT_STDIN: u32 = 0x1;
const REDIRECT_STDOUT: u32 = 0x2;
const REDIRECT_STDERR: u32 = 0x4;

/// Canonical redirect-flags bitfield for the agent process I/O.
///
/// Stdin and stdout are always redirected. Stderr is redirected ONLY in
/// non-interactive mode: in ConPTY mode the OS API merges stderr into
/// stdout and does not populate the stderr handle, so asking for it would
/// produce a handle of 0.
fn compute_redirect_flags(interactive: bool) -> u32 {
    let mut flags = REDIRECT_STDIN | REDIRECT_STDOUT;
    if !interactive {
        flags |= REDIRECT_STDERR;
    }
    flags
}

/// Process creation options decoupled from `ExecutionRequest` and from the
/// WinRT types — small struct so the builder is unit-testable without a
/// live `IsoSessionOps` activation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct ProcessOptions {
    pub process_path: String,
    pub arguments: String,
    /// Execution timeout in milliseconds. 0 = no timeout.
    pub timeout_ms: u32,
    /// Empty = default working directory.
    pub working_directory: String,
    pub env_vars: Vec<(String, String)>,
    pub redirect_flags: u32,
    /// `true` asks the OS API to set up a ConPTY in the isolation session.
    /// Caller (the runner) sets this from `std::io::stdout().is_terminal()`.
    pub interactive: bool,
}

/// Builds `ProcessOptions` from an `ExecutionRequest`. `interactive` flips the
/// backend into ConPTY mode (`InteractiveConsole = true`) and adjusts
/// `redirect_flags` accordingly (no separate stderr stream in ConPTY mode).
///
/// The command line is wrapped with `cmd.exe /c` so shell features (pipes,
/// redirections, chained commands) work — same pattern as the LXC backend's
/// `/bin/sh -c`.
pub(super) fn build_process_options(
    request: &ExecutionRequest,
    interactive: bool,
) -> ProcessOptions {
    let env_vars: Vec<(String, String)> = request
        .env_entries()
        .iter()
        .filter_map(|entry| {
            let (name, value) = entry.split_once('=')?;
            if name.is_empty() {
                None
            } else {
                Some((name.to_string(), value.to_string()))
            }
        })
        .collect();

    // Resolve cmd.exe off `%SystemDrive%` rather than hardcoding `C:`.
    // Fallback `C:` covers the unlikely case of an absent env var.
    let system_drive = std::env::var("SystemDrive").unwrap_or_else(|_| "C:".to_string());
    let process_path = format!(r"{}\Windows\System32\cmd.exe", system_drive);

    let arguments = format!("/c {}", request.script_code);

    ProcessOptions {
        process_path,
        arguments,
        timeout_ms: request.script_timeout,
        working_directory: request.working_directory.clone(),
        env_vars,
        redirect_flags: compute_redirect_flags(interactive),
        interactive,
    }
}

/// Builds caller-controlled PTY options whose command remains blocked until
/// MXC applies the requested initial terminal dimensions.
pub(super) fn build_pty_process_options(request: &ExecutionRequest) -> ProcessOptions {
    let mut options = build_process_options(request, true);
    // Hold the caller's command behind one line of stdin. The PTY spawn path
    // resizes the service-owned ConPTY, then writes that line before returning
    // the handle, so user code cannot observe the default dimensions or run
    // after a failed initial resize. `ver` restores a successful ERRORLEVEL
    // after the intentionally blank `set /p` input without producing output.
    options.arguments = format!(
        r#"/d /q /c set /p "__mxc_pty_start_gate=" & ver >nul & {}"#,
        request.script_code
    );
    options
}

/// How much longer than the caller's deadline the **service-side** timer is
/// armed for on the streaming path.
///
/// The service arms its timer when the process is created and kills the process
/// with an ordinary exit code (the host suite pins this as exit code 1). Our own
/// `WaitForExit` starts later and, with an equal duration, therefore always
/// loses the race — so a genuine timeout arrives looking exactly like a normal
/// exit and cannot be reported as one.
///
/// Giving the service timer a margin lets the local deadline fire first, so a
/// timeout is observed as the wait sentinel plus a still-running process. The
/// service timer stays armed as a watchdog for the case this process dies
/// before it can enforce anything.
///
/// **This is a margin, not a guarantee.** It orders the two timers only while
/// the gap between arming the service timer and entering our own wait — a few
/// COM calls and a thread spawn — stays under the margin; a host stall longer
/// than that could still let the service win. No timeout *signal* is available
/// to key off instead: the process interface exposes lifetime and console
/// members (`ExitCode`, `WaitForExit`, `Terminate`, `CloseStandardInput`,
/// `SendCtrlClose`, the stdio handles, `ResizeConsole`) and nothing that
/// distinguishes a service-enforced kill from an ordinary exit. Five seconds is
/// chosen to dwarf that startup gap rather than to bound the OS.
pub(super) const SERVICE_TIMEOUT_GRACE_MS: u32 = 5_000;

/// Relaxes the service-side timer so the caller's deadline is enforced locally.
///
/// Without the margin the two deadlines coincide, and the service's kill (an
/// ordinary exit code 1) could win the race and hide the timeout.
///
/// Two deadlines are left alone rather than shifted:
///
/// - **Zero** means INFINITE. There is no deadline to move behind, and adding a
///   grace would invent one the caller never asked for.
/// - **A deadline too large to move** (within `SERVICE_TIMEOUT_GRACE_MS` of
///   `u32::MAX`) disarms the service timer instead. Saturating would silently
///   shrink the margin to nothing and re-equalize the timers, restoring the
///   exact misclassification this exists to prevent. At that magnitude — over
///   forty-nine days — a watchdog is meaningless anyway, so the local deadline
///   becomes the only one.
pub(super) fn with_service_timeout_grace(mut options: ProcessOptions) -> ProcessOptions {
    const NO_SERVICE_TIMEOUT: u32 = 0;
    if options.timeout_ms == 0 {
        return options;
    }
    options.timeout_ms = match options.timeout_ms.checked_add(SERVICE_TIMEOUT_GRACE_MS) {
        Some(armed) => armed,
        None => NO_SERVICE_TIMEOUT,
    };
    options
}

/// Translates the MXC-internal `ProcessOptions` into a fresh
/// `IsoSessionProcessOptions` ready for `RunProcessWithOptionsAsync`.
pub(super) fn build_iso_process_options(
    options: &ProcessOptions,
) -> Result<IsoSessionProcessOptions, IsolationSessionError> {
    let proc_options = IsoSessionProcessOptions::new()
        .map_err(|e| transport_err(op::OPTIONS_NEW, "activation failed", &e))?;

    proc_options
        .SetTimeoutMilliseconds(options.timeout_ms)
        .map_err(|e| transport_err(op::OPTIONS_TIMEOUT, "set failed", &e))?;

    if !options.working_directory.is_empty() {
        proc_options
            .SetWorkingDirectory(&HSTRING::from(&options.working_directory))
            .map_err(|e| transport_err(op::OPTIONS_WORKING_DIR, "set failed", &e))?;
    }

    proc_options
        .SetInteractiveConsole(options.interactive)
        .map_err(|e| transport_err(op::OPTIONS_INTERACTIVE, "set failed", &e))?;

    proc_options
        .SetRedirectStandardInput(options.redirect_flags & REDIRECT_STDIN != 0)
        .map_err(|e| transport_err(op::OPTIONS_REDIRECT_STDIN, "set failed", &e))?;
    proc_options
        .SetRedirectStandardOutput(options.redirect_flags & REDIRECT_STDOUT != 0)
        .map_err(|e| transport_err(op::OPTIONS_REDIRECT_STDOUT, "set failed", &e))?;
    proc_options
        .SetRedirectStandardError(options.redirect_flags & REDIRECT_STDERR != 0)
        .map_err(|e| transport_err(op::OPTIONS_REDIRECT_STDERR, "set failed", &e))?;

    if !options.env_vars.is_empty() {
        let env = proc_options
            .Environment()
            .map_err(|e| transport_err(op::OPTIONS_ENVIRONMENT, "get failed", &e))?;
        for (name, value) in &options.env_vars {
            // The variable name rides in the message, never in `operation` —
            // that field stays low-cardinality for telemetry grouping.
            env.Insert(&HSTRING::from(name), &HSTRING::from(value))
                .map_err(|e| {
                    transport_err(
                        op::OPTIONS_ENVIRONMENT,
                        &format!("insert {name} failed"),
                        &e,
                    )
                })?;
        }
    }

    Ok(proc_options)
}

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

    #[test]
    fn options_wraps_command_with_cmd_exe() {
        let request = ExecutionRequest {
            script_code: "echo hello".to_string(),
            ..Default::default()
        };
        let opts = build_process_options(&request, false);
        // Host-relative — drive comes from %SYSTEMDRIVE% (typically `C:`),
        // so assert the trailing path shape rather than the full literal.
        assert!(
            opts.process_path.ends_with(r"\Windows\System32\cmd.exe"),
            "unexpected process_path: {}",
            opts.process_path
        );
        assert_eq!(opts.arguments, "/c echo hello");
    }

    #[test]
    fn interactive_options_do_not_gate_existing_console_paths() {
        let request = ExecutionRequest {
            script_code: "echo hello".to_string(),
            ..Default::default()
        };
        let opts = build_process_options(&request, true);

        assert_eq!(opts.arguments, "/c echo hello");
    }

    #[test]
    fn pty_options_gate_the_command_until_the_initial_resize() {
        let request = ExecutionRequest {
            script_code: "echo hello".to_string(),
            ..Default::default()
        };
        let opts = build_pty_process_options(&request);

        assert_eq!(
            opts.arguments,
            r#"/d /q /c set /p "__mxc_pty_start_gate=" & ver >nul & echo hello"#
        );
    }

    #[test]
    fn options_maps_timeout() {
        let request = ExecutionRequest {
            script_code: "echo hi".to_string(),
            script_timeout: 30000,
            ..Default::default()
        };
        let opts = build_process_options(&request, false);
        assert_eq!(opts.timeout_ms, 30000);
    }

    /// The service-side timer must not be able to fire before the caller's own
    /// deadline on the streaming path.
    ///
    /// The regression this pins: both timers were armed with the same duration,
    /// but the service arms its own at process creation while our wait starts
    /// later, so the service always won the race. A genuine timeout then
    /// arrived looking like an ordinary exit — the host suite pins the
    /// OS-side timeout as exit code 1 — so it could not be reported as a
    /// timeout at all, and the streaming path's timeout reporting never fired.
    #[test]
    fn the_service_timer_is_armed_behind_the_local_deadline() {
        let request = ExecutionRequest {
            script_code: "echo hi".to_string(),
            script_timeout: 30000,
            ..Default::default()
        };
        let local = build_process_options(&request, false);
        let relaxed = with_service_timeout_grace(local);
        assert!(
            relaxed.timeout_ms > 30000,
            "the service timer must outlast the caller's deadline, got {}",
            relaxed.timeout_ms
        );
    }

    /// INFINITE stays INFINITE: there is no deadline to move behind, and
    /// giving it a grace would invent a deadline the caller never asked for.
    #[test]
    fn an_infinite_timeout_is_not_given_a_service_grace() {
        let request = ExecutionRequest {
            script_code: "echo hi".to_string(),
            script_timeout: 0,
            ..Default::default()
        };
        let relaxed = with_service_timeout_grace(build_process_options(&request, false));
        assert_eq!(relaxed.timeout_ms, 0);
    }

    /// A deadline too large to move disarms the service timer rather than
    /// silently re-equalizing the two.
    ///
    /// The regression this pins: saturating arithmetic clamps at `u32::MAX`, so
    /// a deadline within the grace of the maximum would come back with a margin
    /// shrunk to nothing — the timers equal again, and a genuine timeout once
    /// more indistinguishable from an ordinary exit. `timeout` is an
    /// unconstrained `u32` on the wire, so this is reachable input.
    #[test]
    fn a_deadline_too_large_to_move_disarms_the_service_timer() {
        for script_timeout in [
            u32::MAX,
            u32::MAX - 1,
            u32::MAX - SERVICE_TIMEOUT_GRACE_MS + 1,
        ] {
            let request = ExecutionRequest {
                script_code: "echo hi".to_string(),
                script_timeout,
                ..Default::default()
            };
            let relaxed = with_service_timeout_grace(build_process_options(&request, false));
            assert_eq!(
                relaxed.timeout_ms, 0,
                "a deadline of {script_timeout} must disarm the service timer rather than \
                 arm it equal to the caller's deadline"
            );
        }
    }

    /// The largest deadline that can still be moved keeps a full margin.
    #[test]
    fn the_largest_movable_deadline_keeps_its_full_margin() {
        let script_timeout = u32::MAX - SERVICE_TIMEOUT_GRACE_MS;
        let request = ExecutionRequest {
            script_code: "echo hi".to_string(),
            script_timeout,
            ..Default::default()
        };
        let relaxed = with_service_timeout_grace(build_process_options(&request, false));
        assert_eq!(relaxed.timeout_ms, u32::MAX);
        assert!(relaxed.timeout_ms > script_timeout);
    }

    #[test]
    fn options_maps_working_directory() {
        let request = ExecutionRequest {
            script_code: "echo hi".to_string(),
            working_directory: r"C:\Windows".to_string(),
            ..Default::default()
        };
        let opts = build_process_options(&request, false);
        assert_eq!(opts.working_directory, r"C:\Windows");
    }

    #[test]
    fn options_parses_env_vars() {
        let request = ExecutionRequest {
            script_code: "echo hi".to_string(),
            env: Some(vec![
                "FOO=bar".to_string(),
                "PATH=C:\\bin;C:\\tools".to_string(),
            ]),
            ..Default::default()
        };
        let opts = build_process_options(&request, false);
        assert_eq!(opts.env_vars.len(), 2);
        assert_eq!(opts.env_vars[0], ("FOO".to_string(), "bar".to_string()));
        assert_eq!(
            opts.env_vars[1],
            ("PATH".to_string(), r"C:\bin;C:\tools".to_string())
        );
    }

    #[test]
    fn options_skips_malformed_env_vars() {
        let request = ExecutionRequest {
            script_code: "echo hi".to_string(),
            env: Some(vec![
                "GOOD=value".to_string(),
                "TEMP".to_string(),
                "=no_name".to_string(),
                "ALSO_GOOD=".to_string(),
            ]),
            ..Default::default()
        };
        let opts = build_process_options(&request, false);
        assert_eq!(opts.env_vars.len(), 2);
        assert_eq!(opts.env_vars[0].0, "GOOD");
        assert_eq!(opts.env_vars[1], ("ALSO_GOOD".to_string(), String::new()));
    }

    #[test]
    fn options_non_interactive_redirects_all_three_streams() {
        let request = ExecutionRequest {
            script_code: "echo hi".to_string(),
            ..Default::default()
        };
        let opts = build_process_options(&request, false);
        assert!(!opts.interactive);
        assert_eq!(
            opts.redirect_flags,
            REDIRECT_STDIN | REDIRECT_STDOUT | REDIRECT_STDERR
        );
    }

    #[test]
    fn options_interactive_redirects_stdin_stdout_only() {
        let request = ExecutionRequest {
            script_code: "echo hi".to_string(),
            ..Default::default()
        };
        let opts = build_process_options(&request, true);
        assert!(opts.interactive);
        assert_eq!(opts.redirect_flags, REDIRECT_STDIN | REDIRECT_STDOUT);
        assert_eq!(
            opts.redirect_flags & REDIRECT_STDERR,
            0,
            "interactive (ConPTY) mode merges stderr into stdout"
        );
    }

    #[test]
    fn compute_redirect_flags_interactive_omits_stderr() {
        let flags = compute_redirect_flags(true);
        assert!(
            flags & REDIRECT_STDIN != 0,
            "stdin should be redirected even in interactive mode"
        );
        assert!(flags & REDIRECT_STDOUT != 0, "stdout should be redirected");
        assert!(
            flags & REDIRECT_STDERR == 0,
            "stderr should NOT be redirected in interactive (ConPTY) mode \
             — the OS API does not populate ErrorHandle"
        );
    }

    #[test]
    fn compute_redirect_flags_noninteractive_includes_stderr() {
        let flags = compute_redirect_flags(false);
        assert!(flags & REDIRECT_STDIN != 0, "stdin should be redirected");
        assert!(flags & REDIRECT_STDOUT != 0, "stdout should be redirected");
        assert!(
            flags & REDIRECT_STDERR != 0,
            "stderr should be redirected in non-interactive (plain pipes) mode"
        );
    }
}