mxc-sdk 1.0.1

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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! `BubblewrapScriptRunner` — executes scripts inside a Bubblewrap
//! namespace sandbox via the `bwrap` CLI.
//!
//! Bubblewrap uses Linux user namespaces to create an unprivileged sandbox.
//! The runner translates `ExecutionRequest` policy fields into `bwrap` CLI
//! arguments via [`crate::bwrap_common::bwrap_command::build_args`], then spawns `bwrap`
//! with stdout/stderr capture and optional timeout enforcement.
//!
//! Directional network rules are programmed inside a private namespace;
//! ruleless deny uses `--unshare-net` without additional network tooling.
//! A runtime proxy uses the private namespace and a caller-managed endpoint.

use std::collections::HashSet;
use std::fmt::Write as FmtWrite;
use std::os::fd::AsFd;
use std::os::unix::process::CommandExt;
use std::path::{Path, PathBuf};
use std::process::{Child, ChildStdin, Command, ExitStatus, Stdio};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::time::{Duration, Instant};

use crate::mxc_common::filesystem_symlink::resolve_through_symlinks;
use crate::mxc_common::interruptible_reader::{wrap_pipe, InterruptibleReader, ReadCanceller};
use crate::mxc_common::logger::Logger;
use crate::mxc_common::models::{ExecutionRequest, ScriptResponse};
use crate::mxc_common::sandbox_process::{
    boxed_closer, cancel_and_join_discard, duplicate_and_take_native_stdio, group_kill,
    spawn_discard, take_boxed_read, take_boxed_write, wait_with_timeout, NativeStdio, PtySize,
    SandboxBackend, SandboxProcess, StdioMode, StreamCloser, WaitError,
};
use crate::mxc_common::validator::{
    validate_common, validate_network_policy_support, NetworkPolicySupport,
};
use crate::mxc_pty::{LivePty, PtySize as UnixPtySize};

use crate::bwrap_common::{
    bwrap_command::{self, ResolvedNetworkMode},
    bwrap_version, network_rules,
    provider_monitor::ProviderMonitor,
    proxy_network,
};

const DNS_PORT: u16 = 53;

/// Bubblewrap sandbox runner. Uses only shared `ContainerPolicy` fields —
/// no backend-specific config struct required.
#[derive(Default)]
pub struct BubblewrapScriptRunner;

impl BubblewrapScriptRunner {
    pub fn new() -> Self {
        Self
    }
}

impl SandboxBackend for BubblewrapScriptRunner {
    /// Bubblewrap programs directional policy into iptables chains in
    /// the sandbox's own network namespace, so it enforces the outbound default,
    /// the allow/deny rules, and both inbound fields.
    ///
    /// `INGRESS_DEFAULT` and `HOST_LOOPBACK` declare that the backend
    /// *understands* those fields, not that it honors both of their values:
    /// only the deny posture is reachable, and the allow posture is refused by
    /// `bwrap_command::directional_network_rejection` below. Declaring them
    /// without that refusal would be a fail-open, so the two belong together.
    ///
    /// `RUNTIME_PROXY` is declared because `runtimeConfig.networkProxy` is
    /// normalized into `policy.network_proxy` and enforced in a private
    /// namespace with only its pinned endpoint reachable.
    ///
    /// `PROXY_PEER_IDENTITY` stays undeclared: it is a ProcessContainer concept
    /// with no Bubblewrap equivalent, so shared validation refuses it here.
    fn network_policy_support(&self) -> NetworkPolicySupport {
        NetworkPolicySupport::EGRESS_DEFAULT
            | NetworkPolicySupport::EGRESS_RULES
            | NetworkPolicySupport::INGRESS_DEFAULT
            | NetworkPolicySupport::HOST_LOOPBACK
            | NetworkPolicySupport::RUNTIME_PROXY
    }

    fn validate(&self, request: &ExecutionRequest) -> Result<(), ScriptResponse> {
        self.validate_prepared(request).map(|_| ())
    }

    fn spawn(
        &mut self,
        request: &ExecutionRequest,
        logger: &mut Logger,
        stdio: StdioMode,
    ) -> Result<Box<dyn SandboxProcess>, ScriptResponse> {
        validate_common(request)?;
        // Keep the plan validation derived so the firewall path installs
        // exactly what was accepted instead of deriving it again.
        let egress_plan = self.validate_prepared(request)?;
        if let Some(plan) = egress_plan.as_ref() {
            warn_unreachable_v6_targets(plan, logger);
        }
        // Tighten aliases of the same host object to the strictest intent
        // (deny > ro > rw). Done here, close to mount, to minimize the TOCTOU
        // window; an unresolvable path with deniedPaths present fails closed.
        let normalized;
        let request = match crate::mxc_common::filesystem_object::normalize_object_conflicts(
            &request.policy,
            logger,
        ) {
            Ok(Some(policy)) => {
                normalized = ExecutionRequest {
                    policy,
                    ..request.clone()
                };
                &normalized
            }
            Ok(None) => request,
            Err(msg) => return Err(ScriptResponse::error(&msg)),
        };
        // Reject any policy path the invoking user cannot access, so the sandbox
        // never gains access the caller lacks. Runs after object normalization
        // so it sees the already-tightened intents.
        if let Err(msg) = crate::mxc_common::filesystem_access::check_delegation(&request.policy) {
            return Err(ScriptResponse::error(&msg));
        }
        // Resolve denied paths that traverse a symlink to their real host path
        // and classify each as a file/dir mask (see [`resolve_denied_paths`]).
        // Only clones the request when a path needs rewriting (common case:
        // none). See docs/backends/bwrap/bubblewrap-backend.md.
        let plan = match resolve_denied_paths(&request.policy, logger) {
            Ok(plan) => plan,
            Err(msg) => return Err(ScriptResponse::error(&msg)),
        };
        let resolved;
        let request = match plan.paths {
            Some(denied_paths) => {
                let mut policy = request.policy.clone();
                policy.denied_paths = denied_paths;
                resolved = ExecutionRequest {
                    policy,
                    ..request.clone()
                };
                &resolved
            }
            None => request,
        };
        // The masks are built from the list above, not from the one the caller
        // wrote, so the pin conflict has to be judged against it too.
        if let Err(msg) = check_pin_against_denied_hosts(request) {
            return Err(ScriptResponse::error(&msg));
        }
        warn_resolver_mount_hint(request, logger);
        warn_cwd_mount_hint(request, logger);
        let child = self.spawn_bwrap(request, &plan.files, egress_plan, logger, stdio)?;
        Ok(Box::new(BubblewrapSandboxProcess::new(child)))
    }
}

impl BubblewrapScriptRunner {
    /// `validate`, plus the egress plan it derived.
    ///
    /// Returning the plan lets `spawn` install exactly what validation
    /// accepted instead of deriving it a second time on every spawn.
    fn validate_prepared(
        &self,
        request: &ExecutionRequest,
    ) -> Result<Option<network_rules::EgressPlan>, ScriptResponse> {
        // Execution validation deliberately bypasses the advisory success
        // cache: a prior platform-support query must not approve a different
        // executable after `PATH` or its contents change.
        self.validate_prepared_with_probe(request, bwrap_version::probe_bwrap_uncached)
    }

    /// `validate_prepared` with an injectable environment probe.
    ///
    /// Tests use this to assert that user-input validation runs *before* the
    /// environmental `bwrap` probe, and to drive every probe failure without
    /// depending on what the host happens to have installed.
    fn validate_prepared_with_probe<F>(
        &self,
        request: &ExecutionRequest,
        probe: F,
    ) -> Result<Option<network_rules::EgressPlan>, ScriptResponse>
    where
        F: FnOnce() -> Result<bwrap_version::BwrapVersion, bwrap_version::BwrapUnavailable>,
    {
        validate_network_policy_support(request, self.network_policy_support())?;

        // User-input validation runs before the environmental `bwrap`
        // probe so config errors are reported deterministically even on
        // hosts without bwrap installed.
        if request.script_code.is_empty() {
            return Err(ScriptResponse::error(
                "script_code is empty — nothing to execute.",
            ));
        }

        // Refuse a credential-bearing proxy URL here as well as at parse time.
        // The parser guard only covers requests it built; `ExecutionRequest`
        // and `ProxyAddress::from_url` are public, so a caller can hand this
        // runner a policy the parser never saw. `to_url` returns that URL
        // verbatim and `build_args` emits it as a `bwrap --setenv HTTP_PROXY
        // VALUE` argument (bwrap_command.rs), and a process's argv is readable
        // through /proc/<pid>/cmdline by any local user for the lifetime of the
        // command. This mirrors the same guard on the LXC runner, which reaches
        // argv by a different route (`lxc-attach --set-var`).
        //
        // It sits with the input checks, ahead of the bwrap probe, for the
        // reason above: a host without bwrap must still be told what is wrong
        // with the request.
        if let Some(url) = request
            .policy
            .network_proxy
            .address
            .as_ref()
            .map(|address| address.to_url())
        {
            if crate::mxc_common::proxy_env::proxy_url_has_credentials(&url) {
                // Built from the redacted form so the rejection cannot become
                // the leak it is rejecting.
                return Err(ScriptResponse::error(&format!(
                    "Bubblewrap: runtimeConfig.networkProxy must not carry credentials ('{}'). \
                     Bubblewrap passes the proxy URL to bwrap as a --setenv command-line \
                     argument, and process arguments are world-readable through \
                     /proc/<pid>/cmdline, so the password would be visible to every local \
                     user while the command runs. Use a proxy that does not require inline \
                     credentials, or supply them to the proxy itself rather than through \
                     the URL.",
                    crate::mxc_common::proxy_env::redact_proxy_url(&url)
                )));
            }
        }

        // Programmatic requests also need the parser's proxy-only restriction:
        // direct egress must not be accepted and then discarded at launch.
        if let Some(reason) = bwrap_command::proxy_with_egress_rejection(request) {
            return Err(ScriptResponse::error(reason));
        }
        if let Some(reason) = bwrap_command::directional_network_rejection(request) {
            return Err(ScriptResponse::error(reason));
        }

        // Proxy-only networking derives the sandbox-visible endpoint from the
        // configured one and then opens exactly that address in the egress
        // chain. That step can reject an endpoint outright (an IPv6 loopback
        // the gateway cannot reach, a routable or IPv6-only endpoint the
        // IPv4-only rules cannot express, a zero port), and those verdicts
        // follow from the configured address alone, so they are reported here
        // instead of after a proxy has been started.
        //
        // A hostname is deliberately left to `run`: its verdict needs a lookup,
        // and the answer is the pin the sandbox is given, so resolving here
        // would either resolve twice or pin an address the egress chain never
        // opened.
        //
        let proxy_only =
            ResolvedNetworkMode::from_request(request, request.policy.network_proxy.is_enabled())
                == ResolvedNetworkMode::ProxyOnly;
        if proxy_only {
            if let Some(address) = request.policy.network_proxy.address.as_ref() {
                if let Err(error) = proxy_network::SandboxProxy::check_without_resolving(address) {
                    return Err(ScriptResponse::error(&error));
                }
                if let Err(error) =
                    proxy_network::check_hosts_pin_against_policy(address, &request.policy)
                {
                    return Err(ScriptResponse::error(&error));
                }
                // Repeated in `spawn` against the normalized policy.
            }
        }

        // Validate direct CIDR/port rules before any environment probe or
        // provisioning; reuse precisely this plan at spawn.
        let firewall_enforced =
            ResolvedNetworkMode::from_request(request, request.policy.network_proxy.is_enabled())
                == ResolvedNetworkMode::FirewallEnforced;
        let egress_plan = if firewall_enforced {
            match network_rules::EgressPlan::for_request(request) {
                Ok(plan) => Some(plan),
                Err(error) => return Err(ScriptResponse::error(&error)),
            }
        } else {
            None
        };

        // `bwrap` must be present *and* new enough for every flag the argument
        // builder emits — an old binary would otherwise fail at spawn time with
        // an opaque "unknown option" error.
        if let Err(err) = probe() {
            return Err(ScriptResponse::error(&err.to_string()));
        }
        if proxy_only || firewall_enforced {
            // Proxy and firewall are mutually exclusive, so this names the one
            // the caller actually asked for.
            let use_case = if proxy_only {
                proxy_network::PrivateNetworkUse::ProxyOnlyEgress
            } else {
                proxy_network::PrivateNetworkUse::FirewallEnforcement
            };
            if let Err(error) = proxy_network::probe_dependencies(use_case) {
                return Err(ScriptResponse::error(&error));
            }
        }

        Ok(egress_plan)
    }
}

