mxc-sdk 1.0.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
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Shared `processContainer.captureDenials` JSON-output plumbing.
//!
//! Both the native BaseContainer capture path (`base_container_runner`,
//! decoding its own sealed ETL) and the guarded-WPR legacy-tier fallback
//! (`appcontainer_runner`, consuming an already-decoded [`AnalysisResult`]
//! handed back by the elevated PLM guardian) must emit byte-for-byte the same
//! [`DenialsDocument`] JSON shape, username-redacted verbose logging sibling,
//! [`CaptureDenialsOutput`] summary, and resolved output-path convention.
//! Centralizing that here is what guarantees the two paths can't drift.
//!
//! Deliberately free of any Windows API or [`ScriptResponse`] coupling so it
//! can be shared by both runner modules without either owning the other's
//! error type; callers map the plain `String` errors into their own error
//! type at the call site.

use std::path::{Path, PathBuf};

use crate::learning_mode_core::{
    verbose_logging_sibling_path, write_document, write_paired_output_files,
    write_verbose_logging_document, AnalysisResult, DenialSummary, DenialsDocument,
    DenialsOutputPointer, ExistingOutputPolicy, VerboseLoggingDocument,
};
use crate::mxc_common::models::CaptureDenialsOutput;

/// Directory name of the protected retained-ETL store. Sealed ETLs are promoted
/// out of working storage into a per-run directory beneath a sibling directory
/// with this name (`.../capture-denials/retained/<run>/`). Owned here so
/// consumers that need to recognize a promoted retained-capture directory —
/// e.g. `wxc-exec --audit` relocating and pruning the run directory — do not
/// duplicate the literal.
pub const RETAINED_CAPTURE_DIR_NAME: &str = "retained";

/// Whether `directory` is a per-run directory sitting directly beneath the
/// retained-capture store (`.../retained/<run>`). Recognizes the promoted
/// location produced by the BaseContainer capture path so callers can safely
/// prune it once its ETL has moved out, without hard-coding the store name.
pub fn is_retained_capture_run_dir(directory: &Path) -> bool {
    directory
        .parent()
        .and_then(Path::file_name)
        .is_some_and(|name| name.eq_ignore_ascii_case(RETAINED_CAPTURE_DIR_NAME))
}

#[derive(Debug, Eq, PartialEq)]
pub struct DenialsOutputPaths {
    pub denials: PathBuf,
    pub etl: Option<PathBuf>,
}

/// Writes a bounded [`AnalysisResult`] as the actionable denials document and
/// deterministic verbose logging sibling, then returns caller-facing metadata for
/// the actionable document.
///
/// Never overwrites an existing file: a run whose output path collides with a
/// leftover file from a previous run fails loudly rather than clobbering it.
pub fn write_denials_document(
    analysis: AnalysisResult,
    exit_code: i32,
    output_path: &Path,
) -> std::io::Result<CaptureDenialsOutput> {
    let verbose_logging_path = verbose_logging_output_path(output_path)?;
    let summary = DenialSummary::new(
        exit_code,
        analysis.denials.len(),
        analysis.denied_resources_truncated,
    );
    let document = DenialsDocument::new(analysis.denials, summary);
    let verbose_logging_document = VerboseLoggingDocument::new(&analysis.verbose_logging);

    write_paired_output_files(
        "captureDenials",
        output_path,
        &verbose_logging_path,
        ExistingOutputPolicy::CreateNew,
        |writer| write_document(writer, &document),
        |writer| write_verbose_logging_document(writer, &verbose_logging_document),
    )?;

    let pointer = DenialsOutputPointer::new(output_path.to_string_lossy(), &document.summary);
    Ok(CaptureDenialsOutput {
        kind: pointer.kind,
        output_path: pointer.output_path,
        exit_code: pointer.exit_code,
        total_denials: pointer.total_denials,
        denied_resources_truncated: pointer.denied_resources_truncated,
        etl_path: None,
    })
}

/// Derives the deterministic verbose logging sibling path for a denials output.
pub fn verbose_logging_output_path(output_path: &Path) -> std::io::Result<PathBuf> {
    verbose_logging_sibling_path(output_path)
        .map_err(|error| std::io::Error::other(format!("captureDenials {error}")))
}

