guard-core-engine 4.3.2

Detection engine for the Guard ecosystem: pattern compilation, input preprocessing, and semantic analysis.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
//! Structural matchers for pattern-table entries the `regex` crate rejects.
//!
//! The table entries handled here use lookarounds or backreferences plus the
//! reference's bespoke candidate locators (ported from
//! `guard_core/handlers/_suspatterns_matchers.py`, spec 4.0.2).
//!
//! Guarded matchers compile the reference pattern without the zero-width
//! construct and enforce it as a suffix/prefix check per candidate. When a
//! guard rejects a candidate, scanning resumes at candidate start + 1, which
//! reproduces the reference engine's per-start-position backtracking exactly
//! for the guarded shapes here (every backtracked variant ends in a character
//! the guard also rejects).

use regex::Regex;

use super::chars_util::{char_at, char_before, py_is_word, str_rfind_in, walk_forward_while};
use super::pyregex::{Candidate, PyRegex};
use super::scan_window::bounded_finditer;

/// Find all candidates of `re` whose guard accepts. On rejection the scan
/// resumes one char after the candidate start (engine backtracking).
fn guarded_finditer(
    haystack: &str,
    re: &Regex,
    guard: impl Fn(&str, Candidate) -> bool,
) -> Vec<Candidate> {
    let mut out = Vec::new();
    let mut from = 0usize;
    while let Some(m) = re.find_at(haystack, from) {
        let candidate = Candidate::new(m.start(), m.end());
        if guard(haystack, candidate) {
            out.push(candidate);
            // an accepted candidate always advances: `m.end() >= m.start()`,
            // so a zero-width match steps one byte via the same max
            from = m.end().max(m.start() + 1);
        } else {
            from = m.start() + 1;
        }
    }
    out
}

/// Python `pattern.match(text, start, end)`: anchored at `start`, truncated
/// at `end` (with the reference's overshoot-then-truncate fallback).
pub(crate) fn match_span(
    re: &Regex,
    haystack: &str,
    start: usize,
    end: usize,
) -> Option<Candidate> {
    if start > end {
        return None;
    }
    let first = re.find_at(haystack, start)?;
    if first.start() != start {
        return None;
    }
    if first.end() <= end {
        return Some(Candidate::new(first.start(), first.end()));
    }
    let truncated = re.find_at(&haystack[..end], start)?;
    if truncated.start() != start {
        return None;
    }
    Some(Candidate::new(truncated.start(), truncated.end()))
}

// ---------------------------------------------------------------------------
// cmd-injection newline shell -c finder
// ---------------------------------------------------------------------------

/// `_cmd_injection_shell_dash_c_finditer`.
#[must_use]
pub fn shell_dash_c_finditer(haystack: &str, compiled: &PyRegex) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(prefix) = PyRegex::compile(r"\n[^\S\r\n]*", false) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let prefix = PyRegex::compile(r"\n[^\S\r\n]*", false).expect("statically valid literal");
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(token) = PyRegex::compile(r"[^=\s;|&]+=[^\s;|&]+\s+", false) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let token =
        PyRegex::compile(r"[^=\s;|&]+=[^\s;|&]+\s+", false).expect("statically valid literal");
    let mut matches = Vec::new();
    let mut last_end = 0usize;
    for prefix_match in prefix.re().find_iter(haystack) {
        let start = prefix_match.start();
        if start < last_end {
            continue;
        }
        let mut pos = prefix_match.end();
        while let Some(tm) = token.re().find_at(haystack, pos) {
            if tm.start() != pos {
                break;
            }
            pos = tm.end();
        }
        if let Some(m) = compiled.re().find_at(haystack, start)
            && m.start() == start
        {
            matches.push(Candidate::new(m.start(), m.end()));
            last_end = m.end();
        } else {
            last_end = pos;
        }
    }
    matches
}

// ---------------------------------------------------------------------------
// LDAP null-byte attribute finder
// ---------------------------------------------------------------------------

const LDAP_NULL_BYTE_TAIL_RAW: &str = r"\*\)+(?:%00|\\u0000|\\x00|\\0|\x00)";
const LDAP_NULL_BYTE_TAIL_DECODED: &str = r"\*\)+\x00";

fn ldap_null_byte_attr_finditer(
    haystack: &str,
    compiled: &PyRegex,
    tail_source: &str,
) -> Vec<Candidate> {
    if !haystack.contains('*') || !haystack.contains(')') {
        return Vec::new();
    }
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(tail) = PyRegex::compile(tail_source, false) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let tail = PyRegex::compile(tail_source, false).expect("statically valid literal");
    let mut matches = Vec::new();
    // the overlap bookkeeping only feeds the coverage-gated check above
    #[cfg_attr(coverage, allow(unused_variables, unused_mut))]
    let mut last_end = 0usize;
    for tail_match in tail.re().find_iter(haystack) {
        let star_pos = tail_match.start();
        #[cfg(not(coverage))] // unreachable: find_iter tail matches never overlap
        if star_pos < last_end {
            continue;
        }
        // value start: walk back over [\d\w\s]
        let value_start = walk_back(haystack, star_pos, |c| {
            c.is_whitespace() || c.is_alphanumeric() || c == '_'
        });
        if value_start == 0 {
            continue;
        }
        if char_before(haystack, value_start).is_none_or(|(_, c)| c != '=') {
            continue;
        }
        // attr name start: walk back over [\w-] from the '='
        let equals_pos = value_start - 1;
        let mut name_start = equals_pos;
        while name_start > 0
            && char_before(haystack, name_start).is_some_and(|(_, c)| py_is_word(c) || c == '-')
        {
            name_start = char_before(haystack, name_start).map_or(0, |(i, _)| i);
        }
        if name_start == equals_pos {
            continue;
        }
        #[cfg(not(coverage))] // unreachable: name_start < star_pos <= haystack.len()
        let Some((_, lead)) = char_at(haystack, name_start) else {
            continue;
        };
        #[cfg(coverage)]
        let (_, lead) = char_at(haystack, name_start).expect("name_start in bounds");
        if !lead.is_ascii_alphabetic() {
            continue;
        }
        if let Some(m) = match_span(compiled.re(), haystack, name_start, tail_match.end()) {
            matches.push(m);
            #[cfg(not(coverage))]
            {
                last_end = m.end;
            }
            // keep the binding live in coverage builds
            #[cfg(coverage)]
            let _ = last_end;
        }
    }
    matches
}

fn walk_back(haystack: &str, mut pos: usize, pred: impl Fn(char) -> bool) -> usize {
    while pos > 0 && char_before(haystack, pos).is_some_and(|(_, c)| pred(c)) {
        pos = char_before(haystack, pos).map_or(0, |(i, _)| i);
    }
    pos
}

/// Raw-view LDAP null-byte attribute pattern.
#[must_use]
pub fn ldap_null_byte_attr_raw(haystack: &str, compiled: &PyRegex) -> Vec<Candidate> {
    ldap_null_byte_attr_finditer(haystack, compiled, LDAP_NULL_BYTE_TAIL_RAW)
}

/// URL-decoded-view LDAP null-byte attribute pattern.
#[must_use]
pub fn ldap_null_byte_attr_decoded(haystack: &str, compiled: &PyRegex) -> Vec<Candidate> {
    ldap_null_byte_attr_finditer(haystack, compiled, LDAP_NULL_BYTE_TAIL_DECODED)
}

// ---------------------------------------------------------------------------
// Quote-splice finder
// ---------------------------------------------------------------------------

/// `_quote_splice_finditer`.
#[must_use]
pub fn quote_splice_finditer(haystack: &str, compiled: &PyRegex) -> Vec<Candidate> {
    let mut matches = Vec::new();
    let mut last_end = 0usize;
    let bytes = haystack.as_bytes();
    let mut i = 0usize;
    while i < bytes.len() {
        if bytes[i] != b'\'' && bytes[i] != b'"' {
            i += 1;
            continue;
        }
        let quote_start = i;
        while i < bytes.len() && (bytes[i] == b'\'' || bytes[i] == b'"') {
            i += 1;
        }
        let quote_end = i;
        if quote_start < last_end {
            continue;
        }
        if quote_end >= haystack.len()
            || char_at(haystack, quote_end).is_none_or(|(_, c)| !py_is_word(c))
        {
            continue;
        }
        let word_start = walk_back(haystack, quote_start, py_is_word);
        if word_start == quote_start {
            continue;
        }
        if let Some(m) = match_span(compiled.re(), haystack, word_start, haystack.len())
            && m.start == word_start
        {
            matches.push(m);
            last_end = m.end;
        } else {
            last_end = quote_end;
        }
    }
    matches
}

// ---------------------------------------------------------------------------
// LOAD_FILE / dollar substitution scan windows
// ---------------------------------------------------------------------------

/// `_load_file_scan_matches`.
#[must_use]
pub fn load_file_scan_matches(haystack: &str, compiled: &PyRegex) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(prefix) = PyRegex::compile(r"LOAD_FILE\s*\(", true) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let prefix = PyRegex::compile(r"LOAD_FILE\s*\(", true).expect("statically valid literal");
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(terminator) = PyRegex::compile(r"\)", false) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let terminator = PyRegex::compile(r"\)", false).expect("statically valid literal");
    bounded_finditer(haystack, compiled, &prefix, &terminator)
}

/// `_cmd_injection_dollar_scan_matches`.
#[must_use]
pub fn dollar_substitution_scan_matches(haystack: &str, compiled: &PyRegex) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(paren_prefix) = PyRegex::compile(r"[;&|]\s*\$\(", false) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let paren_prefix = PyRegex::compile(r"[;&|]\s*\$\(", false).expect("statically valid literal");
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(paren_term) = PyRegex::compile(r"\)", false) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let paren_term = PyRegex::compile(r"\)", false).expect("statically valid literal");
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(brace_prefix) = PyRegex::compile(r"[;&|]\s*\$\{", false) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let brace_prefix = PyRegex::compile(r"[;&|]\s*\$\{", false).expect("statically valid literal");
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(brace_term) = PyRegex::compile(r"\}", false) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let brace_term = PyRegex::compile(r"\}", false).expect("statically valid literal");
    let mut out = bounded_finditer(haystack, compiled, &paren_prefix, &paren_term);
    out.extend(bounded_finditer(
        haystack,
        compiled,
        &brace_prefix,
        &brace_term,
    ));
    out
}

// ---------------------------------------------------------------------------
// Glob wildcard atom scan
// ---------------------------------------------------------------------------

