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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Policy validation for the IsolationSession backend.
//!
//! Filesystem policy (`rw`, `ro`, `denied`) is rejected at every phase — the
//! backend has no host-folder-sharing primitive.
//!
//! UI policy is rejected at every phase — the backend has no UI-restriction
//! primitive. The isolation session is a *separate OS session*, which isolates
//! the host's UI from the contained code but does not deny the contained code
//! the capability: window creation, GDI, and the session's own clipboard all
//! work inside it. A `ui` policy therefore cannot be honored, and accepting it
//! would assert a guarantee the backend does not provide.
//!
//! Network policy is honesty-gated. The container runs on an unrestricted
//! network that MXC cannot filter or deny, so at provision (and one-shot, which
//! runs the full lifecycle in one call) the caller must describe that actual
//! unrestricted posture through the standard directional all-allow spelling.
//! Anything else, including an absent policy (which defaults to deny), is
//! refused. On post-provision phases the network posture is fixed at provision:
//! any supplied network policy is refused, an absent one is inherited.
//!
//! Every process starts from the agent user's default environment, which
//! `process.env` can be layered over (`process.inheritDefaultEnv`) but cannot
//! replace or empty. A `process.env` without `process.inheritDefaultEnv` is
//! therefore refused wherever a process is launched: one-shot and exec.

use crate::mxc_common::default_env::EnvResolution;
use crate::mxc_common::models::{
    ExecutionRequest, NetworkAction, NetworkEnforcementMode, NetworkPolicy,
};

use super::error::IsolationSessionError;

const ERR_FILESYSTEM_POLICY: &str =
    "filesystem policy is not supported by the isolation session backend";
const ERR_UI_POLICY: &str = "UI policy is not supported by the isolation session backend; the \
    session isolates the host's UI from the contained code but does not deny it UI \
    capabilities (window creation, GDI, and the session's own clipboard all work inside \
    it), so no ui posture is truthful here. Omitting the ui section is accepted but \
    applies no restriction — it is not the lockdown the schema's default implies. Use a \
    backend that enforces UI policy if you need one";
const ERR_NETWORK_POLICY: &str = "the network is unrestricted and cannot be filtered or denied; \
    describe that posture with network.egress.default=allow, \
    network.ingress.default=allow, and network.ingress.hostLoopback=allow; do not supply rules, \
    proxy settings, or legacy network fields, or use a backend that enforces network policy";
const ERR_PROXY_POLICY: &str =
    "the network cannot be routed through a proxy; remove network.proxy \
    (the container's network is unrestricted and unproxied)";
const ERR_NETWORK_IMMUTABLE: &str =
    "network policy is fixed at provision and cannot be changed on \
    this phase; omit the network policy on post-provision phases";
const ERR_ENVIRONMENT_POLICY: &str = "process.env without process.inheritDefaultEnv=true is not \
    supported by the isolation session backend: every process starts from the agent user's \
    default environment, which cannot be replaced or emptied; set process.inheritDefaultEnv to \
    true to layer process.env over it, or omit process.env";

/// Validates the request for the provision phase (also used by the one-shot
/// runner, which runs the whole lifecycle in one call so provision-phase
/// semantics apply). Filesystem policy is rejected first, then the network
/// policy must be one of the canonical unrestricted-network postures.
pub(super) fn validate_provision_policy(
    request: &ExecutionRequest,
) -> Result<(), IsolationSessionError> {
    reject_filesystem_policy(request)?;
    reject_ui_policy(request)?;
    validate_provision_network_policy(request)
}

/// Validates the request for any non-provision phase (start / exec / stop /
/// deprovision). Filesystem policy is rejected (bound to provision and
/// immutable). UI policy is rejected (never supported). The network posture is
/// likewise fixed at provision, so a network policy supplied here is refused;
/// an absent one is inherited.
pub(super) fn validate_post_provision_policy(
    request: &ExecutionRequest,
) -> Result<(), IsolationSessionError> {
    reject_filesystem_policy(request)?;
    reject_ui_policy(request)?;
    if request.policy.network_proxy.is_enabled() {
        return Err(IsolationSessionError::Policy(ERR_PROXY_POLICY.to_string()));
    }
    if request.policy.network_specified
        || request.policy.network_mode_specified
        || request.policy.network_egress.is_some()
        || request.policy.network_ingress.is_some()
    {
        return Err(IsolationSessionError::Policy(
            ERR_NETWORK_IMMUTABLE.to_string(),
        ));
    }
    Ok(())
}