/// Inserts a per-run identifier into a denials output path's file stem so
/// concurrent and sequential captures using the same configured `outputPath`
/// produce distinct files instead of clobbering one another.
///
/// `C:\app\denials.json` → `C:\app\denials.<run_id>.json`. A path with no
/// extension gets `<name>.<run_id>`; a bare filename (no parent) keeps its
/// directory-less form.
pub fn insert_run_id_into_stem(path: &Path, run_id: &str) -> PathBuf {
    let Some(file_name) = path.file_name().and_then(|s| s.to_str()) else {
        return path.to_path_buf();
    };
    let new_name = match path.extension().and_then(|s| s.to_str()) {
        Some(ext) => {
            let stem = path
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or(file_name);
            format!("{stem}.{run_id}.{ext}")
        }
        None => format!("{file_name}.{run_id}"),
    };
    match path.parent() {
        Some(parent) if !parent.as_os_str().is_empty() => parent.join(new_name),
        _ => PathBuf::from(new_name),
    }
}

/// Resolves the paired denials and optional retained-ETL paths for one run.
///
/// Both names contain the same run identifier. The retained path is always
/// distinct, including when the configured denials path has no extension or
/// already ends in `.etl`.
pub fn unique_denials_output_paths(
    configured_path: Option<&str>,
    retain_etl: bool,
) -> Result<DenialsOutputPaths, String> {
    let suffix = random_capture_suffix()?;
    let run_id = format!("{}_{suffix}", std::process::id());
    Ok(denials_output_paths_for_run(
        configured_path,
        &run_id,
        retain_etl,
    ))
}

fn denials_output_paths_for_run(
    configured_path: Option<&str>,
    run_id: &str,
    retain_etl: bool,
) -> DenialsOutputPaths {
    let denials = match configured_path {
        Some(path) => insert_run_id_into_stem(Path::new(path), run_id),
        None => std::env::temp_dir().join(format!("mxc_denials_{run_id}.json")),
    };
    let etl = retain_etl.then(|| match configured_path {
        Some(path) => retained_etl_path(Path::new(path), run_id),
        None => std::env::temp_dir().join(format!("mxc_denials_{run_id}.etl")),
    });
    DenialsOutputPaths { denials, etl }
}

fn retained_etl_path(configured_path: &Path, run_id: &str) -> PathBuf {
    let file_name = configured_path
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or("denials");
    let extension = configured_path.extension().and_then(|ext| ext.to_str());
    let stem = configured_path
        .file_stem()
        .and_then(|stem| stem.to_str())
        .unwrap_or(file_name);
    let retained_name = match extension {
        Some(ext) if ext.eq_ignore_ascii_case("etl") => {
            format!("{stem}.{run_id}.trace.etl")
        }
        Some(_) => format!("{stem}.{run_id}.etl"),
        None => format!("{file_name}.{run_id}.etl"),
    };
    match configured_path.parent() {
        Some(parent) if !parent.as_os_str().is_empty() => parent.join(retained_name),
        _ => PathBuf::from(retained_name),
    }
}

/// A short random hex suffix used to keep per-run temp/output paths from
/// colliding across concurrent or sequential runs sharing the same PID.
pub fn random_capture_suffix() -> Result<String, String> {
    let mut nonce = [0u8; 16];
    getrandom::getrandom(&mut nonce).map_err(|error| {
        format!("captureDenials could not generate a unique output path: {error}")
    })?;
    Ok(nonce
        .iter()
        .map(|byte| format!("{byte:02x}"))
        .collect::<String>())
}

/// Removes an internal (runner-managed) capture temp file. Treats "already
/// gone" as success so a redundant cleanup call is harmless.
pub fn remove_internal_capture_file(path: &Path) -> std::io::Result<()> {
    match std::fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(std::io::Error::other(format!(
            "captureDenials failed to remove internal capture file {}: {error}",
            path.display()
        ))),
    }
}