/// Reject a hostname proxy pin that would defeat a denied `/etc/hosts`.
///
/// Runs in both `validate` (against the policy as written, so the caller hears
/// about it early) and `spawn` (against the normalized policy, which is what
/// the masks are built from). One is not enough: `resolve_denied_paths`
/// rewrites `/etc/../etc/hosts` -- or any symlinked spelling -- to
/// `/etc/hosts`, which the written form never matches, and the pin is spliced
/// after every policy mount, so it hands back the file the policy masked.
fn check_pin_against_denied_hosts(request: &ExecutionRequest) -> Result<(), String> {
    let proxy_only =
        ResolvedNetworkMode::from_request(request, request.policy.network_proxy.is_enabled())
            == ResolvedNetworkMode::ProxyOnly;
    if !proxy_only {
        return Ok(());
    }
    match request.policy.network_proxy.address.as_ref() {
        Some(address) => proxy_network::check_hosts_pin_against_policy(address, &request.policy),
        None => Ok(()),
    }
}

/// Warn that an IPv6 allow rule installs but cannot carry traffic.
///
/// It fails closed, so this warns rather than rejects: refusing would break
/// configs the schema accepts today (see #955). Emitted from `spawn`, which has
/// a logger and which every caller reaches, so the JSON and programmatic paths
/// both hear it from one site.
///
/// Uses [`Logger::warning_line`], not `log_line`: it is the only sink both
/// paths actually read. `log_line` lands in the console/debug buffer, which
/// `mxc_engine::spawn_execution_request` never folds into `Output::warnings` and which
/// `lxc-exec` prints only on an error path (and only under `--debug`, onto
/// stdout, where it would interleave with the workload's own output).
fn warn_unreachable_v6_targets(plan: &network_rules::EgressPlan, logger: &mut Logger) {
    let targets = plan.allowed_v6_targets();
    if targets.is_empty() {
        return;
    }
    logger.warning_line(&format!(
        "WARNING: Bubblewrap allows {} IPv6 destination(s) ({}), but the sandbox \
         namespace has no IPv6 connectivity: slirp4netns is launched without \
         '--enable-ipv6', so these rules install and are never traversed. The \
         destination stays unreachable despite the rule. Use an IPv4 address, or \
         runtimeConfig.networkProxy, if the workload needs to reach it.",
        targets.len(),
        targets.join(", ")
    ));
}

/// Warn that the resolver pin cannot work under the installed chain.
fn warn_resolver_blocked_by_egress(
    plan: &network_rules::EgressPlan,
    pinned: bool,
    logger: &mut Logger,
) {
    if !pinned {
        return;
    }
    let forwarder = proxy_network::SLIRP_DNS_FORWARDER_IP;
    let udp = plan.admits("udp", forwarder, DNS_PORT);
    let tcp = plan.admits("tcp", forwarder, DNS_PORT);
    if udp && tcp {
        return;
    }

    let blocked = match (udp, tcp) {
        (false, false) => "UDP or TCP",
        (true, false) => "TCP",
        _ => "UDP",
    };
    logger.warning_line(&format!(
        "WARNING: Bubblewrap pointed the sandbox at slirp's DNS forwarder {forwarder} because \
         the host resolves through a loopback nameserver, but network.egress does not admit \
         {blocked} port {DNS_PORT} to it, so queries are dropped and names will not resolve. \
         Add an allow rule for '{forwarder}/32' on port {DNS_PORT} with protocol 'any' if the \
         workload needs to resolve names; a resolver falls back to TCP for answers that do not \
         fit a datagram."
    ));
}

fn warn_resolver_mount_hint(request: &ExecutionRequest, logger: &mut Logger) {
    warn_resolver_mount_hint_with(request, logger, |path| std::fs::read_link(path).ok());
}

fn warn_resolver_mount_hint_with(
    request: &ExecutionRequest,
    logger: &mut Logger,
    read_link: impl FnOnce(&Path) -> Option<PathBuf>,
) {
    let Some(target) = read_link(Path::new("/etc/resolv.conf")) else {
        return;
    };
    let Some(target_str) = target.to_str() else {
        return;
    };
    if !bwrap_command::resolver_target_without_matching_mount(&request.policy, target_str) {
        return;
    }
    logger.warning_line(&format!(
        "WARNING: Bubblewrap: /etc/resolv.conf points to {target_str:?}, but no known \
         mount destination overlaps that path. DNS may fail if the link does not \
         resolve inside the sandbox; check the link and consider listing its \
         containing directory in filesystem.readonlyPaths."
    ));
}

fn warn_cwd_mount_hint(request: &ExecutionRequest, logger: &mut Logger) {
    let Some(dir) = bwrap_command::cwd_without_matching_mount(request) else {
        return;
    };
    logger.warning_line(&format!(
        "WARNING: Bubblewrap: process.cwd {dir:?} has no apparent covering mount \
         destination. It may be unavailable inside the sandbox; check \
         filesystem.readonlyPaths or filesystem.readwritePaths for a containing \
         directory. Bubblewrap will decide whether --chdir succeeds."
    ));
}

impl BubblewrapScriptRunner {
    /// Set up networking and spawn `bwrap`, returning a [`BwrapChild`] wrapped
    /// by the [`SandboxProcess`] handle. With [`StdioMode::Pipes`] the child's
    /// stdio is piped (the caller drives it); with [`StdioMode::Inherit`] it
    /// inherits the binary's stdio (a TTY when the binary has one); with
    /// [`StdioMode::Pty`] it is attached to a caller-owned Unix PTY.
    fn spawn_bwrap(
        &self,
        request: &ExecutionRequest,
        denied_files: &HashSet<String>,
        egress_plan: Option<network_rules::EgressPlan>,
        logger: &mut Logger,
        stdio: StdioMode,
    ) -> Result<BwrapChild, ScriptResponse> {
        // The caller manages the proxy; pass its address into the sandbox
        // environment and restrict direct egress to that endpoint.
        let configured_proxy = request.policy.network_proxy.address.as_ref();
        if let Some(address) = configured_proxy {
            logger.log_line(&format!(
                "Unix network proxy active: {}",
                crate::mxc_common::proxy_env::redact_proxy_url(&address.to_url())
            ));
        }

        let network_mode = ResolvedNetworkMode::from_request(request, configured_proxy.is_some());
        let sandbox_proxy = if network_mode == ResolvedNetworkMode::ProxyOnly {
            let address = configured_proxy.ok_or_else(|| {
                ScriptResponse::error(
                    "Bubblewrap: proxy mode was selected without a resolved proxy address.",
                )
            })?;
            Some(
                proxy_network::SandboxProxy::resolve(address)
                    .map_err(|error| ScriptResponse::error(&error))?,
            )
        } else {
            None
        };
        let proxy_address = sandbox_proxy
            .as_ref()
            .map(|resolved| resolved.address())
            .or(configured_proxy);

        let mut proxy_network = match sandbox_proxy.as_ref() {
            // The workload dials the sandbox-visible address, so that is what
            // the egress rule must open.
            Some(resolved) => {
                let egress = resolved.egress();

                // A proxied chain opens no port 53, so the workload resolves
                // through the proxy rather than a nameserver of its own.
                let resolver = None;
                match proxy_network::ProxyNetworkNamespace::start(
                    &egress.plan(),
                    &network_rules::IngressPlan::for_policy(&request.policy),
                    egress.pin(),
                    resolver,
                    logger,
                    request.script_timeout,
                ) {
                    Ok(network) => Some(network),
                    Err(error) => return Err(ScriptResponse::error(&error)),
                }
            }
            // Direct directional egress uses the same private namespace.
            None if network_mode == ResolvedNetworkMode::FirewallEnforced => {
                // Reuse the validated plan. It is derived from network policy
                // only, which the filesystem normalization between the two
                // points cannot touch. Recompute only if the resolved mode
                // disagrees with validation's pre-spawn proxy reading.
                let plan = match egress_plan {
                    Some(plan) => plan,
                    None => match network_rules::EgressPlan::for_request(request) {
                        Ok(plan) => plan,
                        Err(error) => return Err(ScriptResponse::error(&error)),
                    },
                };
                let resolver = proxy_network::ResolverPin::for_host(&request.policy, logger);
                match proxy_network::ProxyNetworkNamespace::start(
                    &plan,
                    &network_rules::IngressPlan::for_policy(&request.policy),
                    None,
                    resolver.as_ref(),
                    logger,
                    request.script_timeout,
                ) {
                    Ok(network) => {
                        warn_resolver_blocked_by_egress(&plan, network.resolver_pinned(), logger);
                        Some(network)
                    }
                    Err(error) => return Err(ScriptResponse::error(&error)),
                }
            }
            None => None,
        };

        // 2. Build the bwrap argument vector. `denied_files` is the file-mask
        //    subset classified during symlink resolution (see
        //    [`resolve_denied_paths`]).
        let mut args = bwrap_command::build_args_classified_with_mode(
            request,
            proxy_address,
            denied_files,
            network_mode,
        );
        let mut network_startup = match proxy_network.as_ref() {
            Some(network) => match network.configure_bwrap(&mut args, logger) {
                Ok(startup) => Some(startup),
                Err(error) => {
                    stop_proxy_network(&mut proxy_network, logger);
                    return Err(ScriptResponse::error(&error));
                }
            },
            None => None,
        };
        let _ = writeln!(
            logger,
            "Bubblewrap: spawning bwrap with {} args",
            args.len()
        );

        // 3. Spawn `bwrap`.
        let mut command = Command::new("bwrap");
        command.args(&args);
        let pty = match stdio {
            StdioMode::Pipes => {
                command
                    .stdin(Stdio::piped())
                    .stdout(Stdio::piped())
                    .stderr(Stdio::piped());
                None
            }
            StdioMode::Inherit => {
                // The child (bwrap) inherits the binary's stdio directly — a
                // TTY when the binary has one.
                command
                    .stdin(Stdio::inherit())
                    .stdout(Stdio::inherit())
                    .stderr(Stdio::inherit());
                None
            }
            StdioMode::Pty(size) => match LivePty::attach(
                &mut command,
                UnixPtySize {
                    rows: size.rows,
                    cols: size.cols,
                    pixel_width: size.pixel_width,
                    pixel_height: size.pixel_height,
                },
                &[],
            ) {
                Ok(pty) => Some(pty),
                Err(error) => {
                    stop_proxy_network(&mut proxy_network, logger);
                    return Err(ScriptResponse::error(&format!(
                        "Bubblewrap: failed to allocate PTY: {error}"
                    )));
                }
            },
        };
        // Pipes and PTY modes put bwrap in its own process group so a timeout
        // or `kill()` can tree-kill it without touching the host's group.
        // Inherit mode keeps bwrap in the executor's group (so it retains the
        // controlling terminal); `--die-with-parent` takes its sandbox down.
        let group = stdio != StdioMode::Inherit;
        if stdio == StdioMode::Pipes {
            command.process_group(0);
        }
        if let Some(startup) = network_startup.as_ref() {
            startup.prepare_command(&mut command);
        }

        let mut child = match command.spawn() {
            Ok(process) => process,
            Err(error) => {
                stop_proxy_network(&mut proxy_network, logger);
                return Err(ScriptResponse::error(&format!(
                    "Bubblewrap: failed to spawn bwrap: {}",
                    error
                )));
            }
        };

        if let Some(mut startup) = network_startup.take() {
            startup.child_spawned();
            if let Some(network) = proxy_network.as_mut() {
                network.userns_handed_off();
            }
            let startup_result = startup
                .child_pid(&mut child)
                .and_then(|child_pid| {
                    let network = proxy_network.as_mut().ok_or_else(|| {
                        "Bubblewrap: proxy network lifecycle disappeared during startup".to_string()
                    })?;
                    network.attach(child_pid, logger)?;
                    network.check_alive()
                })
                .and_then(|()| startup.release());
            if let Err(error) = startup_result {
                let _ = child.kill();
                let _ = child.wait();
                stop_proxy_network(&mut proxy_network, logger);
                return Err(ScriptResponse::error(&error));
            }
        }

        let (stdin, stdout, stderr) = match stdio {
            StdioMode::Pipes => (child.stdin.take(), child.stdout.take(), child.stderr.take()),
            StdioMode::Inherit => (None, None, None),
            StdioMode::Pty(_) => (None, None, None),
        };
        // Wrap the pipe reads so the caller can abandon a stream a backgrounded
        // descendant is holding open (see `SandboxProcess::stdout_closer`)
        // without killing the child. On failure, tear down the per-run network
        // state we already set up before returning the error.
        let (stdout, stdout_canceller, stderr, stderr_canceller) =
            match (wrap_pipe(stdout), wrap_pipe(stderr)) {
                (Ok((out, out_canceller)), Ok((err, err_canceller))) => {
                    (out, out_canceller, err, err_canceller)
                }
                (out_result, err_result) => {
                    let _ = child.kill();
                    let _ = child.wait();
                    stop_proxy_network(&mut proxy_network, logger);
                    let error = out_result.err().or(err_result.err());
                    return Err(ScriptResponse::error(&format!(
                        "Bubblewrap: failed to wrap stdio pipes: {}",
                        error.map_or_else(|| "unknown error".to_string(), |e| e.to_string()),
                    )));
                }
            };
        let timeout = if request.script_timeout == 0 {
            None
        } else {
            Some(Duration::from_millis(u64::from(request.script_timeout)))
        };
        let started = Instant::now();

        let child = Arc::new(Mutex::new(child));
        // Armed only now: until the gate was released a dead provider surfaced
        // as a startup failure instead.
        let monitor = proxy_network
            .as_mut()
            .and_then(|network| network.take_liveness_watch())
            .map(|watch| ProviderMonitor::watch(watch, Arc::clone(&child), group));

        Ok(BwrapChild {
            child,
            stdin,
            stdout,
            stderr,
            stdout_canceller,
            stderr_canceller,
            pty,
            group,
            proxy_network,
            monitor,
            timeout,
            started,
            timed_out: false,
            termination_error: None,
        })
    }
}