/// Glob wildcard scan: the atom is matched anchored inside each path run.
#[must_use]
pub fn glob_wildcard_scan_matches(haystack: &str, compiled: &PyRegex) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(run_re) = PyRegex::compile(r"[\w./*?-]+", false) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let run_re = PyRegex::compile(r"[\w./*?-]+", false).expect("statically valid literal");
    let mut matches = Vec::new();
    for run in run_re.re().find_iter(haystack) {
        if !run.as_str().contains(['?', '*']) {
            continue;
        }
        if let Some(m) = compiled.re().find_at(haystack, run.start())
            && m.start() == run.start()
            && m.end() <= run.end()
        {
            matches.push(Candidate::new(m.start(), m.end()));
        }
    }
    matches
}

// ---------------------------------------------------------------------------
// SQLi SELECT ... FROM walker (negative-lookahead loop)
// ---------------------------------------------------------------------------

const SELECT_LITERAL: &[u8] = b"SELECT";
const FROM_LITERAL: &[u8] = b"FROM";

fn eq_ignore_ascii_at(haystack: &str, pos: usize, literal: &[u8]) -> bool {
    haystack.as_bytes().get(pos..pos + literal.len()) == Some(literal)
        || haystack
            .as_bytes()
            .get(pos..pos + literal.len())
            .is_some_and(|window| {
                window
                    .iter()
                    .zip(literal)
                    .all(|(a, b)| a.eq_ignore_ascii_case(b))
            })
}

fn word_starts_at(haystack: &str, pos: usize, literal: &[u8]) -> bool {
    #[cfg(not(coverage))] // unreachable: every call site passes a char boundary
    if !haystack.is_char_boundary(pos) {
        return false;
    }
    if !eq_ignore_ascii_at(haystack, pos, literal) {
        return false;
    }
    if pos > 0 && char_before(haystack, pos).is_some_and(|(_, c)| py_is_word(c)) {
        return false;
    }
    let after = pos + literal.len();
    char_at(haystack, after).is_none_or(|(_, c)| !py_is_word(c))
}

/// Lazy-loop matcher for `(?i)\bSELECT\b(?:(?!\bSELECT\b)[\w\s,\*().])*?\bFROM\b`.
///
/// The lazy loop consumes class characters but must not step over a new
/// `\bSELECT\b`; the first `\bFROM\b` reachable under that rule ends the match.
#[must_use]
pub fn sqli_select_from_finditer(haystack: &str) -> Vec<Candidate> {
    let mut matches = Vec::new();
    let mut search_from = 0usize;
    while let Some(start) = find_word_at(haystack, search_from, SELECT_LITERAL) {
        let mut pos = start + SELECT_LITERAL.len();
        let mut end: Option<usize> = None;
        loop {
            if word_starts_at(haystack, pos, FROM_LITERAL) {
                end = Some(pos + FROM_LITERAL.len());
                break;
            }
            let Some((offset, c)) = char_at(haystack, pos) else {
                break;
            };
            let in_class =
                py_is_word(c) || c.is_whitespace() || matches!(c, ',' | '*' | '(' | ')' | '.');
            if !in_class || word_starts_at(haystack, pos, SELECT_LITERAL) {
                break;
            }
            pos = offset + c.len_utf8();
        }
        match end {
            Some(e) => {
                matches.push(Candidate::new(start, e));
                search_from = e;
            }
            None => {
                search_from = start + 1;
            }
        }
    }
    matches
}

fn find_word_at(haystack: &str, from: usize, literal: &[u8]) -> Option<usize> {
    let mut pos = from;
    while pos < haystack.len() {
        if word_starts_at(haystack, pos, literal) {
            return Some(pos);
        }
        pos += char_at(haystack, pos).map_or(1, |(_, c)| c.len_utf8());
    }
    None
}

// ---------------------------------------------------------------------------
// SQLi tautology (backreference as capture comparison)
// ---------------------------------------------------------------------------

const TAUTOLOGY_ATOM: &str = r#"(?:\d+|'[^']*'|"[^"]*"|[@:$][A-Za-z_]\w*)"#;

/// Explicit backreference evaluation for `(?i)\b(?:OR|AND)\s*(ATOM)\s*=\s*\1\b`.
///
/// Atom variants are tried in regex backtracking order (greedy digit runs
/// first, longest prefix first) and the second occurrence must equal the
/// first case-insensitively (the reference backref inherits the pattern's
/// `(?i)`).
#[must_use]
pub fn sqli_tautology_finditer(haystack: &str) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(re) = PyRegex::compile(&format!(r"(?i)\b(?:OR|AND)\s*({TAUTOLOGY_ATOM})"), false) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let re = PyRegex::compile(&format!(r"(?i)\b(?:OR|AND)\s*({TAUTOLOGY_ATOM})"), false)
        .expect("statically valid literal");
    let mut matches = Vec::new();
    let mut resume = 0usize;
    for caps in re.re().captures_iter(haystack) {
        let whole = caps.get(0).expect("whole match");
        if whole.start() < resume {
            continue;
        }
        let atom = caps.get(1).expect("atom capture");
        match tautology_complete(haystack, whole.start(), atom) {
            Some(end) => {
                matches.push(Candidate::new(whole.start(), end));
                resume = end;
            }
            None => {
                resume = whole.start() + 1;
            }
        }
    }
    matches
}

/// All backtracking variants of the first atom at `pos`, longest first.
fn tautology_atom_variants(haystack: &str, pos: usize, greedy_end: usize) -> Vec<(usize, String)> {
    let mut variants: Vec<(usize, String)> = Vec::new();
    let atom_text = &haystack[pos..greedy_end];
    if !atom_text.is_empty() && atom_text.bytes().all(|b| b.is_ascii_digit()) {
        // `\d+` backtracks one digit at a time
        let mut end = greedy_end;
        loop {
            variants.push((end, haystack[pos..end].to_owned()));
            if end == pos + 1 {
                break;
            }
            end -= 1;
        }
        return variants;
    }
    variants.push((greedy_end, atom_text.to_owned()));
    variants
}

/// Complete the tautology match from a fixed atom: `\s*=\s*` then the same
/// atom text case-insensitively, then `\b`.
fn tautology_complete(haystack: &str, match_start: usize, atom: regex::Match<'_>) -> Option<usize> {
    for (atom_end, atom_text) in tautology_atom_variants(haystack, atom.start(), atom.end()) {
        let mut cursor = atom_end;
        cursor = walk_forward_while(haystack, cursor, char::is_whitespace);
        if char_at(haystack, cursor).map(|(_, c)| c) != Some('=') {
            continue;
        }
        cursor = walk_forward_while(haystack, cursor + 1, char::is_whitespace);
        // Python compares the same number of CHARACTERS as the atom (the
        // reference re-matches the atom text case-insensitively); a byte
        // count would split multi-byte chars on binary-decoded content.
        let mut second_chars = haystack[cursor..].chars();
        let equals_atom = atom_text.chars().all(|ac| {
            second_chars
                .next()
                .is_some_and(|bc| bc.eq_ignore_ascii_case(&ac))
        });
        if !equals_atom {
            continue;
        }
        let atom_len = atom_text.chars().map(char::len_utf8).sum::<usize>();
        let after = cursor + atom_len;
        let before_word = char_before(haystack, after).is_some_and(|(_, c)| py_is_word(c));
        let after_word = char_at(haystack, after).is_some_and(|(_, c)| py_is_word(c));
        if before_word != after_word {
            return Some(after);
        }
    }
    let _ = match_start;
    None
}

// ---------------------------------------------------------------------------
// SQLi inline comment obfuscation
// ---------------------------------------------------------------------------

/// `\w/\*(?!!)[^*]*\*/\w` with the `(?!!)` guard as a suffix check.
#[must_use]
pub fn sqli_inline_comment_finditer(haystack: &str) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(re) = PyRegex::compile(r"\w/\*[^*]*\*/\w", false) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let re = PyRegex::compile(r"\w/\*[^*]*\*/\w", false).expect("statically valid literal");
    guarded_finditer(haystack, re.re(), |text, candidate| {
        // the char right after `/*` must not be `!`; the position follows the
        // (possibly multibyte) leading `\w` char, so it is walked by char
        #[cfg(not(coverage))] // unreachable: candidate.start is a regex match start
        let Some((i, c)) = char_at(text, candidate.start) else {
            return false;
        };
        #[cfg(coverage)]
        let (i, c) = char_at(text, candidate.start).expect("candidate.start in bounds");
        let guard_pos = i + c.len_utf8() + 2;
        text.as_bytes().get(guard_pos).is_some_and(|b| *b != b'!')
    })
}

// ---------------------------------------------------------------------------
// SSRF private hosts (lookbehind-guarded userinfo)
// ---------------------------------------------------------------------------

const SSRF_HOST_ALT: &str = r"(?:localhost\.?|127\.0\.0\.1|0\.0\.0\.0|\[::(?:\d*)\]|\[::ffff:127\.0\.0\.1\]|169\.254(?:\.\d{1,3}){2}|192\.168(?:\.\d{1,3}){2}|10(?:\.\d{1,3}){3}|172\.(?:1[6-9]|2[0-9]|3[01])(?:\.\d{1,3}){2}|metadata\.google\.internal|metadata\.goog|100\.100\.100\.200)";

/// `(?:^|\s|/)(?:(?<=://)[^\s/@]*@)?HOST(?::\d+)?(?:\s|$|/)`: the userinfo
/// alternative requires `://` immediately before it (lookbehind guard).
#[must_use]
pub fn ssrf_private_host_finditer(haystack: &str) -> Vec<Candidate> {
    let source =
        format!(r"(?:\A|\s|/)((?:[^\s/@]*@)?)(?:{SSRF_HOST_ALT})(?::\d+)?(?:\s|(?:\n?\z)|/)");
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(compiled) = PyRegex::compile(&source, true) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let compiled = PyRegex::compile(&source, true).expect("statically valid literal");
    let mut out = Vec::new();
    let mut from = 0usize;
    while let Some(caps) = compiled.re().captures_at(haystack, from) {
        let m = caps.get(0).expect("whole match");
        let candidate = Candidate::new(m.start(), m.end());
        let userinfo = caps.get(1).expect("userinfo group");
        let guard_ok = userinfo.is_empty() || {
            userinfo.start() >= 3 && &haystack[userinfo.start() - 3..userinfo.start()] == "://"
        };
        if guard_ok {
            out.push(candidate);
            // the prefix alternative and every host are non-empty, so the
            // candidate is never zero-width; the max keeps one advancing line
            from = candidate.end.max(candidate.start + 1);
        } else {
            from = candidate.start + 1;
        }
    }
    out
}

// ---------------------------------------------------------------------------
// SSRF numeric hosts (lookahead-guarded terminator)
// ---------------------------------------------------------------------------

