nono-cli 0.44.0-alpha.1

CLI for nono capability-based sandbox
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
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
//! CLI-specific extensions for CapabilitySet
//!
//! This module provides methods to construct a CapabilitySet from CLI arguments
//! or profiles. These are CLI-specific and not part of the core library.

use crate::cli::SandboxArgs;
use crate::policy;
use crate::profile::{expand_vars, Profile};
use crate::protected_paths::{self, ProtectedRoots};
use nono::{
    AccessMode, CapabilitySet, CapabilitySource, FsCapability, NonoError, Result,
    UnixSocketCapability, UnixSocketMode,
};
use std::path::{Path, PathBuf};
use tracing::{debug, info};

/// Try to create a directory capability, warning and skipping on PathNotFound.
/// Propagates all other errors.
fn try_new_dir(path: &Path, access: AccessMode, label: &str) -> Result<Option<FsCapability>> {
    match FsCapability::new_dir(path, access) {
        Ok(cap) => Ok(Some(cap)),
        Err(NonoError::PathNotFound(_)) => {
            info!("{}: {}", label, path.display());
            Ok(None)
        }
        Err(e) => Err(e),
    }
}

/// Try to create a file capability, warning and skipping on PathNotFound.
/// Propagates all other errors.
fn try_new_file(path: &Path, access: AccessMode, label: &str) -> Result<Option<FsCapability>> {
    match FsCapability::new_file(path, access) {
        Ok(cap) => Ok(Some(cap)),
        Err(NonoError::PathNotFound(_)) => handle_missing_file_capability(path, access, label),
        Err(e) => Err(e),
    }
}

/// Try to create a single-file AF_UNIX socket capability.
///
/// Both modes accept non-existent paths:
/// - `Connect`: via the library's NotFound warn-and-skip path (useful
///   for profile grants that only exist in some environments).
/// - `ConnectBind`: via the library's parent-canonicalise fallback.
///   `bind(2)` creates the socket file, so granting before the file
///   exists is the normal workflow; the caller must separately widen
///   the implied filesystem grant to the parent directory in that
///   case — see [`add_cli_unix_socket_caps`].
fn try_new_unix_socket_file(
    path: &Path,
    mode: UnixSocketMode,
    label: &str,
) -> Result<Option<UnixSocketCapability>> {
    match UnixSocketCapability::new_file(path, mode) {
        Ok(cap) => Ok(Some(cap)),
        Err(NonoError::PathNotFound(_)) => {
            info!("{}: {}", label, path.display());
            Ok(None)
        }
        Err(e) => Err(e),
    }
}

/// Try to create a directory-scoped AF_UNIX socket capability.
fn try_new_unix_socket_dir(
    path: &Path,
    mode: UnixSocketMode,
    label: &str,
) -> Result<Option<UnixSocketCapability>> {
    match UnixSocketCapability::new_dir(path, mode) {
        Ok(cap) => Ok(Some(cap)),
        Err(NonoError::PathNotFound(_)) => {
            info!("{}: {}", label, path.display());
            Ok(None)
        }
        Err(e) => Err(e),
    }
}

/// Apply all four `--allow-unix-socket*` flag groups to `caps`.
///
/// Each flag adds a [`UnixSocketCapability`] and auto-registers the
/// implied [`FsCapability`] (CLI-side sugar per #696). The socket-level
/// grant is non-recursive (enforced by `UnixSocketCapability::covers`);
/// the fs grant for directory forms is recursive by design — that's
/// Landlock's only expressible granularity.
fn add_cli_unix_socket_caps(
    caps: &mut CapabilitySet,
    args: &SandboxArgs,
    protected_roots: &ProtectedRoots,
    allow_parent_of_protected: bool,
) -> Result<()> {
    const LBL_SOCK_FILE: &str = "Skipping non-existent unix socket (connect grant)";
    const LBL_SOCK_FILE_BIND: &str = "Skipping non-existent unix socket (connect+bind grant)";
    const LBL_SOCK_DIR: &str = "Skipping non-existent unix socket directory (connect grant)";
    const LBL_SOCK_DIR_BIND: &str =
        "Skipping non-existent unix socket directory (connect+bind grant)";
    const LBL_FS_FILE_IMPLIED: &str = "Skipping implied fs grant for non-existent unix socket";
    const LBL_FS_DIR_IMPLIED: &str =
        "Skipping implied fs grant for non-existent unix socket directory";
    const LBL_FS_DIR_IMPLIED_BIND_PARENT: &str =
        "Skipping implied fs grant on parent of pending unix socket bind path";

    for path in &args.allow_unix_socket {
        validate_requested_file(path, "CLI", protected_roots, allow_parent_of_protected)?;
        let sock_cap = try_new_unix_socket_file(path, UnixSocketMode::Connect, LBL_SOCK_FILE)?;
        if let Some(cap) = sock_cap {
            caps.add_unix_socket(cap);
            // Only register the implied fs grant when the socket grant
            // itself was accepted — otherwise we'd silently add a
            // filesystem permission on a path the user is also asking
            // to connect to, without the corresponding unix-socket
            // grant (the two must stay coupled).
            if let Some(cap) = try_new_file(path, AccessMode::Read, LBL_FS_FILE_IMPLIED)? {
                caps.add_fs(cap);
            }
        }
    }

    for path in &args.allow_unix_socket_bind {
        validate_requested_file(path, "CLI", protected_roots, allow_parent_of_protected)?;

        // Dangling symlink guard: a symlink that exists on disk but
        // whose target doesn't is NOT the normal "future socket file"
        // case — `bind(2)` would punch through the symlink to create
        // at the target, which might be a different path than the
        // operator supplied. Reject loudly; users with a legitimate
        // dangling-symlink workflow can unlink the symlink first.
        //
        // `path.symlink_metadata().is_ok()` checks the link itself,
        // `path.exists()` follows it. The combination is: link exists
        // (symlink_metadata ok) but target doesn't (exists false).
        if path.symlink_metadata().is_ok() && !path.exists() {
            return Err(NonoError::SandboxInit(format!(
                "connect+bind unix socket grant rejects dangling symlink \
                 (bind would create at the symlink's target path, which is \
                 not what operators usually intend): {}",
                path.display()
            )));
        }

        let sock_cap =
            try_new_unix_socket_file(path, UnixSocketMode::ConnectBind, LBL_SOCK_FILE_BIND)?;
        if let Some(cap) = sock_cap {
            caps.add_unix_socket(cap);
            // Implied fs grant: ReadWrite. If the path exists, grant
            // file-scoped — narrow. If it doesn't, widen to the parent
            // directory because `bind(2)` needs write on the parent to
            // create the inode. Landlock/Seatbelt can't express a
            // per-file "create at this exact path" grant; operators
            // who want tighter scope should prefer
            // `--allow-unix-socket-dir-bind` with a scoped directory.
            if path.exists() {
                if let Some(cap) = try_new_file(path, AccessMode::ReadWrite, LBL_FS_FILE_IMPLIED)? {
                    caps.add_fs(cap);
                }
            } else if let Some(parent) = path.parent() {
                if let Some(cap) = try_new_dir(
                    parent,
                    AccessMode::ReadWrite,
                    LBL_FS_DIR_IMPLIED_BIND_PARENT,
                )? {
                    caps.add_fs(cap);
                }
            }
        }
    }

    for path in &args.allow_unix_socket_dir {
        validate_requested_dir(path, "CLI", protected_roots, allow_parent_of_protected)?;
        let sock_cap = try_new_unix_socket_dir(path, UnixSocketMode::Connect, LBL_SOCK_DIR)?;
        if let Some(cap) = sock_cap {
            caps.add_unix_socket(cap);
            if let Some(cap) = try_new_dir(path, AccessMode::Read, LBL_FS_DIR_IMPLIED)? {
                caps.add_fs(cap);
            }
        }
    }

    for path in &args.allow_unix_socket_dir_bind {
        validate_requested_dir(path, "CLI", protected_roots, allow_parent_of_protected)?;
        let sock_cap =
            try_new_unix_socket_dir(path, UnixSocketMode::ConnectBind, LBL_SOCK_DIR_BIND)?;
        if let Some(cap) = sock_cap {
            caps.add_unix_socket(cap);
            if let Some(cap) = try_new_dir(path, AccessMode::ReadWrite, LBL_FS_DIR_IMPLIED)? {
                caps.add_fs(cap);
            }
        }
    }

    Ok(())
}

/// Create a profile capability for an exact path that is usually expected to be a file.
///
/// Some clients use lock directories at paths that historically held lock files
/// (for example `~/.claude.lock`). On macOS we can preserve exact-path semantics
/// for that directory by emitting a literal path rule instead of widening to a
/// recursive directory grant. On other platforms, fail closed.
fn try_new_profile_exact_path(
    path: &Path,
    access: AccessMode,
    label: &str,
    protected_roots: &ProtectedRoots,
    allow_parent_of_protected: bool,
) -> Result<Option<FsCapability>> {
    validate_requested_file(path, "Profile", protected_roots, allow_parent_of_protected)?;
    match try_new_file(path, access, label) {
        Err(NonoError::ExpectedFile(_)) => {
            handle_exact_directory_path(path, access, protected_roots, allow_parent_of_protected)
        }
        result => result,
    }
}

#[cfg(target_os = "macos")]
fn handle_exact_directory_path(
    path: &Path,
    access: AccessMode,
    protected_roots: &ProtectedRoots,
    allow_parent_of_protected: bool,
) -> Result<Option<FsCapability>> {
    validate_requested_dir(path, "Profile", protected_roots, allow_parent_of_protected)?;
    let resolved = path.canonicalize().map_err(|source| {
        if source.kind() == std::io::ErrorKind::NotFound {
            NonoError::PathNotFound(path.to_path_buf())
        } else {
            NonoError::PathCanonicalization {
                path: path.to_path_buf(),
                source,
            }
        }
    })?;

    debug!(
        "Profile exact-file path resolved as directory; granting exact macOS literal path access: {}",
        path.display()
    );

    Ok(Some(FsCapability {
        original: path.to_path_buf(),
        resolved,
        access,
        // On macOS, `is_file = true` makes Seatbelt emit a literal path rule
        // rather than a recursive subpath rule. The target may still be a
        // directory; the important property is exact-path matching.
        is_file: true,
        source: CapabilitySource::Profile,
    }))
}

#[cfg(not(target_os = "macos"))]
fn handle_exact_directory_path(
    path: &Path,
    _access: AccessMode,
    _protected_roots: &ProtectedRoots,
    _allow_parent_of_protected: bool,
) -> Result<Option<FsCapability>> {
    Err(NonoError::ExpectedFile(path.to_path_buf()))
}

#[cfg(target_os = "macos")]
fn handle_missing_file_capability(
    path: &Path,
    access: AccessMode,
    _label: &str,
) -> Result<Option<FsCapability>> {
    let cap = new_future_file_capability(path, access)?;
    debug!(
        "Granting future exact file capability on macOS for missing path: {}",
        path.display()
    );
    Ok(Some(cap))
}