/// A spawned `bwrap` sandbox: the child process, its parent-side pipe ends,
/// and the per-run private network state torn down once it exits.
struct BwrapChild {
    /// The lock is what keeps [`ProviderMonitor`]'s thread from signalling a
    /// pid this side has already reaped.
    child: Arc<Mutex<Child>>,
    stdin: Option<ChildStdin>,
    stdout: Option<InterruptibleReader>,
    stderr: Option<InterruptibleReader>,
    /// Cancellers for the stdout/stderr reads, kept so the `SandboxProcess`
    /// closers can mint a [`StreamCloser`] even after the stream is taken.
    stdout_canceller: Option<ReadCanceller>,
    stderr_canceller: Option<ReadCanceller>,
    pty: Option<LivePty>,
    /// `true` when bwrap leads its own process group (`Pipes` or `Pty`), so
    /// termination signals the whole group; `false` for `Inherit` mode, where
    /// killing bwrap relies on `--die-with-parent` to take the sandbox with it.
    group: bool,
    proxy_network: Option<proxy_network::ProxyNetworkNamespace>,
    monitor: Option<ProviderMonitor>,
    timeout: Option<Duration>,
    started: Instant,
    timed_out: bool,
    termination_error: Option<String>,
}

impl BwrapChild {
    fn lock_child(&self) -> MutexGuard<'_, Child> {
        self.child.lock().unwrap_or_else(PoisonError::into_inner)
    }

    /// Tear down per-run private network state.
    fn cleanup(&mut self, logger: &mut Logger) {
        // Stopping the supervisor closes the descriptor the monitor watches, so
        // disarming first is what keeps a normal teardown from reading as a
        // provider that died.
        self.monitor.take();
        if let Some(mut network) = self.proxy_network.take() {
            network.stop(logger);
        }
    }
}

/// A running `bwrap` sandbox exposed as a [`SandboxProcess`]. Wraps the spawned
/// [`BwrapChild`] (child, pipes, and per-run network state), tearing the network
/// state down once the child exits.
struct BubblewrapSandboxProcess {
    inner: BwrapChild,
    teardown_done: bool,
}

impl BubblewrapSandboxProcess {
    fn new(child: BwrapChild) -> Self {
        Self {
            inner: child,
            teardown_done: false,
        }
    }

    fn run_teardown(&mut self) {
        if self.teardown_done {
            return;
        }
        self.teardown_done = true;
        let mut logger = Logger::new(crate::mxc_common::logger::Mode::Buffer);
        self.inner.cleanup(&mut logger);
    }

    fn provider_lost(&self) -> bool {
        self.inner
            .monitor
            .as_ref()
            .is_some_and(ProviderMonitor::provider_lost)
    }

    /// Wait for the sandbox to exit, its deadline to pass, or its network
    /// provider to disappear.
    fn await_outcome(&mut self) -> BwrapOutcome {
        const MIN_POLL: Duration = Duration::from_millis(1);
        const MAX_POLL: Duration = Duration::from_millis(50);

        let deadline = self
            .inner
            .timeout
            .map(|timeout| self.inner.started + timeout);
        let mut interval = MIN_POLL;
        loop {
            let exited = match self.inner.lock_child().try_wait() {
                Ok(exited) => exited,
                Err(error) => return BwrapOutcome::Io(error),
            };
            // Checked after the exit rather than before it: the monitor kills
            // the sandbox, so a provider death shows up here as an ordinary
            // exit that would otherwise be reported as the workload's own.
            if self.provider_lost() {
                return BwrapOutcome::ProviderLost;
            }
            if let Some(status) = exited {
                return BwrapOutcome::Exited(status);
            }

            let now = Instant::now();
            let mut nap = interval;
            if let Some(deadline) = deadline {
                if now >= deadline {
                    return BwrapOutcome::Timeout;
                }
                nap = nap.min(deadline - now);
            }
            std::thread::sleep(nap);
            interval = (interval * 2).min(MAX_POLL);
        }
    }

    fn lost_provider_message(&mut self) -> String {
        match self.inner.proxy_network.as_mut() {
            Some(network) => network.lost_provider_detail(),
            None => "Bubblewrap: the sandbox lost its network provider while the workload \
                     was running"
                .to_string(),
        }
    }
}

/// How a `bwrap` run ended.
enum BwrapOutcome {
    Exited(ExitStatus),
    Timeout,
    ProviderLost,
    Io(std::io::Error),
}

impl SandboxProcess for BubblewrapSandboxProcess {
    fn take_native_stdio(&mut self) -> std::io::Result<Option<NativeStdio>> {
        if let Some(pty) = self.inner.pty.as_ref() {
            let stdio = pty.take_native_stdio()?;
            return Ok(Some(NativeStdio {
                stdin: Some(stdio.stdin),
                stdout: Some(stdio.stdout),
                stderr: None,
            }));
        }
        let stdio = duplicate_and_take_native_stdio(
            &mut self.inner.stdin,
            &mut self.inner.stdout,
            &mut self.inner.stderr,
            |stream| stream.as_fd().try_clone_to_owned(),
            InterruptibleReader::try_clone_owned_fd,
            InterruptibleReader::try_clone_owned_fd,
        )?;
        if stdio.is_some() {
            self.inner.stdout_canceller.take();
            self.inner.stderr_canceller.take();
        }
        Ok(stdio)
    }

    fn take_stdin(&mut self) -> Option<Box<dyn std::io::Write + Send>> {
        take_boxed_write(&mut self.inner.stdin)
    }

    fn is_pty(&self) -> bool {
        self.inner.pty.is_some()
    }

    fn pty_clone_reader(&self) -> std::io::Result<Box<dyn std::io::Read + Send>> {
        self.inner
            .pty
            .as_ref()
            .ok_or_else(|| std::io::Error::other("Bubblewrap process has no PTY"))?
            .try_clone_reader()
    }

    fn pty_clone_reader_with_closer(
        &self,
    ) -> std::io::Result<crate::mxc_common::sandbox_process::PtyReaderWithCloser> {
        let (reader, closer) = self
            .inner
            .pty
            .as_ref()
            .ok_or_else(|| std::io::Error::other("Bubblewrap process has no PTY"))?
            .try_clone_reader_with_canceller()?;
        Ok((reader, Some(Box::new(closer))))
    }

    fn pty_take_writer(&self) -> std::io::Result<Box<dyn std::io::Write + Send>> {
        self.inner
            .pty
            .as_ref()
            .ok_or_else(|| std::io::Error::other("Bubblewrap process has no PTY"))?
            .take_writer()
    }

    fn pty_resize(&self, size: PtySize) -> std::io::Result<()> {
        self.inner
            .pty
            .as_ref()
            .ok_or_else(|| std::io::Error::other("Bubblewrap process has no PTY"))?
            .resize(UnixPtySize {
                rows: size.rows,
                cols: size.cols,
                pixel_width: size.pixel_width,
                pixel_height: size.pixel_height,
            })
    }