/// Rejects a `process.env` that asks to replace the default environment
/// rather than be layered over it.
pub(super) fn reject_unhonorable_environment(
    request: &ExecutionRequest,
) -> Result<(), IsolationSessionError> {
    match EnvResolution::of(request) {
        EnvResolution::Replace => Err(IsolationSessionError::Policy(
            ERR_ENVIRONMENT_POLICY.to_string(),
        )),
        EnvResolution::Default | EnvResolution::Overlay => Ok(()),
    }
}

/// Rejects any filesystem policy field. Shared by the provision and
/// post-provision validators — the backend has no host-folder-sharing
/// primitive, so `rw` / `ro` / `denied` are rejected at every phase. Runs
/// before the network check so a filesystem rejection takes precedence.
fn reject_filesystem_policy(request: &ExecutionRequest) -> Result<(), IsolationSessionError> {
    if !request.policy.readwrite_paths.is_empty()
        || !request.policy.readonly_paths.is_empty()
        || !request.policy.enumerate_paths.is_empty()
        || !request.policy.denied_paths.is_empty()
    {
        return Err(IsolationSessionError::Policy(
            ERR_FILESYSTEM_POLICY.to_string(),
        ));
    }
    Ok(())
}

/// Rejects any supplied UI policy. Presence-based, not value-based: the domain
/// `UiPolicy::default()` is full lockdown, so an explicitly-supplied lockdown
/// `ui` is indistinguishable from an absent one by value — the same blind spot
/// `network_specified` closes for the network policy. Runs after the filesystem
/// check so a filesystem rejection keeps precedence.
fn reject_ui_policy(request: &ExecutionRequest) -> Result<(), IsolationSessionError> {
    if request.policy.ui_specified {
        return Err(IsolationSessionError::Policy(ERR_UI_POLICY.to_string()));
    }
    Ok(())
}