/// `://(?:[^/@\s]*@)?((?:0[xX]...)(?:\.(...)){0,3})(?=[:/\s]|$)`: the
/// terminator lookahead is a suffix check; shorter host matches end in host
/// characters, so the check is exact per start position.
#[must_use]
pub fn ssrf_numeric_host_finditer(haystack: &str) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(re) = PyRegex::compile(
        r"://(?:[^/@\s]*@)?((?:0[xX][0-9a-fA-F]+|0[0-7]+|[1-9]\d*|0)(?:\.(?:0[xX][0-9a-fA-F]+|0[0-7]+|[1-9]\d*|0)){0,3})",
        false,
    ) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let re = PyRegex::compile(
        r"://(?:[^/@\s]*@)?((?:0[xX][0-9a-fA-F]+|0[0-7]+|[1-9]\d*|0)(?:\.(?:0[xX][0-9a-fA-F]+|0[0-7]+|[1-9]\d*|0)){0,3})",
        false,
    )
    .expect("statically valid literal");
    guarded_finditer(haystack, re.re(), |text, candidate| {
        terminator_guard(text, candidate.end, |c| {
            matches!(c, ':' | '/') || c.is_whitespace()
        })
    })
}

/// Python terminator guard `(?=T|$)`: `T` at `pos`, or end of text, or a
/// single trailing newline before the end.
fn terminator_guard(haystack: &str, pos: usize, terminator: impl Fn(char) -> bool) -> bool {
    let Some(c) = char_at(haystack, pos).map(|(_, c)| c) else {
        return true; // end of text
    };
    if terminator(c) {
        return true;
    }
    // Python `$`: just before a trailing newline
    c == '\n' && pos + 1 == haystack.len()
}

// ---------------------------------------------------------------------------
// cmd-injection shell dash-flag (trailing lookahead guard)
// ---------------------------------------------------------------------------

const SHELL_DASH_FLAG_BODY: &str = r#"(?:\A|[;|&])\s*/?(?:[\w.-]+/)*(?:env\s+/?(?:[\w.-]+/)*)?(?:bash|sh|ksh|csh|tsch|zsh|ash)\s+-[a-zA-Z]+(?:\s+(?:'[^']*'|"[^"]*"|[^\s;|&]+))?"#;

/// Trailing-lookahead suffix check for `(?:\A|[;|&])\s*...(?=\s*(?:[;|&]|\Z))`.
///
/// Every backtracked variant of the optional tail ends in a word character,
/// which the guard also rejects, so the check is exact.
#[must_use]
pub fn shell_dash_flag_finditer(haystack: &str) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(re) = PyRegex::compile(SHELL_DASH_FLAG_BODY, true) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let re = PyRegex::compile(SHELL_DASH_FLAG_BODY, true).expect("statically valid literal");
    guarded_finditer(haystack, re.re(), |text, candidate| {
        lookahead_shell_chain_terminator(text, candidate.end)
    })
}

fn lookahead_shell_chain_terminator(haystack: &str, pos: usize) -> bool {
    let rest = &haystack[pos..];
    let trimmed = rest.trim_start_matches(|c: char| c.is_whitespace());
    trimmed.is_empty()
        || trimmed.starts_with(';')
        || trimmed.starts_with('|')
        || trimmed.starts_with('&')
}

// ---------------------------------------------------------------------------
// Sensitive path family (segment-loop negative lookahead)
// ---------------------------------------------------------------------------

/// A `BAD` alternative: tried greedily in order, each yielding the BAD end.
type BadMatcher<'a> = Box<dyn Fn(&str, usize) -> Vec<usize> + 'a>;

/// The leading separator of the reference's anchored path shapes:
/// `\A[/\\]?...` (optional) or `\A[/\\]...` (required).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LeadingSep {
    Optional,
    Required,
}

fn is_path_class(c: char) -> bool {
    py_is_word(c) || matches!(c, '.' | '-' | '~' | '%')
}

fn is_word_dash(c: char) -> bool {
    py_is_word(c) || c == '-'
}

fn bad_followed_by_segment_boundary(haystack: &str, ends: &[usize]) -> bool {
    ends.iter().any(|end| {
        char_at(haystack, *end).is_none_or(|(_, c)| matches!(c, '/' | '\\'))
            || *end == haystack.len()
    })
}

/// Generic walker for the anchored sensitive-path shapes:
/// `\A[/\\]?(?:(?!BAD(?:[/\\]|\Z))[\w.\-~%]+[/\\])*BAD(?:[/\\][\w.\-~%]*)*(?:\?\S*)?\s*\z`.
/// The greedy segment loop cannot resume once BAD (followed by a segment
/// boundary) appears, and the tail cannot succeed at a segment start (BAD
/// followed by `/` or `\z` there is exactly what the lookahead blocked), so a
/// single greedy walk plus one tail check is exact.
fn sensitive_path_finditer(
    haystack: &str,
    leading_sep: LeadingSep,
    bad: impl Fn(&str, usize) -> Vec<usize>,
) -> Vec<Candidate> {
    let mut pos = 0usize;
    match (leading_sep, char_at(haystack, 0)) {
        (LeadingSep::Optional | LeadingSep::Required, Some((_, '/' | '\\'))) => pos = 1,
        (LeadingSep::Required, _) => return Vec::new(),
        (LeadingSep::Optional, _) => {}
    }
    loop {
        let ends = bad(haystack, pos);
        if bad_followed_by_segment_boundary(haystack, &ends) {
            break;
        }
        let seg_end = walk_forward_while(haystack, pos, is_path_class);
        if seg_end > pos && char_at(haystack, seg_end).is_some_and(|(_, c)| matches!(c, '/' | '\\'))
        {
            pos = seg_end + 1;
            continue;
        }
        break;
    }
    // tail: BAD + (?:[/\\][\w.\-~%]*)* + (?:\?\S*)? + \s*\z
    for bad_end in bad(haystack, pos) {
        let mut cursor = bad_end;
        while let Some('/' | '\\') = char_at(haystack, cursor).map(|(_, c)| c) {
            cursor += 1;
            cursor = walk_forward_while(haystack, cursor, is_path_class);
        }
        if char_at(haystack, cursor).map(|(_, c)| c) == Some('?') {
            cursor += 1;
            cursor = walk_forward_while(haystack, cursor, |c| !c.is_whitespace());
        }
        let tail_end = walk_forward_while(haystack, cursor, char::is_whitespace);
        if tail_end == haystack.len() {
            return vec![Candidate::new(0, haystack.len())];
        }
    }
    Vec::new()
}

/// Anchored literal alternatives: `(?:alt|alt|...)` matched at `pos`.
///
/// The reference compiles these rows with builtin IGNORECASE
/// (`_BUILTIN_PATTERN_COMPILE_FLAGS`), so the literals fold ASCII case.
fn literal_bad<'a>(alternatives: &'a [&'a str]) -> BadMatcher<'a> {
    Box::new(move |haystack: &str, pos: usize| -> Vec<usize> {
        let mut ends = Vec::new();
        for alt in alternatives {
            // alternatives may contain embedded separators (`system/version`)
            if ascii_starts_with_ignore_case(haystack, pos, alt) {
                ends.push(pos + alt.len());
            }
        }
        ends
    })
}

/// The optional trailing group of the literal-suffix shapes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BadSuffix {
    /// `(?:[.\-][\w.\-~%]*)?` (id 121)
    DashRun,
    /// `(?:\.ya?ml)?` (id 132)
    YamlSuffix,
    /// `(?:\.[\w.\-~%]*)?` (id 125)
    DotRun,
}