    fn pty_size(&self) -> std::io::Result<PtySize> {
        let size = self
            .inner
            .pty
            .as_ref()
            .ok_or_else(|| std::io::Error::other("Bubblewrap process has no PTY"))?
            .size();
        Ok(PtySize {
            rows: size.rows,
            cols: size.cols,
            pixel_width: size.pixel_width,
            pixel_height: size.pixel_height,
        })
    }

    fn take_stdout(&mut self) -> Option<Box<dyn std::io::Read + Send>> {
        take_boxed_read(&mut self.inner.stdout)
    }

    fn take_stderr(&mut self) -> Option<Box<dyn std::io::Read + Send>> {
        take_boxed_read(&mut self.inner.stderr)
    }

    fn stdout_closer(&self) -> Option<Box<dyn StreamCloser>> {
        boxed_closer(&self.inner.stdout_canceller)
    }

    fn stderr_closer(&self) -> Option<Box<dyn StreamCloser>> {
        boxed_closer(&self.inner.stderr_canceller)
    }

    fn try_wait(&mut self) -> std::io::Result<Option<i32>> {
        if let Some(error) = self.inner.termination_error.as_ref() {
            return Err(std::io::Error::other(error.clone()));
        }
        if self.inner.timed_out {
            return Err(std::io::Error::new(
                std::io::ErrorKind::TimedOut,
                "Bubblewrap: script timed out",
            ));
        }
        if let Some(status) = self.inner.lock_child().try_wait()? {
            return Ok(Some(status.code().unwrap_or(-1)));
        }
        if self
            .inner
            .timeout
            .is_some_and(|timeout| self.inner.started.elapsed() >= timeout)
        {
            if let Err(error) = self.kill_for_timeout() {
                let error = format!(
                    "Bubblewrap: script timed out, and the process group could not be terminated: \
                     {error}"
                );
                self.inner.termination_error = Some(error.clone());
                return Err(std::io::Error::other(error));
            }
            self.inner.timed_out = true;
            return Err(std::io::Error::new(
                std::io::ErrorKind::TimedOut,
                "Bubblewrap: script timed out",
            ));
        }
        Ok(None)
    }

    fn id(&self) -> u32 {
        self.inner.lock_child().id()
    }

    fn kill(&mut self) -> std::io::Result<()> {
        // No-op once the child has exited and been reaped: its pid/pgid can be
        // recycled, so signaling it could hit an unrelated process (group). A
        // reaped `Child` returns its cached status here without a syscall.
        let mut child = self.inner.lock_child();
        if child.try_wait()?.is_some() {
            return Ok(());
        }
        if self.inner.group {
            // Pipes and PTY modes give bwrap its own process group.
            group_kill(&mut child)
        } else {
            // Inherit mode: bwrap shares the executor's group (no
            // `process_group(0)`), so a group-kill would hit the executor.
            // Killing bwrap alone suffices because `--die-with-parent` makes
            // the sandbox die with it — bwrap is *not* pid 1 of the namespace
            // (it forks), so without that flag descendants would survive.
            child.kill()
        }
    }

    fn kill_for_timeout(&mut self) -> std::io::Result<()> {
        const REAP_TIMEOUT: Duration = Duration::from_secs(5);

        self.kill()?;
        let mut child = self.inner.lock_child();
        match wait_with_timeout(&mut child, Some(REAP_TIMEOUT)) {
            Ok(_) => {
                drop(child);
                self.inner.timed_out = true;
                Ok(())
            }
            Err(WaitError::Timeout) => Err(std::io::Error::new(
                std::io::ErrorKind::TimedOut,
                "Bubblewrap: killed process did not become reapable within 5 seconds",
            )),
            Err(WaitError::Io(error)) => Err(error),
        }
    }

    fn wait(&mut self) -> std::io::Result<i32> {
        // Close our copy of any not-taken stdin so the child sees EOF.
        self.inner.stdin.take();
        if let Some(pty) = self.inner.pty.as_ref() {
            pty.close_writer();
        }

        // Drain (and discard) any not-taken stdout/stderr concurrently so the
        // child can't block on a full pipe (taken streams are the caller's
        // responsibility).
        let stdout_thread = spawn_discard(self.inner.stdout.take());
        let stderr_thread = spawn_discard(self.inner.stderr.take());
        let (pty_thread, pty_canceller, pty_setup_error) = match self.inner.pty.as_ref() {
            Some(pty) => match pty.take_unclaimed_reader() {
                Ok(Some((reader, canceller))) => {
                    (spawn_discard(Some(reader)), Some(canceller), None)
                }
                Ok(None) => (None, None, None),
                Err(error) => (None, None, Some(error)),
            },
            None => (None, None, None),
        };

        let result = if let Some(error) = pty_setup_error {
            let _ = self.kill();
            {
                let mut child = self.inner.lock_child();
                let _ = wait_with_timeout(&mut child, Some(Duration::from_secs(5)));
            }
            Err(std::io::Error::other(format!(
                "Bubblewrap: failed to prepare PTY output draining: {error}"
            )))
        } else if let Some(error) = self.inner.termination_error.as_ref() {
            Err(std::io::Error::other(error.clone()))
        } else if self.inner.timed_out {
            Err(std::io::Error::new(
                std::io::ErrorKind::TimedOut,
                "Bubblewrap: script timed out",
            ))
        } else {
            match self.await_outcome() {
                BwrapOutcome::Exited(status) => Ok(status.code().unwrap_or(-1)),
                BwrapOutcome::ProviderLost => {
                    let _ = self.kill();
                    let _ = self.inner.lock_child().wait();
                    Err(std::io::Error::other(self.lost_provider_message()))
                }
                BwrapOutcome::Timeout => {
                    // Tree-kill so descendants die too and release any stdout/stderr
                    // pipe write-ends (else the drain threads below could block).
                    // `kill()` group-kills in Pipes mode; in Inherit mode it kills
                    // bwrap, which `--die-with-parent` turns into a full teardown.
                    if let Err(error) = self.kill_for_timeout() {
                        let error =
                            format!("Bubblewrap: script timed out, and teardown failed: {error}");
                        self.inner.termination_error = Some(error.clone());
                        Err(std::io::Error::other(error))
                    } else {
                        self.inner.timed_out = true;
                        Err(std::io::Error::new(
                            std::io::ErrorKind::TimedOut,
                            "Bubblewrap: script timed out",
                        ))
                    }
                }
                BwrapOutcome::Io(error) => {
                    // The child may still be alive; kill+reap it before
                    // `run_teardown()` removes the iptables/proxy enforcement out
                    // from under it.
                    let _ = self.kill();
                    let _ = self.inner.lock_child().wait();
                    Err(std::io::Error::other(format!(
                        "Bubblewrap: wait failed: {error}"
                    )))
                }
            }
        };

        cancel_and_join_discard(stdout_thread, &self.inner.stdout_canceller);
        cancel_and_join_discard(stderr_thread, &self.inner.stderr_canceller);
        if let Some(pty) = self.inner.pty.as_ref() {
            pty.finish_native_bridge();
        }
        cancel_and_join_discard(pty_thread, &pty_canceller);
        self.run_teardown();
        result
    }
}

impl Drop for BubblewrapSandboxProcess {
    fn drop(&mut self) {
        // Disarmed first so the monitor cannot be holding the child lock while
        // the reap below waits on it.
        self.inner.monitor.take();
        // Kill and reap the child *before* removing network enforcement —
        // otherwise an abandoned-but-running sandbox would keep egressing after
        // its iptables/proxy rules were torn down, and the child would leak as
        // a zombie. `kill()` group-kills in `Pipes` mode and relies on
        // `--die-with-parent` otherwise, then we reap.
        let _ = self.kill();
        let _ = self.inner.lock_child().wait();
        self.run_teardown();
    }
}

/// Tear down the proxy network namespace against the caller's logger.
///
/// `Drop` would also stop it, but only through a throwaway in-memory logger, so
/// warnings about slirp needing forced termination are lost on exactly the
/// startup paths that already failed.
fn stop_proxy_network(
    network: &mut Option<proxy_network::ProxyNetworkNamespace>,
    logger: &mut Logger,
) {
    if let Some(mut network) = network.take() {
        network.stop(logger);
    }
}

/// Outcome of resolving and classifying `deniedPaths` in a single pass.
struct DeniedPlan {
    /// Rewritten denied-path list. `Some` only when at least one entry differs
    /// from the input (a symlink was resolved to its real target), so the caller
    /// can skip cloning the request in the common no-symlink case.
    paths: Option<Vec<String>>,
    /// Subset of the (final) denied paths that must be masked as files with
    /// `--ro-bind /dev/null`; every other denied path is masked with `--tmpfs`.
    files: HashSet<String>,
}

/// Resolve every `deniedPaths` entry that traverses a symlink to its real host
/// path **and** classify each entry as a file- or directory-mask, in a single
/// pass (one `symlink_metadata` stat per entry).
///
/// Resolution (via [`resolve_through_symlinks`]) is required because bwrap masks
/// by mounting over `DEST`; if any component of `DEST` is a host symlink bound
/// into the sandbox, bwrap aborts with an opaque `ENOENT`. Masking the resolved
/// real path avoids that and still hides the object. Classification is folded in
/// here because it must observe the *resolved* path (a symlink-to-dir must be
/// `--tmpfs`, not `/dev/null`).
///
/// Fails closed if a resolved path is not valid UTF-8: the `String`-based bwrap
/// arg pipeline can't represent it faithfully, and a lossy replacement would mask
/// the wrong path and leave the target exposed.
///
/// An entry still a symlink after resolution (dangling/unresolvable) is kept and
/// file-masked with `/dev/null` — nothing resolvable is behind it to leak, and
/// bwrap tolerates `/dev/null` over a symlink node (whereas `--tmpfs` aborts).
fn resolve_denied_paths(
    policy: &crate::mxc_common::models::ContainerPolicy,
    logger: &mut Logger,
) -> Result<DeniedPlan, String> {
    let mut changed = false;
    let mut out = Vec::with_capacity(policy.denied_paths.len());
    let mut files = HashSet::new();
    for p in &policy.denied_paths {
        if let Some(resolved) = resolve_through_symlinks(Path::new(p)) {
            let resolved = resolved.to_str().ok_or_else(|| {
                format!(
                    "Bubblewrap: deniedPaths entry '{p}' resolves to a non-UTF-8 host path that \
                     cannot be safely masked; refusing to start."
                )
            })?;
            if resolved != p.as_str() {
                let _ = writeln!(
                    logger,
                    "Bubblewrap: deniedPaths entry '{p}' resolves through a symlink; masking \
                     its real path '{resolved}' instead."
                );
                if is_file_mask_target(resolved) {
                    files.insert(resolved.to_owned());
                }
                out.push(resolved.to_owned());
                changed = true;
                continue;
            }
        }
        // Not rewritten: a real path, a rooted not-yet-existing path, or a
        // dangling symlink. Classify by stat'ing the entry itself.
        if is_file_mask_target(p) {
            files.insert(p.clone());
        }
        out.push(p.clone());
    }
    Ok(DeniedPlan {
        paths: changed.then_some(out),
        files,
    })
}