/// Combines a primary result with a best-effort secondary `()` result, keeping
/// the four-arm pattern in one place. On success the primary value flows
/// through; if exactly one side fails its error is returned unchanged; if both
/// fail, `combine_errors` merges them (owning both errors so callers control
/// the resulting message and [`std::io::ErrorKind`]).
fn combine_results<T>(
    primary: std::io::Result<T>,
    secondary: std::io::Result<()>,
    combine_errors: impl FnOnce(std::io::Error, std::io::Error) -> std::io::Error,
) -> std::io::Result<T> {
    match (primary, secondary) {
        (Ok(value), Ok(())) => Ok(value),
        (Err(primary_error), Ok(())) => Err(primary_error),
        (Ok(_), Err(secondary_error)) => Err(secondary_error),
        (Err(primary_error), Err(secondary_error)) => {
            Err(combine_errors(primary_error, secondary_error))
        }
    }
}

/// Combines a capture result with a best-effort cleanup result, preserving
/// both failure messages when both operations fail.
pub fn combine_capture_and_cleanup_results<T>(
    capture_result: std::io::Result<T>,
    cleanup_result: std::io::Result<()>,
) -> std::io::Result<T> {
    combine_results(
        capture_result,
        cleanup_result,
        |capture_error, cleanup_error| {
            std::io::Error::other(format!(
            "{capture_error}; additionally failed to clean up the internal capture state: {cleanup_error}"
        ))
        },
    )
}

/// Combines a sandboxed process's wait result with a best-effort
/// `captureDenials` teardown result, preferring the teardown error when both
/// are present so a capture failure is never silently swallowed by a
/// successful process exit. Shared by the native BaseContainer capture path
/// and the guarded-WPR legacy-tier fallback so both surface capture failures
/// through [`crate::mxc_common::sandbox_process::SandboxProcess::wait`] identically.
///
/// When *both* the wait and teardown fail, the wait error kind is preserved
/// while both messages are returned. This keeps retained-ETL paths and other
/// capture recovery details discoverable.
pub fn combine_process_and_teardown_results(
    process_result: std::io::Result<i32>,
    teardown_result: std::io::Result<()>,
) -> std::io::Result<i32> {
    combine_results(
        process_result,
        teardown_result,
        |wait_error, teardown_error| {
            std::io::Error::new(
                wait_error.kind(),
                format!("{wait_error}; captureDenials teardown also failed: {teardown_error}"),
            )
        },
    )
}