fn literal_suffix_bad<'a>(alternatives: &'a [&'a str], suffix: BadSuffix) -> BadMatcher<'a> {
    Box::new(move |haystack: &str, pos: usize| -> Vec<usize> {
        let mut ends = Vec::new();
        for alt in alternatives {
            if !ascii_starts_with_ignore_case(haystack, pos, alt) {
                continue;
            }
            let head = pos + alt.len();
            // the bare alternative is the last backtracking branch
            ends.push(head);
            let rest = &haystack[head..];
            match suffix {
                BadSuffix::YamlSuffix => {
                    if let Some(after_dot) = rest.strip_prefix('.') {
                        if ascii_starts_with_ignore_case(after_dot, 0, "yaml") {
                            ends.push(head + 5);
                        }
                        if ascii_starts_with_ignore_case(after_dot, 0, "yml") {
                            ends.push(head + 4);
                        }
                    }
                }
                BadSuffix::DotRun => {
                    if rest.starts_with('.') {
                        ends.push(walk_forward_while(haystack, head + 1, is_path_class));
                        ends.push(head + 1);
                    }
                }
                BadSuffix::DashRun => {
                    if let Some((i, c)) = char_at(haystack, head)
                        && matches!(c, '.' | '-')
                    {
                        let run_end = walk_forward_while(haystack, i + c.len_utf8(), is_path_class);
                        ends.push(run_end);
                        ends.push(i + c.len_utf8());
                    }
                }
            }
        }
        ends
    })
}

/// `[\w.\-~%]*\.(?:ext|ext|...)`: every extension occurrence reachable through
/// the surrounding path-class run (ids 104, 113).
fn extension_run_bad<'a>(extensions: &'a [&'a str]) -> BadMatcher<'a> {
    Box::new(move |haystack: &str, pos: usize| -> Vec<usize> {
        let run_end = walk_forward_while(haystack, pos, is_path_class);
        let mut ends = Vec::new();
        let mut cursor = pos;
        while cursor < run_end {
            #[cfg(not(coverage))] // unreachable: cursor < run_end is always a boundary
            let Some((i, c)) = char_at(haystack, cursor) else {
                break;
            };
            #[cfg(coverage)]
            let (i, c) = char_at(haystack, cursor).expect("cursor within the path run");
            if c == '.' {
                let after_dot = i + 1;
                for ext in extensions {
                    if ascii_starts_with_ignore_case(haystack, after_dot, ext) {
                        ends.push(after_dot + ext.len());
                    }
                }
            }
            cursor = i + c.len_utf8();
        }
        ends
    })
}

/// `[\w.\-~%]*(?:secrets?|credentials?)\.(?:ext|...)` (id 133).
fn secrets_run_bad<'a>(extensions: &'a [&'a str]) -> BadMatcher<'a> {
    Box::new(move |haystack: &str, pos: usize| -> Vec<usize> {
        let run_end = walk_forward_while(haystack, pos, is_path_class);
        let mut ends = Vec::new();
        let mut cursor = pos;
        while cursor < run_end {
            #[cfg(not(coverage))] // unreachable: cursor < run_end is always a boundary
            let Some((i, c)) = char_at(haystack, cursor) else {
                break;
            };
            #[cfg(coverage)]
            let (i, c) = char_at(haystack, cursor).expect("cursor within the path run");
            for stem in ["secret", "credential"] {
                if !ascii_starts_with_ignore_case(haystack, i, stem) {
                    continue;
                }
                let mut after_stem = i + stem.len();
                if let Some((_, plural)) = char_at(haystack, after_stem)
                    && matches!(plural, 's' | 'S')
                {
                    after_stem += 1;
                }
                if char_at(haystack, after_stem).map(|(_, c)| c) != Some('.') {
                    continue;
                }
                let after_dot = after_stem + 1;
                for ext in extensions {
                    if ascii_starts_with_ignore_case(haystack, after_dot, ext) {
                        ends.push(after_dot + ext.len());
                    }
                }
            }
            cursor = i + c.len_utf8();
        }
        ends
    })
}

const CONFIG_EXTENSIONS: &[&str] = &["env", "yml", "yaml", "json", "toml", "ini", "xml", "conf"];

/// `(?:(?!config)[\w-])*config[\w-]*\.(?:env|yml|...)` (id 103). Only the first
/// `config` occurrence in a `[\w-]` run is reachable: every later occurrence
/// has a `config` start in its consumed prefix, which the per-char lookahead
/// rejects. Literals fold ASCII case (builtin IGNORECASE).
fn config_bad(haystack: &str, pos: usize) -> Vec<usize> {
    let run_end = walk_forward_while(haystack, pos, is_word_dash);
    let mut config_start = None;
    let mut cursor = pos;
    while cursor + "config".len() <= run_end {
        if ascii_starts_with_ignore_case(haystack, cursor, "config") {
            config_start = Some(cursor);
            break;
        }
        cursor = char_at(haystack, cursor).map_or(cursor + 1, |(i, c)| i + c.len_utf8());
    }
    let Some(config_start) = config_start else {
        return Vec::new();
    };
    let word_end = walk_forward_while(haystack, config_start + "config".len(), is_word_dash);
    if char_at(haystack, word_end).map(|(_, c)| c) != Some('.') {
        return Vec::new();
    }
    let after_dot = word_end + 1;
    let mut ends = Vec::new();
    for ext in CONFIG_EXTENSIONS {
        if ascii_starts_with_ignore_case(haystack, after_dot, ext) {
            ends.push(after_dot + ext.len());
        }
    }
    ends
}

/// `.env` family: BAD = `\.env(?:\.\w+)?` (longest first).
fn env_bad(haystack: &str, pos: usize) -> Vec<usize> {
    let mut ends = Vec::new();
    if ascii_starts_with_ignore_case(haystack, pos, ".env") {
        let after = pos + 4;
        if char_at(haystack, after).map(|(_, c)| c) == Some('.') {
            let word_end = walk_forward_while(haystack, after + 1, py_is_word);
            if word_end > after + 1 {
                ends.push(word_end);
            }
        }
        ends.push(after);
    }
    ends
}

/// `\.(?:git|svn|hg|bzr)` (id 106).
fn dot_alternative_bad<'a>(names: &'a [&'a str]) -> BadMatcher<'a> {
    Box::new(move |haystack: &str, pos: usize| -> Vec<usize> {
        let mut ends = Vec::new();
        if char_at(haystack, pos).map(|(_, c)| c) == Some('.') {
            for name in names {
                if ascii_starts_with_ignore_case(haystack, pos + 1, name) {
                    ends.push(pos + 1 + name.len());
                }
            }
        }
        ends
    })
}

/// `(?:wp-(admin|login|content|includes|config)|administrator|xmlrpc)\.?(?:php)?`
/// (id 109).
fn wp_admin_bad(haystack: &str, pos: usize) -> Vec<usize> {
    const HEADS: &[&str] = &[
        "wp-admin",
        "wp-login",
        "wp-content",
        "wp-includes",
        "wp-config",
        "administrator",
        "xmlrpc",
    ];
    let mut ends = Vec::new();
    for head in HEADS {
        if ascii_starts_with_ignore_case(haystack, pos, head) {
            let after = pos + head.len();
            // greedy `\.?` then `(?:php)?`
            let dotted = char_at(haystack, after).map(|(_, c)| c) == Some('.');
            let after_dot = if dotted { after + 1 } else { after };
            if ascii_starts_with_ignore_case(haystack, after_dot, "php") {
                ends.push(after_dot + 3);
            }
            if dotted {
                ends.push(after_dot);
            }
            ends.push(after);
        }
    }
    ends
}

const MANAGEMENT_BAD: &[&str] = &[
    "management",
    "config_dump",
    "credentials",
    "system/version",
    "system\\version",
    "version/system",
    "version\\system",
];

pub const RECON_APP_BAD: &[&str] = &[
    "geoserver",
    "confluence",
    "nifi",
    "ScadaBR",
    "pandora_console",
    "centreon",
    "kylin",
    "decisioncenter",
    "evox",
    "MagicInfo",
    "metasys",
    "officescan",
    "helpdesk",
    "ignite",
];

pub const RECON_README_BAD: &[&str] = &[
    "readme.txt",
    "README.md",
    "CHANGELOG",
    "pom.xml",
    "build.gradle",
    "appsettings.json",
    "crossdomain.xml",
];

pub const BACKUP_EXTENSIONS: &[&str] = &[
    "bak", "backup", "old", "orig", "save", "swp", "swo", "tmp", "temp",
];

pub const SECRETS_EXTENSIONS: &[&str] = &[
    "py", "json", "yml", "yaml", "toml", "txt", "env", "xml", "conf", "cfg",
];

pub const DOCKERFILE_BAD: &[&str] = &[
    "docker-compose",
    "Dockerfile",
    "Makefile",
    "Vagrantfile",
    "Jenkinsfile",
    "Procfile",
];

pub const DOUBLE_DOT_BAD: &[&str] = &[
    ".htaccess",
    ".htpasswd",
    ".DS_Store",
    "Thumbs.db",
    ".npmrc",
    ".dockerenv",
    "web.config",
];

/// `\A[/\\]?...\.env...` (id 101).
#[must_use]
pub fn sensitive_path_env(haystack: &str) -> Vec<Candidate> {
    sensitive_path_finditer(haystack, LeadingSep::Optional, env_bad)
}

/// `\A[/\\]?...(?:wp-...|administrator|xmlrpc)...` (id 109).
#[must_use]
pub fn sensitive_path_wp_admin(haystack: &str) -> Vec<Candidate> {
    sensitive_path_finditer(haystack, LeadingSep::Optional, wp_admin_bad)
}

/// `\A[/\\]...(?:management|config_dump|credentials|system[/\\]version|...)`
/// (id 117; the nested shape requires the leading separator).
#[must_use]
pub fn sensitive_path_management(haystack: &str) -> Vec<Candidate> {
    sensitive_path_finditer(haystack, LeadingSep::Required, literal_bad(MANAGEMENT_BAD))
}

/// `\A[/\\]?...[^\w.\-~%]*\.(?:ext|...)` (ids 104 `.map`, 113 backup files).
#[must_use]
pub fn sensitive_path_scan_ext(haystack: &str, extensions: &[&str]) -> Vec<Candidate> {
    sensitive_path_finditer(
        haystack,
        LeadingSep::Optional,
        extension_run_bad(extensions),
    )
}

/// `\A[/\\]?...\.(?:git|svn|hg|bzr)...` (id 106).
#[must_use]
pub fn sensitive_path_dot_alt(haystack: &str, names: &[&str]) -> Vec<Candidate> {
    sensitive_path_finditer(haystack, LeadingSep::Optional, dot_alternative_bad(names))
}

/// `\A[/\\]?...(?:phpinfo|info|test|php_info)\.php...` (id 111) and the
/// literal-plus-extension shapes.
#[must_use]
pub fn sensitive_path_literal_ext(haystack: &str, names: &[&str], ext: &str) -> Vec<Candidate> {
    let literals: Vec<String> = names.iter().map(|name| format!("{name}.{ext}")).collect();
    let refs: Vec<&str> = literals.iter().map(String::as_str).collect();
    sensitive_path_finditer(haystack, LeadingSep::Optional, literal_bad(&refs))
}

/// `\A[/\\]?...(?:alt|alt|...)...` anchored literal shapes (ids 114, 119, 122,
/// 123, 124, 126, 128, 130, 131, 134, 135, 136).
#[must_use]
pub fn sensitive_path_literal(haystack: &str, literals: &[&str]) -> Vec<Candidate> {
    sensitive_path_finditer(haystack, LeadingSep::Optional, literal_bad(literals))
}

/// Anchored literal shapes with an optional trailing group (ids 121, 132).
#[must_use]
pub fn sensitive_path_literal_suffix(
    haystack: &str,
    literals: &[&str],
    suffix: BadSuffix,
) -> Vec<Candidate> {
    sensitive_path_finditer(
        haystack,
        LeadingSep::Optional,
        literal_suffix_bad(literals, suffix),
    )
}

/// Anchored literal shapes with `(?:\.[\w.\-~%]*)?` (id 125).
#[must_use]
pub fn sensitive_path_literal_suffix_dot(haystack: &str, literals: &[&str]) -> Vec<Candidate> {
    sensitive_path_literal_suffix(haystack, literals, BadSuffix::DotRun)
}

/// `\A[/\\]?...(?:secrets?|credentials?)\.(?:ext|...)...` (id 133).
#[must_use]
pub fn sensitive_path_secrets(haystack: &str) -> Vec<Candidate> {
    sensitive_path_finditer(
        haystack,
        LeadingSep::Optional,
        secrets_run_bad(SECRETS_EXTENSIONS),
    )
}

/// `\A[/\\]?...(?:(?!config)[\w-])*config[\w-]*\.(?:env|yml|...)...` (id 103).
#[must_use]
pub fn sensitive_path_config(haystack: &str) -> Vec<Candidate> {
    sensitive_path_finditer(haystack, LeadingSep::Optional, config_bad)
}

/// The attack-report lexicon of the embedded-prose shapes (`ids 33, 102, 107,
/// 110, 112, 115`), verbatim from the canonical sources.
const ATTACK_REPORT_LEXICON: &str = r"\b(?:scan(?:ner|ning|ned|s)?|attack(?:er|ers|ed|s)?|attempt(?:ed|s)?|exploit(?:ation|ed|s|ing|kit)?|prob(?:e|ed|es|ing)|malicious|intrusion(?:s)?|botnet(?:s)?|honeypot(?:s)?|brute[- ]force|credential[- ]stuffing|threat feed|vulnerabilit(?:y|ies)|hostile|recon(?:naissance)?|spoofed referer|bad actor(?:s)?|WAF|IDS|SOC|pentest(?:ing)?|blocked|flagged|triggered|denied|enumerat(?:e|ed|ing)|suspicious)\b";

/// Lexicon-lookahead path shapes (ids 33/102/107/110/112/115).
///
/// The reference pattern is `\A(?=(?:(?!\n).)*LEXICON)\A(?:(?!\n).)*<path-shape>`,
/// so the shape must sit in the first line and the first line must carry a
/// lexicon word.
#[must_use]
pub fn lexicon_path_finditer(haystack: &str, shape: &str, require_lexicon: bool) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: every caller passes a statically valid shape
    let Ok(compiled) = PyRegex::compile(shape, true) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let compiled = PyRegex::compile(shape, true).expect("statically valid shape");
    let matches: Vec<Candidate> = compiled
        .re()
        .find_iter(haystack)
        .map(|m| Candidate::new(m.start(), m.end()))
        .collect();
    if !require_lexicon || matches.is_empty() {
        return matches;
    }
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(lexicon) = PyRegex::compile(ATTACK_REPORT_LEXICON, true) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let lexicon = PyRegex::compile(ATTACK_REPORT_LEXICON, true).expect("statically valid literal");
    let first_line_end = haystack.find('\n').unwrap_or(haystack.len());
    if lexicon.re().is_match(&haystack[..first_line_end]) {
        matches
    } else {
        Vec::new()
    }
}

/// `Object\.prototype\.[A-Za-z_$][\w$]*\s*=(?!=)` (id 139).
///
/// The negative lookahead becomes a suffix check on the assignment operator.
/// The row carries the reference's builtin IGNORECASE, so it compiles
/// case-folded.
#[must_use]
pub fn proto_pollution_assign_finditer(haystack: &str) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(re) = PyRegex::compile(r"Object\.prototype\.[A-Za-z_$][\w$]*\s*=", true) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let re = PyRegex::compile(r"Object\.prototype\.[A-Za-z_$][\w$]*\s*=", true)
        .expect("statically valid literal");
    guarded_finditer(haystack, re.re(), |text, candidate| {
        text.as_bytes().get(candidate.end) != Some(&b'=')
    })
}
// ---------------------------------------------------------------------------
// Deserialization base64 magic prefixes
// ---------------------------------------------------------------------------

/// `(?<![A-Za-z0-9+/])(?-i:MAGIC)` for the four magic prefixes; the
/// lookbehind is a boundary check on the literal occurrence.
#[must_use]
pub fn deserialization_b64_finditer(haystack: &str, magic: &str) -> Vec<Candidate> {
    let mut out = Vec::new();
    let mut from = 0usize;
    while let Some(rel) = haystack[from..].find(magic) {
        let start = from + rel;
        let end = start + magic.len();
        let boundary_ok = start == 0
            || char_before(haystack, start)
                .is_none_or(|(_, c)| !c.is_ascii_alphanumeric() && c != '/' && c != '+');
        if boundary_ok {
            out.push(Candidate::new(start, end));
            from = end;
        } else {
            from = start + 1;
        }
    }
    out
}

// ---------------------------------------------------------------------------
// SQLi ORDER BY terminator (second alternative guard)
// ---------------------------------------------------------------------------

/// `(?i)\bORDER\s+BY\s+\d+\s*(?:--|#|;|\)|,|/\*|\Z)|(?<=[=?&])ORDER\s+BY\s+\d+\s*\n`
#[must_use]
pub fn sqli_order_by_terminator_finditer(haystack: &str) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(alt1) = PyRegex::compile(r"\bORDER\s+BY\s+\d+\s*(?:--|#|;|\)|,|/\*|\z)", true) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let alt1 = PyRegex::compile(r"\bORDER\s+BY\s+\d+\s*(?:--|#|;|\)|,|/\*|\z)", true)
        .expect("statically valid literal");
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(alt2) = PyRegex::compile(r"ORDER\s+BY\s+\d+\s*\n", true) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let alt2 = PyRegex::compile(r"ORDER\s+BY\s+\d+\s*\n", true).expect("statically valid literal");
    let mut out: Vec<Candidate> = alt1
        .re()
        .find_iter(haystack)
        .map(|m| Candidate::new(m.start(), m.end()))
        .collect();
    let guarded = guarded_finditer(haystack, alt2.re(), |text, candidate| {
        candidate.start > 0
            && char_before(text, candidate.start).is_some_and(|(_, c)| matches!(c, '=' | '?' | '&'))
    });
    out.extend(guarded);
    out.sort_by_key(|c| (c.start, std::cmp::Reverse(c.end)));
    out
}

// ---------------------------------------------------------------------------
// File-inclusion bare host (lookbehind guard)
// ---------------------------------------------------------------------------

/// `(?:(?<!:)\/\/HOST...)`: the lookbehind is a one-char prefix check.
#[must_use]
pub fn file_inclusion_bare_host_finditer(haystack: &str) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(re) = PyRegex::compile(
        r"//(?:[0-9a-zA-Z](?:[-\w]*[0-9a-zA-Z])?(?:\.[0-9a-zA-Z](?:[-\w]*[0-9a-zA-Z])?)+)(:[0-9]+)?(?:/?)(?:[a-zA-Z0-9\-\.\?,'/\\+&amp;%$#_]*)?",
        false,
    ) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let re = PyRegex::compile(
        r"//(?:[0-9a-zA-Z](?:[-\w]*[0-9a-zA-Z])?(?:\.[0-9a-zA-Z](?:[-\w]*[0-9a-zA-Z])?)+)(:[0-9]+)?(?:/?)(?:[a-zA-Z0-9\-\.\?,'/\\+&amp;%$#_]*)?",
        false,
    )
    .expect("statically valid literal");
    guarded_finditer(haystack, re.re(), |text, candidate| {
        candidate.start == 0 || char_before(text, candidate.start).is_none_or(|(_, c)| c != ':')
    })
}