/// Classify a denied path for masking: `true` = `--ro-bind /dev/null` (file),
/// `false` = `--tmpfs` (directory). A real directory and any path that cannot be
/// stat'd are directory-masked; regular files and symlink nodes are file-masked.
fn is_file_mask_target(path: &str) -> bool {
    std::fs::symlink_metadata(path)
        .map(|md| !md.file_type().is_dir())
        .unwrap_or(false)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mxc_common::models::{ProxyAddress, ProxyConfig};

    fn base_request() -> ExecutionRequest {
        ExecutionRequest {
            script_code: "echo hi".into(),
            ..Default::default()
        }
    }

    #[test]
    fn resolver_hint_is_retained_for_unmatched_absolute_targets() {
        use crate::mxc_common::logger::Mode;

        let mut request = base_request();
        let mut logger = Logger::new(Mode::Buffer);
        let target = || Some(PathBuf::from("/opt/private/resolv.conf"));
        warn_resolver_mount_hint_with(&request, &mut logger, |_| target());
        assert_eq!(logger.warnings().len(), 1);
        assert!(logger.warnings()[0].contains("no known mount destination"));
        assert!(logger.warnings()[0].contains("filesystem.readonlyPaths"));
        assert!(logger.get_buffer().is_empty());

        let mut logger = Logger::new(Mode::Buffer);
        warn_resolver_mount_hint_with(&request, &mut logger, |_| None);
        warn_resolver_mount_hint_with(&request, &mut logger, |_| {
            Some(PathBuf::from("relative/resolv.conf"))
        });
        warn_resolver_mount_hint_with(&request, &mut logger, |_| {
            Some(PathBuf::from("/run/systemd/resolve/resolv.conf"))
        });
        assert!(logger.warnings().is_empty());

        request.policy.readonly_paths = vec!["/other".into()];
        warn_resolver_mount_hint_with(&request, &mut logger, |_| target());
        assert_eq!(logger.warnings().len(), 1);
        let mut logger = Logger::new(Mode::Buffer);
        request.policy.denied_paths = vec!["/etc".into()];
        warn_resolver_mount_hint_with(&request, &mut logger, |_| target());
        assert!(logger.warnings().is_empty());
    }

    #[test]
    fn resolver_hint_checks_real_link_shapes_without_changing_host_etc() {
        use crate::mxc_common::logger::Mode;
        use std::os::unix::fs::symlink;

        let home = PathBuf::from(std::env::var_os("HOME").expect("HOME is required for this test"));
        assert!(home.is_absolute());
        let dir = tempfile::tempdir_in(home).unwrap();
        let target = dir.path().join("resolver-target");
        std::fs::write(&target, "nameserver 127.0.0.1\n").unwrap();
        let link = dir.path().join("resolv.conf");
        symlink(&target, &link).unwrap();
        let warn = |request: &ExecutionRequest, logger: &mut Logger| {
            warn_resolver_mount_hint_with(request, logger, |_| std::fs::read_link(&link).ok());
        };
        let mut request = base_request();
        let mut logger = Logger::new(Mode::Buffer);
        warn(&request, &mut logger);
        assert_eq!(logger.warnings().len(), 1);
        assert!(logger.warnings()[0].contains(&target.display().to_string()));
        assert!(logger.warnings()[0].contains("filesystem.readonlyPaths"));
        assert!(logger.get_buffer().is_empty());

        request.policy.readonly_paths = vec!["/other".into()];
        let mut unrelated_logger = Logger::new(Mode::Buffer);
        warn(&request, &mut unrelated_logger);
        assert_eq!(unrelated_logger.warnings().len(), 1);

        request.policy.readonly_paths = vec![dir.path().display().to_string()];
        let mut covered_logger = Logger::new(Mode::Buffer);
        warn(&request, &mut covered_logger);
        assert!(covered_logger.warnings().is_empty());

        request.policy.readonly_paths.clear();
        std::fs::remove_file(&link).unwrap();
        std::fs::write(&link, "nameserver 127.0.0.1\n").unwrap();
        let mut regular_logger = Logger::new(Mode::Buffer);
        warn(&request, &mut regular_logger);
        assert!(regular_logger.warnings().is_empty());

        std::fs::remove_file(&link).unwrap();
        let chained = dir.path().join("chained");
        symlink(&target, &chained).unwrap();
        let directory = dir.path().join("directory");
        std::fs::create_dir(&directory).unwrap();
        for (name, link_target, expected_hints) in [
            ("relative", PathBuf::from("resolver-target"), 0),
            ("chained", chained, 1),
            ("dangling", dir.path().join("missing"), 1),
            ("directory", directory, 1),
        ] {
            symlink(link_target, &link).unwrap();
            let mut hint_logger = Logger::new(Mode::Buffer);
            warn(&request, &mut hint_logger);
            assert_eq!(hint_logger.warnings().len(), expected_hints, "{name}");
            std::fs::remove_file(&link).unwrap();
        }
    }

    #[test]
    fn cwd_mount_hint_is_retained_for_unmatched_paths() {
        use crate::mxc_common::logger::Mode;

        let mut request = base_request();
        request.working_directory = "/home/user".into();
        let mut logger = Logger::new(Mode::Buffer);
        warn_cwd_mount_hint(&request, &mut logger);
        assert_eq!(logger.warnings().len(), 1);
        assert!(logger.warnings()[0].contains("no apparent covering mount"));
        assert!(logger.warnings()[0].contains("filesystem.readonlyPaths"));
        assert!(logger.get_buffer().is_empty());

        request.policy.readonly_paths = vec!["/other".into()];
        let mut unrelated_logger = Logger::new(Mode::Buffer);
        warn_cwd_mount_hint(&request, &mut unrelated_logger);
        assert_eq!(unrelated_logger.warnings().len(), 1);
        request.policy.readonly_paths = vec!["/home/user/data".into()];
        let mut covered_logger = Logger::new(Mode::Buffer);
        warn_cwd_mount_hint(&request, &mut covered_logger);
        assert!(covered_logger.warnings().is_empty());
    }

    /// A request carrying `alice:hunter2@` in its proxy URL, built the way a
    /// programmatic caller would rather than through the JSON parser.
    fn request_with_a_credential_bearing_proxy() -> ExecutionRequest {
        let mut req = base_request();
        req.policy.network_proxy = ProxyConfig {
            address: Some(ProxyAddress::from_url(
                "http://alice:hunter2@proxy.example.com:3128",
                "proxy.example.com".into(),
                3128,
            )),
        };
        req
    }

    /// Every declared feature must be matched by a backend-local refusal of
    /// the values it cannot honor.
    ///
    /// Declaring `INGRESS_DEFAULT`/`HOST_LOOPBACK` tells shared validation to
    /// stop checking those fields. That is the point — it is what lets the
    /// honorable deny posture through — but it also means an unsupported value
    /// now reaches the runner unchallenged. This asserts both halves at once:
    /// shared validation waves the allow posture through *because* of the
    /// declaration, and the backend gate is what stops it. Delete either half
    /// and this fails, which is the fail-open the two-part change exists to
    /// prevent.
    #[test]
    fn every_declared_inbound_feature_has_a_backend_refusal_behind_it() {
        use crate::mxc_common::models::{NetworkAction, NetworkEgressPolicy, NetworkIngressPolicy};

        use crate::bwrap_common::bwrap_command::{
            BWRAP_HOST_LOOPBACK_ALLOW, BWRAP_INGRESS_DEFAULT_ALLOW,
        };

        let runner = BubblewrapScriptRunner::new();
        let support = runner.network_policy_support();

        for (default, host_loopback) in [
            (NetworkAction::Allow, NetworkAction::Deny),
            (NetworkAction::Deny, NetworkAction::Allow),
        ] {
            let mut request = base_request();
            request.policy.network_egress = Some(NetworkEgressPolicy::default());
            request.policy.network_ingress = Some(NetworkIngressPolicy {
                default,
                host_loopback,
            });

            assert!(
                validate_network_policy_support(&request, support).is_ok(),
                "shared validation should defer to the declaration \
                 (default={default:?} hostLoopback={host_loopback:?})"
            );
            // Through `validate`, not the gate function directly: this must
            // also fail if the gate is written but never wired in. The
            // rejection sits ahead of the `bwrap` probe, so the verdict is the
            // same on a host without bwrap installed.
            let refusal = runner
                .validate(&request)
                .expect_err("the backend must refuse what the declaration stopped checking");
            assert!(
                refusal.error_message == BWRAP_INGRESS_DEFAULT_ALLOW
                    || refusal.error_message == BWRAP_HOST_LOOPBACK_ALLOW,
                "unexpected refusal for default={default:?} \
                 hostLoopback={host_loopback:?}: {}",
                refusal.error_message
            );
        }
    }

    /// Every `NetworkPolicySupport` bit must be a deliberate decision, proven
    /// against a config the shared gate actually rules on.
    ///
    /// The bits are a hand-written declaration: nothing derives them from the
    /// fields this backend really consumes, so a bit that was simply never
    /// added reads exactly like one that was considered and refused. Both
    /// produce the same clean rejection. That is how `RUNTIME_PROXY` stayed
    /// undeclared while the backend had full proxy support the whole time --
    /// no test could see the difference, because there was nothing to compare
    /// the declaration against.
    ///
    /// This closes that gap from both sides. The union assertion means a newly
    /// added bit fails here until someone categorizes it, so the decision can
    /// no longer be made by omission. Each entry is then proven against a
    /// request that exercises its field: declared bits must be accepted,
    /// undeclared ones must be refused by name. Every probe is also run against
    /// `ALL`, so a probe that stops reaching its gate fails loudly instead of
    /// quietly passing for the wrong reason.
    #[test]
    fn every_network_policy_support_bit_is_a_deliberate_decision() {
        use crate::mxc_common::models::{
            NetworkAction, NetworkEgressPolicy, NetworkIngressPolicy, NetworkRule,
        };

        use crate::bwrap_common::network_rules::{
            render_filter_payloads, EgressPlan, IngressPlan, RuleFamily,
        };

        // A directional posture makes the gates that key off it fire. Both
        // sections are always set, because that is the only shape the parser
        // produces (`apply_directional_network` fills them in together).
        fn directional() -> ExecutionRequest {
            let mut request = base_request();
            request.policy.network_mode_specified = true;
            request.policy.network_egress = Some(NetworkEgressPolicy::default());
            request.policy.network_ingress = Some(NetworkIngressPolicy::default());
            request
        }

        // (bit, declared, a request that exercises it, the field it names,
        //  a probe proving the field reaches enforcement)
        //
        // Acceptance alone proved nothing: `HOST_LOOPBACK` sat here declared
        // and accepted for its whole unenforced life. Each probe flips the
        // field in a copy of its request and requires the rendered chain to
        // change, so an over-declared bit fails here.
        type Probe = fn(&ExecutionRequest) -> Result<(), String>;

        /// The rendered v4 payload, exactly as the supervisor would install it.
        fn chain_payload(request: &ExecutionRequest) -> Vec<String> {
            let egress =
                EgressPlan::for_request(request).expect("the probe requests are all renderable");
            let ingress = IngressPlan::for_policy(&request.policy);
            render_filter_payloads(
                &egress,
                &ingress,
                RuleFamily::V4,
                "MXC_EGRESS",
                "MXC_INGRESS",
            )
        }

        /// Requires the rendered chain to change, naming what was flipped.
        fn must_differ(
            flipped: &str,
            before: Vec<String>,
            after: Vec<String>,
        ) -> Result<(), String> {
            if before == after {
                return Err(format!(
                    "flipping {flipped} left the rendered chain unchanged: {before:?}"
                ));
            }
            Ok(())
        }

        let egress_default: Probe = |request| {
            let mut flipped = request.clone();
            flipped.policy.network_egress = Some(NetworkEgressPolicy {
                default: NetworkAction::Allow,
                ..request.policy.network_egress.clone().unwrap_or_default()
            });
            must_differ(
                "egress.default",
                chain_payload(request),
                chain_payload(&flipped),
            )
        };

        let egress_rules: Probe = |request| {
            let mut flipped = request.clone();
            flipped.policy.network_egress = Some(NetworkEgressPolicy {
                allow: Vec::new(),
                deny: Vec::new(),
                ..request.policy.network_egress.clone().unwrap_or_default()
            });
            must_differ(
                "the egress rule lists",
                chain_payload(request),
                chain_payload(&flipped),
            )
        };

        let ingress_default: Probe = |request| {
            let mut flipped = request.clone();
            flipped.policy.network_ingress = Some(NetworkIngressPolicy {
                default: NetworkAction::Allow,
                ..request.policy.network_ingress.clone().unwrap_or_default()
            });
            must_differ(
                "ingress.default",
                chain_payload(request),
                chain_payload(&flipped),
            )
        };

        let host_loopback: Probe = |request| {
            let mut flipped = request.clone();
            flipped.policy.network_ingress = Some(NetworkIngressPolicy {
                host_loopback: NetworkAction::Allow,
                ..request.policy.network_ingress.clone().unwrap_or_default()
            });
            must_differ(
                "ingress.hostLoopback",
                chain_payload(request),
                chain_payload(&flipped),
            )
        };

        let runtime_proxy: Probe = |request| {
            let address = request
                .policy
                .network_proxy
                .address
                .clone()
                .ok_or("the runtime-proxy probe carries no proxy address")?;
            // The claim the bit makes is that a `runtimeConfig.networkProxy`
            // lands on the proxy machinery this backend already enforces:
            // the env injection, and no refusal on the way there.
            if crate::bwrap_common::bwrap_command::proxy_with_egress_rejection(request).is_some() {
                return Err(
                    "the runtime-proxy shape is refused before it reaches the proxy".into(),
                );
            }
            must_differ(
                "the proxy address",
                crate::bwrap_common::bwrap_command::build_args(request, Some(&address)),
                crate::bwrap_common::bwrap_command::build_args(request, None),
            )
        };

        let decisions: [(
            NetworkPolicySupport,
            bool,
            ExecutionRequest,
            &str,
            Option<Probe>,
        ); 6] = [
            (
                NetworkPolicySupport::EGRESS_DEFAULT,
                true,
                directional(),
                "network.egress.default",
                Some(egress_default),
            ),
            (
                NetworkPolicySupport::EGRESS_RULES,
                true,
                {
                    let mut request = directional();
                    request.policy.network_egress = Some(NetworkEgressPolicy {
                        allow: vec![NetworkRule::default()],
                        ..Default::default()
                    });
                    request
                },
                "network.egress allow/deny rules",
                Some(egress_rules),
            ),
            (
                NetworkPolicySupport::INGRESS_DEFAULT,
                true,
                directional(),
                "network.ingress.default",
                Some(ingress_default),
            ),
            (
                NetworkPolicySupport::HOST_LOOPBACK,
                true,
                directional(),
                "network.ingress.hostLoopback",
                Some(host_loopback),
            ),
            (
                // The normalized runtime proxy reaches the private-namespace
                // proxy-only enforcement path.
                NetworkPolicySupport::RUNTIME_PROXY,
                true,
                {
                    let mut request = directional();
                    request.policy.network_egress = Some(NetworkEgressPolicy {
                        default: NetworkAction::Deny,
                        ..Default::default()
                    });
                    request.policy.runtime_network_proxy_specified = true;
                    request.policy.network_proxy = ProxyConfig {
                        address: Some(ProxyAddress::new("127.0.0.1".to_string(), 3128)),
                    };
                    request
                },
                "runtimeConfig.networkProxy",
                Some(runtime_proxy),
            ),
            (
                // Undeclared: a ProcessContainer concept with no Bubblewrap
                // equivalent, so shared validation must keep refusing it.
                NetworkPolicySupport::PROXY_PEER_IDENTITY,
                false,
                {
                    let mut request = base_request();
                    request.policy.allowed_proxy_peer = Some("Contoso.Proxy_123".to_string());
                    request
                },
                "processContainer.network.allowedProxyPeer",
                None,
            ),
        ];

        // A bit added to `ALL` but not categorized above fails here.
        let categorized = decisions
            .iter()
            .fold(NetworkPolicySupport::default(), |acc, (bit, ..)| acc | *bit);
        assert!(
            categorized.contains(NetworkPolicySupport::ALL)
                && NetworkPolicySupport::ALL.contains(categorized),
            "a NetworkPolicySupport bit is missing from this table -- decide whether \
             Bubblewrap declares it, and prove that decision with a probe here"
        );

        let support = BubblewrapScriptRunner::new().network_policy_support();

        for (bit, declared, request, field, probe) in decisions {
            assert!(
                validate_network_policy_support(&request, NetworkPolicySupport::ALL).is_ok(),
                "the probe for {field} no longer reaches its gate, so it proves nothing"
            );
            assert_eq!(
                support.contains(bit),
                declared,
                "the declaration for {field} disagrees with this table"
            );

            match validate_network_policy_support(&request, support) {
                Ok(()) => assert!(declared, "{field} is accepted but recorded as undeclared"),
                Err(error) => {
                    assert!(
                        !declared,
                        "{field} is declared but was refused: {}",
                        error.error_message
                    );
                    assert!(
                        error.error_message.contains(field),
                        "the refusal for {field} names something else: {}",
                        error.error_message
                    );
                }
            }

            match (declared, probe) {
                (true, Some(probe)) => probe(&request)
                    .unwrap_or_else(|reason| panic!("{field} is over-declared: {reason}")),
                (true, None) => panic!(
                    "{field} is declared with no enforcement probe -- acceptance is not \
                     evidence that anything acts on the field"
                ),
                (false, Some(_)) => panic!(
                    "{field} is undeclared, so there is nothing for an enforcement probe \
                     to prove; the refusal assertion above already covers it"
                ),
                (false, None) => {}
            }
        }
    }

    #[test]
    fn an_ipv6_allow_warns_that_it_cannot_carry_traffic() {
        use crate::mxc_common::models::{NetworkEgressPolicy, NetworkPeer, NetworkRule};

        let allow = |cidr: &str| NetworkRule {
            to: vec![NetworkPeer {
                cidr: cidr.parse().expect("test CIDR"),
                except: vec![],
            }],
            ports: vec![],
        };
        let mut req = base_request();
        req.policy.network_egress = Some(NetworkEgressPolicy {
            allow: vec![allow("2001:db8::1/128"), allow("203.0.113.5/32")],
            ..Default::default()
        });
        let plan =
            network_rules::EgressPlan::for_request(&req).expect("both literals are enforceable");

        let mut logger = Logger::new(crate::mxc_common::logger::Mode::Buffer);
        warn_unreachable_v6_targets(&plan, &mut logger);
        let out = logger.warnings().join("\n");
        assert!(
            out.contains("2001:db8::1/128"),
            "must name the v6 target: {out}"
        );
        assert!(
            !out.contains("203.0.113.5"),
            "must not name the reachable v4 target: {out}"
        );
        // The retained-warning channel is the point: the debug buffer is not
        // read back by `mxc_engine::spawn_execution_request`, so a warning left
        // there is silent.
        assert!(
            logger.get_buffer().is_empty(),
            "the warning must travel as a retained warning, not as buffer output"
        );

        // Nothing unreachable, nothing to say.
        let mut v4_only = base_request();
        v4_only.policy.network_egress = Some(NetworkEgressPolicy {
            allow: vec![allow("203.0.113.5/32")],
            ..Default::default()
        });
        let plan =
            network_rules::EgressPlan::for_request(&v4_only).expect("a v4 literal is enforceable");
        let mut logger = Logger::new(crate::mxc_common::logger::Mode::Buffer);
        warn_unreachable_v6_targets(&plan, &mut logger);
        assert!(logger.warnings().is_empty(), "no v6 allow, no warning");
    }

    /// A plan whose only allow rule opens `cidr` on `ports`.
    fn plan_allowing(
        cidr: &str,
        ports: Vec<crate::mxc_common::models::NetworkPort>,
    ) -> network_rules::EgressPlan {
        use crate::mxc_common::models::{NetworkEgressPolicy, NetworkPeer, NetworkRule};

        let mut req = base_request();
        req.policy.network_egress = Some(NetworkEgressPolicy {
            allow: vec![NetworkRule {
                to: vec![NetworkPeer {
                    cidr: cidr.parse().expect("test CIDR"),
                    except: vec![],
                }],
                ports,
            }],
            ..Default::default()
        });
        network_rules::EgressPlan::for_request(&req).expect("the allowlist is enforceable")
    }

    fn dns_port(
        protocol: crate::mxc_common::models::NetworkProtocol,
    ) -> Vec<crate::mxc_common::models::NetworkPort> {
        vec![crate::mxc_common::models::NetworkPort {
            protocol,
            port: Some(53),
            end_port: None,
        }]
    }

    /// An allowlist that never names slirp's forwarder leaves the pinned
    /// resolver unreachable, which the sandbox cannot report for itself: the
    /// resolver file looks correct and the query is simply dropped.
    #[test]
    fn a_pinned_resolver_the_chain_drops_is_warned_about() {
        let plan = plan_allowing("140.82.112.0/20", vec![]);

        let mut logger = Logger::new(crate::mxc_common::logger::Mode::Buffer);
        warn_resolver_blocked_by_egress(&plan, true, &mut logger);
        let out = logger.warnings().join("\n");
        assert!(out.contains("10.0.2.3"), "must name the forwarder: {out}");
        assert!(
            out.contains("53") && out.contains("any"),
            "must name the rule the caller needs: {out}"
        );
        assert!(
            logger.get_buffer().is_empty(),
            "the warning must travel as a retained warning, not as buffer output"
        );

        // A host whose resolver already works is never pinned, so there is
        // nothing to warn about however closed the chain is.
        let mut unpinned = Logger::new(crate::mxc_common::logger::Mode::Buffer);
        warn_resolver_blocked_by_egress(&plan, false, &mut unpinned);
        assert!(
            unpinned.warnings().is_empty(),
            "no pin, no warning: {:?}",
            unpinned.warnings()
        );
    }

    /// A resolver falls back to TCP for an answer that does not fit a datagram,
    /// and `options use-vc` -- which the pin carries over -- makes it use TCP
    /// for everything, so a UDP-only rule still loses names.
    #[test]
    fn a_udp_only_rule_is_warned_about_for_tcp() {
        use crate::mxc_common::models::NetworkProtocol;

        let plan = plan_allowing("10.0.2.3/32", dns_port(NetworkProtocol::Udp));

        let mut logger = Logger::new(crate::mxc_common::logger::Mode::Buffer);
        warn_resolver_blocked_by_egress(&plan, true, &mut logger);
        let out = logger.warnings().join("\n");
        assert!(
            out.contains("TCP"),
            "a UDP-only rule must still report TCP as blocked: {out}"
        );
    }

    /// Warning at a caller who already opened the forwarder would send them to
    /// change a rule that is already correct.
    #[test]
    fn a_pinned_resolver_the_chain_admits_is_not_warned_about() {
        use crate::mxc_common::models::NetworkProtocol;

        let plan = plan_allowing("10.0.2.3/32", dns_port(NetworkProtocol::Any));

        let mut logger = Logger::new(crate::mxc_common::logger::Mode::Buffer);
        warn_resolver_blocked_by_egress(&plan, true, &mut logger);
        assert!(
            logger.warnings().is_empty(),
            "the chain admits DNS on both protocols, so there is nothing to report: {:?}",
            logger.warnings()
        );
    }

    /// Proxy-only mode rewrites the endpoint to slirp's gateway and opens
    /// exactly that address, so an endpoint neither step can express has to be
    /// refused at policy time -- before a proxy is started.
    #[test]
    fn validate_rejects_an_ipv6_loopback_proxy_endpoint_before_the_environment_probe() {
        use crate::mxc_common::models::NetworkEgressPolicy;

        let mut req = base_request();
        req.policy.network_egress = Some(NetworkEgressPolicy::default());
        req.policy.network_proxy = ProxyConfig {
            address: Some(ProxyAddress::new("[::1]".into(), 3128)),
        };

        let runner = BubblewrapScriptRunner::new();
        let err = runner
            .validate_prepared_with_probe(&req, || {
                panic!("an invalid endpoint must be rejected before probing bwrap")
            })
            .unwrap_err();

        assert!(
            err.error_message.contains("IPv6 loopback"),
            "an endpoint the gateway cannot reach must be refused by validate: {}",
            err.error_message
        );
    }

    /// The runner-level twin of the proxy/directional conflict check.
    ///
    /// The helper has its own unit test, but nothing asserted that `validate`
    /// actually *calls* it: deleting the wiring left every test green while
    /// reopening the fail-open. This test fails if that call is removed.
    ///
    /// It also pins the ordering claim -- the refusal must land before the
    /// environmental `bwrap` probe, so a host without bwrap still reports the
    /// policy error rather than a missing binary.
    #[test]
    fn validate_rejects_a_directional_rule_set_combined_with_a_proxy() {
        use crate::mxc_common::models::{NetworkAction, NetworkEgressPolicy, NetworkRule};

        let unhonorable = [
            NetworkEgressPolicy {
                default: NetworkAction::Allow,
                ..Default::default()
            },
            NetworkEgressPolicy {
                default: NetworkAction::Deny,
                allow: vec![NetworkRule::default()],
                ..Default::default()
            },
        ];

        for egress in unhonorable {
            let mut req = base_request();
            req.policy.network_egress = Some(egress);
            req.policy.network_proxy.address = Some(ProxyAddress::new("127.0.0.1".into(), 3128));
            let err = BubblewrapScriptRunner::new()
                .validate_prepared_with_probe(&req, || panic!("policy must fail before probe"))
                .unwrap_err();
            assert_eq!(
                err.error_message,
                bwrap_command::BWRAP_PROXY_DIRECTIONAL_EGRESS
            );
        }
    }

    /// The proxy-only posture is the one directional shape a proxy may carry,
    /// so it must survive the guard above rather than being caught by it.
    #[test]
    fn validate_accepts_the_proxy_only_directional_posture() {
        use crate::mxc_common::models::{NetworkAction, NetworkEgressPolicy};

        let mut req = base_request();
        req.policy.network_egress = Some(NetworkEgressPolicy {
            default: NetworkAction::Deny,
            ..Default::default()
        });
        req.policy.network_proxy = ProxyConfig {
            address: Some(ProxyAddress::new("127.0.0.1".into(), 3128)),
        };

        let unavailable = bwrap_version::BwrapUnavailable::NotFound;
        let expected = unavailable.to_string();
        let err = BubblewrapScriptRunner::new()
            .validate_prepared_with_probe(&req, || Err(unavailable))
            .unwrap_err();
        assert_eq!(err.error_message, expected);
    }

    #[test]
    fn validate_rejects_a_routable_ipv6_proxy_endpoint_before_the_environment_probe() {
        use crate::mxc_common::models::NetworkEgressPolicy;

        // The egress rules are IPv4-only, so this endpoint could never be
        // opened -- `run` would discover that only after starting slirp.
        let mut req = base_request();
        req.policy.network_egress = Some(NetworkEgressPolicy::default());
        req.policy.network_proxy = ProxyConfig {
            address: Some(ProxyAddress::new("2001:db8::1".into(), 3128)),
        };

        let runner = BubblewrapScriptRunner::new();
        let err = runner
            .validate_prepared_with_probe(&req, || {
                panic!("an invalid endpoint must be rejected before probing bwrap")
            })
            .unwrap_err();

        assert!(
            err.error_message.contains("IPv4 proxy endpoint"),
            "the endpoint check must explain the unsupported IPv6 address: {}",
            err.error_message
        );
    }

    /// A hostname endpoint is reached by pinning it over `/etc/hosts`, and the
    /// pin outranks every policy mount -- so a policy that denies the file must
    /// refuse the combination rather than silently hand the file back.
    #[test]
    fn validate_rejects_a_hostname_proxy_that_would_defeat_a_denied_hosts_file() {
        use crate::mxc_common::models::NetworkEgressPolicy;

        let mut req = base_request();
        req.policy.network_egress = Some(NetworkEgressPolicy::default());
        req.policy.denied_paths = vec!["/etc/hosts".into()];
        req.policy.network_proxy = ProxyConfig {
            address: Some(ProxyAddress::new("proxy.example.com".into(), 3128)),
        };

        let runner = BubblewrapScriptRunner::new();
        let err = runner
            .validate_prepared_with_probe(&req, || {
                panic!("a denied hosts file must be rejected before probing bwrap")
            })
            .unwrap_err();

        assert!(
            err.error_message.contains("deniedPaths"),
            "the conflict must be refused at policy time: {}",
            err.error_message
        );
    }

    /// `validate` compares the denied paths as written, so a spelling that only
    /// becomes `/etc/hosts` after normalization slips past it. `spawn` builds
    /// the masks from the normalized list, so the mask lands on `/etc/hosts`
    /// and the pin -- spliced after every policy mount -- then hands the file
    /// back. Regression test: the check must also see the normalized policy.
    #[test]
    fn a_dotdot_spelling_of_a_denied_hosts_file_still_refuses_the_pin() {
        let mut req = base_request();
        req.policy.network_proxy = ProxyConfig {
            address: Some(ProxyAddress::new("proxy.example.com".into(), 3128)),
        };

        // As written, this matches nothing the check looks for.
        req.policy.denied_paths = vec!["/etc/../etc/hosts".into()];
        assert!(
            check_pin_against_denied_hosts(&req).is_ok(),
            "precondition: the written form is not recognized"
        );

        // Normalized -- what `spawn` actually builds the masks from.
        let mut normalized = req.clone();
        normalized.policy.denied_paths = vec!["/etc/hosts".into()];
        let err = check_pin_against_denied_hosts(&normalized)
            .expect_err("the normalized policy must refuse the pin");
        assert!(err.contains("deniedPaths"), "{err}");
    }

    /// The rejection is scoped to endpoints that actually produce a pin. An IP
    /// literal is reached without touching `/etc/hosts`, so denying the file
    /// stays compatible -- and that is the escape hatch the message offers.
    #[test]
    fn validate_accepts_an_ip_proxy_endpoint_alongside_a_denied_hosts_file() {
        use crate::mxc_common::models::NetworkEgressPolicy;

        let mut req = base_request();
        req.policy.network_egress = Some(NetworkEgressPolicy::default());
        req.policy.denied_paths = vec!["/etc/hosts".into()];
        req.policy.network_proxy = ProxyConfig {
            address: Some(ProxyAddress::new("127.0.0.1".into(), 3128)),
        };

        let unavailable = bwrap_version::BwrapUnavailable::NotFound;
        let expected = unavailable.to_string();
        let err = BubblewrapScriptRunner::new()
            .validate_prepared_with_probe(&req, || Err(unavailable))
            .unwrap_err();
        assert_eq!(err.error_message, expected);
    }

    #[test]
    fn validate_accepts_programmatic_directional_cidr_rules() {
        use crate::mxc_common::models::{NetworkEgressPolicy, NetworkPeer, NetworkRule};

        let mut req = base_request();
        let rule = |cidr: &str| NetworkRule {
            to: vec![NetworkPeer {
                cidr: cidr.parse().expect("test CIDR"),
                except: vec![],
            }],
            ports: vec![],
        };
        req.policy.network_egress = Some(NetworkEgressPolicy {
            allow: vec![rule("203.0.113.7/32"), rule("10.0.0.0/8")],
            deny: vec![rule("2001:db8::/32")],
            ..Default::default()
        });

        let unavailable = bwrap_version::BwrapUnavailable::NotFound;
        let expected = unavailable.to_string();
        let err = BubblewrapScriptRunner::new()
            .validate_prepared_with_probe(&req, || Err(unavailable))
            .unwrap_err();
        assert_eq!(err.error_message, expected);
    }

    #[test]
    fn validate_rejects_empty_script_before_environment_probe() {
        // Empty script_code is a user-input error and must be surfaced
        // even on hosts without bwrap installed (independent of CI image).
        let mut req = base_request();
        req.script_code = String::new();

        let runner = BubblewrapScriptRunner::new();
        let err = runner
            .validate_prepared_with_probe(&req, || {
                panic!("environment probe must not run for invalid input")
            })
            .unwrap_err();
        assert!(err.error_message.contains("script_code is empty"));
    }

    #[test]
    fn validate_rejects_a_credential_bearing_proxy_url_before_the_environment_probe() {
        // The parser's credential guard only covers requests the parser built.
        // `ExecutionRequest` and `ProxyAddress::from_url` are both public, so a
        // caller can hand this runner a proxy URL the parser never saw --
        // `to_url` returns it verbatim and `build_args` turns it into a
        // `bwrap --setenv HTTP_PROXY <url>` argument, which any local user can
        // read out of /proc/<pid>/cmdline while the sandbox runs.
        //
        // Like the empty-script check this is user input, so it has to be
        // reported ahead of the bwrap probe: on a host with no bwrap installed
        // a later guard would return the probe's error instead of this one.
        let runner = BubblewrapScriptRunner::new();
        let err = runner
            .validate(&request_with_a_credential_bearing_proxy())
            .unwrap_err();

        assert!(
            err.error_message.contains("must not carry credentials"),
            "a credential-bearing proxy URL must be refused before it can reach \
             `bwrap --setenv`, but validate said: {}",
            err.error_message
        );
    }

    #[test]
    fn the_credential_rejection_does_not_repeat_the_password_it_rejects() {
        // An error message is logged and returned to the caller, so quoting the
        // URL verbatim would publish the secret the guard exists to protect.
        let runner = BubblewrapScriptRunner::new();
        let err = runner
            .validate(&request_with_a_credential_bearing_proxy())
            .unwrap_err();

        assert!(
            !err.error_message.contains("hunter2"),
            "the rejection leaked the password it was rejecting: {}",
            err.error_message
        );
    }

    #[test]
    fn validate_surfaces_every_environment_probe_failure() {
        let request = base_request();
        let failures = [
            bwrap_version::BwrapUnavailable::NotFound,
            bwrap_version::BwrapUnavailable::ProbeFailed {
                status: Some(126),
                detail: "permission denied".to_string(),
            },
            bwrap_version::BwrapUnavailable::UnrecognizedVersion("junk".to_string()),
            bwrap_version::BwrapUnavailable::TooOld(bwrap_version::BwrapVersion::new(0, 4, 1)),
        ];

        for failure in failures {
            let expected = failure.to_string();
            let error = BubblewrapScriptRunner::new()
                .validate_prepared_with_probe(&request, || Err(failure))
                .unwrap_err();
            assert_eq!(error.error_message, expected);
        }
    }

    /// A denied symlink pointing at a **directory** is rewritten to its canonical
    /// target so the mask lands on the real directory (bwrap cannot mount a mask
    /// over a symlink whose parent is bound). The resolved directory is
    /// classified as a `--tmpfs` (directory) mask, not a file mask.
    #[cfg(unix)]
    #[test]
    fn resolve_denied_paths_rewrites_symlink_to_dir() {
        use crate::mxc_common::logger::{Logger, Mode};
        let dir = tempfile::tempdir().unwrap();
        let target = dir.path().join("real_dir");
        std::fs::create_dir(&target).unwrap();
        let link = dir.path().join("link_to_dir");
        std::os::unix::fs::symlink(&target, &link).unwrap();

        let policy = crate::mxc_common::models::ContainerPolicy {
            denied_paths: vec![link.to_string_lossy().into_owned()],
            ..Default::default()
        };

        let mut logger = Logger::new(Mode::Buffer);
        let plan = resolve_denied_paths(&policy, &mut logger).expect("must not fail closed");
        let out = plan.paths.expect("symlink must be rewritten");
        let canonical = std::fs::canonicalize(&target).unwrap();
        let canonical = canonical.to_string_lossy().into_owned();
        assert_eq!(out, vec![canonical.clone()]);
        // The link path itself must no longer appear.
        assert!(!out.contains(&link.to_string_lossy().into_owned()));
        // A directory is masked with `--tmpfs`, so it is NOT a file-mask target.
        assert!(!plan.files.contains(&canonical));
    }

    /// A denied symlink pointing at a **file** is likewise rewritten to its
    /// target and classified as a `/dev/null` (file) mask.
    #[cfg(unix)]
    #[test]
    fn resolve_denied_paths_rewrites_symlink_to_file() {
        use crate::mxc_common::logger::{Logger, Mode};
        let dir = tempfile::tempdir().unwrap();
        let target = dir.path().join("real_file.txt");
        std::fs::write(&target, b"secret").unwrap();
        let link = dir.path().join("link_to_file");
        std::os::unix::fs::symlink(&target, &link).unwrap();

        let policy = crate::mxc_common::models::ContainerPolicy {
            denied_paths: vec![link.to_string_lossy().into_owned()],
            ..Default::default()
        };

        let mut logger = Logger::new(Mode::Buffer);
        let plan = resolve_denied_paths(&policy, &mut logger).expect("must not fail closed");
        let out = plan.paths.expect("symlink must be rewritten");
        let canonical = std::fs::canonicalize(&target).unwrap();
        let canonical = canonical.to_string_lossy().into_owned();
        assert_eq!(out, vec![canonical.clone()]);
        // A regular file is masked with `/dev/null`.
        assert!(plan.files.contains(&canonical));
    }

    /// A denied path whose **ancestor** directory is a symlink (not the leaf) is
    /// also rewritten to its real path — bwrap aborts on an ancestor symlink just
    /// as it does on a leaf symlink.
    #[cfg(unix)]
    #[test]
    fn resolve_denied_paths_rewrites_ancestor_symlink() {
        use crate::mxc_common::logger::{Logger, Mode};
        let dir = tempfile::tempdir().unwrap();
        let base = std::fs::canonicalize(dir.path()).unwrap();
        let real = base.join("real");
        std::fs::create_dir_all(real.join("secret")).unwrap();
        let link = base.join("link");
        std::os::unix::fs::symlink(&real, &link).unwrap();

        // Deny .../link/secret — the leaf `secret` is a real dir, `link` is the
        // symlinked ancestor.
        let denied = link.join("secret");
        let policy = crate::mxc_common::models::ContainerPolicy {
            denied_paths: vec![denied.to_string_lossy().into_owned()],
            ..Default::default()
        };

        let mut logger = Logger::new(Mode::Buffer);
        let plan = resolve_denied_paths(&policy, &mut logger).expect("must not fail closed");
        let out = plan.paths.expect("ancestor symlink must be rewritten");
        assert_eq!(
            out,
            vec![real.join("secret").to_string_lossy().into_owned()]
        );
    }

    /// An ancestor symlink with a **not-yet-created** leaf is resolved by
    /// canonicalizing the deepest existing ancestor and re-appending the missing
    /// tail (bwrap aborts here too, and `canonicalize` alone cannot resolve it).
    #[cfg(unix)]
    #[test]
    fn resolve_denied_paths_rewrites_ancestor_symlink_missing_leaf() {
        use crate::mxc_common::logger::{Logger, Mode};
        let dir = tempfile::tempdir().unwrap();
        let base = std::fs::canonicalize(dir.path()).unwrap();
        let real = base.join("real");
        std::fs::create_dir(&real).unwrap();
        let link = base.join("link");
        std::os::unix::fs::symlink(&real, &link).unwrap();

        // Deny .../link/newfile — `newfile` does not exist yet.
        let denied = link.join("newfile");
        let policy = crate::mxc_common::models::ContainerPolicy {
            denied_paths: vec![denied.to_string_lossy().into_owned()],
            ..Default::default()
        };

        let mut logger = Logger::new(Mode::Buffer);
        let plan = resolve_denied_paths(&policy, &mut logger).expect("must not fail closed");
        let out = plan
            .paths
            .expect("ancestor symlink must be rewritten even with a missing leaf");
        assert_eq!(
            out,
            vec![real.join("newfile").to_string_lossy().into_owned()]
        );
    }

    /// Regular files, directories, and missing paths with no symlink anywhere in
    /// the path are a no-op for rewriting (`paths` is `None`, avoiding an
    /// unnecessary clone), but are still classified: the file → `/dev/null`, the
    /// directory → `--tmpfs`.
    #[cfg(unix)]
    #[test]
    fn resolve_denied_paths_noop_for_non_symlinks() {
        use crate::mxc_common::logger::{Logger, Mode};
        let dir = tempfile::tempdir().unwrap();
        // Canonicalize up front so a symlinked tempdir root (e.g. via TMPDIR)
        // doesn't spuriously trigger a rewrite — we are testing symlink-free paths.
        let base = std::fs::canonicalize(dir.path()).unwrap();
        let file = base.join("f.txt");
        std::fs::write(&file, b"x").unwrap();
        let subdir = base.join("d");
        std::fs::create_dir(&subdir).unwrap();
        let missing = base.join("does_not_exist");

        let policy = crate::mxc_common::models::ContainerPolicy {
            denied_paths: vec![
                file.to_string_lossy().into_owned(),
                subdir.to_string_lossy().into_owned(),
                missing.to_string_lossy().into_owned(),
            ],
            ..Default::default()
        };

        let mut logger = Logger::new(Mode::Buffer);
        let plan = resolve_denied_paths(&policy, &mut logger).expect("must not fail closed");
        assert!(plan.paths.is_none());
        // Classification still happens on the no-rewrite path.
        assert!(plan.files.contains(&file.to_string_lossy().into_owned()));
        assert!(!plan.files.contains(&subdir.to_string_lossy().into_owned()));
    }

    /// A **dangling** symlink cannot be resolved to a real target, so it is kept
    /// as-is and file-masked (`/dev/null`), which bwrap tolerates over a symlink
    /// node. It must not be dropped and must not be directory-masked.
    #[cfg(unix)]
    #[test]
    fn resolve_denied_paths_masks_dangling_symlink_as_file() {
        use crate::mxc_common::logger::{Logger, Mode};
        let dir = tempfile::tempdir().unwrap();
        let base = std::fs::canonicalize(dir.path()).unwrap();
        let link = base.join("dangling");
        std::os::unix::fs::symlink(base.join("nonexistent_target"), &link).unwrap();

        let policy = crate::mxc_common::models::ContainerPolicy {
            denied_paths: vec![link.to_string_lossy().into_owned()],
            ..Default::default()
        };

        let mut logger = Logger::new(Mode::Buffer);
        let plan = resolve_denied_paths(&policy, &mut logger).expect("must not fail closed");
        // Nothing was rewritten (the target does not exist).
        assert!(plan.paths.is_none());
        // The dangling link is still masked, as a file.
        assert!(plan.files.contains(&link.to_string_lossy().into_owned()));
    }

    /// A denied path that traverses `..` under a symlinked ancestor with a
    /// missing intermediate directory must fold the `..` and resolve to the real
    /// target the kernel would reach. Regression test for a `..`-dropping bug
    /// that reconstructed a bystander target (`/real/missing/secret`) and left
    /// the real denied path (`/real/secret`) exposed.
    #[cfg(unix)]
    #[test]
    fn resolve_denied_paths_folds_dotdot_under_symlinked_ancestor() {
        use crate::mxc_common::logger::{Logger, Mode};
        let dir = tempfile::tempdir().unwrap();
        let base = std::fs::canonicalize(dir.path()).unwrap();
        let real = base.join("real");
        std::fs::create_dir(&real).unwrap();
        let link = base.join("link");
        std::os::unix::fs::symlink(&real, &link).unwrap();

        // Deny .../link/missing/../secret — `missing` does not exist and the
        // `..` cancels it, so the real target is .../real/secret.
        let denied = link.join("missing").join("..").join("secret");
        let policy = crate::mxc_common::models::ContainerPolicy {
            denied_paths: vec![denied.to_string_lossy().into_owned()],
            ..Default::default()
        };

        let mut logger = Logger::new(Mode::Buffer);
        let plan = resolve_denied_paths(&policy, &mut logger).expect("must not fail closed");
        let out = plan
            .paths
            .expect("`..` under a symlinked ancestor must be rewritten");
        assert_eq!(
            out,
            vec![real.join("secret").to_string_lossy().into_owned()],
            "`..` must fold so the mask targets the real denied path, not a bystander"
        );
    }
}