#[cfg(not(target_os = "macos"))]
fn handle_missing_file_capability(
    path: &Path,
    _access: AccessMode,
    label: &str,
) -> Result<Option<FsCapability>> {
    info!("{}: {}", label, path.display());
    Ok(None)
}

#[cfg(target_os = "macos")]
fn new_future_file_capability(path: &Path, access: AccessMode) -> Result<FsCapability> {
    let absolute = if path.is_absolute() {
        path.to_path_buf()
    } else {
        std::env::current_dir().map_err(NonoError::Io)?.join(path)
    };

    Ok(FsCapability {
        original: path.to_path_buf(),
        resolved: resolve_missing_leaf_path(&absolute)?,
        access,
        is_file: true,
        source: CapabilitySource::User,
    })
}

#[cfg(target_os = "macos")]
fn resolve_missing_leaf_path(path: &Path) -> Result<PathBuf> {
    for ancestor in path.ancestors() {
        match ancestor.canonicalize() {
            Ok(mut canonical) => {
                if let Ok(relative) = path.strip_prefix(ancestor) {
                    canonical.push(relative);
                }
                return Ok(canonical);
            }
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => continue,
            Err(err) => {
                return Err(NonoError::PathCanonicalization {
                    path: path.to_path_buf(),
                    source: err,
                });
            }
        }
    }

    Err(NonoError::PathNotFound(path.to_path_buf()))
}

/// Add a platform rule to allow atomic-write temp files for a writable file.
///
/// Many tools (e.g. Claude Code) write atomically by creating a temp file
/// (`file.tmp.PID.TIMESTAMP`) then renaming it over the target. This function
/// adds a Seatbelt regex rule to permit creating/writing those temp files
/// alongside the allowed file.
#[cfg(target_os = "macos")]
fn add_atomic_write_rule(caps: &mut CapabilitySet, cap: &FsCapability) -> Result<()> {
    if !matches!(cap.access, AccessMode::Write | AccessMode::ReadWrite) {
        return Ok(());
    }

    fn add_rule_for_path(caps: &mut CapabilitySet, path: &Path) -> Result<()> {
        let path_str = path.to_str().ok_or_else(|| {
            NonoError::SandboxInit(format!(
                "non-UTF-8 path for atomic write rule: {}",
                path.display()
            ))
        })?;
        let escaped = regex_escape_path(path_str);
        let rule = format!(
            "(allow file-write* (regex #\"^{}\\.tmp\\.[0-9]+\\.[0-9]+$\"))",
            escaped
        );
        caps.add_platform_rule(&rule)
    }

    add_rule_for_path(caps, &cap.resolved)?;
    if cap.original != cap.resolved {
        add_rule_for_path(caps, &cap.original)?;
    }
    Ok(())
}

#[cfg(not(target_os = "macos"))]
fn add_atomic_write_rule(_caps: &mut CapabilitySet, _cap: &FsCapability) -> Result<()> {
    Ok(())
}

/// Escape a filesystem path for use in a Seatbelt regex.
/// Only metacharacters that could appear in typical paths need escaping.
#[cfg(target_os = "macos")]
fn regex_escape_path(path: &str) -> String {
    let mut out = String::with_capacity(path.len() + 8);
    for c in path.chars() {
        match c {
            '.' | '+' | '*' | '?' | '(' | ')' | '[' | ']' | '{' | '}' | '|' | '^' | '$' | '\\' => {
                out.push('\\');
                out.push(c);
            }
            _ => out.push(c),
        }
    }
    out
}

fn apply_profile_dir_allows(
    path_templates: &[String],
    access: AccessMode,
    workdir: &Path,
    protected_roots: &ProtectedRoots,
    caps: &mut CapabilitySet,
    label_prefix: &str,
    allow_parent_of_protected: bool,
) -> Result<()> {
    for path_template in path_templates {
        let path = expand_vars(path_template, workdir)?;
        validate_requested_dir(&path, "Profile", protected_roots, allow_parent_of_protected)?;
        let label = format!(
            "{label_prefix} '{}' does not exist, skipping",
            path_template
        );
        if let Some(mut cap) = try_new_dir(&path, access, &label)? {
            cap.source = CapabilitySource::Profile;
            caps.add_fs(cap);
        }
    }
    Ok(())
}

fn validate_requested_dir(
    path: &Path,
    source: &str,
    protected_roots: &ProtectedRoots,
    allow_parent_of_protected: bool,
) -> Result<()> {
    protected_paths::validate_requested_path_against_protected_roots(
        path,
        false,
        source,
        protected_roots.as_paths(),
        allow_parent_of_protected,
    )
}

fn validate_requested_file(
    path: &Path,
    source: &str,
    protected_roots: &ProtectedRoots,
    allow_parent_of_protected: bool,
) -> Result<()> {
    protected_paths::validate_requested_path_against_protected_roots(
        path,
        true,
        source,
        protected_roots.as_paths(),
        allow_parent_of_protected,
    )
}

pub(crate) fn default_profile_groups() -> Result<Vec<String>> {
    let profile = crate::policy::get_policy_profile("default")?
        .ok_or_else(|| NonoError::ProfileNotFound("default".to_string()))?;
    Ok(profile.security.groups)
}

#[must_use]
pub(crate) fn retains_missing_exact_file_grants() -> bool {
    cfg!(target_os = "macos")
}

/// Extension trait for CapabilitySet to add CLI-specific construction methods.
///
/// Both methods return `(CapabilitySet, bool)` where the bool indicates whether
/// `policy::apply_unlink_overrides()` must be called after all writable paths
/// are finalized (including CWD). The caller is responsible for calling it.
pub trait CapabilitySetExt {
    /// Create a capability set from CLI sandbox arguments.
    /// Returns `(caps, needs_unlink_overrides)`.
    fn from_args(args: &SandboxArgs) -> Result<(CapabilitySet, bool)>;

    /// Create a capability set from a profile with CLI overrides.
    /// Returns `(caps, needs_unlink_overrides)`.
    fn from_profile(
        profile: &Profile,
        workdir: &Path,
        args: &SandboxArgs,
    ) -> Result<(CapabilitySet, bool)>;
}

impl CapabilitySetExt for CapabilitySet {
    fn from_args(args: &SandboxArgs) -> Result<(CapabilitySet, bool)> {
        let mut caps = CapabilitySet::new();
        let protected_roots = ProtectedRoots::from_defaults()?;

        // Resolve base policy groups (system paths, deny rules, dangerous commands)
        let loaded_policy = policy::load_embedded_policy()?;
        let default_groups = default_profile_groups()?;
        let mut resolved = policy::resolve_groups(&loaded_policy, &default_groups, &mut caps)?;

        // Directory permissions (canonicalize handles existence check atomically)
        for path in &args.allow {
            validate_requested_dir(path, "CLI", &protected_roots, false)?;
            if let Some(cap) =
                try_new_dir(path, AccessMode::ReadWrite, "Skipping non-existent path")?
            {
                caps.add_fs(cap);
            }
        }

        for path in &args.read {
            validate_requested_dir(path, "CLI", &protected_roots, false)?;
            if let Some(cap) = try_new_dir(path, AccessMode::Read, "Skipping non-existent path")? {
                caps.add_fs(cap);
            }
        }

        for path in &args.write {
            validate_requested_dir(path, "CLI", &protected_roots, false)?;
            if let Some(cap) = try_new_dir(path, AccessMode::Write, "Skipping non-existent path")? {
                caps.add_fs(cap);
            }
        }

        // Single file permissions
        for path in &args.allow_file {
            validate_requested_file(path, "CLI", &protected_roots, false)?;
            if let Some(cap) =
                try_new_file(path, AccessMode::ReadWrite, "Skipping non-existent file")?
            {
                caps.add_fs(cap);
            }
        }

        for path in &args.read_file {
            validate_requested_file(path, "CLI", &protected_roots, false)?;
            if let Some(cap) = try_new_file(path, AccessMode::Read, "Skipping non-existent file")? {
                caps.add_fs(cap);
            }
        }

        for path in &args.write_file {
            validate_requested_file(path, "CLI", &protected_roots, false)?;
            if let Some(cap) = try_new_file(path, AccessMode::Write, "Skipping non-existent file")?
            {
                caps.add_fs(cap);
            }
        }

        // AF_UNIX socket capabilities (issue #685 / #696). See
        // add_cli_unix_socket_caps for the full flag handling + implied
        // fs-grant sugar.
        add_cli_unix_socket_caps(&mut caps, args, &protected_roots, false)?;

        apply_cli_network_mode(&mut caps, args);

        // Localhost IPC ports
        for port in &args.allow_port {
            caps.add_localhost_port(*port);
        }

        // Outbound TCP connect port allowlist (Linux Landlock V4+ only)
        #[cfg(target_os = "macos")]
        if !args.allow_connect_port.is_empty() {
            return Err(NonoError::UnsupportedPlatform(
                "--allow-connect-port is not supported on macOS: Seatbelt cannot filter by TCP port. \
                 Use --allow-domain for host-level filtering, or ProxyOnly mode."
                    .to_string(),
            ));
        }
        #[cfg(not(target_os = "macos"))]
        for port in &args.allow_connect_port {
            caps.add_tcp_connect_port(*port);
        }

        // Command allow/block lists
        for cmd in &args.allow_command {
            caps.add_allowed_command(cmd.clone());
        }

        for cmd in &args.block_command {
            caps.add_blocked_command(cmd);
        }

        finalize_caps(&mut caps, &mut resolved, &loaded_policy, args, &[])?;

        Ok((caps, resolved.needs_unlink_overrides))
    }