/// Best-effort write of a single diagnostic line to stderr, used for failures
/// that occur too late (e.g. during `Drop`) to be returned as a `Result`.
pub fn write_stderr_line_best_effort(message: std::fmt::Arguments<'_>) {
    let stderr = std::io::stderr();
    let mut stderr = stderr.lock();
    let _ = std::io::Write::write_fmt(&mut stderr, format_args!("{message}\n"));
    let _ = std::io::Write::flush(&mut stderr);
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::learning_mode_core::{
        AccessType, DeniedResource, ResourceType, VerboseLoggingOutcomeReason,
        VerboseLoggingProvider, VerboseLoggingSignature,
    };
    use std::io::Write;

    #[test]
    fn write_denials_document_writes_summary_and_document() {
        let directory = tempfile::tempdir().expect("temp directory");
        let output_path = directory.path().join("denials.json");
        let analysis = AnalysisResult::complete(vec![DeniedResource {
            resource: r"C:\blocked.txt".to_string(),
            resource_type: ResourceType::File,
            access_type: AccessType::Read,
            pid: 42,
            filetime: 99,
        }]);

        let metadata =
            write_denials_document(analysis, 7, &output_path).expect("write should succeed");

        assert_eq!(metadata.kind, CaptureDenialsOutput::KIND);
        assert_eq!(metadata.exit_code, 7);
        assert_eq!(metadata.total_denials, 1);
        assert!(!metadata.denied_resources_truncated);
        let document: DenialsDocument =
            serde_json::from_slice(&std::fs::read(&output_path).unwrap()).unwrap();
        assert_eq!(document.denials.len(), 1);
        let verbose_logging: VerboseLoggingDocument = serde_json::from_slice(
            &std::fs::read(verbose_logging_output_path(&output_path).unwrap()).unwrap(),
        )
        .unwrap();
        assert_eq!(verbose_logging.version, VerboseLoggingDocument::VERSION);
    }

    #[test]
    fn write_denials_document_handles_empty_result() {
        let directory = tempfile::tempdir().expect("temp directory");
        let output_path = directory.path().join("denials.json");

        let metadata =
            write_denials_document(AnalysisResult::complete(Vec::new()), 0, &output_path)
                .expect("write should succeed");

        assert_eq!(metadata.total_denials, 0);
        assert!(output_path.exists());
        assert!(verbose_logging_output_path(&output_path).unwrap().exists());
    }

    #[test]
    fn write_denials_document_writes_verbose_logging_aggregates() {
        let directory = tempfile::tempdir().expect("temp directory");
        let output_path = directory.path().join("denials.json");
        let mut analysis = AnalysisResult::complete(Vec::new());
        analysis.verbose_logging.record(VerboseLoggingSignature {
            provider: VerboseLoggingProvider::KernelGeneral,
            provider_guid: "{A68CA8B7-004F-D7B6-A698-07E2DE0F1F5D}".to_string(),
            event_id: 14,
            reason: VerboseLoggingOutcomeReason::Actionable,
            pid: 42,
            access_type: Some(crate::learning_mode_core::AccessType::Read),
            resource_type: Some(crate::learning_mode_core::ResourceType::File),
            properties: vec![("Sid".to_string(), "S-1-15-3-1".to_string())],
        });

        write_denials_document(analysis, 0, &output_path).expect("write should succeed");

        let verbose_logging: VerboseLoggingDocument = serde_json::from_slice(
            &std::fs::read(verbose_logging_output_path(&output_path).unwrap()).unwrap(),
        )
        .unwrap();
        assert_eq!(verbose_logging.signatures.len(), 1);
        assert_eq!(verbose_logging.signatures[0].count, 1);
        assert_eq!(verbose_logging.signatures[0].signature.pid, 42);
    }

    #[test]
    fn verbose_logging_path_is_a_deterministic_json_sibling() {
        assert_eq!(
            verbose_logging_output_path(Path::new(r"C:\out\denials.123.json")).unwrap(),
            PathBuf::from(r"C:\out\denials.123.verbose.json")
        );
        assert_eq!(
            verbose_logging_output_path(Path::new("denials")).unwrap(),
            PathBuf::from("denials.verbose.json")
        );
    }

    #[test]
    fn verbose_logging_collision_leaves_actionable_output_absent() {
        let directory = tempfile::tempdir().expect("temp directory");
        let output_path = directory.path().join("denials.json");
        let verbose_logging_path = verbose_logging_output_path(&output_path).unwrap();
        std::fs::write(&verbose_logging_path, b"existing").expect("seed verbose logging output");

        write_denials_document(AnalysisResult::complete(Vec::new()), 0, &output_path)
            .expect_err("collision should fail");

        assert!(!output_path.exists());
        assert_eq!(std::fs::read(verbose_logging_path).unwrap(), b"existing");
    }

    #[test]
    fn second_staged_write_failure_leaves_no_final_output() {
        let directory = tempfile::tempdir().expect("temp directory");
        let output_path = directory.path().join("denials.json");
        let verbose_logging_path = verbose_logging_output_path(&output_path).unwrap();

        let error = write_paired_output_files(
            "captureDenials",
            &output_path,
            &verbose_logging_path,
            ExistingOutputPolicy::CreateNew,
            |writer| writer.write_all(b"denials"),
            |writer| {
                writer.write_all(b"partial")?;
                Err(std::io::Error::other("simulated verbose logging failure"))
            },
        )
        .expect_err("paired write should fail");

        assert!(error
            .to_string()
            .contains("simulated verbose logging failure"));
        assert!(!output_path.exists());
        assert!(!verbose_logging_path.exists());
    }

    #[test]
    fn insert_run_id_into_stem_injects_id_before_extension() {
        let got = insert_run_id_into_stem(Path::new(r"C:\app\denials.json"), "1234_abcd");
        assert_eq!(got, PathBuf::from(r"C:\app\denials.1234_abcd.json"));
    }

    #[test]
    fn insert_run_id_into_stem_handles_no_extension() {
        let got = insert_run_id_into_stem(Path::new(r"C:\app\denials"), "77_abcd");
        assert_eq!(got, PathBuf::from(r"C:\app\denials.77_abcd"));
    }

    #[test]
    fn insert_run_id_into_stem_handles_bare_filename() {
        let got = insert_run_id_into_stem(Path::new("denials.json"), "9_abcd");
        assert_eq!(got, PathBuf::from("denials.9_abcd.json"));
    }

    #[test]
    fn insert_run_id_into_stem_preserves_multi_dot_stem() {
        let got = insert_run_id_into_stem(Path::new(r"C:\app\out.denials.json"), "5_abcd");
        assert_eq!(got, PathBuf::from(r"C:\app\out.denials.5_abcd.json"));
    }

    #[test]
    fn managed_denials_paths_are_unique_per_run() {
        let first = unique_denials_output_paths(None, false)
            .expect("first path")
            .denials;
        let second = unique_denials_output_paths(None, false)
            .expect("second path")
            .denials;

        assert_ne!(first, second);
        assert_eq!(first.parent(), Some(std::env::temp_dir().as_path()));
        assert_eq!(second.parent(), Some(std::env::temp_dir().as_path()));
        assert_eq!(first.extension().and_then(|ext| ext.to_str()), Some("json"));
    }

    #[test]
    fn paired_paths_preserve_run_id_for_extensionless_output() {
        let paths = denials_output_paths_for_run(Some(r"C:\app\denials"), "77_abcd", true);

        assert_eq!(paths.denials, PathBuf::from(r"C:\app\denials.77_abcd"));
        assert_eq!(
            paths.etl,
            Some(PathBuf::from(r"C:\app\denials.77_abcd.etl"))
        );
    }

    #[test]
    fn paired_paths_are_distinct_for_etl_output() {
        let paths = denials_output_paths_for_run(Some(r"C:\app\denials.ETL"), "77_abcd", true);

        assert_eq!(paths.denials, PathBuf::from(r"C:\app\denials.77_abcd.ETL"));
        assert_eq!(
            paths.etl,
            Some(PathBuf::from(r"C:\app\denials.77_abcd.trace.etl"))
        );
        assert_ne!(
            paths.denials.to_string_lossy().to_ascii_lowercase(),
            paths
                .etl
                .as_ref()
                .expect("retained path")
                .to_string_lossy()
                .to_ascii_lowercase()
        );
    }

    #[test]
    fn missing_internal_capture_file_is_already_clean() {
        let directory = tempfile::tempdir().expect("temp directory");
        let missing = directory.path().join("missing.etl");
        remove_internal_capture_file(&missing).expect("missing file should be clean");
    }

    #[test]
    fn capture_and_cleanup_failures_are_both_preserved() {
        let error = combine_capture_and_cleanup_results::<()>(
            Err(std::io::Error::other("decode failed")),
            Err(std::io::Error::other("delete failed")),
        )
        .expect_err("combined operation should fail");

        let message = error.to_string();
        assert!(message.contains("decode failed"));
        assert!(message.contains("delete failed"));
    }

    #[test]
    fn successful_process_reports_capture_teardown_failure() {
        let error =
            combine_process_and_teardown_results(Ok(0), Err(std::io::Error::other("seal failed")))
                .expect_err("capture failure must override successful process exit");

        assert!(error.to_string().contains("seal failed"));
    }

    #[test]
    fn failed_process_result_is_preserved_over_successful_teardown() {
        let error =
            combine_process_and_teardown_results(Err(std::io::Error::other("wait failed")), Ok(()))
                .expect_err("wait failure should propagate");

        assert!(error.to_string().contains("wait failed"));
    }

    #[test]
    fn wait_failure_takes_precedence_when_teardown_also_fails() {
        let error = combine_process_and_teardown_results(
            Err(std::io::Error::other("wait failed")),
            Err(std::io::Error::other("teardown failed")),
        )
        .expect_err("wait failure should still propagate");

        assert!(error.to_string().contains("wait failed"));
        assert!(error.to_string().contains("teardown failed"));
    }
}