// ---------------------------------------------------------------------------
// File-inclusion scheme URL (trailing lookahead + bounded window)
// ---------------------------------------------------------------------------

const SCHEME_PATH_TRIMMED: &str =
    r#"=(?:https?|ftp)://[^\s'"<>]+/[^\s'"<>/]*\.(?:phtml|php[3-5]?|phar|jsp|aspx?|pl|py|txt|inc)"#;

/// id 63: the reference runs the UNMODIFIED pattern inside the bounded window.
///
/// The trailing `(?![a-zA-Z0-9])` is enforced as a suffix guard; on rejection
/// the scan abandons the prefix candidate and moves to the next one, exactly
/// like a failed window match.
#[must_use]
pub fn file_inclusion_scheme_path_finditer(haystack: &str) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: statically valid literals
    let (Some(compiled), Some(prefix), Some(terminator)) = (
        PyRegex::compile(SCHEME_PATH_TRIMMED, true).ok(),
        PyRegex::compile(r"=(?:https?|ftp)://", true).ok(),
        PyRegex::compile(
            r"\.(?:phtml|php\d*|phar|jsp|aspx?|pl|py|txt|inc)[a-zA-Z0-9]*",
            true,
        )
        .ok(),
    ) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let (compiled, prefix, terminator) = (
        PyRegex::compile(SCHEME_PATH_TRIMMED, true),
        PyRegex::compile(r"=(?:https?|ftp)://", true),
        PyRegex::compile(
            r"\.(?:phtml|php\d*|phar|jsp|aspx?|pl|py|txt|inc)[a-zA-Z0-9]*",
            true,
        ),
    );
    #[cfg(coverage)]
    let (compiled, prefix, terminator) = (
        compiled.expect("statically valid literal"),
        prefix.expect("statically valid literal"),
        terminator.expect("statically valid literal"),
    );
    super::scan_window::bounded_finditer_guarded(
        haystack,
        &compiled,
        &prefix,
        &terminator,
        |text, candidate| {
            char_at(text, candidate.end).is_none_or(|(_, c)| !c.is_ascii_alphanumeric())
        },
    )
}

// ---------------------------------------------------------------------------
// File-inclusion template URL (inner lookahead implied by the closing quote)
// ---------------------------------------------------------------------------

const TEMPLATE_URL_TRIMMED: &str = r#"["'](?:template|include|tpl|module|layout)["']\s*:\s*["'](?:https?|ftp)://[^\s'"<>]+/[^\s'"<>/]*\.(?:phtml|php[3-5]?|phar|jsp|aspx?|cgi|pl|py|sh|txt|inc)["']"#;

/// id 64: the source's `(?![a-zA-Z0-9])` sits directly before the required
/// closing quote, so any match of the lookahead-free equivalent already
/// satisfies it (the quote is never alphanumeric).
#[must_use]
pub fn file_inclusion_template_url_finditer(haystack: &str) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(compiled) = PyRegex::compile(TEMPLATE_URL_TRIMMED, true) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let compiled = PyRegex::compile(TEMPLATE_URL_TRIMMED, true).expect("statically valid literal");
    compiled
        .re()
        .find_iter(haystack)
        .map(|m| Candidate::new(m.start(), m.end()))
        .collect()
}

// ---------------------------------------------------------------------------
// Glued backtick candidate (lookbehind guard)
// ---------------------------------------------------------------------------

/// `(?<!\x60)\x60(?:[A-Za-z0-9_./~]|\$[({])(?:[^\x60\\\n]|\\.)*\x60`
#[must_use]
pub fn glued_backtick_candidate_finditer(haystack: &str) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(re) = PyRegex::compile(r"`(?:[A-Za-z0-9_./~]|\$[({])(?:[^`\\\n]|\\.)*`", false) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let re = PyRegex::compile(r"`(?:[A-Za-z0-9_./~]|\$[({])(?:[^`\\\n]|\\.)*`", false)
        .expect("statically valid literal");
    guarded_finditer(haystack, re.re(), |text, candidate| {
        candidate.start == 0 || char_before(text, candidate.start).is_none_or(|(_, c)| c != '`')
    })
}

// ---------------------------------------------------------------------------
// XSS event-handler attribute walker
// ---------------------------------------------------------------------------