    fn from_profile(
        profile: &Profile,
        workdir: &Path,
        args: &SandboxArgs,
    ) -> Result<(CapabilitySet, bool)> {
        let mut caps = CapabilitySet::new();
        let protected_roots = ProtectedRoots::from_defaults()?;
        let allow_parent_of_protected = profile.allow_parent_of_protected.unwrap_or(false);

        // Resolve policy groups from the already-finalized profile.
        let loaded_policy = policy::load_embedded_policy()?;
        let groups = profile.security.groups.clone();
        let mut resolved = policy::resolve_groups(&loaded_policy, &groups, &mut caps)?;
        debug!("Resolved {} policy groups", resolved.names.len());

        // Process profile filesystem config (profile-specific paths on top of groups).
        // These are marked as CapabilitySource::Profile so they are displayed in
        // the banner but NOT tracked for rollback snapshots (only User-sourced paths
        // representing the project workspace are tracked).
        let fs = &profile.filesystem;

        // Directories with read+write access
        for path_template in &fs.allow {
            let path = expand_vars(path_template, workdir)?;
            validate_requested_dir(
                &path,
                "Profile",
                &protected_roots,
                allow_parent_of_protected,
            )?;
            let label = format!("Profile path '{}' does not exist, skipping", path_template);
            if let Some(mut cap) = try_new_dir(&path, AccessMode::ReadWrite, &label)? {
                cap.source = CapabilitySource::Profile;
                caps.add_fs(cap);
            }
        }

        // Read-only filesystem entries (directory or file)
        for path_template in &fs.read {
            let path = expand_vars(path_template, workdir)?;
            let label = format!("Profile path '{}' does not exist, skipping", path_template);

            let reads_file = std::fs::metadata(&path)
                .map(|metadata| !metadata.is_dir())
                .unwrap_or(false);

            let maybe_cap = if reads_file {
                validate_requested_file(
                    &path,
                    "Profile",
                    &protected_roots,
                    allow_parent_of_protected,
                )?;
                try_new_file(&path, AccessMode::Read, &label)?
            } else {
                validate_requested_dir(
                    &path,
                    "Profile",
                    &protected_roots,
                    allow_parent_of_protected,
                )?;
                try_new_dir(&path, AccessMode::Read, &label)?
            };

            if let Some(mut cap) = maybe_cap {
                cap.source = CapabilitySource::Profile;
                caps.add_fs(cap);
            }
        }

        // Directories with write-only access
        for path_template in &fs.write {
            let path = expand_vars(path_template, workdir)?;
            validate_requested_dir(
                &path,
                "Profile",
                &protected_roots,
                allow_parent_of_protected,
            )?;
            let label = format!("Profile path '{}' does not exist, skipping", path_template);
            if let Some(mut cap) = try_new_dir(&path, AccessMode::Write, &label)? {
                cap.source = CapabilitySource::Profile;
                caps.add_fs(cap);
            }
        }

        // Single files with read+write access
        for path_template in &fs.allow_file {
            let path = expand_vars(path_template, workdir)?;
            let label = format!("Profile file '{}' does not exist, skipping", path_template);
            if let Some(mut cap) = try_new_profile_exact_path(
                &path,
                AccessMode::ReadWrite,
                &label,
                &protected_roots,
                allow_parent_of_protected,
            )? {
                // Also allow atomic-write temp files (e.g. .claude.json.tmp.PID.TS).
                // Many tools write to a temp file then rename for crash safety.
                add_atomic_write_rule(&mut caps, &cap)?;
                cap.source = CapabilitySource::Profile;
                caps.add_fs(cap);
            }
        }

        // Single files with read-only access
        for path_template in &fs.read_file {
            let path = expand_vars(path_template, workdir)?;
            let label = format!("Profile file '{}' does not exist, skipping", path_template);
            if let Some(mut cap) = try_new_profile_exact_path(
                &path,
                AccessMode::Read,
                &label,
                &protected_roots,
                allow_parent_of_protected,
            )? {
                cap.source = CapabilitySource::Profile;
                caps.add_fs(cap);
            }
        }

        // Single files with write-only access
        for path_template in &fs.write_file {
            let path = expand_vars(path_template, workdir)?;
            let label = format!("Profile file '{}' does not exist, skipping", path_template);
            if let Some(mut cap) = try_new_profile_exact_path(
                &path,
                AccessMode::Write,
                &label,
                &protected_roots,
                allow_parent_of_protected,
            )? {
                add_atomic_write_rule(&mut caps, &cap)?;
                cap.source = CapabilitySource::Profile;
                caps.add_fs(cap);
            }
        }

        // AF_UNIX socket capabilities from profile (issue #685 / #696).
        //
        // Mirrors the CLI-side sugar in from_args: each unix_socket*
        // field adds a UnixSocketCapability and auto-registers the
        // implied FsCapability with matching access mode. Source is
        // marked as Profile so `--dry-run -v` can show provenance.
        for path_template in &fs.unix_socket {
            let path = expand_vars(path_template, workdir)?;
            validate_requested_file(
                &path,
                "Profile",
                &protected_roots,
                allow_parent_of_protected,
            )?;
            let label = format!(
                "Profile unix socket '{}' does not exist, skipping",
                path_template
            );
            if let Some(mut cap) = try_new_unix_socket_file(&path, UnixSocketMode::Connect, &label)?
            {
                cap.source = CapabilitySource::Profile;
                caps.add_unix_socket(cap);
                // Implied fs grant only when the socket grant itself
                // was accepted — the two must stay coupled so we never
                // add an fs grant for a path with no matching socket
                // grant.
                if let Some(mut cap) = try_new_file(&path, AccessMode::Read, &label)? {
                    cap.source = CapabilitySource::Profile;
                    caps.add_fs(cap);
                }
            }
        }

        for path_template in &fs.unix_socket_bind {
            let path = expand_vars(path_template, workdir)?;
            validate_requested_file(
                &path,
                "Profile",
                &protected_roots,
                allow_parent_of_protected,
            )?;
            // Dangling-symlink guard — see add_cli_unix_socket_caps.
            if path.symlink_metadata().is_ok() && !path.exists() {
                return Err(NonoError::SandboxInit(format!(
                    "Profile unix_socket_bind rejects dangling symlink \
                     (bind would punch through to the link target): '{}'",
                    path_template
                )));
            }
            let label = format!(
                "Profile unix socket '{}' does not exist, skipping",
                path_template
            );
            if let Some(mut cap) =
                try_new_unix_socket_file(&path, UnixSocketMode::ConnectBind, &label)?
            {
                cap.source = CapabilitySource::Profile;
                caps.add_unix_socket(cap);
                // Implied fs grant. See add_cli_unix_socket_caps for the
                // parent-widening rationale.
                if path.exists() {
                    if let Some(mut cap) = try_new_file(&path, AccessMode::ReadWrite, &label)? {
                        cap.source = CapabilitySource::Profile;
                        caps.add_fs(cap);
                    }
                } else if let Some(parent) = path.parent() {
                    if let Some(mut cap) = try_new_dir(parent, AccessMode::ReadWrite, &label)? {
                        cap.source = CapabilitySource::Profile;
                        caps.add_fs(cap);
                    }
                }
            }
        }

        for path_template in &fs.unix_socket_dir {
            let path = expand_vars(path_template, workdir)?;
            validate_requested_dir(
                &path,
                "Profile",
                &protected_roots,
                allow_parent_of_protected,
            )?;
            let label = format!(
                "Profile unix socket dir '{}' does not exist, skipping",
                path_template
            );
            if let Some(mut cap) = try_new_unix_socket_dir(&path, UnixSocketMode::Connect, &label)?
            {
                cap.source = CapabilitySource::Profile;
                caps.add_unix_socket(cap);
                if let Some(mut cap) = try_new_dir(&path, AccessMode::Read, &label)? {
                    cap.source = CapabilitySource::Profile;
                    caps.add_fs(cap);
                }
            }
        }

        for path_template in &fs.unix_socket_dir_bind {
            let path = expand_vars(path_template, workdir)?;
            validate_requested_dir(
                &path,
                "Profile",
                &protected_roots,
                allow_parent_of_protected,
            )?;
            let label = format!(
                "Profile unix socket dir '{}' does not exist, skipping",
                path_template
            );
            if let Some(mut cap) =
                try_new_unix_socket_dir(&path, UnixSocketMode::ConnectBind, &label)?
            {
                cap.source = CapabilitySource::Profile;
                caps.add_unix_socket(cap);
                if let Some(mut cap) = try_new_dir(&path, AccessMode::ReadWrite, &label)? {
                    cap.source = CapabilitySource::Profile;
                    caps.add_fs(cap);
                }
            }
        }

        // Policy patch additions
        apply_profile_dir_allows(
            &profile.policy.add_allow_readwrite,
            AccessMode::ReadWrite,
            workdir,
            &protected_roots,
            &mut caps,
            "Profile policy path",
            allow_parent_of_protected,
        )?;
        apply_profile_dir_allows(
            &profile.policy.add_allow_read,
            AccessMode::Read,
            workdir,
            &protected_roots,
            &mut caps,
            "Profile policy path",
            allow_parent_of_protected,
        )?;
        apply_profile_dir_allows(
            &profile.policy.add_allow_write,
            AccessMode::Write,
            workdir,
            &protected_roots,
            &mut caps,
            "Profile policy path",
            allow_parent_of_protected,
        )?;

        for path_template in &profile.policy.add_deny_access {
            let path = expand_vars(path_template, workdir)?;
            let path_str = path.to_str().ok_or_else(|| {
                NonoError::ConfigParse(format!(
                    "Profile policy deny path contains non-UTF-8 bytes: {}",
                    path.display()
                ))
            })?;
            policy::add_deny_access_rules(path_str, &mut caps, &mut resolved.deny_paths)?;
        }

        for cmd in &profile.policy.add_deny_commands {
            caps.add_blocked_command(cmd);
        }

        // Network blocking or proxy mode from profile
        if profile.network.block {
            caps.set_network_blocked(true);
        } else if profile.network.has_proxy_flags() {
            let bind_ports =
                crate::merge_dedup_ports(&profile.network.listen_port, &args.allow_bind);
            // Profile requests proxy mode; port 0 is a placeholder.
            // bind_ports come from profile listen_port plus CLI --listen-port.
            caps = caps.set_network_mode(nono::NetworkMode::ProxyOnly {
                port: 0,
                bind_ports,
            });
        }

        // Localhost IPC ports from profile
        for port in &profile.network.open_port {
            caps.add_localhost_port(*port);
        }

        // Outbound TCP connect port allowlist from profile (Linux Landlock V4+ only)
        #[cfg(target_os = "macos")]
        if !profile.network.connect_port.is_empty() {
            return Err(NonoError::UnsupportedPlatform(
                "profile `connect_port` is not supported on macOS: Seatbelt cannot filter by TCP \
                 port. Use `allow_domain` for host-level filtering, or ProxyOnly mode."
                    .to_string(),
            ));
        }
        #[cfg(not(target_os = "macos"))]
        for port in &profile.network.connect_port {
            caps.add_tcp_connect_port(*port);
        }

        // Apply allowed commands from profile
        for cmd in &profile.security.allowed_commands {
            caps.add_allowed_command(cmd.as_str());
        }

        // Apply signal mode from profile (None defaults to Isolated)
        let mode = profile
            .security
            .signal_mode
            .map(nono::SignalMode::from)
            .unwrap_or_default();
        caps = caps.set_signal_mode(mode);

        // Apply process inspection mode from profile (None defaults to Isolated)
        let process_info_mode = profile
            .security
            .process_info_mode
            .map(nono::ProcessInfoMode::from)
            .unwrap_or_default();
        caps.set_process_info_mode_mut(process_info_mode);

        // Apply IPC mode from profile (None defaults to SharedMemoryOnly)
        let ipc_mode = profile
            .security
            .ipc_mode
            .map(nono::IpcMode::from)
            .unwrap_or_default();
        caps.set_ipc_mode_mut(ipc_mode);

        // Apply CLI overrides (CLI args take precedence)
        add_cli_overrides(&mut caps, args, allow_parent_of_protected)?;

        // Expand profile-level override_deny paths for finalize_caps.
        // Existing override targets must fail closed in apply_deny_overrides
        // when they lack a matching user-intent grant. Non-existent paths are
        // skipped here to preserve platform-specific built-in profiles whose
        // grants are intentionally absent on other OSes.
        let mut profile_overrides = Vec::with_capacity(profile.policy.override_deny.len());
        for path_template in &profile.policy.override_deny {
            let path = expand_vars(path_template, workdir)?;
            if path.exists() {
                profile_overrides.push(path);
            }
        }

        finalize_caps(
            &mut caps,
            &mut resolved,
            &loaded_policy,
            args,
            &profile_overrides,
        )?;

        Ok((caps, resolved.needs_unlink_overrides))
    }
}

