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
//! P5-1 (COMPOSABLE-HARNESS-DESIGN.md §5.3 risk 1, D-3): the command
//! canonicalizer — the security-critical parser rule-matching depends on.
//!
//! **Why tree-sitter, not a hand-rolled splitter** (risk-1 mitigation, §5.3:
//! "tree-sitter-based parsing (oc's proven approach, oc§1) rather than a
//! hand-rolled splitter"): a regex/split-on-`;` approach cannot distinguish a
//! real separator from one that's quoted (`echo "a; b"` is ONE command), and
//! cannot recurse into `$(...)`/backtick command substitution without
//! reimplementing a chunk of shell grammar by hand — exactly the class of bug
//! this design calls "a silent privilege escalation" (§5.3 risk 1). This
//! module instead asks `tree-sitter-bash` (the same grammar opencode's own
//! parser uses, oc§1) to build a real parse tree and walks it.
//!
//! **Fail-closed contract** (the load-bearing invariant): [`canonicalize`]
//! NEVER returns a silently-empty or silently-optimistic result for input it
//! can't fully make sense of. Any parse error (an ERROR/MISSING node
//! anywhere in the tree) yields [`CanonResult::Unparseable`] — callers MUST
//! treat that as "requires approval", never "allow" (see
//! `crate::permissions::rules::evaluate_command`, which does exactly this).
use Mutex;
/// Wrapper commands stripped before matching, so a rule written against the
/// INNER command (e.g. a deny rule on `rm -rf*`) cannot be bypassed by
/// prefixing it with a process wrapper. Superset of cc§4's own documented
/// list (`timeout|time|nice|nohup|stdbuf` + bare `xargs` —
/// `docs/composable-harness/inventory/claude-code.md` "Compound-command
/// awareness") plus `env`/`sudo`/`command`/`builtin` per this unit's build
/// brief: cc's list is a lower bound, not a ceiling — a privilege- or
/// identity-changing wrapper like `sudo` MUST be stripped, or a deny rule on
/// the wrapped command is trivially bypassed (`sudo rm -rf /`).
const STRIPPABLE_WRAPPERS: & = &;
/// cc§4: "exec wrappers (`watch`, `setsid`, `ionice`, `flock`, `find
/// -exec/-delete`) always prompt" — NOT stripped (unlike
/// [`STRIPPABLE_WRAPPERS`], their effective inner command is not statically
/// determinable the same way `timeout N cmd` is: `find -exec` may run its
/// target zero, one, or many times over a runtime-discovered file list).
/// [`CanonSubcommand::opaque`] is set instead, which forces a minimum `Ask`
/// decision in [`crate::permissions::rules`] regardless of what an
/// otherwise-matching rule would say — never silently `Allow`. `xargs` is
/// ALSO always-opaque (see [`strip_wrappers`]'s doc comment on why it is not
/// in [`STRIPPABLE_WRAPPERS`]) despite being on cc's own stripped list: cc's
/// own docs note the env-runner caveat that some wrapped-command forms are
/// NOT safely resolvable, and unlike `timeout`/`nice` (whose flag shapes are
/// small and well-known), `xargs`'s argument grammar (`-I{}`, `-P4`, `-n1`,
/// a literal `{}` placeholder default) makes which token is "the command"
/// genuinely ambiguous — guessing wrong in the PERMISSIVE direction (picking
/// a flag's value as if it were the command name) is exactly the silent-
/// escalation risk §5.3 risk 1 names. Opaque (forced Ask), never stripped.
const OPAQUE_WRAPPERS: & = &;
/// Safety cap on total sub-commands extracted from one input — bounds the
/// cost of a pathologically nested `$(...)`/backtick chain. Exceeding it is
/// treated as [`CanonResult::Unparseable`] (fail-closed), not silently
/// truncated.
const MAX_SUBCOMMANDS: usize = 256;
/// Safety cap on [`strip_wrappers`]'s unwrap loop — bounds a maliciously (or
/// accidentally) deep wrapper stack (`env X=1 sudo nice timeout 5 nice …`).
const MAX_WRAPPER_UNWRAPS: usize = 16;
/// One extracted sub-command: its canonicalized argv, and whether it is
/// "opaque" — a wrapper whose real effect can't be statically resolved, so
/// [`crate::permissions::rules::evaluate_command`] must never let it resolve
/// to `Allow` purely by absence of a matching rule.
/// The result of [`canonicalize`]: either a flat list of every sub-command
/// found (top-level compounds, and every command nested inside a
/// `$(...)`/backtick substitution anywhere in the tree — see the module doc
/// for why a single recursive `command`-node walk is sufficient to find
/// both), or a fail-closed reason a caller must treat as REQUIRING approval.
/// Parse `command` (a bash command string, as a model would pass to the
/// `bash`/`shell` tool) into its canonical sub-commands — the D-3 security
/// core every rule-engine decision in `crate::permissions::rules` is built
/// on. See the module doc comment for the fail-closed contract.
/// Recursively collect every `command` node's canonicalized form anywhere in
/// the tree — this single pass is what finds sub-commands nested inside
/// `$(...)`/backtick substitution (used either AS a command name or as an
/// ordinary argument) without any special-cased recursion: the grammar
/// already represents a substitution's payload as an ordinary nested
/// `command`/`list`/`pipeline` subtree, so a generic "every command node"
/// walk visits it for free.
/// F4: does `command_node` directly own an enclosing `redirected_statement`
/// (i.e. do ITS redirect targets apply to `command_node`'s own stdout/
/// stdin)? Two shapes are recognized, both confirmed against the real
/// tree-sitter-bash grammar:
///
/// - `command_node`'s immediate parent IS the `redirected_statement`
/// (`echo evil > .env`, `cat x > .git/config`, `cmd > a > b`).
/// - `command_node`'s immediate parent is a `pipeline`, that pipeline's own
/// parent is a `redirected_statement`, AND `command_node` is the LAST
/// command stage in the pipeline (`a | b > f` redirects `b`'s stdout, not
/// `a`'s — bash's actual semantics for a pipeline-level redirect).
///
/// Anything else (a redirect on an EARLIER pipeline stage, a redirect
/// nested inside a subshell/group this function doesn't specifically walk
/// into, …) returns `None` — no targets are attached for that shape. This
/// is a conscious, named residual (see this crate's permissions module doc
/// / the F4 build brief's honesty note): it covers every row the review
/// proved a bypass on, not literally every redirect shape bash's grammar
/// can produce. A shape this function misses is not silently declared
/// "safe" — it simply isn't asserted about here, exactly like any other
/// statically-intractable case in this module.
/// F4: walk `redirected_statement`'s direct children for `file_redirect`
/// nodes (skipping the `command`/`pipeline` child itself) and extract every
/// output-redirect target into `sub.write_redirect_targets` / every input-
/// redirect target into `sub.read_redirect_targets`. `herestring_redirect`
/// (`<<<`)/`heredoc_redirect` (`<<`) carry inline DATA, not a file path —
/// intentionally not a source of targets here.
/// The recognized WRITE-intent redirect operators (F4 build brief: `>`,
/// `>>`, `&>`, `>|`, `&>>`). Anything else (`<&`, `>&` fd-duplication,
/// `<>` read-write, …) is deliberately NOT matched — a fd-duplication
/// operator's "target" is another file descriptor number, not a path.
const WRITE_REDIRECT_OPERATORS: & = &;
/// Fail-closed sentinel returned by [`known_writer_targets`] (bypass #1,
/// Fable-5 delta review) for a known-writer invocation whose real
/// destination this heuristic cannot confidently identify — rather than
/// risk confidently picking the WRONG argv token (e.g. `cp -t DIR a b`'s
/// plain "last positional is the destination" shape would pick `b`, which
/// is a SOURCE — the real write lands in `DIR`), this sentinel stands in
/// for "a write is happening here, but not at a token this function can
/// name". It deliberately contains `$` so [`is_concrete_path_text`] always
/// classifies it as non-concrete, which forces
/// `rules::fold_target_decisions` to at least `Ask` for it — an
/// unresolvable known-writer target is NEVER silently dropped to an empty
/// target list (which the caller would otherwise treat as "no write to
/// check", i.e. a silent `Allow`).
const UNRESOLVABLE_WRITE_TARGET: &str = "$<unresolvable-known-writer-target>";
/// See [`bundled_flag_scan`]. `NotPresent` when the target flag never
/// appears in `argv` at all; `Value` when confidently resolved to a literal
/// (possibly an empty string for a [`FlagValueMode::OptionalAttached`] flag
/// given bare, e.g. sed's bare `-i`); `Unresolvable` — the fail-closed floor
/// — when the flag is present in SOME form this scan cannot confidently
/// resolve to a value (a mandatory-value flag with nothing following it, or
/// a bundled short-flag group whose earlier letters this writer's safe-flag
/// table doesn't vouch for as zero-value).
/// See [`bundled_flag_scan`]'s doc comment for the GNU semantics each mode
/// encodes.
/// The result of [`bundled_flag_scan`]: see its doc comment.
/// **Class-closing shared helper** (round-4, Fable-5 adversarial review):
/// round-3's fix for bypass #1 (see [`target_directory_writer_targets`])
/// only made `cp`/`mv`/`install`/`ln`'s `-t`/`--target-directory` detection
/// getopt-bundled-short-flag aware — leaving the SAME blind spot open on
/// `sed -i`/`--in-place` and `sort -o`/`--output`, whose detection was still
/// each its own ad-hoc `a == "-x" || a.starts_with("-x")` scan that only
/// recognizes the flag as the FIRST letter of a token. `sed -ni
/// s/../PWNED/ .env` (bundled `-n` + `-i`) and `sort -uo .env a` (bundled
/// `-u` + `-o`) both silently `Allow`ed a real in-place/output write before
/// this fix, proven against real GNU sed/coreutils, even though the
/// unbundled `sed -i ...`/`sort -o ...` forms were already correctly
/// denied.
///
/// Rather than patch those two call sites individually — which would leave
/// a FIFTH flag-driven known-writer free to reintroduce the exact same
/// class of bug the next time one is added — every flag-driven known-writer
/// detection in this module (`-t` for cp/mv/install/ln, `-i` for sed, `-o`
/// for sort) is routed through this ONE generalized scan. A future
/// flag-driven writer only has to call it with its own (flag letter, long
/// name, [`safe_zero_value_short_flags_for`] table, [`FlagValueMode`]) to
/// inherit the same bundled-short-flag-aware detection, structurally
/// closing the class rather than leaving it a per-flag habit to remember.
///
/// Recognizes, for a target `flag_letter`/`long_name` pair, every
/// GNU-getopt-equivalent shape:
/// - the bare short flag (`-t`, `-o`, `-i`);
/// - the attached short form (`-tDIR`, `-oFILE`, `-i.bak`);
/// - the long form, spaced or `=`-joined (`--target-directory DIR`/
/// `--target-directory=DIR`, `--output FILE`/`--output=FILE`,
/// `--in-place`/`--in-place=.bak`);
/// - a getopt-BUNDLED short-flag group containing the letter anywhere
/// (`-ft DIR`, `-uo FILE`, `-ni`) — the letters BEFORE it in the same
/// token are checked against `safe_zero_value_short_flags` (this writer's
/// own verified table of short options documented to take no value); if
/// EVERY one of them is vouched for, the target letter unambiguously
/// starts consuming its own value at that position, exactly as real
/// getopt would parse it. If even one of them is NOT in that table — an
/// option this canonicalizer doesn't recognize, OR one it knows IS
/// value-taking — that earlier option might itself swallow the target
/// letter (or more) as ITS OWN value, so whether `flag_letter` is even
/// present as a flag here at all is genuinely ambiguous to a static,
/// non-getopt-implementing scan. Fail-closed: never guess —
/// [`FlagLookup::Unresolvable`], sticky for the whole scan (a LATER
/// unambiguous occurrence elsewhere in `rest` never un-flags an earlier
/// ambiguous one).
///
/// [`FlagValueMode`] governs whether a bare/bundled occurrence with no
/// attached value consumes a separate next token or never does — see its
/// own doc comment.
///
/// **Round-6 DEFECT-FIX (Fable-5 round-6 adversarial review):** a bare `--`
/// end-of-options marker STOPS the scan from recognizing `flag_letter`/
/// `long_name` in any form for every token after it — the identical rule
/// [`value_flag_aware_positionals`] already enforces for its own positional
/// walk. Before this fix, this scan kept reading option-shaped tokens after
/// `--` as live flags, even though real GNU getopt treats everything after
/// `--` as a literal operand, never an option. That let
/// [`target_directory_writer_targets`] (this scan's `cp`/`mv`/`install`/`ln`
/// `-t` caller) misread a real coreutils FILENAME that merely happened to
/// look like a bundled `-t` flag (`cp -- -vt a .git`: after `--`, `-vt` is a
/// SOURCE, not `-v -t`) as the target-directory flag, computing `Some(...)`
/// off a bogus DIR and SHADOWING the correct `--`-aware
/// [`positional_dest_writer_targets`] fallback that would have flagged
/// `.git` as DEST — a silent `Allow` for a real protected write, proven
/// against real GNU coreutils. Every token after `--` (the `--` token
/// itself is dropped, exactly like [`value_flag_aware_positionals`] drops
/// it) is passed straight into `remainder` unexamined, never matched against
/// `long_name`, `long_eq_prefix`, or the short-flag-bundle branch. This
/// applies uniformly to all three [`bundled_flag_scan`] call sites
/// (`-t`/`-i`/`-o`) for consistency, per this unit's build brief, even
/// though `sed -i`/`sort -o` had no positional-fallback sibling to be
/// shadowed by the old `--`-blind behavior — their own downstream
/// `remainder` filtering only ever over-blocks to `Ask`/`Deny` on a
/// dash-shaped post-`--` operand, never silently `Allow`s (see
/// [`known_writer_targets`]'s doc comment), so this was a consistency fix
/// there, not a second DEFECT-FIX.
///
/// Returns a [`FlagScan`]. A malformed occurrence — the flag present with
/// nothing following it in [`FlagValueMode::Separate`] mode — folds into
/// [`FlagLookup::Unresolvable`] too (never a bare empty-string
/// [`FlagLookup::Value`]), the same fail-closed floor as an ambiguous
/// bundle.
/// F4 "known argv-writers": commands whose argv is well-known enough that
/// the destination PATH they write to can be statically extracted without
/// opaque/`-c`-string ambiguity — a shell redirect (`>`) is not the only
/// way a bash command writes a file; `tee FILE`, `dd of=FILE`, `cp/mv/
/// install DEST`, `sed -i FILE`, `truncate FILE`, `ln … LINK_NAME`, `sort -o
/// FILE`, `split … PREFIX` all do it via an ordinary argv token the
/// redirect-extraction above never sees. Returns every path `argv` (already
/// wrapper-stripped) is known to WRITE to; `[]` if `argv`'s head isn't one
/// of these recognized writers, or is empty; `[`[`UNRESOLVABLE_WRITE_TARGET`]`]`
/// when the heuristic recognizes a write is happening but cannot confidently
/// name its destination token (bypass #1 fail-closed floor — see below).
///
/// **Every flag-driven writer below shares ONE detection helper**
/// (round-4, Fable-5 adversarial review — see [`bundled_flag_scan`]'s doc
/// comment for the full rationale): `cp`/`mv`/`install`/`ln`'s
/// `-t DIR`/`--target-directory=DIR` ([`target_directory_writer_targets`]),
/// `sed`'s `-i`/`--in-place` ([`sed_inplace_writer_targets`]), and `sort`'s
/// `-o FILE`/`--output=FILE` ([`sort_output_target`]) are all thin
/// call-sites over [`bundled_flag_scan`], each supplying only its own (flag
/// letter, long name, [`safe_zero_value_short_flags_for`] table, value
/// mode). Round-3 originally built the getopt-bundled-short-flag-aware
/// scan (`-ft DIR` recognized as `-f -t DIR`) ONLY for `-t`; round-4 found
/// the same blind spot still open on `sed -i`/`sort -o` (each was still its
/// own `a == "-x" || a.starts_with("-x")` scan that missed a bundle like
/// `-ni`/`-uo`) and closed it structurally by routing all three through the
/// shared helper — a future flag-driven writer inherits the fix for free
/// instead of needing its own bundled-flag audit.
///
/// **`cp`/`mv`/`install`/`ln` and `-t DIR`/`--target-directory=DIR`**
/// (bypass #1, Fable-5 delta review; extended round-3; generalized
/// round-4): GNU coreutils' target-directory flag relocates the destination
/// OFF the positional-argument list entirely — `cp -t DIR a b` writes
/// `DIR/a` and `DIR/b`; EVERY remaining positional is a SOURCE, not this
/// function's usual "last positional is the destination" shape.
/// [`target_directory_writer_targets`] recognizes this flag in every form
/// [`bundled_flag_scan`] understands (`-t DIR`, `-tDIR`,
/// `--target-directory=DIR`, `--target-directory DIR`, and a getopt-BUNDLED
/// short-flag group containing `t`, e.g. `-ft DIR`, `-sft DIR`, `-Dt DIR`,
/// `-ft.git`) and, when found, computes `DIR/basename(source)` for every
/// remaining source instead of falling through to the plain last-positional
/// heuristic. A concrete `DIR` is evaluated against `protected_paths` like
/// any other target; a `DIR` this canonicalizer can't resolve
/// ($VAR/glob/backtick) stays embedded in the computed `DIR/basename` text,
/// so [`is_concrete_path_text`] still forces `Ask` on it downstream — no
/// separate check needed here. A malformed invocation (the flag present
/// with no value, no sources left, or a bundle whose letters can't be
/// confidently classified) returns [`UNRESOLVABLE_WRITE_TARGET`] rather
/// than guess.
///
/// **`sed -i`/`--in-place`** (round-4 DEFECT-FIX, Fable-5 round-4
/// adversarial review): [`sed_inplace_writer_targets`] recognizes `-i` in
/// every form [`bundled_flag_scan`] understands (bare, bundled like `-ni`/
/// `-Ei`/`-zi`/`-sni`, attached-suffix like `-i.bak`/`-ni.bak`, and the long
/// `--in-place[=SUFFIX]` form) — previously only the FIRST-letter shape
/// (`-i`, `-i.bak`, `--in-place`) was recognized, so a bundle like `-ni`
/// (`sed -ni s/../PWNED/ .env`) silently fell through to `Vec::new()` (no
/// target flagged -> a silent `Allow` for a real in-place write). Presence
/// in ANY form means in-place editing is happening; the write targets are
/// the sed script's FILE operand(s) (every positional after the first, the
/// script) — best-effort, see this function's "Named residual" note below.
///
/// **`sort -o FILE`** (round-3 hardening as a new addition, generalized
/// round-4; round-4 ALSO closed the same bundled-flag blind spot here as
/// `sed -i` above — `sort -uo .env a`/`-ro`/`-bo` previously fell through to
/// `Vec::new()`): [`sort_output_target`] recognizes `-o FILE`/
/// `--output=FILE`/`--output FILE` in every form [`bundled_flag_scan`]
/// understands, including a getopt-bundled group like `-uo FILE`.
///
/// **`split … PREFIX`** (round-5 DEFECT-FIX, Fable-5 round-5 adversarial
/// review — the LAST out-of-class residual after round-4 structurally closed
/// the bundled-short-flag CLASS; NOT flag-driven, so it does not go through
/// [`bundled_flag_scan`], which is built to resolve exactly ONE target
/// flag's value, not a whole positional-operand list): the `"split"` arm
/// below calls [`split_writer_targets`], which is
/// [`value_flag_aware_positionals`]-based (see that function's doc comment
/// for the shared GNU-option-permutation-aware walker both this and the
/// round-5 `cp`/`mv`/`install`/`ln` fix below are built on) and correctly
/// identifies `split`'s `[INPUT [PREFIX]]` positionals — PREFIX (the
/// write-target base for `PREFIXaa`, `PREFIXab`, …; the literal default `x`
/// when omitted) is checked regardless of where `split`'s value-taking flags
/// (`-a`/`-b`/`-C`/`-l`/`-n`/`-t`, `--additional-suffix`, `--filter`, …) land
/// in the argv, including AFTER the positionals (GNU option permutation).
/// Round-3 originally added a `split` arm with a naive `!starts_with('-')`
/// positional filter that did NOT parse split's value-taking flags at all;
/// round-5 proved that gap exploitable against real GNU coreutils:
/// `split a .env -b 100` (permuted — the unbundled `split -b 100 a .env`
/// form was already correctly denied) let `-b`'s value token `100` get
/// miscounted as the PREFIX positional instead of the true PREFIX `.env`,
/// a silent `Allow` for a real write to `.env` (`.envaa`, …). The doc
/// comment on that round-3 arm previously claimed this miscount "only ever
/// shifts which token this heuristic flags — the fail-closed floor
/// (`is_concrete_path_text`) still holds regardless"; that claim was FALSE —
/// the shift moved the flagged token from the protected `.env` to the
/// concrete, non-protected `100`, which is a silent `Allow`, not an `Ask`.
/// [`split_writer_targets`] now fails closed
/// ([`UNRESOLVABLE_WRITE_TARGET`]) instead of guessing whenever the argv
/// shape can't be confidently parsed (an unrecognized flag, a value-taking
/// flag with nothing following it, …).
///
/// **`cp`/`mv`/`install`/`ln`'s plain (non-`-t`) DEST positional**
/// (round-5 DEFECT-FIX, found during this unit's mandated audit of every
/// OTHER known-writer that identifies its target as a positional, prompted
/// by the `split` fix above): the SAME GNU-option-permutation miscount class
/// was open here too — the previous `positional().next_back()` fallback
/// (used whenever [`target_directory_writer_targets`] confirms `-t`/
/// `--target-directory` is absent) never parsed `-S`/`--suffix=SUFFIX`
/// (`cp`/`mv`/`ln`) or `-g`/`-m`/`-o`/`--group`/`--mode`/`--owner`
/// (`install`), all value-taking. Proven against real GNU coreutils:
/// `install a .env -m 644` (MODE's value `644` trailing the true DEST
/// `.env`) writes `.env` with the new mode, but the old heuristic picked
/// `644` as the "destination" — a silent `Allow`.
/// [`positional_dest_writer_targets`] (also
/// [`value_flag_aware_positionals`]-based) closes this the same way `split`
/// was closed, and fails closed on any unrecognized flag rather than guess.
///
/// **Named residual** (honesty, per this unit's build brief): beyond the
/// cases named above, this is still a best-effort heuristic, not a full
/// argument-grammar parser for each of these tools — e.g. `sed`'s "-i
/// implies every positional after the first (the script) is a target"
/// heuristic doesn't distinguish a `-e`/`-f`-supplied external script from a
/// positional one. That is a false NEGATIVE (a write this function fails
/// to flag) — never a false positive that would over-block a legitimate
/// write, and never a silent `Allow` for a write this function DOES
/// recognize but can't resolve (that path always returns
/// [`UNRESOLVABLE_WRITE_TARGET`], per the fail-closed law above) — the
/// caller ([`crate::permissions::rules::evaluate_command`]) still applies
/// its own fail-closed floor for anything genuinely unresolvable (see
/// [`is_concrete_path_text`]). Full OS-level write confinement of arbitrary
/// bash argv — every unenumerated writer this heuristic doesn't name at
/// all (an interpreter's own file-write builtins, a compiler's `-o`, a
/// database client's export command, …) is a false NEGATIVE at this
/// rule layer today, not a silently-claimed-covered case — is
/// `permissions.sandbox`'s job (P5 module 10, a later unit), not this
/// rule-layer heuristic's.
///
/// **Confirmed NOT vulnerable to this class** (round-4 audit, reconfirmed
/// round-5 against real GNU coreutils — `truncate .env -s 0` permuted still
/// truncates `.env`): `tee`/`truncate` take EVERY non-flag positional as a
/// write target directly — they have no flag whose VALUE designates the
/// SOLE destination the way `split`'s PREFIX or `cp`/`mv`/`install`/`ln`'s
/// DEST does (GNU `tee`'s only short flags — `-a`, `-i`, `-p` — are all
/// zero-value; `--output-error[=MODE]` is long-only; `truncate`'s
/// `-s`/`--size=SIZE` and `-r`/`--reference=RFILE` ARE value-taking, but a
/// permuted `truncate .env -s 0` still leaves `.env` in the filtered
/// positional list alongside `0` — over-inclusive, not under-inclusive, so
/// the real target is never dropped), so there is no flag-value-hiding shape
/// that can make the true target vanish. `dd` designates its target via a
/// `key=value` token (`of=FILE`), not getopt short-flag syntax, so getopt
/// bundling does not apply to it at all. Neither is routed through
/// [`bundled_flag_scan`] or [`value_flag_aware_positionals`] — there is
/// nothing for either to close there.
pub
/// The GNU short-option LETTERS documented as taking NO value, for each
/// flag-driven known-writer's OWN target flag — verified against each
/// command's real `--help` output (GNU coreutils' `cp`/`mv`/`ln`/`install`,
/// round-3 audited; GNU sed, GNU coreutils' `sort`, round-4 audited).
/// Deliberately EXCLUDES every value-taking short option for that writer
/// (see each arm's comment) as well as the target flag's own letter
/// (`t`/`i`/`o` respectively — handled by [`bundled_flag_scan`]'s caller).
/// Used by [`bundled_flag_scan`] to decide whether a bundled short-flag
/// group's letters BEFORE the target letter can be trusted not to have
/// already consumed its value slot: if every one of them is in this set,
/// they are all known zero-value flags, so the target letter unambiguously
/// starts consuming its own value at that position; if a bundle contains
/// anything else, the bundle is ambiguous and the caller fails closed to
/// [`FlagLookup::Unresolvable`] rather than guess. Unknown command names
/// (not one of the six below) return an empty string, so `.contains` is
/// vacuously false and every bundle for them is treated as ambiguous.
///
/// Round-5: also reused as-is by [`positional_dest_writer_targets`]'s
/// `zero_short` table for `cp`/`mv`/`ln`/`install` — the exact same
/// "every OTHER short letter this writer supports is zero-value" fact holds
/// regardless of which specific flag (`-t`, or the plain positional DEST)
/// is being resolved, so one audited table serves both call sites.
/// Bypass #1 helper (Fable-5 delta review; round-3 bundled-flag extension;
/// round-4 generalized onto [`bundled_flag_scan`], the class-closing shared
/// helper — see its doc comment): recognizes GNU coreutils' `-t DIR` /
/// `-tDIR` / `--target-directory=DIR` / `--target-directory DIR` and a
/// getopt-BUNDLED short-flag group whose letters include `t` (`-ft DIR`,
/// `-sft DIR`, `-Dt DIR`, `-ft.git`, …) anywhere in `rest` (a `cp`/`mv`/
/// `install`/`ln` invocation's args, already wrapper-stripped and past the
/// command name; `head` is the command name itself, needed to pick the
/// right [`safe_zero_value_short_flags_for`] set). Returns `None` when no
/// `t`-bearing flag form is present at all (the caller falls back to its
/// normal last-positional heuristic). Returns `Some(vec![...])` otherwise —
/// the computed `DIR/basename(source)` write target for every remaining
/// non-flag token, or `[`[`UNRESOLVABLE_WRITE_TARGET`]`]` (never an empty
/// `Vec`, which the caller would read as "no write to check") when the flag
/// was given with no value, left no sources to combine it with, or a
/// bundle's letters couldn't be confidently classified.
///
/// **Round-3 (Fable-5 adversarial review, bundled short-flag bypass):**
/// `cp -ft .git a` (getopt-equivalent to `cp -f -t .git a`) previously fell
/// through every branch here — `-ft` matches none of the plain `-t`/
/// `--target-directory` shapes — straight to the plain last-positional
/// heuristic, which picked `a` (a SOURCE) as the "destination" while real
/// coreutils wrote `.git/a`. Proven against real coreutils before the fix.
/// Round-3 hardening as a new addition (`sort -o FILE`), round-4 DEFECT-FIX
/// (generalized onto [`bundled_flag_scan`] — was previously its own
/// hand-rolled `==`/`starts_with` scan that only recognized `-o` as the
/// FIRST letter of a token, missing a getopt-bundled form like `-uo FILE`/
/// `-ro FILE`/`-bo FILE` — the same blind spot round-3 had already closed
/// for `-t` but not yet generalized here; proven against real GNU coreutils,
/// `sort -uo .env a` silently `Allow`ed before this fix). `FILE` is where
/// `sort` writes its sorted output, even when `FILE` is ALSO one of the
/// inputs being read (`sort -o a a` sorts `a` in place). Returns `[]` when
/// no output flag is present at all (nothing to flag — `sort` with no `-o`
/// writes to stdout), or `[`[`UNRESOLVABLE_WRITE_TARGET`]`]` when the flag
/// is present in a form [`bundled_flag_scan`] can't confidently resolve
/// (no value at all, or an ambiguous bundle — e.g. `-So FILE`: `-S` is
/// sort's OWN value-taking `--buffer-size`, so a bundled `-So` can't be
/// trusted to mean "`-o`'s value slot starts right after `S`").
/// Round-4 DEFECT-FIX (Fable-5 round-4 adversarial review, the SAME
/// bundled-short-flag blind spot round-3 closed for `-t`, found still open
/// here): `sed -i`/`--in-place`'s in-place-edit detection generalized onto
/// [`bundled_flag_scan`] — was previously its own
/// `a == "-i" || a.starts_with("-i")` scan, which only recognizes `-i` as
/// the FIRST character of a token, missing every getopt-bundled form
/// (`-ni`, `-Ei`, `-zi`, `-sni`, `-ni.bak`). Proven against real GNU sed:
/// `sed -ni s/../PWNED/ .env` silently `Allow`ed a real in-place write to
/// `.env` before this fix, even though the unbundled `sed -i s/a/b/ .env`
/// form was already correctly denied.
///
/// GNU sed's `-i[SUFFIX]`/`--in-place[=SUFFIX]` value is OPTIONAL and
/// attached-only (never a separate token — [`FlagValueMode::
/// OptionalAttached`]), so presence in ANY form (bundled or not, with or
/// without an attached suffix) means in-place editing is happening; the
/// SUFFIX's own value is irrelevant to which file gets written (it only
/// names an extra backup copy this rule layer doesn't separately track —
/// named residual, matches this module's existing honesty note). The write
/// targets are the sed script's FILE operand(s) — every positional after
/// the first (the script), best-effort exactly like before this fix (see
/// [`known_writer_targets`]'s doc comment's residual note: a `-e`/`-f`-
/// supplied external script isn't distinguished from a positional one).
///
/// Returns `[]` when `-i` never appears (`sed` without `-i` reads and
/// writes to stdout — no write target, no over-block), or
/// `[`[`UNRESOLVABLE_WRITE_TARGET`]`]` when [`bundled_flag_scan`] can't
/// confidently resolve whether `-i` is even present (an ambiguous bundle —
/// e.g. `-ei` is GNU sed's OWN `-e` with attached script value `"i"`, not
/// `-e -i`, so `sed`'s own value-taking short flags `e`/`f`/`l` are
/// deliberately excluded from [`safe_zero_value_short_flags_for`]'s `"sed"`
/// table). Fail-closed, per this unit's law: over-detection (`Ask`) is an
/// acceptable cost for `sed` — a silent `Allow` is not.
/// **Round-5, shared class-closing helper**: a GNU-getopt-permutation-aware
/// POSITIONAL-operand extractor for a known-writer whose write target is a
/// positional argument itself (not a flag's own value, unlike `-t`/`-i`/`-o`
/// above, so [`bundled_flag_scan`] — built to resolve exactly ONE named
/// target flag — does not apply) but whose argv also carries OTHER,
/// value-taking flags. GNU getopt allows options to trail operands
/// ("permutation" — `split a .env -b 100` parses identically to
/// `split -b 100 a .env`; `install a .env -m 644` identically to
/// `install -m 644 a .env`), so a naive `!starts_with('-')` filter over the
/// whole argv silently miscounts a trailing value-taking flag's OWN VALUE
/// token (`100`, `644`) as an extra positional — shifting which token a
/// last-positional/Nth-positional heuristic reads off the END of the list.
/// Proven against real GNU coreutils for both callers (see
/// [`known_writer_targets`]'s doc comment for the two proven rows).
///
/// Walks the WHOLE `rest` list (not just a prefix — permutation means a
/// value-taking flag can appear anywhere, before OR after the true
/// positionals), consuming:
/// - `--`: GNU's end-of-options marker — every token after it is a literal
/// positional, even one that starts with `-`;
/// - a lone `-` token: always a positional (`split`'s "read stdin" marker) —
/// never mistaken for a flag (`tok.len() > 1` gates the flag-parsing
/// branches below, exactly like [`bundled_flag_scan`]'s own guard);
/// - a long flag (`--name`, optionally `=value`-joined): matched against
/// `value_taking_long` (mandatory value — consumes an `=`-joined value on
/// the SAME token, or the next SEPARATE token when there's no `=`; nothing
/// following in [`FlagValueMode::Separate`]-style — ambiguous, fails
/// closed), `optional_long` (value ONLY via `=`; a bare occurrence
/// consumes nothing — real GNU getopt_long semantics: an optional-argument
/// long option's value is NEVER a separate token, confirmed against real
/// `cp --backup numbered a b`, which treats `numbered` as a SOURCE, not
/// `--backup`'s value), or `zero_long` (never takes a value at all — an
/// attached `=value` on one of these is itself a malformed/unrecognized
/// shape, fails closed); an unrecognized long flag is ambiguous — this
/// scan cannot know whether it would swallow the next token as a value;
/// - a short-flag token (bundling-aware, exactly like [`bundled_flag_scan`]):
/// each letter in turn is looked up in `zero_short` (consumed, no value),
/// `optional_short` (attached value only, e.g. `split`'s `-d5`; bare `-d`
/// consumes nothing and never reaches for a separate token, mirroring the
/// long-flag optional-value rule above), or `value_short` (the rest of
/// THIS token if non-empty, else the next separate token); any letter in
/// none of these three sets is ambiguous — the same fail-closed floor
/// [`bundled_flag_scan`] uses for an unvouched bundle letter, since a
/// genuinely unrecognized short option's arity can't be known statically;
/// - anything else: an ordinary positional operand, appended in order.
///
/// Returns `None` — fail-closed, the caller must treat this as
/// [`UNRESOLVABLE_WRITE_TARGET`], never as "no positionals" — the instant
/// ANY token can't be confidently classified (a value-taking flag with
/// nothing following it, an unrecognized long flag or short letter).
/// Returns `Some(positionals)` (original order preserved) otherwise.
/// Over-`Ask` on a command shape this scan can't fully resolve is an
/// acceptable cost; a silent `Allow` that mis-locates the real write target
/// is not — the fail-closed law this whole module is built on.
/// Round-5 DEFECT-FIX (Fable-5 round-5 adversarial review — see
/// [`known_writer_targets`]'s doc comment for the full proven-bypass writeup
/// and [`value_flag_aware_positionals`]'s doc comment for the shared walker
/// this is built on): `split [OPTION]... [INPUT [PREFIX]]` writes
/// `PREFIXaa`, `PREFIXab`, … — `PREFIX` (the literal default `x` when
/// omitted — GNU coreutils' own documented default, carrying no
/// attacker-controlled text but still evaluated against `protected_paths`
/// like any other target, per this unit's build brief) is the write-target
/// base. Every one of `split`'s value-taking/optional/zero-value flags
/// (short AND long, audited against real GNU coreutils 9.1 `split --help`)
/// is named here so the true `[INPUT [PREFIX]]` positionals are found no
/// matter where those flags land in the argv (GNU option permutation).
/// Returns [`UNRESOLVABLE_WRITE_TARGET`] rather than guess when the argv
/// shape can't be confidently parsed.
/// Round-5 DEFECT-FIX (found during this unit's mandated audit of every
/// OTHER known-writer that identifies its target as a positional, prompted
/// by the `split` fix above — see [`known_writer_targets`]'s doc comment for
/// the full proven-bypass writeup): `cp`/`mv`/`install`/`ln`'s plain
/// (non-`-t`) DEST-is-the-last-positional fallback, now
/// [`value_flag_aware_positionals`]-based instead of a naive
/// `!starts_with('-')` filter, so a value-taking flag GNU-permuted after the
/// true DEST (`-S`/`--suffix=SUFFIX` for `cp`/`mv`/`ln`;
/// `-g`/`-m`/`-o`/`--group`/`--mode`/`--owner`/`--strip-program` for
/// `install`) can no longer have its VALUE token miscounted as the
/// destination. Every flag table below is audited against real GNU
/// coreutils `--help` output; `zero_short` reuses
/// [`safe_zero_value_short_flags_for`] (already the audited zero-value-short
/// table for this exact writer, minus `t` itself — see that function's own
/// doc comment) rather than duplicate it. Only called when
/// [`target_directory_writer_targets`] has already confirmed `-t`/
/// `--target-directory` is absent in every form it recognizes, so `'t'`
/// never appears as a live flag letter by the time this runs.
///
/// `min_positionals` preserves each writer's own pre-existing "is there
/// even a destination to name" gate: `cp`/`mv`/`install` flag a target once
/// there is at least one positional (a single SOURCE with an implicit
/// same-name DEST is not this heuristic's concern); `ln` needs at least two
/// (TARGET and LINK_NAME) — `ln TARGET` alone creates a link named
/// `basename(TARGET)` in the cwd, carrying no attacker-controlled token,
/// exactly like `split`'s default-PREFIX case above but, unlike `split`,
/// out of this round's scope to additionally flag. Returns
/// [`UNRESOLVABLE_WRITE_TARGET`] rather than guess when the argv shape can't
/// be confidently parsed, regardless of `min_positionals`.
/// Join a `-t`/`--target-directory` directory with one source's basename —
/// `cp -t DIR a` writes `DIR/a`, not `DIR/<full source path>` (coreutils
/// strips any leading directory components off the source before joining).
/// F4: is `text` a concrete, statically-known path literal — safe to match
/// against `protected_paths` rules directly — or does it still carry an
/// unexpanded shell construct (`$VAR`, `` `cmd` ``) or an unquoted glob
/// pathname-expansion metacharacter (`*`, `?`, `[`) whose real expanded
/// value this canonicalizer cannot know? A dynamic redirect/argv-writer
/// target (`tee $FILE`, `> "$OUT"`) that resolves to a protected path at
/// runtime must not be able to glob-match nothing and silently fall through
/// to `Allow` just because the LITERAL text `"$FILE"` doesn't look like
/// `.env` — the caller treats a non-concrete target as requiring `Ask`,
/// unconditionally (this module's fail-closed law applies regardless of
/// whether `protected_paths` specifically is configured — see the F4
/// build brief's honesty note on this crate's permissions module doc).
///
/// **Bypass #2 (Fable-5 delta review):** the same reasoning applies to a
/// glob target — bash pathname-expands an unquoted `*`/`?`/`[...]` in a
/// redirect/argv-writer target BEFORE the write happens (`echo x > .en*`
/// with `.env` already on disk writes `.env`), but the literal text `.en*`
/// doesn't glob-match a `write(.env*)` protected-path rule, so treating it
/// as "concrete" let a real write to `.env` sail past the rule as a silent
/// `Allow`. `{...}` brace expansion is deliberately NOT included here: an
/// unquoted brace with no comma/range inside (e.g. `.e{n}v`) is NOT
/// expanded by bash at all — it stays the literal filename `.e{n}v` — so
/// flagging bare `{`/`}` would over-block a legitimate non-protected write
/// for no real safety gain (confirmed against real bash).
pub
/// Extract one `command` node's argv (command name + arguments, redirects
/// skipped) and apply wrapper-stripping. F4 (Fable-5 adversarial review):
/// output/input redirect TARGET paths attached directly to this `command`
/// (or, for the last stage of a pipeline, attached to the enclosing
/// `redirected_statement`) are separately collected onto the returned
/// [`CanonSubcommand`] by `walk_collect_commands` — this function only
/// builds argv, since a redirect target is never an argv token.
/// The literal text of a `command_name` node — `None` if it can't be
/// resolved to a static literal (the caller treats that as a dynamic/
/// unresolvable command name, forcing `opaque`). F3/F5 (Fable-5 adversarial
/// review): unlike a plain argument, a command name is resolved through
/// [`dequote_command_name`] so that a backslash-escaped (`\rm`, `r\m`) or
/// fully-quoted (`'rm'`, `"rm"`, `$'rm'`) literal name normalizes to the
/// SAME text a deny/allow rule was written against — the exact mechanism
/// that closes both bypasses. Anything that isn't a clean literal (a mixed
/// concatenation like `r"m"`, an unresolved expansion like `"$x"`, an
/// ambiguous escape) stays `None` — fail-closed, never guessed.
/// Resolve a `command_name`'s single child to a literal string, or `None`
/// if it can't be safely resolved. See [`command_name_text`]'s doc comment.
/// Bash unquoted-backslash normalization (F3): outside quotes, a backslash
/// removes itself and makes the following character literal — `\rm` ->
/// `rm`, `r\m` -> `rm`, `rm\ x` -> `rm x` (the escaped space becomes a
/// literal space INSIDE one token, not an argument boundary). Returns
/// `None` — fail-closed, the caller treats this exactly like an
/// unresolvable dynamic name/argument — for a trailing lone backslash
/// (nothing left to escape): an ambiguous/malformed case, not something to
/// silently drop or leave as a stray backslash a rule glob wouldn't expect.
/// The literal text of an argument node. `string`/`raw_string` nodes keep
/// their INNER content only (quotes stripped) — this is the exact mechanism
/// that keeps a quoted separator from ever being mistaken for a real one:
/// `echo "a; b"` yields the single argv token `"a; b"` (semicolon included,
/// literally, as part of one argument), never two sub-commands, because the
/// `;` lives inside a `string_content` node, not as a sibling `;` operator.
///
/// F3: a bare `word` argument is bash-unescaped the same way a command name
/// is (see `unescape_word`) — `rm -rf \/` and `rm -rf /` must canonicalize
/// identically, or a rule matched against the normalized command name but
/// not its (still backslash-laden) arguments would be an inconsistency a
/// future bypass could exploit. Returns `None` — fail-closed — when that
/// normalization is ambiguous (a trailing lone backslash); the caller marks
/// the whole sub-command `opaque` rather than use the mangled text.
/// Repeatedly strip a leading process wrapper (see [`STRIPPABLE_WRAPPERS`])
/// from `argv`, re-evaluating the remaining tokens as the actual command —
/// handles stacked wrappers (`sudo timeout 5 nice rm -rf /`) up to
/// `MAX_WRAPPER_UNWRAPS` layers. Marks the result `opaque` (forced-Ask
/// floor, never silently `Allow`) when the final head is one of
/// [`OPAQUE_WRAPPERS`], or `find` invoked with `-exec`/`-delete` (cc§4: exec
/// wrappers always prompt — their real effect isn't statically known).
/// Remove leading `-`-prefixed flag tokens from `rest`, consuming each
/// flag's SEPARATE value token too when the flag is listed in
/// `value_taking` (e.g. `-n 10`, `-u root`) — an inline `flag=value` form
/// (which carries no separate value token) is left alone. Leaving a
/// separate value token un-consumed would POLLUTE the wrapper-stripped
/// argv — e.g. `nice -n 10 rm -rf /` stripping only `-n` would leave
/// `["10", "rm", "-rf", "/"]`, silently defeating a rule written against
/// `rm -rf*` since the canonicalized argv no longer starts with `rm`. That
/// is a bypass, not a safe over-strip, so every wrapper with a
/// separate-value flag must be listed here.
/// `NAME=value` assignment-prefix syntax `env`'s own argv uses.
/// See [`OPAQUE_WRAPPERS`] and [`strip_wrappers`]'s doc comment.
/// Process-wide guard so tests that touch process-global state (none today,
/// but every other `crate::permissions` test module that spawns a parser
/// concurrently benefits from a single documented seam) can serialize if a
/// future tree-sitter version ever needs it. Unused today — kept `pub(crate)`
/// only to document the seam rather than silently omitted.
pub static PARSE_SERIALIZE: = new;