/// The frozen 2026-08-20 `_HTML_EVENT_HANDLER_ATTRS` set, in reference order
/// (extracted verbatim from the canonical pattern source).
pub const HTML_EVENT_HANDLER_ATTRS: &[&str] = &[
    "onwebkitplaybacktargetavailabilitychanged",
    "oncontentvisibilityautostatechange",
    "onwebkitpresentationmodechanged",
    "onwebkitmouseforcewillbegin",
    "onwebkitanimationiteration",
    "onsecuritypolicyviolation",
    "onwebkitmouseforcechanged",
    "onvalidationstatuschange",
    "onwebkitfullscreenchange",
    "onwebkitwillrevealbottom",
    "onwebkitanimationstart",
    "onwebkitmouseforcedown",
    "onbeforescriptexecute",
    "onmozfullscreenchange",
    "onwebkittransitionend",
    "onafterscriptexecute",
    "onanimationiteration",
    "onlostpointercapture",
    "onscrollsnapchanging",
    "onunhandledrejection",
    "onwebkitanimationend",
    "onwebkitmouseforceup",
    "ondeviceorientation",
    "ongotpointercapture",
    "onbeforedeactivate",
    "onfullscreenchange",
    "onpointerrawupdate",
    "onreadystatechange",
    "onrejectionhandled",
    "onscrollsnapchange",
    "ontransitioncancel",
    "onanimationcancel",
    "onbeforeeditfocus",
    "oncontextrestored",
    "ondatasetcomplete",
    "onselectionchange",
    "ontransitionstart",
    "onanimationstart",
    "onbeforeactivate",
    "oncanplaythrough",
    "ondatasetchanged",
    "ondurationchange",
    "onlanguagechange",
    "onlayoutcomplete",
    "onloadedmetadata",
    "onpropertychange",
    "oncontrolselect",
    "ondataavailable",
    "ongesturechange",
    "onmediacomplete",
    "onpointercancel",
    "onpromptdismiss",
    "ontransitionend",
    "ontransitionrun",
    "onwebkitneedkey",
    "onanimationend",
    "onbeforetoggle",
    "onbeforeunload",
    "onbeforeupdate",
    "ondevicemotion",
    "onfilterchange",
    "ongesturestart",
    "onmessageerror",
    "onpointerenter",
    "onpointerleave",
    "onpromptaction",
    "onsyncrestored",
    "onvolumechange",
    "onafterupdate",
    "onbeforeinput",
    "onbeforematch",
    "onbeforepaste",
    "onbeforeprint",
    "oncontextlost",
    "oncontextmenu",
    "onerrorupdate",
    "onlosecapture",
    "onpointerdown",
    "onpointermove",
    "onpointerover",
    "onresizestart",
    "onrowinserted",
    "onselectstart",
    "ontouchcancel",
    "ontrackchange",
    "onafterprint",
    "onbeforecopy",
    "oncellchange",
    "ondeactivate",
    "ongestureend",
    "onhashchange",
    "onloadeddata",
    "onmediaerror",
    "onmouseenter",
    "onmouseleave",
    "onmousewheel",
    "onpagereveal",
    "onpointerout",
    "onratechange",
    "onslotchange",
    "ontimeupdate",
    "ontouchstart",
    "onbeforecut",
    "oncuechange",
    "ondragenter",
    "ondragleave",
    "ondragstart",
    "onloadstart",
    "onmousedown",
    "onmousemove",
    "onmouseover",
    "onmovestart",
    "onoutofsync",
    "onpointerup",
    "onresizeend",
    "onrowdelete",
    "onrowsenter",
    "onscrollend",
    "ontimeerror",
    "ontouchmove",
    "onactivate",
    "onauxclick",
    "ondblclick",
    "ondragdrop",
    "ondragexit",
    "ondragover",
    "onfocusout",
    "onformdata",
    "onkeypress",
    "onlocation",
    "onmouseout",
    "onpagehide",
    "onpageshow",
    "onpageswap",
    "onpopstate",
    "onprogress",
    "ontouchend",
    "oncanplay",
    "oncommand",
    "ondragend",
    "onemptied",
    "onfocusin",
    "oninvalid",
    "onkeydown",
    "onmessage",
    "onmouseup",
    "onmoveend",
    "onoffline",
    "onplaying",
    "onreverse",
    "onrowexit",
    "onseeking",
    "onstalled",
    "onstorage",
    "onsuspend",
    "onurlflip",
    "onwaiting",
    "onbounce",
    "oncancel",
    "onchange",
    "onfinish",
    "ononline",
    "onrepeat",
    "onresize",
    "onresume",
    "onscroll",
    "onsearch",
    "onseeked",
    "onselect",
    "onsubmit",
    "ontoggle",
    "onunload",
    "onabort",
    "onbegin",
    "onclick",
    "onclose",
    "onended",
    "onerror",
    "onfocus",
    "oninput",
    "onkeyup",
    "onpaste",
    "onpause",
    "onreset",
    "onstart",
    "onwheel",
    "onblur",
    "oncopy",
    "ondrag",
    "ondrop",
    "onhelp",
    "onload",
    "onmove",
    "onplay",
    "onredo",
    "onseek",
    "onstop",
    "onundo",
    "oncut",
    "onend",
];

/// ASCII case-folded `starts_with` at a byte position: the reference's builtin
/// `re.IGNORECASE` folding for the ASCII literals of the structural rows.
fn ascii_starts_with_ignore_case(haystack: &str, pos: usize, needle: &str) -> bool {
    let bytes = haystack.as_bytes();
    let Some(window) = bytes.get(pos..pos + needle.len()) else {
        return false;
    };
    window
        .iter()
        .zip(needle.bytes())
        .all(|(a, b)| a.eq_ignore_ascii_case(&b))
}

/// The value part: `\s*=\s{0,20}(?:["'][^"']*["']|[^\s>]+)`, returning the end.
fn event_handler_value_end(haystack: &str, pos: usize) -> Option<usize> {
    let mut cursor = walk_forward_while(haystack, pos, char::is_whitespace);
    if char_at(haystack, cursor).map(|(_, c)| c) != Some('=') {
        return None;
    }
    cursor = char_at(haystack, cursor).map_or(cursor, |(i, c)| i + c.len_utf8());
    // `\s{0,20}`
    let mut taken = 0usize;
    while taken < 20 && char_at(haystack, cursor).is_some_and(|(_, c)| c.is_whitespace()) {
        cursor = char_at(haystack, cursor).map_or(cursor, |(i, c)| i + c.len_utf8());
        taken += 1;
    }
    match char_at(haystack, cursor).map(|(_, c)| c) {
        Some(quote @ ('"' | '\'')) => {
            // `["'][^"']*["']`: the body excludes BOTH quote characters and
            // the FIRST quote encountered ends the value (either flavor)
            let mut scan = cursor + quote.len_utf8();
            while let Some((i, c)) = char_at(haystack, scan) {
                if c == '"' || c == '\'' {
                    return Some(i + c.len_utf8());
                }
                scan = i + c.len_utf8();
            }
            None
        }
        Some(_) => {
            let end = walk_forward_while(haystack, cursor, |c| !c.is_whitespace() && c != '>');
            (end > cursor).then_some(end)
        }
        None => None,
    }
}

/// `(?<!=)(?<!=\")(?<!=')` evaluated at the whitespace/slash run start: the
/// 1-char lookbehind rejects a bare `=`; the 2-char lookbehinds reject the
/// sequences `="` and `='`.
fn event_handler_lookbehind_ok(haystack: &str, run_start: usize) -> bool {
    #[cfg(not(coverage))] // unreachable: the run always starts past the tag-open letter
    let Some((c1_pos, c1)) = char_before(haystack, run_start) else {
        return true; // nothing before the run: the lookbehinds pass trivially
    };
    #[cfg(coverage)]
    let (c1_pos, c1) = char_before(haystack, run_start).expect("run past the tag-open letter");
    if c1 == '=' {
        return false;
    }
    if c1 == '"' || c1 == '\'' {
        let quoted_after_equals = char_before(haystack, c1_pos).is_some_and(|(_, c2)| c2 == '=');
        if quoted_after_equals {
            return false;
        }
    }
    true
}

/// The maximal whitespace/slash run ending at/just before `run_start`.
fn event_handler_run_end(haystack: &str, run_start: usize) -> usize {
    let mut pos = run_start;
    while char_at(haystack, pos).is_some_and(|(_, rc)| rc.is_whitespace() || rc == '/') {
        pos = char_at(haystack, pos).map_or(haystack.len(), |(i, rc)| i + rc.len_utf8());
    }
    pos
}

/// The monster pattern:
/// `(?:<[A-Za-z/](?:[^<>]*[^<>\s/])?(?<!=)(?<!=\")(?<!=')[\s/]+(?:NAMES)\s*=\s{0,20}(?:["'][^"']*["']|[^\s>]+))`.
///
/// The three lookbehinds sit between the attribute group and the whitespace
/// run, and the group is greedy: the engine tries the RIGHTMOST run split
/// first (longest attribute group) and backtracks leftward, with the
/// group-absent split (right after the tag-open letter) last. Each split's
/// guard is a 1-2 char sequence check on the characters before the run.
#[must_use]
pub fn xss_event_handler_finditer(haystack: &str) -> Vec<Candidate> {
    #[cfg(not(coverage))] // unreachable: statically valid literal
    let Ok(tag_open) = PyRegex::compile(r"<[A-Za-z/]", false) else {
        return Vec::new();
    };
    #[cfg(coverage)]
    let tag_open = PyRegex::compile(r"<[A-Za-z/]", false).expect("statically valid literal");
    let mut matches = Vec::new();
    let mut resume = 0usize;
    for open in tag_open.re().find_iter(haystack) {
        if open.start() < resume {
            continue;
        }
        // whitespace/slash run starts in the attribute region (bounded by the
        // next angle bracket), leftmost-first
        let mut runs: Vec<usize> = Vec::new();
        let mut cursor = open.end();
        let mut open_run: Option<usize> = None;
        while let Some((offset, c)) = char_at(haystack, cursor) {
            if c == '<' || c == '>' {
                break;
            }
            if c.is_whitespace() || c == '/' {
                if open_run.is_none() {
                    open_run = Some(offset);
                }
            } else if let Some(r) = open_run.take() {
                runs.push(r);
            }
            cursor = offset + c.len_utf8();
        }
        if let Some(r) = open_run {
            runs.push(r);
        }
        // the greedy attribute group tries the longest run first
        runs.reverse();
        for r in runs {
            let run_end = event_handler_run_end(haystack, r);
            if event_handler_lookbehind_ok(haystack, r)
                && let Some(end) = try_event_name_value(haystack, run_end)
            {
                matches.push(Candidate::new(open.start(), end));
                resume = end;
                break;
            }
        }
    }
    matches
}

fn try_event_name_value(haystack: &str, name_pos: usize) -> Option<usize> {
    for name in HTML_EVENT_HANDLER_ATTRS {
        if ascii_starts_with_ignore_case(haystack, name_pos, name) {
            let after_name = name_pos + name.len();
            if let Some(end) = event_handler_value_end(haystack, after_name) {
                return Some(end);
            }
        }
    }
    None
}