/// Shared finalization: deny overrides, overlap validation, keychain exception, dedup.
///
/// Called by both `from_args()` and `from_profile()` after all grants are added.
/// Caller must still call `apply_unlink_overrides()` after CWD and any other
/// writable paths are added, if `resolved.needs_unlink_overrides` is true.
fn finalize_caps(
    caps: &mut CapabilitySet,
    resolved: &mut policy::ResolvedGroups,
    _loaded_policy: &policy::Policy,
    args: &SandboxArgs,
    profile_override_deny: &[PathBuf],
) -> Result<()> {
    // Apply profile-level deny overrides first, then CLI overrides.
    // Profile overrides come from `policy.override_deny` in the profile JSON.
    // CLI `--override-deny` flags are applied on top.
    policy::apply_deny_overrides(profile_override_deny, &mut resolved.deny_paths, caps)?;
    policy::apply_deny_overrides(&args.override_deny, &mut resolved.deny_paths, caps)?;

    // Remove exact file grants for the deny paths that remain after overrides.
    // This lets profile deny patches override inherited file capabilities while
    // preserving `--override-deny` validation against the original grant set.
    caps.remove_exact_file_caps_for_paths(&resolved.deny_paths);

    // Validate deny/allow overlaps (hard-fail on Linux where Landlock cannot enforce denies)
    policy::validate_deny_overlaps(&resolved.deny_paths, caps)?;

    // Keep broad keychain deny groups active, but allow explicit
    // keychain DB read grants (profile/CLI) on macOS.
    policy::apply_macos_keychain_db_exception(caps);

    // Deduplicate capabilities
    caps.deduplicate();

    Ok(())
}

fn apply_cli_network_mode(caps: &mut CapabilitySet, args: &SandboxArgs) {
    if args.block_net {
        caps.set_network_blocked(true);
    } else if args.allow_net {
        caps.set_network_mode_mut(nono::NetworkMode::AllowAll);
    } else if args.has_proxy_flags() {
        // Proxy mode: port 0 is a placeholder, updated when proxy starts.
        // bind_ports are passed through allow_bind CLI flag.
        caps.set_network_mode_mut(nono::NetworkMode::ProxyOnly {
            port: 0,
            bind_ports: args.allow_bind.clone(),
        });
    }
}