/// Accepts only the canonical directional unrestricted-network spelling.
fn validate_provision_network_policy(
    request: &ExecutionRequest,
) -> Result<(), IsolationSessionError> {
    let policy = &request.policy;
    let is_directional_allow = policy.default_network_policy == NetworkPolicy::Block
        && !policy.allow_local_network
        && policy.allowed_hosts.is_empty()
        && policy.blocked_hosts.is_empty()
        && policy.network_enforcement_mode == NetworkEnforcementMode::Capabilities
        && policy.network_egress.as_ref().is_some_and(|egress| {
            egress.default == NetworkAction::Allow
                && egress.allow.is_empty()
                && egress.deny.is_empty()
        })
        && policy.network_ingress.as_ref().is_some_and(|ingress| {
            ingress.default == NetworkAction::Allow && ingress.host_loopback == NetworkAction::Allow
        });
    if !is_directional_allow {
        return Err(IsolationSessionError::Policy(
            ERR_NETWORK_POLICY.to_string(),
        ));
    }
    if policy.network_proxy.is_enabled() {
        return Err(IsolationSessionError::Policy(ERR_PROXY_POLICY.to_string()));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mxc_common::models::{
        ContainerPolicy, DefaultEnvCompatibility, NetworkEgressPolicy, NetworkIngressPolicy,
        ProxyAddress, ProxyConfig, UiPolicy,
    };
    use crate::mxc_common::mxc_error::MxcErrorCode;

    fn assert_policy_err_contains(err: IsolationSessionError, expected: &str) {
        match err {
            IsolationSessionError::Policy(msg) => {
                assert!(msg.contains(expected), "expected '{}' in {}", expected, msg)
            }
            other => panic!("expected Policy variant, got {:?}", other),
        }
    }

    /// The removed legacy unrestricted-network spelling.
    fn canonical_allow_policy() -> ContainerPolicy {
        ContainerPolicy {
            default_network_policy: NetworkPolicy::Allow,
            allow_local_network: true,
            ..Default::default()
        }
    }

    fn directional_allow_policy() -> ContainerPolicy {
        ContainerPolicy {
            network_egress: Some(NetworkEgressPolicy {
                default: NetworkAction::Allow,
                ..Default::default()
            }),
            network_ingress: Some(NetworkIngressPolicy {
                default: NetworkAction::Allow,
                host_loopback: NetworkAction::Allow,
            }),
            network_specified: true,
            network_mode_specified: true,
            ..Default::default()
        }
    }

    // ====== Phase-specific policy validation ======
    //
    // Filesystem policy is rejected at every phase (shared
    // `reject_filesystem_policy`, checked before the network policy).
    // Provision (and one-shot) additionally require the canonical
    // unrestricted-network acknowledgment; post-provision phases reject any
    // supplied network policy (`network_specified`) and inherit an absent one.

    #[test]
    fn provision_policy_rejects_readwrite_paths() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                readwrite_paths: vec!["C:\\src".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_FILESYSTEM_POLICY,
        );
    }

    #[test]
    fn provision_policy_rejects_readonly_paths() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                readonly_paths: vec!["C:\\data".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_FILESYSTEM_POLICY,
        );
    }

    #[test]
    fn provision_policy_rejects_enumerate_paths() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                enumerate_paths: vec!["C:\\data".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_FILESYSTEM_POLICY,
        );
    }

    #[test]
    fn provision_policy_rejects_readwrite_and_readonly_together() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                readwrite_paths: vec!["C:\\src".to_string()],
                readonly_paths: vec!["C:\\data".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_FILESYSTEM_POLICY,
        );
    }

    #[test]
    fn provision_policy_rejects_denied_paths() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                denied_paths: vec!["C:\\secret".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_FILESYSTEM_POLICY,
        );
    }

    #[test]
    fn provision_policy_rejects_denied_even_with_rw() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                readwrite_paths: vec!["C:\\src".to_string()],
                denied_paths: vec!["C:\\secret".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_FILESYSTEM_POLICY,
        );
    }

    #[test]
    fn provision_policy_rejects_legacy_allow() {
        let request = ExecutionRequest {
            policy: canonical_allow_policy(),
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_NETWORK_POLICY,
        );
    }

    #[test]
    fn provision_policy_accepts_directional_allow() {
        let request = ExecutionRequest {
            policy: directional_allow_policy(),
            ..Default::default()
        };
        validate_provision_policy(&request).unwrap();
    }

    #[test]
    fn provision_policy_rejects_mixed_or_partial_directional_postures() {
        for policy in [
            ContainerPolicy {
                network_ingress: None,
                ..directional_allow_policy()
            },
            ContainerPolicy {
                network_egress: None,
                ..directional_allow_policy()
            },
            ContainerPolicy {
                network_ingress: Some(NetworkIngressPolicy {
                    default: NetworkAction::Allow,
                    host_loopback: NetworkAction::Deny,
                }),
                ..directional_allow_policy()
            },
            ContainerPolicy {
                default_network_policy: NetworkPolicy::Allow,
                allow_local_network: true,
                ..directional_allow_policy()
            },
            ContainerPolicy {
                network_egress: Some(NetworkEgressPolicy {
                    default: NetworkAction::Allow,
                    allow: vec![Default::default()],
                    deny: Vec::new(),
                }),
                ..directional_allow_policy()
            },
        ] {
            assert_policy_err_contains(
                validate_provision_policy(&ExecutionRequest {
                    policy,
                    ..Default::default()
                })
                .unwrap_err(),
                ERR_NETWORK_POLICY,
            );
        }
    }

    #[test]
    fn provision_policy_rejects_default_request() {
        // Absent network policy → domain default `Block`, which the backend
        // cannot enforce, so provision refuses it.
        let request = ExecutionRequest::default();
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_NETWORK_POLICY,
        );
    }

    #[test]
    fn provision_policy_rejects_block_even_with_local_network() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                default_network_policy: NetworkPolicy::Block,
                allow_local_network: true,
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_NETWORK_POLICY,
        );
    }

    #[test]
    fn provision_policy_rejects_allow_without_local_network() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                default_network_policy: NetworkPolicy::Allow,
                allow_local_network: false,
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_NETWORK_POLICY,
        );
    }

    #[test]
    fn provision_policy_rejects_allowed_hosts() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                allowed_hosts: vec!["example.com".to_string()],
                ..directional_allow_policy()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_NETWORK_POLICY,
        );
    }

    #[test]
    fn provision_policy_rejects_blocked_hosts() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                blocked_hosts: vec!["evil.com".to_string()],
                ..directional_allow_policy()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_NETWORK_POLICY,
        );
    }

    #[test]
    fn provision_policy_rejects_firewall_enforcement() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                network_enforcement_mode: NetworkEnforcementMode::Firewall,
                ..canonical_allow_policy()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_NETWORK_POLICY,
        );
    }

    #[test]
    fn provision_policy_rejects_both_enforcement() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                network_enforcement_mode: NetworkEnforcementMode::Both,
                ..canonical_allow_policy()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_NETWORK_POLICY,
        );
    }

    #[test]
    fn provision_policy_rejects_proxy() {
        // Canonical on the network axis, but a proxy the backend cannot route.
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                network_proxy: ProxyConfig {
                    address: Some(ProxyAddress::new("127.0.0.1".to_string(), 8080)),
                    builtin_test_server: false,
                },
                ..directional_allow_policy()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_PROXY_POLICY,
        );
    }

    #[test]
    fn provision_policy_filesystem_error_takes_precedence_over_network() {
        // Both a filesystem field and a non-canonical (absent/Block) network:
        // the filesystem rejection fires first.
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                readwrite_paths: vec!["C:\\src".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            ERR_FILESYSTEM_POLICY,
        );
    }

    // ====== UI policy (rejected at every phase) ======

    #[test]
    fn provision_policy_rejects_supplied_ui() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                ui_specified: true,
                ..canonical_allow_policy()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            "UI policy is not supported",
        );
    }

    #[test]
    fn provision_policy_accepts_absent_ui() {
        // Guard against over-rejection: the canonical request carries no `ui`.
        let request = ExecutionRequest {
            policy: directional_allow_policy(),
            ..Default::default()
        };
        validate_provision_policy(&request).unwrap();
    }

    #[test]
    fn provision_policy_rejects_lockdown_equivalent_ui() {
        // Presence, not value, drives the refusal. `UiPolicy::default()` is
        // full lockdown, so a caller sending an explicit lockdown `ui` is
        // indistinguishable by value from one sending none — but the backend
        // still cannot deliver the Win32k/clipboard denial the policy asserts.
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                ui_specified: true,
                ui: UiPolicy::default(),
                ..canonical_allow_policy()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&request).unwrap_err(),
            "UI policy is not supported",
        );
    }

    #[test]
    fn post_provision_policy_rejects_supplied_ui() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                ui_specified: true,
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_post_provision_policy(&request).unwrap_err(),
            "UI policy is not supported",
        );
    }

    #[test]
    fn post_provision_policy_accepts_absent_ui() {
        let request = ExecutionRequest::default();
        assert!(validate_post_provision_policy(&request).is_ok());
    }

    #[test]
    fn ui_error_takes_precedence_over_network_but_not_filesystem() {
        // Ordering is filesystem -> ui -> network, so each existing
        // precedence test stays valid and the new check slots in between.
        let fs_and_ui = ExecutionRequest {
            policy: ContainerPolicy {
                readwrite_paths: vec!["C:\\src".to_string()],
                ui_specified: true,
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&fs_and_ui).unwrap_err(),
            ERR_FILESYSTEM_POLICY,
        );

        // `ui` supplied with a non-canonical (absent -> Block) network: the ui
        // rejection fires first.
        let ui_and_network = ExecutionRequest {
            policy: ContainerPolicy {
                ui_specified: true,
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_provision_policy(&ui_and_network).unwrap_err(),
            "UI policy is not supported",
        );
    }

    #[test]
    fn ui_rejection_maps_to_policy_validation() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                ui_specified: true,
                ..Default::default()
            },
            ..Default::default()
        };
        let err = super::super::error::map_lifecycle_error(
            validate_provision_policy(&request).unwrap_err(),
        );
        assert_eq!(err.code, MxcErrorCode::PolicyValidation);
    }

    #[test]
    fn post_provision_policy_rejects_readwrite_paths() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                readwrite_paths: vec!["C:\\src".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_post_provision_policy(&request).unwrap_err(),
            ERR_FILESYSTEM_POLICY,
        );
    }

    #[test]
    fn post_provision_policy_rejects_readonly_paths() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                readonly_paths: vec!["C:\\data".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_post_provision_policy(&request).unwrap_err(),
            ERR_FILESYSTEM_POLICY,
        );
    }

    #[test]
    fn post_provision_policy_rejects_denied_paths() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                denied_paths: vec!["C:\\secret".to_string()],
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_post_provision_policy(&request).unwrap_err(),
            ERR_FILESYSTEM_POLICY,
        );
    }

    #[test]
    fn post_provision_policy_rejects_specified_network() {
        // Any supplied network policy is refused post-provision (fixed at
        // provision), regardless of value — here a canonical allow.
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                network_specified: true,
                ..canonical_allow_policy()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_post_provision_policy(&request).unwrap_err(),
            ERR_NETWORK_IMMUTABLE,
        );
    }

    #[test]
    fn post_provision_policy_rejects_specified_network_even_when_block() {
        // Closes the presence-signal blind spot: an explicit default-valued
        // (Block) network is indistinguishable from absent in the domain model,
        // so the `network_specified` flag — not the value — drives the refusal.
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                network_specified: true,
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_post_provision_policy(&request).unwrap_err(),
            ERR_NETWORK_IMMUTABLE,
        );
    }

    #[test]
    fn post_provision_policy_accepts_absent_network() {
        // No network supplied → inherit what provision established.
        let request = ExecutionRequest::default();
        assert!(validate_post_provision_policy(&request).is_ok());
    }

    #[test]
    fn post_provision_policy_rejects_raw_directional_policy() {
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                network_egress: Some(NetworkEgressPolicy::default()),
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_post_provision_policy(&request).unwrap_err(),
            ERR_NETWORK_IMMUTABLE,
        );
    }

    #[test]
    fn post_provision_policy_filesystem_error_takes_precedence() {
        // Filesystem rejection fires before the network-immutability check.
        let request = ExecutionRequest {
            policy: ContainerPolicy {
                readwrite_paths: vec!["C:\\src".to_string()],
                network_specified: true,
                ..Default::default()
            },
            ..Default::default()
        };
        assert_policy_err_contains(
            validate_post_provision_policy(&request).unwrap_err(),
            ERR_FILESYSTEM_POLICY,
        );
    }

    // ====== process.env (refused when it would replace the default) ======

    fn request_with_env(
        compatibility: DefaultEnvCompatibility,
        env: Option<Vec<&str>>,
        inherit_default_env: bool,
    ) -> ExecutionRequest {
        ExecutionRequest {
            default_env_compatibility: compatibility,
            env: env.map(|e| e.into_iter().map(String::from).collect()),
            inherit_default_env,
            ..Default::default()
        }
    }

    #[test]
    fn only_an_environment_that_would_replace_the_default_is_refused() {
        use DefaultEnvCompatibility::DefaultBlock;

        let cases = [
            (None, false, DefaultBlock, true),
            (None, true, DefaultBlock, true),
            (Some(vec![]), false, DefaultBlock, false),
            (Some(vec![]), true, DefaultBlock, true),
            (Some(vec!["FOO=bar"]), false, DefaultBlock, false),
            (Some(vec!["FOO=bar"]), true, DefaultBlock, true),
        ];

        for (env, inherit_default_env, compatibility, accepted) in cases {
            let state =
                format!("{env:?}, inherit_default_env={inherit_default_env}, {compatibility:?}");
            let request = request_with_env(compatibility, env, inherit_default_env);
            match (reject_unhonorable_environment(&request), accepted) {
                (Ok(()), true) => {}
                (Err(err), false) => assert_policy_err_contains(err, ERR_ENVIRONMENT_POLICY),
                (result, _) => panic!("{state}: expected accepted={accepted}, got {result:?}"),
            }
        }
    }
}