// helper re-export for the scan-window module users
#[must_use]
pub fn rfind_in(haystack: &str, needle: &str, start: usize, end: usize) -> Option<usize> {
    str_rfind_in(haystack, needle, start, end)
}

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

    #[test]
    fn select_from_first_reachable_from() {
        let ms = sqli_select_from_finditer("SELECT a FROM b");
        assert_eq!(ms.len(), 1);
        // the lazy loop stops at the first `\bFROM\b`
        assert_eq!(ms[0].text("SELECT a FROM b"), "SELECT a FROM");
    }

    #[test]
    fn select_from_blocked_by_second_select() {
        // the first scan is blocked by the second SELECT, but finditer
        // rescans from the next position and finds `SELECT FROM`
        let ms = sqli_select_from_finditer("SELECT x SELECT FROM");
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].start, 9);
        assert_eq!(ms[0].text("SELECT x SELECT FROM"), "SELECT FROM");
    }

    #[test]
    fn tautology_requires_equal_atoms() {
        let ms = sqli_tautology_finditer("' OR 1=1--");
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].text("' OR 1=1--"), "OR 1=1");
        assert!(sqli_tautology_finditer("' OR 1=2--").is_empty());
    }

    #[test]
    fn ssrf_private_requires_scheme_for_userinfo() {
        let ms = ssrf_private_host_finditer("http://10.0.0.1/shell.txt");
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].text("http://10.0.0.1/shell.txt"), "/10.0.0.1/");
        // bare host without scheme context still fires via whitespace prefix
        let ms = ssrf_private_host_finditer("| nc 10.0.0.1 4444");
        assert_eq!(ms.len(), 1);
    }

    #[test]
    fn ssrf_numeric_guard() {
        let ms = ssrf_numeric_host_finditer("curl http://10.0.0.1/x.sh");
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].text("curl http://10.0.0.1/x.sh"), "://10.0.0.1");
        // terminator must follow the host
        assert!(ssrf_numeric_host_finditer("curl http://10.0.0.1x/").is_empty());
    }

    #[test]
    fn shell_dash_flag_lookahead() {
        let ms = shell_dash_flag_finditer("bash -c \"id\"");
        assert_eq!(ms.len(), 1);
        assert!(shell_dash_flag_finditer("bash -c \"id\" x tail").is_empty());
    }

    #[test]
    fn sensitive_env_path() {
        let ms = sensitive_path_env("/.env");
        assert_eq!(ms.len(), 1);
        // `.env.local` is itself the BAD alternative (`.env` + `.\w+`)
        let ms = sensitive_path_env("/app/.env.local/config");
        assert_eq!(ms.len(), 1);
        let ms = sensitive_path_env("/app/.env");
        assert_eq!(ms.len(), 1);
        // a dot with no word after it falls back to the bare `.env` arm
        assert_eq!(env_bad("/app/.env./x", 5), vec![9]);
        assert_eq!(env_bad("/app/.env.local", 5), vec![15, 9]);
    }

    #[test]
    fn sensitive_wp_admin_path() {
        assert_eq!(sensitive_path_wp_admin("/wp-login.php").len(), 1);
        assert_eq!(
            sensitive_path_wp_admin("/wp-admin/setup-config.php").len(),
            1
        );
        assert!(sensitive_path_wp_admin("/admin/login/?next=/admin/").is_empty());
    }

    #[test]
    fn sensitive_backup_path() {
        assert_eq!(
            sensitive_path_scan_ext("/config.php.bak", BACKUP_EXTENSIONS).len(),
            1
        );
        assert!(sensitive_path_scan_ext("/config.php", BACKUP_EXTENSIONS).is_empty());
    }

    #[test]
    fn management_requires_leading_separator() {
        assert_eq!(sensitive_path_management("/.aws/credentials").len(), 1);
        assert!(sensitive_path_management(".aws/credentials").is_empty());
    }

    #[test]
    fn deserialization_magic_boundary() {
        assert_eq!(deserialization_b64_finditer("rO0ABXNy", "rO0AB").len(), 1);
        assert!(deserialization_b64_finditer("BASE64rO0AB", "rO0AB").is_empty());
        // `/` and `+` are mid-token base64 alphabet: no boundary, no match
        assert!(deserialization_b64_finditer("/rO0AB", "rO0AB").is_empty());
        assert!(deserialization_b64_finditer("+rO0AB", "rO0AB").is_empty());
        // any other non-alphanumeric boundary passes
        assert_eq!(deserialization_b64_finditer("x rO0AB", "rO0AB").len(), 1);
    }

    #[test]
    fn event_handler_matches_unquoted() {
        let ms = xss_event_handler_finditer("<img src=x onerror=alert(1)>");
        assert_eq!(ms.len(), 1);
        assert_eq!(
            ms[0].text("<img src=x onerror=alert(1)>"),
            "<img src=x onerror=alert(1)"
        );
    }

    #[test]
    fn event_handler_slash_separated() {
        let ms = xss_event_handler_finditer("/search?q=<svg/onload=alert(1)>");
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].start, 10);
    }

    #[test]
    fn event_handler_quoted_attr_value_still_matches() {
        // oracle-verified: the lookbehinds reject the SEQUENCES `=`/`="/`='`
        // before the run; a bare `"` before the run does not block, and the
        // greedy group picks the rightmost split (`onerror`)
        let ms = xss_event_handler_finditer("<img src=\"x\" onerror=alert(1)>");
        assert_eq!(ms.len(), 1);
        assert_eq!(
            ms[0].text("<img src=\"x\" onerror=alert(1)>"),
            "<img src=\"x\" onerror=alert(1)"
        );
    }

    #[test]
    fn event_handler_greedy_group_picks_rightmost_split() {
        // oracle-verified: the greedy attribute group tries the rightmost
        // run first, so the second handler wins the match
        let text = "<a b onclick=x c onload=y>";
        let ms = xss_event_handler_finditer(text);
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].text(text), "<a b onclick=x c onload=y");
    }

    #[test]
    fn event_handler_equals_sequence_lookbehind_blocks() {
        // `="` directly before the run: the 2-char lookbehind blocks, and no
        // other split can rescue the match
        assert!(xss_event_handler_finditer("<a =\" onclick=x>").is_empty());
        // a bare `=` directly before the run: the 1-char lookbehind blocks
        assert!(xss_event_handler_finditer("<img src= onerror=x>").is_empty());
        // oracle-verified: `=x` before the run does NOT block (rightmost
        // split sees `x`, not `=`)
        let ms = xss_event_handler_finditer("<img =x onerror=alert(1)>");
        assert_eq!(ms.len(), 1);
        assert_eq!(
            ms[0].text("<img =x onerror=alert(1)>"),
            "<img =x onerror=alert(1)"
        );
    }

    #[test]
    fn event_handler_nested_quote_value_ends_at_first_quote() {
        // the value body excludes both quote flavors, and the first quote
        // encountered closes the value (either flavor): `"alert('`
        let ms = xss_event_handler_finditer("<div onmouseover=\"alert('x')\">hover</div>");
        assert_eq!(ms.len(), 1);
        assert_eq!(
            ms[0].text("<div onmouseover=\"alert('x')\">hover</div>"),
            "<div onmouseover=\"alert('"
        );
    }

    #[test]
    fn inline_comment_guard() {
        let ms = sqli_inline_comment_finditer("UNI/**/ON SE/**/LECT");
        assert_eq!(ms.len(), 2);
        assert!(sqli_inline_comment_finditer("UNI/*!*/ON").is_empty());
    }

    #[test]
    fn backtick_candidate_guard() {
        let compiled =
            PyRegex::compile(r"`(?:[A-Za-z0-9_./~]|\$[({])(?:[^`\\\n]|\\.)*`", false).unwrap();
        let ms = glued_backtick_candidate_finditer("a`id`b");
        assert_eq!(ms.len(), 1);
        let _ = compiled;
        assert!(glued_backtick_candidate_finditer("``id`").is_empty());
    }

    #[test]
    fn terminator_guard_accepts_terminators_end_and_trailing_newline() {
        let guard = |c: char| c == ';';
        // end of text and the terminator itself pass
        assert!(terminator_guard("abc", 3, guard));
        assert!(terminator_guard("ab;c", 2, guard));
        // Python `$`: one newline immediately before the end is an anchor
        assert!(terminator_guard("ab;\n", 3, guard));
        // a newline with content after it is not
        assert!(!terminator_guard("ab;\ncd", 3, guard));
    }

    #[test]
    fn secrets_run_bad_accepts_the_plural_stems() {
        assert_eq!(sensitive_path_secrets("/x/secret.txt").len(), 1);
        assert_eq!(sensitive_path_secrets("/x/secrets.txt").len(), 1);
        assert_eq!(sensitive_path_secrets("/x/credentials.json").len(), 1);
    }

    #[test]
    fn match_span_truncates_overshoots_and_rejects_bad_bounds() {
        let re = PyRegex::compile(r"ab?", false).unwrap();
        // start past end
        assert!(match_span(re.re(), "abab", 3, 2).is_none());
        // the greedy match overshoots `end`: the truncated retry wins
        let m = match_span(re.re(), "ab", 0, 1).expect("truncated match");
        assert_eq!((m.start, m.end), (0, 1));
        // the truncated retry cannot match either
        assert!(match_span(re.re(), "ab", 1, 1).is_none());
        // the truncated window has no room for the whole pattern at all
        assert!(match_span(PyRegex::compile(r"ab", false).unwrap().re(), "ab", 0, 1).is_none());
        // no match at `start` at all
        assert!(match_span(PyRegex::compile(r"zz", false).unwrap().re(), "ab", 0, 2).is_none());
        // a match found only after `start` is not an anchor
        assert!(match_span(PyRegex::compile(r"b", false).unwrap().re(), "ab", 0, 2).is_none());
        // the truncated retry can only anchor at `start` (or not match)
        let alt = PyRegex::compile(r"aba|b", false).unwrap();
        assert!(match_span(alt.re(), "aba", 0, 2).is_none());
    }

    #[test]
    fn shell_dash_c_skips_prefixes_swallowed_by_tokens_and_matches() {
        let compiled = PyRegex::compile(r"\n[^\S\r\n]*evil\s+-c", false).unwrap();
        // the row matches right at the newline prefix
        let ms = shell_dash_c_finditer("\n  evil -c id", &compiled);
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].text("\n  evil -c id"), "\n  evil -c");
        // no newline prefix, no candidate
        assert!(shell_dash_c_finditer("evil -c id", &compiled).is_empty());
        // token runs extend the resume point past a failed row match
        assert!(shell_dash_c_finditer("\n  X=1  Y=2 evil -c id", &compiled).is_empty());
        // a later prefix swallowed by the previous resume point is skipped
        assert!(shell_dash_c_finditer("\n x=1\n y=2 evil", &compiled).is_empty());
    }

    #[test]
    fn shell_dash_c_finds_the_row_after_newline_indent() {
        let compiled = PyRegex::compile(r"\n[^\S\r\n]*evil\s+-c", false).unwrap();
        let ms = shell_dash_c_finditer("\n  evil -c id", &compiled);
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].text("\n  evil -c id"), "\n  evil -c");
        // no newline prefix, no candidate
        assert!(shell_dash_c_finditer("evil -c id", &compiled).is_empty());
    }

    #[test]
    fn ldap_null_byte_attr_skips_names_the_pattern_cannot_anchor() {
        let compiled = PyRegex::compile(r"uidabcdef", false).unwrap();
        // the attribute name carries no match for the compiled pattern, so
        // the anchored window match fails and the tail row is dropped
        assert!(ldap_null_byte_attr_raw("(uidlong=*)%00", &compiled).is_empty());
    }

    #[test]
    fn ldap_null_byte_attr_walks_back_to_the_attribute() {
        let compiled = PyRegex::compile(r"uid", false).unwrap();
        let ms = ldap_null_byte_attr_raw("(uid=*)%00", &compiled);
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].start, 1);
        assert_eq!(ms[0].end, 4);
        let ms = ldap_null_byte_attr_decoded("(uid=*)\0", &compiled);
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].start, 1);
    }

    #[test]
    fn ldap_null_byte_attr_rejects_non_attribute_prefixes() {
        let compiled = PyRegex::compile(r"uid", false).unwrap();
        // no `*` + `)` pair at all
        assert!(ldap_null_byte_attr_raw("(uid=x)", &compiled).is_empty());
        // nothing word-like before the star run
        assert!(ldap_null_byte_attr_raw("*)%00", &compiled).is_empty());
        // the character before the value run is not `=`
        assert!(ldap_null_byte_attr_raw("-a*)%00", &compiled).is_empty());
        // no attribute name between a boundary and the `=`
        assert!(ldap_null_byte_attr_raw("=*)%00", &compiled).is_empty());
        // the attribute name does not start with a letter
        assert!(ldap_null_byte_attr_raw("1a=*)%00", &compiled).is_empty());
    }

    #[test]
    fn quote_splice_resumes_past_failed_candidates() {
        let compiled = PyRegex::compile(r"zzz", false).unwrap();
        assert!(quote_splice_finditer("ab\"cd", &compiled).is_empty());
    }

    #[test]
    fn quote_splice_matches_word_then_quote_and_skips_consumed_quotes() {
        // a quote splicing a word onto the row pattern matches
        let compiled = PyRegex::compile(r"ab", false).unwrap();
        let ms = quote_splice_finditer("ab\"cd", &compiled);
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].text("ab\"cd"), "ab");
        // a quote inside the previous match's span is skipped
        let greedy = PyRegex::compile(r"ab.+", false).unwrap();
        let ms = quote_splice_finditer("ab\"cd\"ef", &greedy);
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].text("ab\"cd\"ef"), "ab\"cd\"ef");
        // a quote at end of input has no word after it
        assert!(quote_splice_finditer("ab\"", &compiled).is_empty());
        // a quote not preceded by a word character cannot splice
        assert!(
            quote_splice_finditer(" \"cd", &PyRegex::compile(r"cd", false).unwrap()).is_empty()
        );
    }

    #[test]
    fn ssrf_private_userinfo_requires_scheme_context() {
        // userinfo present but not directly after `://`: rejected
        assert!(ssrf_private_host_finditer("evil@10.0.0.1/x").is_empty());
        // userinfo directly after `://`: accepted
        let ms = ssrf_private_host_finditer("http://user@10.0.0.1/x");
        assert_eq!(ms.len(), 1);
    }

    #[test]
    fn ssrf_numeric_host_accepts_end_of_text_terminator() {
        let ms = ssrf_numeric_host_finditer("curl http://10.0.0.1");
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].text("curl http://10.0.0.1"), "://10.0.0.1");
    }

    #[test]
    fn select_from_requires_word_boundaries() {
        // SELECT glued to a word character is never a loop start
        assert!(sqli_select_from_finditer("xSELECT a FROM b").is_empty());
        // FROM must also sit on its own word boundary
        assert!(sqli_select_from_finditer("SELECT a xFROM b").is_empty());
    }

    #[test]
    fn tautology_backtracks_numeric_atoms_digit_by_digit() {
        // neither 12 nor the backtracked 1 completes `12=1`
        assert!(sqli_tautology_finditer("1 OR 12=1").is_empty());
        // a word character after the second atom kills the boundary
        assert!(sqli_tautology_finditer("' OR 1=1x").is_empty());
    }

    #[test]
    fn tautology_swallows_candidates_inside_completed_matches() {
        // the completed `='a OR 1'` comparison ends past the inner OR/AND
        // candidate, so the engine skips it (the reference backtracks the same
        // way: the whole span is one match)
        let ms = sqli_tautology_finditer("' OR 'a OR 1'='a OR 1'x");
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].start, 2);
        assert_eq!(ms[0].end, 22);
    }

    #[test]
    fn sensitive_path_tails_accept_query_strings() {
        assert_eq!(sensitive_path_env("/.env?token=abc").len(), 1);
    }

    #[test]
    fn sensitive_paths_walk_segments_and_reject_bad_tails() {
        // the segment loop walks several directories before the BAD literal
        assert_eq!(sensitive_path_management("/x/.aws/credentials").len(), 1);
        // the required shape without a leading separator never matches
        assert!(sensitive_path_management("x/.aws/credentials").is_empty());
        // a BAD literal whose tail does not end the path is rejected
        assert!(sensitive_path_wp_admin("/wp-admin.X").is_empty());
        // the same tail rejection on the boxed-literal rows
        assert!(sensitive_path_management(".aws/credentialsX").is_empty());
    }

    #[test]
    fn literal_suffix_variants_cover_yaml_dot_and_dash_runs() {
        assert_eq!(
            sensitive_path_literal_suffix("/site.yml", &["site"], BadSuffix::YamlSuffix).len(),
            1
        );
        assert_eq!(
            sensitive_path_literal_suffix("/site.yaml", &["site"], BadSuffix::YamlSuffix).len(),
            1
        );
        // the optional suffix group stays absent when no dot follows
        assert!(
            sensitive_path_literal_suffix("/site:8080", &["site"], BadSuffix::YamlSuffix)
                .is_empty()
        );
        assert_eq!(
            sensitive_path_literal_suffix_dot("/app.settings", &["app"]).len(),
            1
        );
        assert_eq!(
            sensitive_path_literal_suffix("/secret-backup", &["secret"], BadSuffix::DashRun).len(),
            1
        );
    }

    #[test]
    fn lexicon_paths_require_the_lexicon_word_on_the_first_line() {
        let shape = r"\A[^\n]*/etc/passwd";
        assert_eq!(
            lexicon_path_finditer("scanner found /etc/passwd", shape, true).len(),
            1
        );
        assert!(lexicon_path_finditer("nothing here /etc/passwd", shape, true).is_empty());
        // the lexicon gate is skipped entirely when not required
        assert_eq!(
            lexicon_path_finditer("nothing here /etc/passwd", shape, false).len(),
            1
        );
    }

    #[test]
    fn order_by_terminator_accepts_both_alternatives() {
        let ms = sqli_order_by_terminator_finditer("ORDER BY 1 -- comment");
        assert_eq!(ms.len(), 1);
        // the lookbehind-guarded newline alternative fires after `=` and the
        // two candidates are returned sorted by (start, end descending)
        let ms = sqli_order_by_terminator_finditer("x=ORDER BY 1\n");
        assert_eq!(ms.len(), 2);
        assert_eq!(ms[0].start, 2);
        assert_eq!(ms[1].start, 2);
    }

    #[test]
    fn file_inclusion_scheme_path_guard_rejects_trailing_alnum() {
        let ms = file_inclusion_scheme_path_finditer("=http://h/a.php");
        assert_eq!(ms.len(), 1);
        // the extension runs on into alphanumerics: the suffix guard rejects
        assert!(file_inclusion_scheme_path_finditer("=http://h/a.php3x").is_empty());
    }

    #[test]
    fn file_inclusion_template_url_matches_quoted_keys() {
        let hay = "\"tpl\" : \"http://h/x.jsp\"";
        let ms = file_inclusion_template_url_finditer(hay);
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].text(hay), "\"tpl\" : \"http://h/x.jsp\"");
    }

    #[test]
    fn event_handler_value_shapes_without_a_value_do_not_match() {
        // no `=` after the handler name
        assert!(xss_event_handler_finditer("<img onclick").is_empty());
        // unterminated quoted value
        assert!(xss_event_handler_finditer("<a onclick=\"unterminated").is_empty());
        // end of input right after the `=`
        assert!(xss_event_handler_finditer("<a onclick=").is_empty());
    }

    #[test]
    fn event_handler_value_takes_up_to_twenty_whitespace_chars() {
        let ms = xss_event_handler_finditer("<a onclick=                    x>");
        assert_eq!(ms.len(), 1);
        // the 21st whitespace char falls outside `\s{0,20}`: the unquoted
        // value would be empty, so nothing matches
        assert!(xss_event_handler_finditer("<a onclick=                     x>").is_empty());
    }

    #[test]
    fn event_handler_skips_tag_opens_inside_consumed_matches() {
        // the `<img` open sits inside the consumed quoted value
        let ms = xss_event_handler_finditer("<div onmouseover=\"<img src=x> onload=y\">");
        assert_eq!(ms.len(), 1);
        assert_eq!(ms[0].start, 0);
    }

    #[test]
    fn event_handler_trailing_whitespace_run_still_scans_the_run() {
        // the whitespace run runs into the closing bracket: the rightmost-run
        // split is tried with no attribute name there, so nothing matches
        assert!(xss_event_handler_finditer("<img src=x >").is_empty());
    }

    #[test]
    fn rfind_in_searches_within_the_window() {
        assert_eq!(rfind_in("abcabc", "abc", 0, 6), Some(3));
        assert_eq!(rfind_in("abcabc", "abc", 0, 3), Some(0));
        assert_eq!(rfind_in("abcabc", "zz", 0, 6), None);
    }
}

#[cfg(test)]
mod unit_twins {
    use super::*;
    use crate::patterns::pyregex::PyRegex;

    #[test]
    fn a_misaligned_token_stops_the_env_prefix_walk() {
        // after the newline prefix the text holds `;a=b `: the first token
        // match starts past the resume point, so the anchor check abandons
        // the prefix and no candidate renders
        let compiled = PyRegex::compile(r"\n[^\S\r\n]*evil\s+-c", false).unwrap();
        assert!(shell_dash_c_finditer("\n;a=b ", &compiled).is_empty());
    }

    #[test]
    fn a_query_tail_after_a_sensitive_path_scans_to_the_whitespace_edge() {
        // the extension ends before a `?query`: the tail consumes the query
        // and the candidate spans the whole string
        let hits = sensitive_path_scan_ext("/x/world.map?a=1", &["map"]);
        assert_eq!(hits.len(), 1, "the query tail keeps the match: {hits:?}");
        assert_eq!((hits[0].start, hits[0].end), (0, 16));
    }
}