/// Apply CLI argument overrides on top of existing capabilities.
///
/// CLI directory args are always added, even if the path is already covered by
/// a profile or group capability. The subsequent `deduplicate()` call resolves
/// conflicts using source priority (User wins over Group/System) and merges
/// complementary access modes (Read + Write = ReadWrite).
fn add_cli_overrides(
    caps: &mut CapabilitySet,
    args: &SandboxArgs,
    allow_parent_of_protected: bool,
) -> Result<()> {
    let protected_roots = ProtectedRoots::from_defaults()?;

    // Additional directories from CLI
    for path in &args.allow {
        validate_requested_dir(path, "CLI", &protected_roots, allow_parent_of_protected)?;
        if let Some(cap) = try_new_dir(path, AccessMode::ReadWrite, "Skipping non-existent path")? {
            caps.add_fs(cap);
        }
    }

    for path in &args.read {
        validate_requested_dir(path, "CLI", &protected_roots, allow_parent_of_protected)?;
        if let Some(cap) = try_new_dir(path, AccessMode::Read, "Skipping non-existent path")? {
            caps.add_fs(cap);
        }
    }

    for path in &args.write {
        validate_requested_dir(path, "CLI", &protected_roots, allow_parent_of_protected)?;
        if let Some(cap) = try_new_dir(path, AccessMode::Write, "Skipping non-existent path")? {
            caps.add_fs(cap);
        }
    }

    // Additional files from CLI
    for path in &args.allow_file {
        validate_requested_file(path, "CLI", &protected_roots, allow_parent_of_protected)?;
        if let Some(cap) = try_new_file(path, AccessMode::ReadWrite, "Skipping non-existent file")?
        {
            caps.add_fs(cap);
        }
    }

    for path in &args.read_file {
        validate_requested_file(path, "CLI", &protected_roots, allow_parent_of_protected)?;
        if let Some(cap) = try_new_file(path, AccessMode::Read, "Skipping non-existent file")? {
            caps.add_fs(cap);
        }
    }

    for path in &args.write_file {
        validate_requested_file(path, "CLI", &protected_roots, allow_parent_of_protected)?;
        if let Some(cap) = try_new_file(path, AccessMode::Write, "Skipping non-existent file")? {
            caps.add_fs(cap);
        }
    }

    // AF_UNIX socket capabilities from CLI overrides.
    add_cli_unix_socket_caps(caps, args, &protected_roots, allow_parent_of_protected)?;

    // CLI network flags override profile network settings.
    apply_cli_network_mode(caps, args);

    // Localhost IPC ports from CLI
    for port in &args.allow_port {
        caps.add_localhost_port(*port);
    }

    // Outbound TCP connect port allowlist from CLI (Linux Landlock V4+ only)
    #[cfg(target_os = "macos")]
    if !args.allow_connect_port.is_empty() {
        return Err(NonoError::UnsupportedPlatform(
            "--allow-connect-port is not supported on macOS: Seatbelt cannot filter by TCP port. \
             Use --allow-domain for host-level filtering, or ProxyOnly mode."
                .to_string(),
        ));
    }
    #[cfg(not(target_os = "macos"))]
    for port in &args.allow_connect_port {
        caps.add_tcp_connect_port(*port);
    }

    // Command allow/block from CLI
    for cmd in &args.allow_command {
        caps.add_allowed_command(cmd.clone());
    }

    for cmd in &args.block_command {
        caps.add_blocked_command(cmd);
    }

    Ok(())
}

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

    fn with_env_lock<T>(f: impl FnOnce() -> T) -> T {
        let _guard = match crate::test_env::ENV_LOCK.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };
        f()
    }

    fn from_args_locked(args: &SandboxArgs) -> Result<(CapabilitySet, bool)> {
        with_env_lock(|| CapabilitySet::from_args(args))
    }

    fn from_profile_locked(
        profile: &crate::profile::Profile,
        workdir: &Path,
        args: &SandboxArgs,
    ) -> Result<(CapabilitySet, bool)> {
        with_env_lock(|| CapabilitySet::from_profile(profile, workdir, args))
    }

    fn sandbox_args() -> SandboxArgs {
        SandboxArgs::default()
    }

    #[test]
    fn test_from_args_basic() {
        let dir = tempdir().expect("Failed to create temp dir");

        let args = SandboxArgs {
            allow: vec![dir.path().to_path_buf()],
            ..sandbox_args()
        };

        let (caps, _) = from_args_locked(&args).expect("Failed to build caps");
        assert!(caps.has_fs());
        assert!(!caps.is_network_blocked());
    }

    #[test]
    fn test_from_args_network_blocked() {
        let args = SandboxArgs {
            block_net: true,
            ..sandbox_args()
        };

        let (caps, _) = from_args_locked(&args).expect("Failed to build caps");
        assert!(caps.is_network_blocked());
    }

    #[test]
    fn test_from_args_with_commands() {
        let args = SandboxArgs {
            override_deny: vec![],
            allow_command: vec!["rm".to_string()],
            block_command: vec!["custom".to_string()],
            ..sandbox_args()
        };

        let (caps, _) = from_args_locked(&args).expect("Failed to build caps");
        assert!(caps.allowed_commands().contains(&"rm".to_string()));
        assert!(caps.blocked_commands().contains(&"custom".to_string()));
    }

    #[test]
    fn test_from_args_rejects_protected_state_subtree() {
        with_env_lock(|| {
            let home = dirs::home_dir().expect("home");
            let protected_subtree = home.join(".nono").join("rollbacks");

            let args = SandboxArgs {
                allow: vec![protected_subtree],
                ..sandbox_args()
            };

            let err =
                CapabilitySet::from_args(&args).expect_err("must reject protected state path");
            assert!(
                err.to_string()
                    .contains("overlaps protected nono state root"),
                "unexpected error: {err}",
            );
        });
    }

    #[test]
    fn test_from_args_uses_default_profile_groups_for_runtime_policy() {
        with_env_lock(|| {
            let args = sandbox_args();
            let (caps, _) = CapabilitySet::from_args(&args).expect("build caps from args");

            let policy = crate::policy::load_embedded_policy().expect("load embedded policy");
            let default_groups = default_profile_groups().expect("get default profile groups");
            let deny_paths = crate::policy::resolve_deny_paths_for_groups(&policy, &default_groups)
                .expect("resolve deny paths");

            crate::policy::validate_deny_overlaps(&deny_paths, &caps)
                .expect("from_args caps should match default profile deny policy");
        });
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_from_args_skips_linux_temp_root_when_home_is_nested() {
        // Use `keep()` so the temp dir is NOT auto-deleted. Tests that call
        // `tempdir()` concurrently (without the env lock) may create dirs
        // inside our temp_root while TMPDIR points to it. If we deleted it,
        // those dirs would vanish and cause flaky failures.
        let temp_root = tempdir().expect("tmpdir").keep();
        let home = temp_root.join("home");
        let allowed = temp_root.join("other");
        std::fs::create_dir_all(&home).expect("create home");
        std::fs::create_dir_all(&allowed).expect("create allowed dir");

        let _guard = match crate::test_env::ENV_LOCK.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };

        let home_str = home.to_string_lossy().into_owned();
        let tmpdir_str = temp_root.to_string_lossy().into_owned();
        let _env = crate::test_env::EnvVarGuard::set_all(&[
            ("HOME", home_str.as_str()),
            ("TMPDIR", tmpdir_str.as_str()),
        ]);

        let args = SandboxArgs {
            allow: vec![allowed.clone()],
            ..sandbox_args()
        };

        let result = CapabilitySet::from_args(&args);

        let (caps, _) = result.expect(
            "from_args should succeed when HOME is nested under TMPDIR and the user grants a sibling path",
        );
        let allowed_canonical = allowed.canonicalize().expect("canonicalize allowed dir");
        assert!(
            caps.fs_capabilities()
                .iter()
                .any(|cap| !cap.is_file && cap.resolved == allowed_canonical),
            "explicit user grant under TMPDIR should still be present"
        );
    }

    #[test]
    fn test_from_profile_allowed_commands() {
        let dir = tempdir().expect("tmpdir");
        let profile_path = dir.path().join("rm-test.json");
        std::fs::write(
            &profile_path,
            r#"{
                "meta": { "name": "rm-test" },
                "filesystem": { "allow": ["/tmp"] },
                "security": { "allowed_commands": ["rm", "shred"] }
            }"#,
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");
        assert!(
            caps.allowed_commands().contains(&"rm".to_string()),
            "profile allowed_commands should include 'rm'"
        );
        assert!(
            caps.allowed_commands().contains(&"shred".to_string()),
            "profile allowed_commands should include 'shred'"
        );
    }

    #[test]
    fn test_from_profile_filesystem_read_accepts_file_paths() {
        let dir = tempdir().expect("tmpdir");
        let read_file = dir.path().join("config.txt");
        std::fs::write(&read_file, "token=123").expect("write file");

        let profile_path = dir.path().join("read-file-profile.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                "meta": {{ "name": "read-file-profile" }},
                "filesystem": {{ "read": ["{}"] }}
            }}"#,
                read_file.display()
            ),
        )
        .expect("write profile");

        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");
        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");
        let resolved_file = read_file.canonicalize().expect("canonicalize file");

        assert!(
            caps.fs_capabilities().iter().any(|cap| {
                cap.is_file && cap.access == AccessMode::Read && cap.resolved == resolved_file
            }),
            "filesystem.read file entries should be granted as read-only file capabilities"
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_from_profile_allow_file_keeps_missing_exact_file_on_macos() {
        let dir = tempdir().expect("tmpdir");
        let missing_file = dir.path().join("future.lock");
        let expected_resolved = dir.path().canonicalize().expect("canonicalize dir").join(
            missing_file
                .file_name()
                .expect("future file should have file name"),
        );

        let profile_path = dir.path().join("missing-file-profile.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                "meta": {{ "name": "missing-file-profile" }},
                "filesystem": {{ "allow_file": ["{}"] }}
            }}"#,
                missing_file.display()
            ),
        )
        .expect("write profile");

        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");
        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");

        assert!(
            caps.fs_capabilities().iter().any(|cap| {
                cap.is_file
                    && cap.access == AccessMode::ReadWrite
                    && cap.original == missing_file
                    && cap.resolved == expected_resolved
            }),
            "macOS profiles should preserve explicit missing exact-file grants"
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_from_args_allow_file_resolves_parent_symlinks_for_missing_file_on_macos() {
        let dir = tempdir().expect("tmpdir");
        let target_dir = dir.path().join("target");
        let link_dir = dir.path().join("link");
        std::fs::create_dir_all(&target_dir).expect("create target dir");
        std::os::unix::fs::symlink(&target_dir, &link_dir).expect("create symlink");

        let missing_file = link_dir.join("future.lock");
        let resolved_file = target_dir
            .canonicalize()
            .expect("canonicalize target dir")
            .join("future.lock");
        let args = SandboxArgs {
            allow_file: vec![missing_file.clone()],
            ..sandbox_args()
        };

        let (caps, _) = from_args_locked(&args).expect("build caps");

        assert!(
            caps.fs_capabilities().iter().any(|cap| {
                cap.is_file
                    && cap.access == AccessMode::ReadWrite
                    && cap.original == missing_file
                    && cap.resolved == resolved_file
            }),
            "macOS CLI exact-file grants should preserve original path and resolve parent symlinks"
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_from_profile_allow_file_falls_back_to_exact_directory_when_present() {
        let dir = tempdir().expect("tmpdir");
        let lock_dir = dir.path().join("claude.lock");
        std::fs::create_dir_all(&lock_dir).expect("create lock dir");
        let resolved_dir = lock_dir.canonicalize().expect("canonicalize lock dir");
        let child = lock_dir.join("nested.txt");

        let profile_path = dir.path().join("lock-dir-profile.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                "meta": {{ "name": "lock-dir-profile" }},
                "filesystem": {{ "allow_file": ["{}"] }}
            }}"#,
                lock_dir.display()
            ),
        )
        .expect("write profile");

        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");
        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");

        assert!(
            caps.fs_capabilities().iter().any(|cap| {
                cap.is_file
                    && cap.access == AccessMode::ReadWrite
                    && cap.original == lock_dir
                    && cap.resolved == resolved_dir
            }),
            "macOS profiles should preserve exact-path semantics when an allow_file entry resolves to a directory"
        );
        assert!(
            !caps.path_covered(&child),
            "exact-path fallback must not recursively cover descendants"
        );
    }

    #[cfg(not(target_os = "macos"))]
    #[test]
    fn test_from_profile_allow_file_rejects_directory_when_exact_dir_unsupported() {
        let dir = tempdir().expect("tmpdir");
        let lock_dir = dir.path().join("claude.lock");
        std::fs::create_dir_all(&lock_dir).expect("create lock dir");

        let profile_path = dir.path().join("lock-dir-profile.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                "meta": {{ "name": "lock-dir-profile" }},
                "filesystem": {{ "allow_file": ["{}"] }}
            }}"#,
                lock_dir.display()
            ),
        )
        .expect("write profile");

        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");
        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let err = from_profile_locked(&profile, workdir.path(), &args).expect_err("should fail");
        assert!(
            matches!(err, NonoError::ExpectedFile(ref p) if p == &lock_dir),
            "expected exact-file entries resolving to directories to fail closed, got: {err}"
        );
    }

    #[test]
    fn test_from_profile_policy_exclude_groups_removes_non_required_group() {
        let dir = tempdir().expect("tmpdir");
        let profile_path = dir.path().join("exclude-groups.json");
        std::fs::write(
            &profile_path,
            r#"{
                "meta": { "name": "exclude-groups" },
                "filesystem": { "allow": ["/tmp"] },
                "policy": {
                    "exclude_groups": ["dangerous_commands", "dangerous_commands_linux"]
                }
            }"#,
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");
        assert!(
            !caps.blocked_commands().contains(&"rm".to_string()),
            "excluded dangerous_commands should remove rm from blocked commands"
        );
        assert!(
            !caps.blocked_commands().contains(&"shred".to_string()),
            "excluded dangerous_commands_linux should remove shred from blocked commands"
        );
    }

    #[test]
    fn test_from_loaded_profile_extends_default_respects_excluded_blocked_commands() {
        let dir = tempdir().expect("tmpdir");
        let profile_path = dir.path().join("no-dangerous-commands.json");
        std::fs::write(
            &profile_path,
            r#"{
                "meta": { "name": "no-dangerous-commands", "version": "1.0.0" },
                "extends": "default",
                "policy": {
                    "exclude_groups": [
                        "dangerous_commands",
                        "dangerous_commands_linux",
                        "dangerous_commands_macos"
                    ]
                },
                "workdir": { "access": "readwrite" }
            }"#,
        )
        .expect("write profile");

        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");
        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();
        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");

        assert!(
            !caps.blocked_commands().contains(&"rm".to_string()),
            "excluded dangerous_commands should remove rm from blocked commands"
        );
        assert!(
            !caps.blocked_commands().contains(&"shred".to_string()),
            "excluded dangerous_commands_linux should remove shred from blocked commands"
        );
    }

    #[test]
    fn test_from_profile_policy_add_allow_paths_add_capabilities() {
        let dir = tempdir().expect("tmpdir");
        let read_dir = dir.path().join("read-dir");
        let write_dir = dir.path().join("write-dir");
        let rw_dir = dir.path().join("rw-dir");
        std::fs::create_dir_all(&read_dir).expect("mkdir read");
        std::fs::create_dir_all(&write_dir).expect("mkdir write");
        std::fs::create_dir_all(&rw_dir).expect("mkdir rw");

        let profile_path = dir.path().join("policy-adds.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                    "meta": {{ "name": "policy-adds" }},
                    "policy": {{
                        "add_allow_read": ["{}"],
                        "add_allow_write": ["{}"],
                        "add_allow_readwrite": ["{}"]
                    }}
                }}"#,
                read_dir.display(),
                write_dir.display(),
                rw_dir.display()
            ),
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");

        let read_canonical = read_dir.canonicalize().expect("canonicalize read");
        let write_canonical = write_dir.canonicalize().expect("canonicalize write");
        let rw_canonical = rw_dir.canonicalize().expect("canonicalize rw");

        let read_cap = caps
            .fs_capabilities()
            .iter()
            .find(|c| c.resolved == read_canonical)
            .expect("read dir cap");
        let write_cap = caps
            .fs_capabilities()
            .iter()
            .find(|c| c.resolved == write_canonical)
            .expect("write dir cap");
        let rw_cap = caps
            .fs_capabilities()
            .iter()
            .find(|c| c.resolved == rw_canonical)
            .expect("rw dir cap");

        assert_eq!(read_cap.access, AccessMode::Read);
        assert_eq!(write_cap.access, AccessMode::Write);
        assert_eq!(rw_cap.access, AccessMode::ReadWrite);
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_from_profile_policy_add_deny_access_participates_in_overlap_validation() {
        let dir = tempdir().expect("tmpdir");
        let allowed = dir.path().join("allowed");
        let denied = allowed.join("child");
        std::fs::create_dir_all(&denied).expect("mkdir denied child");

        let profile_path = dir.path().join("policy-deny.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                    "meta": {{ "name": "policy-deny" }},
                    "policy": {{
                        "add_allow_readwrite": ["{}"],
                        "add_deny_access": ["{}"]
                    }}
                }}"#,
                allowed.display(),
                denied.display()
            ),
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let err = from_profile_locked(&profile, workdir.path(), &args)
            .expect_err("profile deny overlap should fail on linux");
        assert!(
            err.to_string().contains("Landlock deny-overlap"),
            "unexpected error: {err}"
        );
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_from_profile_policy_add_deny_access_tracks_symlink_target_for_overlap_validation() {
        let dir = tempdir().expect("tmpdir");
        let target_dir = dir.path().join("target");
        let denied_target = target_dir.join("child");
        std::fs::create_dir_all(&denied_target).expect("mkdir denied target");

        let symlink_dir = dir.path().join("symlinked");
        std::os::unix::fs::symlink(&denied_target, &symlink_dir).expect("create symlink");

        let profile_path = dir.path().join("policy-deny-symlink.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                    "meta": {{ "name": "policy-deny-symlink" }},
                    "policy": {{
                        "add_allow_readwrite": ["{}"],
                        "add_deny_access": ["{}"]
                    }}
                }}"#,
                target_dir.display(),
                symlink_dir.display()
            ),
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let err = from_profile_locked(&profile, workdir.path(), &args)
            .expect_err("symlinked deny overlap should fail on linux");
        assert!(
            err.to_string().contains("Landlock deny-overlap"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn test_from_profile_policy_add_deny_access_removes_symlinked_file_grant() {
        let dir = tempdir().expect("tmpdir");
        let target = dir.path().join("real_gitconfig");
        std::fs::write(&target, "[user]\n").expect("write target");
        let target_canonical = target.canonicalize().expect("canonicalize target");
        let link = dir.path().join(".gitconfig");
        std::os::unix::fs::symlink(&target, &link).expect("create symlink");

        let profile_path = dir.path().join("policy-deny-file-symlink.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                    "meta": {{ "name": "policy-deny-file-symlink" }},
                    "filesystem": {{
                        "read_file": ["{}"]
                    }},
                    "policy": {{
                        "exclude_groups": ["system_read_linux", "system_write_linux"],
                        "add_deny_access": ["{}"]
                    }}
                }}"#,
                target.display(),
                link.display()
            ),
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");

        assert!(
            !caps
                .fs_capabilities()
                .iter()
                .any(|cap| cap.is_file && cap.resolved == target_canonical),
            "deny patch should remove the inherited file grant for the symlink target"
        );
    }

    #[test]
    fn test_from_profile_policy_add_deny_access_respects_override_deny_for_symlinked_file() {
        let dir = tempdir().expect("tmpdir");
        let target = dir.path().join("real_gitconfig");
        std::fs::write(&target, "[user]\n").expect("write target");
        let target_canonical = target.canonicalize().expect("canonicalize target");
        let link = dir.path().join(".gitconfig");
        std::os::unix::fs::symlink(&target, &link).expect("create symlink");

        let profile_path = dir.path().join("policy-deny-file-symlink-override.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                    "meta": {{ "name": "policy-deny-file-symlink-override" }},
                    "filesystem": {{
                        "read_file": ["{}"]
                    }},
                    "policy": {{
                        "exclude_groups": ["system_read_linux", "system_write_linux"],
                        "add_deny_access": ["{}"]
                    }}
                }}"#,
                target.display(),
                link.display()
            ),
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let mut args = sandbox_args();
        args.override_deny = vec![target.clone()];

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");

        assert!(
            caps.fs_capabilities()
                .iter()
                .any(|cap| cap.is_file && cap.resolved == target_canonical),
            "override should preserve the inherited file grant for the denied symlink target"
        );
    }

    #[test]
    fn test_from_profile_policy_override_deny_via_symlink_path() {
        let dir = tempdir().expect("tmpdir");
        let target = dir.path().join("real_gitconfig");
        std::fs::write(&target, "[user]\n").expect("write target");
        let target_canonical = target.canonicalize().expect("canonicalize target");
        let link = dir.path().join(".gitconfig");
        std::os::unix::fs::symlink(&target, &link).expect("create symlink");

        // Override via the symlink path (not the canonical target)
        let profile_path = dir.path().join("override-deny-symlink.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                    "meta": {{ "name": "override-deny-symlink" }},
                    "filesystem": {{
                        "read_file": ["{target}"]
                    }},
                    "policy": {{
                        "add_deny_access": ["{link}"],
                        "override_deny": ["{link}"]
                    }}
                }}"#,
                target = target.display(),
                link = link.display()
            ),
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");

        assert!(
            caps.fs_capabilities()
                .iter()
                .any(|cap| cap.is_file && cap.resolved == target_canonical),
            "override via symlink path should preserve the file grant for the canonical target"
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_from_profile_workdir_deny_env_with_extends() {
        let workdir = tempdir().expect("workdir");
        std::fs::write(workdir.path().join(".env"), "SECRET=test").expect("write .env");

        // Synthetic profile that extends `default` (no fixed-path filesystem
        // grants) and applies a policy.add_deny_access for the workdir's .env.
        let profile_path = workdir.path().join("deny-env.json");
        std::fs::write(
            &profile_path,
            r#"{
                "extends": "default",
                "meta": { "name": "deny-env-test" },
                "workdir": { "access": "readwrite" },
                "policy": {
                    "add_deny_access": ["$WORKDIR/.env"]
                }
            }"#,
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let args = sandbox_args();
        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");

        let rules = caps.platform_rules().join("\n");
        // On macOS, tempdir is under /var/folders which is a symlink to /private/var/folders.
        // The deny rule must use the canonical path so it matches the kernel-resolved path
        // that Seatbelt sees at runtime.
        let env_path = workdir.path().join(".env");
        let env_canonical = env_path.canonicalize().expect("canonicalize .env");

        // Check: does the deny rule use the canonical path?
        let has_canonical_deny = rules.contains(&format!(
            "deny file-read-data (literal \"{}\")",
            env_canonical.display()
        ));
        // Check: does the deny rule use the original (possibly non-canonical) path?
        let has_original_deny = rules.contains(&format!(
            "deny file-read-data (literal \"{}\")",
            env_path.display()
        ));

        // The deny must cover the canonical path, otherwise Seatbelt won't enforce it
        assert!(
            has_canonical_deny,
            "deny rule must use canonical path {}.\n\
             Has original path deny: {}\n\
             Original path: {}\n\
             Canonical path: {}\n\
             All platform rules:\n{}",
            env_canonical.display(),
            has_original_deny,
            env_path.display(),
            env_canonical.display(),
            rules
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_from_profile_policy_add_deny_access_emits_seatbelt_rules() {
        let dir = tempdir().expect("tmpdir");
        let denied = dir.path().join("denied");
        std::fs::create_dir_all(&denied).expect("mkdir denied");

        let profile_path = dir.path().join("policy-deny-macos.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                    "meta": {{ "name": "policy-deny-macos" }},
                    "policy": {{
                        "add_deny_access": ["{}"]
                    }}
                }}"#,
                denied.display()
            ),
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");

        let rules = caps.platform_rules().join("\n");
        assert!(
            rules.contains("deny file-read-data"),
            "expected macOS deny read rule, got:\n{}",
            rules
        );
        assert!(
            rules.contains("deny file-write*"),
            "expected macOS deny write rule, got:\n{}",
            rules
        );
    }

    #[test]
    fn test_from_profile_policy_override_deny_punches_through_deny_group() {
        let dir = tempdir().expect("tmpdir");
        let denied = dir.path().join("denied_dir");
        std::fs::create_dir_all(&denied).expect("mkdir denied");

        let profile_path = dir.path().join("override-deny-profile.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                    "meta": {{ "name": "override-deny-test" }},
                    "policy": {{
                        "add_allow_readwrite": ["{path}"],
                        "add_deny_access": ["{path}"],
                        "override_deny": ["{path}"]
                    }}
                }}"#,
                path = denied.display()
            ),
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");

        // The allow should survive because override_deny punches through the deny
        let canonical = denied.canonicalize().expect("canonicalize");
        assert!(
            caps.fs_capabilities()
                .iter()
                .any(|cap| !cap.is_file && cap.resolved == canonical),
            "override_deny should preserve the directory grant despite deny group"
        );
    }

    #[test]
    fn test_cli_override_deny_requires_matching_grant() {
        // Override path is under temp dir which is covered by system groups,
        // but the grant check requires user-intent sources (User/Profile),
        // so group coverage is not sufficient. Profile-level override_deny
        // entries may be skipped when their platform-specific grants do not
        // resolve on the current platform; CLI overrides should still fail.
        let dir = tempdir().expect("tmpdir");
        let denied = dir.path().join("denied_no_grant");
        std::fs::create_dir_all(&denied).expect("mkdir denied");

        let profile_path = dir.path().join("override-deny-no-grant.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                    "meta": {{ "name": "override-deny-no-grant" }},
                    "policy": {{
                        "add_deny_access": ["{path}"]
                    }}
                }}"#,
                path = denied.display()
            ),
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = SandboxArgs {
            override_deny: vec![denied.clone()],
            ..sandbox_args()
        };

        let err = from_profile_locked(&profile, workdir.path(), &args)
            .expect_err("CLI override_deny without user-intent grant should fail");
        assert!(
            err.to_string().contains("no matching grant"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn test_profile_override_deny_requires_matching_grant() {
        let dir = tempdir().expect("tmpdir");
        let denied = dir.path().join("denied_no_grant");
        std::fs::create_dir_all(&denied).expect("mkdir denied");

        let profile_path = dir.path().join("override-deny-no-grant.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                    "meta": {{ "name": "override-deny-no-grant" }},
                    "policy": {{
                        "add_deny_access": ["{path}"],
                        "override_deny": ["{path}"]
                    }}
                }}"#,
                path = denied.display()
            ),
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let err = from_profile_locked(&profile, workdir.path(), &args)
            .expect_err("profile override_deny without grant should fail");
        assert!(
            err.to_string().contains("no matching grant"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn test_from_profile_with_groups() {
        // Synthetic profile: extends `default` (so it inherits deny groups
        // and dangerous_commands) plus its own groups. We avoid depending on
        // a built-in's filesystem layout because the test process may not
        // have access to the paths that built-ins reference (e.g. ~/.codex).
        let dir = tempdir().expect("tmpdir");
        let profile_path = dir.path().join("groups-test.json");
        std::fs::write(
            &profile_path,
            r#"{
                "extends": "default",
                "meta": { "name": "groups-test" },
                "security": {
                    "groups": ["node_runtime", "rust_runtime", "unlink_protection"]
                }
            }"#,
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("Failed to create temp dir");
        let args = sandbox_args();

        let (mut caps, needs_unlink_overrides) =
            from_profile_locked(&profile, workdir.path(), &args).expect("Failed to build");

        // Simulate what main.rs does: apply unlink overrides after all paths finalized
        if needs_unlink_overrides {
            policy::apply_unlink_overrides(&mut caps);
        }

        // Groups should have populated filesystem capabilities
        assert!(caps.has_fs());

        if cfg!(target_os = "macos") {
            // On macOS: deny groups generate Seatbelt platform rules
            assert!(!caps.platform_rules().is_empty());

            let rules = caps.platform_rules().join("\n");
            assert!(rules.contains("deny file-read-data"));
            assert!(rules.contains("deny file-write*"));

            // Unlink protection should be present
            assert!(rules.contains("deny file-write-unlink"));

            // Unlink overrides must exist for writable paths (including ~/.claude from
            // the profile [filesystem] section, which is added AFTER group resolution).
            assert!(
                rules.contains("allow file-write-unlink"),
                "Expected unlink overrides for writable paths, got:\n{}",
                rules
            );
        }
        // On Linux: deny/unlink rules are not generated (Landlock has no deny semantics),
        // but deny_paths are collected for overlap validation.

        // Dangerous commands should be blocked (cross-platform)
        assert!(caps.blocked_commands().contains(&"rm".to_string()));
        assert!(caps.blocked_commands().contains(&"dd".to_string()));
    }

    #[test]
    fn test_cli_allow_upgrades_profile_read_path() {
        // Regression test: a profile sets a path as read-only, and --allow on
        // the CLI should upgrade it to ReadWrite. Previously, path_covered()
        // in add_cli_overrides() silently dropped the CLI entry because it
        // only checked path containment, not access mode.
        let dir = tempdir().expect("tmpdir");
        let target = dir.path().join("readonly_dir");
        std::fs::create_dir(&target).expect("create target dir");

        let profile_path = dir.path().join("test-profile.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                    "meta": {{ "name": "test-upgrade" }},
                    "filesystem": {{ "read": ["{}"] }}
                }}"#,
                target.display()
            ),
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = SandboxArgs {
            allow: vec![target.clone()],
            ..sandbox_args()
        };

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");

        let canonical = target.canonicalize().expect("canonicalize target");
        let cap = caps
            .fs_capabilities()
            .iter()
            .find(|c| c.resolved == canonical)
            .expect("target path should be in capabilities");

        assert_eq!(
            cap.access,
            AccessMode::ReadWrite,
            "CLI --allow should upgrade profile read-only path to ReadWrite, got {:?}",
            cap.access,
        );
    }

    #[test]
    fn test_cli_write_merges_with_profile_read_path() {
        // Same regression but with --write instead of --allow.
        // Profile read + CLI write should merge to ReadWrite.
        let dir = tempdir().expect("tmpdir");
        let target = dir.path().join("readonly_dir");
        std::fs::create_dir(&target).expect("create target dir");

        let profile_path = dir.path().join("test-profile.json");
        std::fs::write(
            &profile_path,
            format!(
                r#"{{
                    "meta": {{ "name": "test-merge" }},
                    "filesystem": {{ "read": ["{}"] }}
                }}"#,
                target.display()
            ),
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = SandboxArgs {
            write: vec![target.clone()],
            ..sandbox_args()
        };

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");

        let canonical = target.canonicalize().expect("canonicalize target");
        let cap = caps
            .fs_capabilities()
            .iter()
            .find(|c| c.resolved == canonical)
            .expect("target path should be in capabilities");

        assert_eq!(
            cap.access,
            AccessMode::ReadWrite,
            "CLI --write + profile read should merge to ReadWrite, got {:?}",
            cap.access,
        );
    }

    #[test]
    fn test_from_profile_allow_net_overrides_proxy_mode() {
        let dir = tempdir().expect("tmpdir");
        let profile_path = dir.path().join("allow-net-test.json");
        std::fs::write(
            &profile_path,
            r#"{
                "extends": "default",
                "meta": { "name": "allow-net-test" },
                "network": { "block": true }
            }"#,
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");
        let workdir = tempdir().expect("workdir");
        let args = SandboxArgs {
            allow_net: true,
            ..sandbox_args()
        };

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");

        assert_eq!(*caps.network_mode(), nono::NetworkMode::AllowAll);
    }

    #[test]
    fn test_from_profile_allow_net_overrides_blocked_network() {
        let dir = tempdir().expect("tmpdir");
        let profile_path = dir.path().join("blocked.json");
        std::fs::write(
            &profile_path,
            r#"{
                "meta": { "name": "blocked" },
                "filesystem": { "allow": ["/tmp"] },
                "network": { "block": true }
            }"#,
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = SandboxArgs {
            allow_net: true,
            ..sandbox_args()
        };

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");

        assert_eq!(*caps.network_mode(), nono::NetworkMode::AllowAll);
    }

    #[test]
    fn test_from_profile_process_info_mode_same_sandbox() {
        let dir = tempdir().expect("tmpdir");
        let profile_path = dir.path().join("pim-test.json");
        std::fs::write(
            &profile_path,
            r#"{
                "meta": { "name": "pim-test" },
                "filesystem": { "allow": ["/tmp"] },
                "security": { "process_info_mode": "allow_same_sandbox" }
            }"#,
        )
        .expect("write profile");
        let workdir = tempdir().expect("tmpdir");
        let args = sandbox_args();
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");
        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");
        assert_eq!(
            caps.process_info_mode(),
            nono::ProcessInfoMode::AllowSameSandbox,
            "profile process_info_mode should propagate to CapabilitySet"
        );
    }

    #[test]
    fn test_from_profile_ipc_mode_full() {
        let dir = tempdir().expect("tmpdir");
        let profile_path = dir.path().join("ipc-test.json");
        std::fs::write(
            &profile_path,
            r#"{
                "meta": { "name": "ipc-test" },
                "filesystem": { "allow": ["/tmp"] },
                "security": { "ipc_mode": "full" }
            }"#,
        )
        .expect("write profile");
        let workdir = tempdir().expect("tmpdir");
        let args = sandbox_args();
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");
        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");
        assert_eq!(
            caps.ipc_mode(),
            nono::IpcMode::Full,
            "profile ipc_mode should propagate to CapabilitySet"
        );
    }

    #[test]
    fn test_from_profile_ipc_mode_shared_memory_only() {
        let dir = tempdir().expect("tmpdir");
        let profile_path = dir.path().join("ipc-test-shm.json");
        std::fs::write(
            &profile_path,
            r#"{
                "meta": { "name": "ipc-test-shm" },
                "filesystem": { "allow": ["/tmp"] },
                "security": { "ipc_mode": "shared_memory_only" }
            }"#,
        )
        .expect("write profile");
        let workdir = tempdir().expect("tmpdir");
        let args = sandbox_args();
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");
        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");
        assert_eq!(
            caps.ipc_mode(),
            nono::IpcMode::SharedMemoryOnly,
            "profile ipc_mode: shared_memory_only should propagate to CapabilitySet"
        );
    }

    #[test]
    fn test_from_profile_ipc_mode_default() {
        let dir = tempdir().expect("tmpdir");
        let profile_path = dir.path().join("ipc-test-default.json");
        std::fs::write(
            &profile_path,
            r#"{
                "meta": { "name": "ipc-test-default" },
                "filesystem": { "allow": ["/tmp"] },
                "security": {}
            }"#,
        )
        .expect("write profile");
        let workdir = tempdir().expect("tmpdir");
        let args = sandbox_args();
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");
        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");
        assert_eq!(
            caps.ipc_mode(),
            nono::IpcMode::SharedMemoryOnly,
            "absent profile ipc_mode should default to SharedMemoryOnly"
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_regex_escape_path_dots() {
        assert_eq!(
            regex_escape_path("/Users/me/.claude.json"),
            "/Users/me/\\.claude\\.json"
        );
    }

    #[cfg(not(target_os = "macos"))]
    #[test]
    fn test_from_args_allow_connect_port_populates_tcp_connect_ports() {
        let args = SandboxArgs {
            allow_connect_port: vec![443, 80, 5432],
            ..sandbox_args()
        };

        let (caps, _) = from_args_locked(&args).expect("build caps");
        assert_eq!(caps.tcp_connect_ports(), &[443, 80, 5432]);
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_from_args_allow_connect_port_errors_on_macos() {
        let args = SandboxArgs {
            allow_connect_port: vec![443],
            ..sandbox_args()
        };
        let err = from_args_locked(&args).expect_err("should fail on macOS");
        assert!(
            err.to_string().contains("not supported on macOS"),
            "unexpected error: {err}"
        );
    }

    #[cfg(not(target_os = "macos"))]
    #[test]
    fn test_from_profile_connect_port_populates_tcp_connect_ports() {
        let dir = tempdir().expect("tmpdir");
        let profile_path = dir.path().join("connect-port-profile.json");
        std::fs::write(
            &profile_path,
            r#"{
                "meta": { "name": "connect-port-profile" },
                "network": { "connect_port": [443, 5432] }
            }"#,
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");
        assert_eq!(caps.tcp_connect_ports(), &[443, 5432]);
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_from_profile_connect_port_errors_on_macos() {
        let dir = tempdir().expect("tmpdir");
        let profile_path = dir.path().join("connect-port-profile.json");
        std::fs::write(
            &profile_path,
            r#"{
                "meta": { "name": "connect-port-profile" },
                "network": { "connect_port": [443] }
            }"#,
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let err =
            from_profile_locked(&profile, workdir.path(), &args).expect_err("should fail on macOS");
        assert!(
            err.to_string().contains("not supported on macOS"),
            "unexpected error: {err}"
        );
    }

    #[cfg(not(target_os = "macos"))]
    #[test]
    fn test_cli_allow_connect_port_overrides_profile_connect_port() {
        let dir = tempdir().expect("tmpdir");
        let profile_path = dir.path().join("connect-port-override.json");
        std::fs::write(
            &profile_path,
            r#"{
                "meta": { "name": "connect-port-override" },
                "network": { "connect_port": [443] }
            }"#,
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = SandboxArgs {
            allow_connect_port: vec![5432],
            ..sandbox_args()
        };

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");
        // Both profile and CLI ports should be present
        let ports = caps.tcp_connect_ports();
        assert!(ports.contains(&443), "profile port 443 should be present");
        assert!(ports.contains(&5432), "CLI port 5432 should be present");
    }

    #[test]
    fn test_from_profile_allow_domain_does_not_open_raw_tcp_ports() {
        let dir = tempdir().expect("tmpdir");
        let profile_path = dir.path().join("allow-domain-no-raw-ports.json");
        std::fs::write(
            &profile_path,
            r#"{
                "meta": { "name": "allow-domain-no-raw-ports" },
                "filesystem": { "allow": ["/tmp"] },
                "network": {
                    "allow_domain": [
                        "api.example.com",
                        "nats.example.com:4222",
                        "postgres.example.com:5432"
                    ]
                }
            }"#,
        )
        .expect("write profile");
        let profile = crate::profile::load_profile_from_path(&profile_path).expect("load profile");

        let workdir = tempdir().expect("workdir");
        let args = sandbox_args();

        let (caps, _) = from_profile_locked(&profile, workdir.path(), &args).expect("build caps");

        assert!(
            caps.tcp_connect_ports().is_empty(),
            "allow_domain should not grant direct TCP ports in proxy mode, got: {:?}",
            caps.tcp_connect_ports()
        );
    }

    #[test]
    fn test_from_args_allow_proxy_does_not_open_raw_tcp_ports() {
        let args = SandboxArgs {
            allow_proxy: vec![
                "api.example.com".to_string(),
                "nats.example.com:4222".to_string(),
            ],
            ..sandbox_args()
        };

        let (caps, _) = from_args_locked(&args).expect("build caps");

        assert!(
            caps.tcp_connect_ports().is_empty(),
            "allow-domain should not grant direct TCP ports in proxy mode, got: {:?}",
            caps.tcp_connect_ports()
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_regex_escape_path_no_metacharacters() {
        assert_eq!(regex_escape_path("/usr/local/bin"), "/usr/local/bin");
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_regex_escape_path_special_chars() {
        assert_eq!(
            regex_escape_path("/path/with+parens(1)[2]"),
            "/path/with\\+parens\\(1\\)\\[2\\]"
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_atomic_write_rule_adds_regex_for_writable_file() {
        let tmp = tempdir().expect("tempdir");
        let file_path = tmp.path().join("test.json");
        std::fs::write(&file_path, "{}").expect("write");
        let cap = FsCapability::new_file(&file_path, AccessMode::ReadWrite).expect("cap");
        let mut caps = CapabilitySet::new();
        add_atomic_write_rule(&mut caps, &cap).expect("add rule");
        let rules = caps.platform_rules().join("\n");
        assert!(
            rules.contains("file-write*"),
            "should contain file-write rule"
        );
        assert!(
            rules.contains(r"\.tmp\.[0-9]+\.[0-9]+"),
            "should contain temp file pattern, got: {}",
            rules
        );
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_atomic_write_rule_skips_readonly_file() {
        let tmp = tempdir().expect("tempdir");
        let file_path = tmp.path().join("readonly.json");
        std::fs::write(&file_path, "{}").expect("write");
        let cap = FsCapability::new_file(&file_path, AccessMode::Read).expect("cap");
        let mut caps = CapabilitySet::new();
        add_atomic_write_rule(&mut caps, &cap).expect("add rule");
        assert!(
            caps.platform_rules().is_empty(),
            "read-only file should not get atomic write rule"
        );
    }

    // --- --allow-unix-socket* flag tests (issue #685 / #696) -----------------

    #[test]
    fn test_allow_unix_socket_adds_cap_and_implied_read_fs_grant() {
        let dir = tempdir().expect("tempdir");
        let sock = dir.path().join("a.sock");
        std::fs::write(&sock, b"").expect("create socket stub");

        let args = SandboxArgs {
            allow_unix_socket: vec![sock.clone()],
            ..sandbox_args()
        };

        let (caps, _) = from_args_locked(&args).expect("from_args");

        // One UnixSocketCapability with mode=Connect.
        let socks = caps.unix_socket_capabilities();
        assert_eq!(socks.len(), 1);
        assert_eq!(socks[0].mode, UnixSocketMode::Connect);
        assert!(!socks[0].is_directory);

        // Exactly one implied FsCapability at Read.
        let fs_matches: Vec<_> = caps
            .fs_capabilities()
            .iter()
            .filter(|c| c.is_file && c.resolved == sock.canonicalize().expect("canonicalize sock"))
            .collect();
        assert_eq!(fs_matches.len(), 1);
        assert_eq!(fs_matches[0].access, AccessMode::Read);
    }

    #[test]
    #[cfg(unix)]
    fn test_allow_unix_socket_bind_rejects_dangling_symlink() {
        // A dangling symlink is not a typical "future socket file" — bind(2)
        // follows the link and creates at the target, which is usually not
        // what the operator intended. Must reject loudly.
        let dir = tempdir().expect("tempdir");
        let link = dir.path().join("dangling.sock");
        let missing_target = dir.path().join("does-not-exist");
        std::os::unix::fs::symlink(&missing_target, &link).expect("create dangling symlink");

        let args = SandboxArgs {
            allow_unix_socket_bind: vec![link],
            ..sandbox_args()
        };

        let err = from_args_locked(&args)
            .expect_err("dangling symlink must be rejected by the bind guard");
        assert!(
            format!("{err}").contains("dangling symlink"),
            "error message should mention dangling symlink"
        );
    }

    #[test]
    fn test_allow_unix_socket_missing_skips_both_socket_and_fs_grants() {
        // On macOS, try_new_file's handle_missing_file_capability can
        // manufacture an exact-file FsCapability even when the path
        // doesn't exist. The unix-socket branch must NOT register that
        // fs grant when the socket grant itself was skipped — otherwise
        // the user gets a filesystem permission for a path they can't
        // `connect()` to anyway.
        let dir = tempdir().expect("tempdir");
        let missing = dir.path().join("never-exists.sock");

        let args = SandboxArgs {
            allow_unix_socket: vec![missing.clone()],
            ..sandbox_args()
        };

        let (caps, _) = from_args_locked(&args).expect("from_args");

        assert!(
            caps.unix_socket_capabilities().is_empty(),
            "no unix-socket grant for missing path"
        );
        assert!(
            !caps
                .fs_capabilities()
                .iter()
                .any(|c| c.original == missing || c.resolved == missing),
            "no implied fs grant when the socket grant was skipped"
        );
    }

    #[test]
    fn test_allow_unix_socket_bind_accepts_nonexistent_path_and_widens_fs_to_parent() {
        // The normal bind(2) workflow: grant is registered before the
        // socket file exists. The UnixSocketCapability is added
        // (ConnectBind, file-scoped, canonical parent + filename), and
        // the implied fs grant widens to ReadWrite on the parent
        // directory since the kernel needs write on the parent to
        // create the new inode.
        let dir = tempdir().expect("tempdir");
        let pending = dir.path().join("future.sock");
        assert!(!pending.exists(), "test precondition: path must not exist");

        let args = SandboxArgs {
            allow_unix_socket_bind: vec![pending.clone()],
            ..sandbox_args()
        };

        let (caps, _) = from_args_locked(&args).expect("from_args");

        let socks = caps.unix_socket_capabilities();
        assert_eq!(socks.len(), 1);
        assert_eq!(socks[0].mode, UnixSocketMode::ConnectBind);
        assert!(!socks[0].is_directory);

        // Implied fs grant should cover the parent dir with ReadWrite.
        let canonical_parent = dir.path().canonicalize().expect("canonicalize dir");
        let parent_grant = caps
            .fs_capabilities()
            .iter()
            .find(|c| !c.is_file && c.resolved == canonical_parent)
            .expect("implied parent-dir fs grant missing");
        assert_eq!(parent_grant.access, AccessMode::ReadWrite);
    }

    #[test]
    fn test_allow_unix_socket_bind_existing_grants_readwrite_fs() {
        let dir = tempdir().expect("tempdir");
        let sock = dir.path().join("b.sock");
        std::fs::write(&sock, b"").expect("create socket stub");

        let args = SandboxArgs {
            allow_unix_socket_bind: vec![sock.clone()],
            ..sandbox_args()
        };

        let (caps, _) = from_args_locked(&args).expect("from_args");

        let socks = caps.unix_socket_capabilities();
        assert_eq!(socks.len(), 1);
        assert_eq!(socks[0].mode, UnixSocketMode::ConnectBind);

        let fs_match = caps
            .fs_capabilities()
            .iter()
            .find(|c| c.is_file && c.resolved == sock.canonicalize().expect("canonicalize sock"))
            .expect("implied fs cap not found");
        assert_eq!(fs_match.access, AccessMode::ReadWrite);
    }

    #[test]
    fn test_allow_unix_socket_dir_bind_directory_grants_readwrite_fs() {
        // The tsx case (#685): runtime-generated socket filenames inside a
        // known directory.
        let dir = tempdir().expect("tempdir");

        let args = SandboxArgs {
            allow_unix_socket_dir_bind: vec![dir.path().to_path_buf()],
            ..sandbox_args()
        };

        let (caps, _) = from_args_locked(&args).expect("from_args");

        let socks = caps.unix_socket_capabilities();
        assert_eq!(socks.len(), 1);
        assert_eq!(socks[0].mode, UnixSocketMode::ConnectBind);
        assert!(socks[0].is_directory);

        let fs_match = caps
            .fs_capabilities()
            .iter()
            .find(|c| {
                !c.is_file && c.resolved == dir.path().canonicalize().expect("canonicalize dir")
            })
            .expect("implied fs dir cap not found");
        assert_eq!(fs_match.access, AccessMode::ReadWrite);
    }

    #[test]
    fn test_allow_unix_socket_dir_implies_read_fs_grant() {
        let dir = tempdir().expect("tempdir");

        let args = SandboxArgs {
            allow_unix_socket_dir: vec![dir.path().to_path_buf()],
            ..sandbox_args()
        };

        let (caps, _) = from_args_locked(&args).expect("from_args");

        let socks = caps.unix_socket_capabilities();
        assert_eq!(socks.len(), 1);
        assert_eq!(socks[0].mode, UnixSocketMode::Connect);
        assert!(socks[0].is_directory);

        let fs_match = caps
            .fs_capabilities()
            .iter()
            .find(|c| {
                !c.is_file && c.resolved == dir.path().canonicalize().expect("canonicalize dir")
            })
            .expect("implied fs dir cap not found");
        assert_eq!(fs_match.access, AccessMode::Read);
    }

    /// Build a minimal profile JSON with a single filesystem field set,
    /// then parse it into a [`crate::profile::Profile`].
    fn profile_with_fs_field(field: &str, value: &str) -> crate::profile::Profile {
        let json = format!(
            r#"{{
                "meta": {{ "name": "test-unix-socket" }},
                "security": {{ "groups": [] }},
                "filesystem": {{ "{field}": ["{value}"] }}
            }}"#
        );
        serde_json::from_str(&json).expect("parse profile")
    }

    #[test]
    fn test_profile_unix_socket_field_connect_file() {
        let dir = tempdir().expect("tempdir");
        let sock = dir.path().join("a.sock");
        std::fs::write(&sock, b"").expect("create socket stub");
        let profile = profile_with_fs_field("unix_socket", &sock.display().to_string());

        let (caps, _) =
            from_profile_locked(&profile, dir.path(), &sandbox_args()).expect("from_profile");

        let socks: Vec<_> = caps
            .unix_socket_capabilities()
            .iter()
            .filter(|c| c.source == CapabilitySource::Profile)
            .collect();
        assert_eq!(socks.len(), 1);
        assert_eq!(socks[0].mode, UnixSocketMode::Connect);
        assert!(!socks[0].is_directory);
    }

    #[test]
    fn test_profile_unix_socket_field_connect_bind_file() {
        let dir = tempdir().expect("tempdir");
        let sock = dir.path().join("b.sock");
        std::fs::write(&sock, b"").expect("create socket stub");
        let profile = profile_with_fs_field("unix_socket_bind", &sock.display().to_string());

        let (caps, _) =
            from_profile_locked(&profile, dir.path(), &sandbox_args()).expect("from_profile");

        let socks: Vec<_> = caps
            .unix_socket_capabilities()
            .iter()
            .filter(|c| c.source == CapabilitySource::Profile)
            .collect();
        assert_eq!(socks.len(), 1);
        assert_eq!(socks[0].mode, UnixSocketMode::ConnectBind);
        assert!(!socks[0].is_directory);
    }

    #[test]
    fn test_profile_unix_socket_field_connect_dir() {
        let dir = tempdir().expect("tempdir");
        let profile = profile_with_fs_field("unix_socket_dir", &dir.path().display().to_string());

        let (caps, _) =
            from_profile_locked(&profile, dir.path(), &sandbox_args()).expect("from_profile");

        let socks: Vec<_> = caps
            .unix_socket_capabilities()
            .iter()
            .filter(|c| c.source == CapabilitySource::Profile)
            .collect();
        assert_eq!(socks.len(), 1);
        assert_eq!(socks[0].mode, UnixSocketMode::Connect);
        assert!(socks[0].is_directory);
    }

    #[test]
    fn test_profile_unix_socket_field_connect_bind_dir() {
        // The tsx (#685) case, expressed via profile JSON.
        let dir = tempdir().expect("tempdir");
        let profile =
            profile_with_fs_field("unix_socket_dir_bind", &dir.path().display().to_string());

        let (caps, _) =
            from_profile_locked(&profile, dir.path(), &sandbox_args()).expect("from_profile");

        let socks: Vec<_> = caps
            .unix_socket_capabilities()
            .iter()
            .filter(|c| c.source == CapabilitySource::Profile)
            .collect();
        assert_eq!(socks.len(), 1);
        assert_eq!(socks[0].mode, UnixSocketMode::ConnectBind);
        assert!(socks[0].is_directory);
    }
}