lonkero 3.6.2

Web scanner built for actual pentests. Fast, modular, Rust.
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
// Copyright (c) 2026 Bountyy Oy. All rights reserved.
// This software is proprietary and confidential.

/**
 * Bountyy Oy - Advanced Mass Assignment Scanner
 * Detects mass assignment vulnerabilities with advanced injection techniques
 *
 * Detects:
 * - Parameter pollution attacks
 * - Role/privilege escalation via mass assignment
 * - Hidden field manipulation
 * - Account property injection
 * - Price/amount manipulation
 * - Nested object injection (2-4 levels deep)
 * - JSON deep merge attacks
 * - Prototype pollution via mass assignment
 * - Array parameter pollution
 * - Constructor property injection
 *
 * ADVANCED TECHNIQUES:
 * ===================
 * 1. NESTED OBJECT INJECTION:
 *    - Tests deep property injection like user[role][admin]=true
 *    - Tests dot notation: profile.permissions.admin=1
 *    - Tests multiple nesting levels (2-4 levels)
 *
 * 2. JSON DEEP MERGE ATTACKS:
 *    - Tests if nested JSON objects merge unexpectedly
 *    - Verifies actual property injection, not just reflection
 *
 * 3. PROTOTYPE POLLUTION VIA MASS ASSIGNMENT:
 *    - Tests __proto__, constructor.prototype, prototype injection
 *    - Verifies actual pollution through unique markers
 *
 * 4. ARRAY PARAMETER POLLUTION:
 *    - Tests array injection like users[0][admin]=true
 *    - Tests array index manipulation
 *
 * 5. CONSTRUCTOR PROPERTY INJECTION:
 *    - Tests constructor[name], constructor[prototype]
 *    - Verifies actual property modification
 *
 * ZERO FALSE POSITIVES:
 * =====================
 * - Only reports when injection is CONFIRMED
 * - Uses unique test markers to verify actual injection
 * - Checks for structured data responses (JSON)
 * - Verifies privilege escalation or property modification
 * - Distinguishes between reflection and actual injection
 *
 * @copyright 2026 Bountyy Oy
 * @license Proprietary
 */
use crate::detection_helpers::AppCharacteristics;
use crate::http_client::HttpClient;
use crate::types::{Confidence, ScanConfig, Severity, Vulnerability};
use serde_json::json;
use std::sync::Arc;
use tracing::{debug, info};

pub struct MassAssignmentScanner {
    http_client: Arc<HttpClient>,
    test_marker: String,
}

impl MassAssignmentScanner {
    pub fn new(http_client: Arc<HttpClient>) -> Self {
        // Generate unique test marker for verification
        let test_marker = format!("ma_{}", uuid::Uuid::new_v4().to_string().replace("-", ""));
        Self {
            http_client,
            test_marker,
        }
    }

    /// Scan endpoint for mass assignment vulnerabilities
    pub async fn scan(
        &self,
        url: &str,
        _config: &ScanConfig,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        // Intelligent detection - skip for static sites
        if let Ok(response) = self.http_client.get(url).await {
            let characteristics = AppCharacteristics::from_response(&response, url);
            if characteristics.should_skip_injection_tests() {
                info!("[MassAssignment] Skipping - static/SPA site detected");
                return Ok((Vec::new(), 0));
            }
        }

        let mut vulnerabilities = Vec::new();
        let mut tests_run = 0;

        info!("Testing advanced mass assignment vulnerabilities");

        // Phase 1: Basic mass assignment (existing tests)
        let (vulns, tests) = self.test_role_escalation(url).await?;
        vulnerabilities.extend(vulns);
        tests_run += tests;

        if vulnerabilities.is_empty() {
            let (vulns, tests) = self.test_price_manipulation(url).await?;
            vulnerabilities.extend(vulns);
            tests_run += tests;
        }

        if vulnerabilities.is_empty() {
            let (vulns, tests) = self.test_hidden_field_injection(url).await?;
            vulnerabilities.extend(vulns);
            tests_run += tests;
        }

        // Phase 2-6: Advanced mass assignment techniques (PREMIUM FEATURE)
        if vulnerabilities.is_empty()
            && crate::license::is_feature_available("mass_assignment_advanced")
        {
            // Phase 2: Advanced nested object injection (URL-encoded)
            if vulnerabilities.is_empty() {
                let (vulns, tests) = self.test_nested_object_injection(url).await?;
                vulnerabilities.extend(vulns);
                tests_run += tests;
            }

            // Phase 3: JSON deep merge attacks
            if vulnerabilities.is_empty() {
                let (vulns, tests) = self.test_json_deep_merge(url).await?;
                vulnerabilities.extend(vulns);
                tests_run += tests;
            }

            // Phase 4: Prototype pollution via mass assignment
            if vulnerabilities.is_empty() {
                let (vulns, tests) = self.test_prototype_pollution_mass_assignment(url).await?;
                vulnerabilities.extend(vulns);
                tests_run += tests;
            }

            // Phase 5: Array parameter pollution
            if vulnerabilities.is_empty() {
                let (vulns, tests) = self.test_array_parameter_pollution(url).await?;
                vulnerabilities.extend(vulns);
                tests_run += tests;
            }

            // Phase 6: Constructor property injection
            if vulnerabilities.is_empty() {
                let (vulns, tests) = self.test_constructor_property_injection(url).await?;
                vulnerabilities.extend(vulns);
                tests_run += tests;
            }
        } else if vulnerabilities.is_empty() {
            debug!("Advanced mass assignment techniques require premium license");
        }

        Ok((vulnerabilities, tests_run))
    }

    /// Test role/privilege escalation via mass assignment
    async fn test_role_escalation(&self, url: &str) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        let mut vulnerabilities = Vec::new();
        let tests_run = 6;

        info!("[Mass] Testing role escalation via mass assignment");

        let dangerous_params = vec![
            ("role", "admin"),
            ("role", "administrator"),
            ("is_admin", "true"),
            ("is_admin", "1"),
            ("admin", "true"),
            ("privilege", "admin"),
        ];

        for (param, value) in dangerous_params {
            let test_url = if url.contains('?') {
                format!("{}&{}={}", url, param, value)
            } else {
                format!("{}?{}={}", url, param, value)
            };

            match self.http_client.get(&test_url).await {
                Ok(response) => {
                    if self.detect_privilege_escalation(&response.body, param, value) {
                        info!(
                            "Mass assignment privilege escalation detected: {}={}",
                            param, value
                        );
                        vulnerabilities.push(self.create_vulnerability(
                            url,
                            "Privilege Escalation via Mass Assignment",
                            &format!("{}={}", param, value),
                            "User privileges can be escalated by adding parameters",
                            &format!("Successfully set {}={} via mass assignment", param, value),
                            Severity::Critical,
                            "CWE-915",
                        ));
                        break;
                    }
                }
                Err(e) => {
                    debug!("Request failed: {}", e);
                }
            }
        }

        Ok((vulnerabilities, tests_run))
    }

    /// Test price/amount manipulation
    async fn test_price_manipulation(
        &self,
        url: &str,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        let mut vulnerabilities = Vec::new();
        let tests_run = 5;

        debug!("Testing price manipulation via mass assignment");

        let price_params = vec![
            ("price", "0"),
            ("price", "0.01"),
            ("amount", "0"),
            ("cost", "0"),
            ("total", "0"),
        ];

        for (param, value) in price_params {
            let test_url = if url.contains('?') {
                format!("{}&{}={}", url, param, value)
            } else {
                format!("{}?{}={}", url, param, value)
            };

            match self.http_client.get(&test_url).await {
                Ok(response) => {
                    if self.detect_price_manipulation(&response.body, value) {
                        info!(
                            "Mass assignment price manipulation detected: {}={}",
                            param, value
                        );
                        vulnerabilities.push(self.create_vulnerability(
                            url,
                            "Price Manipulation via Mass Assignment",
                            &format!("{}={}", param, value),
                            "Product prices can be manipulated via mass assignment",
                            &format!("Successfully set price to {} via mass assignment", value),
                            Severity::High,
                            "CWE-915",
                        ));
                        break;
                    }
                }
                Err(e) => {
                    debug!("Request failed: {}", e);
                }
            }
        }

        Ok((vulnerabilities, tests_run))
    }

    /// Test hidden field injection
    async fn test_hidden_field_injection(
        &self,
        url: &str,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        let mut vulnerabilities = Vec::new();
        let tests_run = 6;

        debug!("Testing hidden field injection");

        let hidden_params = vec![
            ("user_id", "1"),
            ("id", "1"),
            ("account_id", "1"),
            ("verified", "true"),
            ("active", "true"),
            ("status", "active"),
        ];

        for (param, value) in hidden_params {
            let test_url = if url.contains('?') {
                format!("{}&{}={}", url, param, value)
            } else {
                format!("{}?{}={}", url, param, value)
            };

            match self.http_client.get(&test_url).await {
                Ok(response) => {
                    if self.detect_hidden_field_manipulation(&response.body, param) {
                        info!("Hidden field manipulation detected: {}={}", param, value);
                        vulnerabilities.push(self.create_vulnerability(
                            url,
                            "Hidden Field Manipulation",
                            &format!("{}={}", param, value),
                            "Hidden fields can be manipulated via mass assignment",
                            &format!("Successfully manipulated hidden field: {}", param),
                            Severity::High,
                            "CWE-915",
                        ));
                        break;
                    }
                }
                Err(e) => {
                    debug!("Request failed: {}", e);
                }
            }
        }

        Ok((vulnerabilities, tests_run))
    }

    /// Test nested object injection (2-4 levels deep)
    /// Tests both bracket notation and dot notation
    async fn test_nested_object_injection(
        &self,
        url: &str,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        let mut vulnerabilities = Vec::new();
        let tests_run = 15;

        debug!("Testing nested object injection");

        // Test payloads with different nesting levels and notation styles
        let nested_payloads = vec![
            // 2-level nesting - bracket notation
            ("user[role]=admin", "2-level bracket: user[role]"),
            ("user[isAdmin]=true", "2-level bracket: user[isAdmin]"),
            ("profile[admin]=true", "2-level bracket: profile[admin]"),
            // 3-level nesting - bracket notation
            (
                "user[role][admin]=true",
                "3-level bracket: user[role][admin]",
            ),
            (
                "profile[permissions][admin]=true",
                "3-level bracket: profile[permissions][admin]",
            ),
            (
                "account[settings][role]=admin",
                "3-level bracket: account[settings][role]",
            ),
            // 4-level nesting - bracket notation
            (
                "user[profile][role][admin]=true",
                "4-level bracket: user[profile][role][admin]",
            ),
            (
                "account[data][permissions][admin]=1",
                "4-level bracket: account[data][permissions][admin]",
            ),
            // Dot notation
            ("user.role=admin", "dot notation: user.role"),
            ("user.isAdmin=true", "dot notation: user.isAdmin"),
            (
                "profile.permissions.admin=1",
                "dot notation: profile.permissions.admin",
            ),
            // Mixed notation
            ("user[profile].role=admin", "mixed: user[profile].role"),
            (
                "profile.settings[admin]=true",
                "mixed: profile.settings[admin]",
            ),
        ];

        // Add marker-based payloads separately to avoid lifetime issues
        let marker_payload_1 = format!("user[role][{}]=injected", self.test_marker);
        let marker_payload_2 = format!("profile.{}=injected", self.test_marker);

        // Test regular payloads
        for (payload, technique) in &nested_payloads {
            let test_url = if url.contains('?') {
                format!("{}&{}", url, payload)
            } else {
                format!("{}?{}", url, payload)
            };

            match self.http_client.get(&test_url).await {
                Ok(response) => {
                    if self.detect_nested_injection(&response.body, payload) {
                        info!("Nested object injection detected: {}", technique);
                        vulnerabilities.push(self.create_vulnerability(
                            url,
                            "Nested Object Injection via Mass Assignment",
                            payload,
                            "Deep nested object properties can be injected via mass assignment",
                            &format!("Successfully injected nested property using {}", technique),
                            Severity::Critical,
                            "CWE-915",
                        ));
                        break;
                    }
                }
                Err(e) => {
                    debug!("Nested object test failed: {}", e);
                }
            }
        }

        // Test marker-based payloads
        if vulnerabilities.is_empty() {
            for (payload, technique) in &[
                (marker_payload_1.as_str(), "marker-based verification"),
                (marker_payload_2.as_str(), "marker-based dot notation"),
            ] {
                let test_url = if url.contains('?') {
                    format!("{}&{}", url, payload)
                } else {
                    format!("{}?{}", url, payload)
                };

                match self.http_client.get(&test_url).await {
                    Ok(response) => {
                        if self.detect_nested_injection(&response.body, payload) {
                            info!("Nested object injection detected: {}", technique);
                            vulnerabilities.push(self.create_vulnerability(
                                url,
                                "Nested Object Injection via Mass Assignment",
                                payload,
                                "Deep nested object properties can be injected via mass assignment",
                                &format!(
                                    "Successfully injected nested property using {}",
                                    technique
                                ),
                                Severity::Critical,
                                "CWE-915",
                            ));
                            break;
                        }
                    }
                    Err(e) => {
                        debug!("Nested object test failed: {}", e);
                    }
                }
            }
        }

        Ok((vulnerabilities, tests_run))
    }

    /// Test JSON deep merge attacks
    /// Tests if nested JSON objects merge unexpectedly, allowing property injection
    async fn test_json_deep_merge(&self, url: &str) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        let mut vulnerabilities = Vec::new();
        let tests_run = 12;

        debug!("Testing JSON deep merge attacks");

        // Test payloads with nested JSON structures
        let merge_payloads = vec![
            // Basic nested merge
            json!({
                "user": {
                    "role": "admin"
                }
            }),
            json!({
                "user": {
                    "isAdmin": true
                }
            }),
            // Deep nested merge (3 levels)
            json!({
                "user": {
                    "profile": {
                        "role": "admin"
                    }
                }
            }),
            json!({
                "profile": {
                    "permissions": {
                        "admin": true
                    }
                }
            }),
            // Deep nested merge (4 levels)
            json!({
                "account": {
                    "user": {
                        "profile": {
                            "admin": true
                        }
                    }
                }
            }),
            // Merge with arrays
            json!({
                "user": {
                    "roles": ["admin", "superuser"]
                }
            }),
            // Merge with mixed types
            json!({
                "settings": {
                    "permissions": {
                        "isAdmin": true,
                        "level": 9999
                    }
                }
            }),
            // Merge with unique marker for verification
            json!({
                "user": {
                    self.test_marker.clone(): "injected"
                }
            }),
            json!({
                "profile": {
                    "permissions": {
                        self.test_marker.clone(): "injected"
                    }
                }
            }),
            // Property override attempts
            json!({
                "id": 1,
                "user_id": 1,
                "admin": true
            }),
            // Nested property with price manipulation
            json!({
                "order": {
                    "price": 0,
                    "amount": 0
                }
            }),
            // Nested with verification marker
            json!({
                "data": {
                    "nested": {
                        "marker": self.test_marker.clone()
                    }
                }
            }),
        ];

        for payload in merge_payloads {
            let headers = vec![("Content-Type".to_string(), "application/json".to_string())];

            match self
                .http_client
                .post_with_headers(url, &payload.to_string(), headers)
                .await
            {
                Ok(response) => {
                    if self.detect_deep_merge_injection(&response.body, &payload) {
                        info!("JSON deep merge attack successful");
                        vulnerabilities.push(self.create_vulnerability(
                            url,
                            "JSON Deep Merge Attack",
                            &payload.to_string(),
                            "Nested JSON objects merge unexpectedly, allowing property injection",
                            "Successfully injected properties through deep merge",
                            Severity::High,
                            "CWE-915",
                        ));
                        break;
                    }
                }
                Err(e) => {
                    debug!("JSON merge test failed: {}", e);
                }
            }
        }

        Ok((vulnerabilities, tests_run))
    }

    /// Test prototype pollution via mass assignment
    /// Tests __proto__, constructor.prototype, and prototype injection
    async fn test_prototype_pollution_mass_assignment(
        &self,
        url: &str,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        let mut vulnerabilities = Vec::new();
        let tests_run = 18;

        debug!("Testing prototype pollution via mass assignment");

        // URL-encoded prototype pollution payloads
        let marker_proto_1 = format!("__proto__[{}]=polluted", self.test_marker);
        let marker_proto_2 = format!("__proto__.{}=polluted", self.test_marker);
        let marker_constructor = format!("constructor[prototype][{}]=polluted", self.test_marker);
        let marker_prototype = format!("prototype[{}]=polluted", self.test_marker);

        let url_payloads = vec![
            (marker_proto_1.as_str(), "URL __proto__ with marker"),
            (marker_proto_2.as_str(), "URL __proto__ dot notation"),
            (
                "__proto__[isAdmin]=true",
                "URL __proto__ privilege escalation",
            ),
            ("__proto__[admin]=true", "URL __proto__ admin flag"),
            ("__proto__[role]=admin", "URL __proto__ role injection"),
            (
                marker_constructor.as_str(),
                "URL constructor.prototype with marker",
            ),
            (
                "constructor[prototype][isAdmin]=true",
                "URL constructor.prototype privilege",
            ),
            (
                "constructor[prototype][admin]=true",
                "URL constructor.prototype admin",
            ),
            (marker_prototype.as_str(), "URL prototype with marker"),
            ("prototype[isAdmin]=true", "URL prototype privilege"),
        ];

        // Test URL-encoded payloads
        for (payload, technique) in url_payloads {
            let test_url = if url.contains('?') {
                format!("{}&{}", url, payload)
            } else {
                format!("{}?{}", url, payload)
            };

            match self.http_client.get(&test_url).await {
                Ok(response) => {
                    if self.detect_prototype_pollution(&response.body, &payload) {
                        info!(
                            "Prototype pollution via mass assignment detected: {}",
                            technique
                        );
                        vulnerabilities.push(self.create_vulnerability(
                            url,
                            "Prototype Pollution via Mass Assignment",
                            &payload,
                            "Prototype chain can be polluted through mass assignment parameters",
                            &format!("Successfully polluted prototype using {}", technique),
                            Severity::Critical,
                            "CWE-1321",
                        ));
                        return Ok((vulnerabilities, tests_run));
                    }
                }
                Err(e) => {
                    debug!("Prototype pollution test failed: {}", e);
                }
            }
        }

        // JSON prototype pollution payloads
        let json_payloads = vec![
            json!({
                "__proto__": {
                    self.test_marker.clone(): "polluted"
                }
            }),
            json!({
                "__proto__": {
                    "isAdmin": true
                }
            }),
            json!({
                "constructor": {
                    "prototype": {
                        self.test_marker.clone(): "polluted"
                    }
                }
            }),
            json!({
                "constructor": {
                    "prototype": {
                        "isAdmin": true
                    }
                }
            }),
            json!({
                "prototype": {
                    self.test_marker.clone(): "polluted"
                }
            }),
            json!({
                "prototype": {
                    "admin": true
                }
            }),
            // Nested prototype pollution
            json!({
                "user": {
                    "__proto__": {
                        "isAdmin": true
                    }
                }
            }),
            json!({
                "profile": {
                    "constructor": {
                        "prototype": {
                            "admin": true
                        }
                    }
                }
            }),
        ];

        for payload in json_payloads {
            let headers = vec![("Content-Type".to_string(), "application/json".to_string())];

            match self
                .http_client
                .post_with_headers(url, &payload.to_string(), headers)
                .await
            {
                Ok(response) => {
                    if self.detect_prototype_pollution(&response.body, &payload.to_string()) {
                        info!("JSON prototype pollution via mass assignment detected");
                        vulnerabilities.push(self.create_vulnerability(
                            url,
                            "Prototype Pollution via JSON Mass Assignment",
                            &payload.to_string(),
                            "Prototype chain can be polluted through JSON mass assignment",
                            "Successfully polluted prototype through JSON payload",
                            Severity::Critical,
                            "CWE-1321",
                        ));
                        break;
                    }
                }
                Err(e) => {
                    debug!("JSON prototype pollution test failed: {}", e);
                }
            }
        }

        Ok((vulnerabilities, tests_run))
    }

    /// Test array parameter pollution
    /// Tests array index manipulation and injection
    async fn test_array_parameter_pollution(
        &self,
        url: &str,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        let mut vulnerabilities = Vec::new();
        let tests_run = 12;

        debug!("Testing array parameter pollution");

        // Array pollution payloads
        let marker_array_1 = format!("users[0][{}]=injected", self.test_marker);
        let marker_array_2 = format!("data[0][test][{}]=injected", self.test_marker);

        let array_payloads = vec![
            // Simple array injection
            ("users[0][admin]=true", "array injection: users[0][admin]"),
            ("users[0][role]=admin", "array injection: users[0][role]"),
            (
                "users[0][isAdmin]=true",
                "array injection: users[0][isAdmin]",
            ),
            // Multi-index array injection
            ("items[0][price]=0", "array injection: items[0][price]"),
            ("orders[0][amount]=0", "array injection: orders[0][amount]"),
            // Array with nested objects
            (
                "users[0][profile][admin]=true",
                "nested array: users[0][profile][admin]",
            ),
            (
                "accounts[0][permissions][role]=admin",
                "nested array: accounts[0][permissions][role]",
            ),
            // Array with marker
            (marker_array_1.as_str(), "array with marker"),
            (marker_array_2.as_str(), "nested array with marker"),
            // Multiple indices
            (
                "users[0][admin]=true&users[1][admin]=true",
                "multi-index injection",
            ),
            ("items[0][price]=0&items[1][price]=0", "multi-index price"),
            // Negative indices
            ("users[-1][admin]=true", "negative index injection"),
        ];

        for (payload, technique) in array_payloads {
            let test_url = if url.contains('?') {
                format!("{}&{}", url, payload)
            } else {
                format!("{}?{}", url, payload)
            };

            match self.http_client.get(&test_url).await {
                Ok(response) => {
                    if self.detect_array_pollution(&response.body, payload) {
                        info!("Array parameter pollution detected: {}", technique);
                        vulnerabilities.push(self.create_vulnerability(
                            url,
                            "Array Parameter Pollution",
                            payload,
                            "Array elements can be manipulated through mass assignment",
                            &format!("Successfully polluted array using {}", technique),
                            Severity::High,
                            "CWE-915",
                        ));
                        break;
                    }
                }
                Err(e) => {
                    debug!("Array pollution test failed: {}", e);
                }
            }
        }

        Ok((vulnerabilities, tests_run))
    }

    /// Test constructor property injection
    /// Tests constructor[name], constructor[prototype], etc.
    async fn test_constructor_property_injection(
        &self,
        url: &str,
    ) -> anyhow::Result<(Vec<Vulnerability>, usize)> {
        let mut vulnerabilities = Vec::new();
        let tests_run = 10;

        debug!("Testing constructor property injection");

        // Constructor injection payloads
        let marker_constructor_1 = format!("constructor[{}]=injected", self.test_marker);
        let marker_constructor_2 = format!("constructor[prototype][{}]=injected", self.test_marker);

        let constructor_payloads = vec![
            (marker_constructor_1.as_str(), "constructor with marker"),
            ("constructor[name]=admin", "constructor name injection"),
            (
                marker_constructor_2.as_str(),
                "constructor.prototype with marker",
            ),
            (
                "constructor[prototype][isAdmin]=true",
                "constructor.prototype privilege",
            ),
            (
                "constructor[prototype][role]=admin",
                "constructor.prototype role",
            ),
        ];

        // Test URL-encoded
        for (payload, technique) in &constructor_payloads {
            let test_url = if url.contains('?') {
                format!("{}&{}", url, payload)
            } else {
                format!("{}?{}", url, payload)
            };

            match self.http_client.get(&test_url).await {
                Ok(response) => {
                    if self.detect_constructor_injection(&response.body, payload) {
                        info!("Constructor property injection detected: {}", technique);
                        vulnerabilities.push(self.create_vulnerability(
                            url,
                            "Constructor Property Injection",
                            payload,
                            "Constructor properties can be modified through mass assignment",
                            &format!(
                                "Successfully injected constructor property using {}",
                                technique
                            ),
                            Severity::High,
                            "CWE-915",
                        ));
                        return Ok((vulnerabilities, tests_run));
                    }
                }
                Err(e) => {
                    debug!("Constructor injection test failed: {}", e);
                }
            }
        }

        // Test JSON constructor injection
        let json_payloads = vec![
            json!({
                "constructor": {
                    self.test_marker.clone(): "injected"
                }
            }),
            json!({
                "constructor": {
                    "name": "admin"
                }
            }),
            json!({
                "constructor": {
                    "prototype": {
                        self.test_marker.clone(): "injected"
                    }
                }
            }),
            json!({
                "constructor": {
                    "prototype": {
                        "isAdmin": true
                    }
                }
            }),
            json!({
                "user": {
                    "constructor": {
                        "name": "Administrator"
                    }
                }
            }),
        ];

        for payload in json_payloads {
            let headers = vec![("Content-Type".to_string(), "application/json".to_string())];

            match self
                .http_client
                .post_with_headers(url, &payload.to_string(), headers)
                .await
            {
                Ok(response) => {
                    if self.detect_constructor_injection(&response.body, &payload.to_string()) {
                        info!("JSON constructor property injection detected");
                        vulnerabilities.push(self.create_vulnerability(
                            url,
                            "Constructor Property Injection via JSON",
                            &payload.to_string(),
                            "Constructor properties can be modified through JSON mass assignment",
                            "Successfully injected constructor property through JSON",
                            Severity::High,
                            "CWE-915",
                        ));
                        break;
                    }
                }
                Err(e) => {
                    debug!("JSON constructor injection test failed: {}", e);
                }
            }
        }

        Ok((vulnerabilities, tests_run))
    }

    /// Detect nested object injection in response
    fn detect_nested_injection(&self, body: &str, payload: &str) -> bool {
        // Check if response is JSON
        let is_json = body.trim().starts_with('{') || body.trim().starts_with('[');
        if !is_json {
            return false;
        }

        let body_lower = body.to_lowercase();

        // Check for our unique test marker (strongest evidence)
        if body_lower.contains(&self.test_marker.to_lowercase()) {
            return true;
        }

        // Extract the injected property/value from payload
        if payload.contains("role]=admin") || payload.contains(".role=admin") {
            // Look for role being set to admin in JSON response
            if (body_lower.contains("\"role\":\"admin\"")
                || body_lower.contains("\"role\": \"admin\"")
                || body_lower.contains("'role':'admin'"))
                && (body_lower.contains("user") || body_lower.contains("profile"))
            {
                return true;
            }
        }

        if payload.contains("isadmin]=true") || payload.contains(".isadmin=true") {
            // Look for isAdmin being true in structured response
            if body_lower.contains("\"isadmin\":true")
                || body_lower.contains("\"isadmin\": true")
                || body_lower.contains("'isadmin':true")
            {
                return true;
            }
        }

        if payload.contains("admin]=true")
            || payload.contains(".admin=true")
            || payload.contains("admin]=1")
            || payload.contains(".admin=1")
        {
            // Look for admin property in nested context
            if (body_lower.contains("\"admin\":true")
                || body_lower.contains("\"admin\": true")
                || body_lower.contains("\"admin\":1")
                || body_lower.contains("'admin':true"))
                && (body_lower.contains("permissions")
                    || body_lower.contains("settings")
                    || body_lower.contains("profile"))
            {
                return true;
            }
        }

        false
    }

    /// Detect JSON deep merge injection
    fn detect_deep_merge_injection(&self, body: &str, payload: &serde_json::Value) -> bool {
        // Check if response is JSON
        let is_json = body.trim().starts_with('{') || body.trim().starts_with('[');
        if !is_json {
            return false;
        }

        let body_lower = body.to_lowercase();

        // Check for our unique test marker (strongest evidence)
        if body_lower.contains(&self.test_marker.to_lowercase()) {
            return true;
        }

        // Parse the payload to extract injected properties
        if let Some(obj) = payload.as_object() {
            // Check for user/profile/account properties
            if obj.contains_key("user")
                || obj.contains_key("profile")
                || obj.contains_key("account")
            {
                // Look for role: admin pattern
                if body_lower.contains("\"role\":\"admin\"")
                    || body_lower.contains("\"role\": \"admin\"")
                {
                    return true;
                }

                // Look for isAdmin: true pattern
                if body_lower.contains("\"isadmin\":true")
                    || body_lower.contains("\"isadmin\": true")
                {
                    return true;
                }

                // Look for admin: true in permissions context
                if (body_lower.contains("\"admin\":true") || body_lower.contains("\"admin\": true"))
                    && (body_lower.contains("permissions") || body_lower.contains("settings"))
                {
                    return true;
                }
            }

            // Check for price/amount manipulation
            if obj.contains_key("order") || obj.contains_key("price") || obj.contains_key("amount")
            {
                if body_lower.contains("\"price\":0")
                    || body_lower.contains("\"price\": 0")
                    || body_lower.contains("\"amount\":0")
                {
                    return true;
                }
            }
        }

        false
    }

    /// Detect prototype pollution via mass assignment
    fn detect_prototype_pollution(&self, body: &str, payload: &str) -> bool {
        let body_lower = body.to_lowercase();

        // PRIMARY: Check for our unique test marker (strongest evidence)
        if body_lower.contains(&self.test_marker.to_lowercase()) {
            return true;
        }

        // Check if response is JSON
        let is_json = body.trim().starts_with('{') || body.trim().starts_with('[');

        // SECONDARY: Look for __proto__ or constructor.prototype in response with injected properties
        if payload.contains("__proto__") && is_json {
            // Look for __proto__ object in response
            if body_lower.contains("\"__proto__\"") || body_lower.contains("'__proto__'") {
                // Check if it contains our injected properties
                if body_lower.contains("\"isadmin\":true")
                    || body_lower.contains("\"admin\":true")
                    || body_lower.contains("\"role\":\"admin\"")
                {
                    return true;
                }
            }
        }

        if payload.contains("constructor") && payload.contains("prototype") && is_json {
            // Look for constructor.prototype in response
            if (body_lower.contains("\"constructor\"") || body_lower.contains("'constructor'"))
                && (body_lower.contains("\"prototype\"") || body_lower.contains("'prototype'"))
            {
                if body_lower.contains("\"isadmin\":true") || body_lower.contains("\"admin\":true")
                {
                    return true;
                }
            }
        }

        // TERTIARY: Look for pollution-related errors or warnings
        if body_lower.contains("prototype pollution")
            || (body_lower.contains("proto__")
                && (body_lower.contains("error") || body_lower.contains("warning")))
        {
            return true;
        }

        false
    }

    /// Detect array parameter pollution
    fn detect_array_pollution(&self, body: &str, payload: &str) -> bool {
        // Check if response is JSON or structured data
        let is_json = body.trim().starts_with('{') || body.trim().starts_with('[');
        if !is_json {
            return false;
        }

        let body_lower = body.to_lowercase();

        // Check for our unique test marker
        if body_lower.contains(&self.test_marker.to_lowercase()) {
            return true;
        }

        // Look for array patterns in response
        if payload.contains("[0]") || payload.contains("[-1]") {
            // Check for admin/role injection in array context
            if payload.contains("admin]=true") || payload.contains("role]=admin") {
                if body_lower.contains("\"admin\":true")
                    || body_lower.contains("\"role\":\"admin\"")
                {
                    // Verify it's in an array context
                    if body.contains('[') && body.contains(']') {
                        return true;
                    }
                }
            }

            // Check for price manipulation in array
            if payload.contains("price]=0") || payload.contains("amount]=0") {
                if (body_lower.contains("\"price\":0") || body_lower.contains("\"amount\":0"))
                    && body.contains('[')
                {
                    return true;
                }
            }
        }

        false
    }

    /// Detect constructor property injection
    fn detect_constructor_injection(&self, body: &str, payload: &str) -> bool {
        // Check if response is JSON
        let is_json = body.trim().starts_with('{') || body.trim().starts_with('[');
        if !is_json {
            return false;
        }

        let body_lower = body.to_lowercase();

        // Check for our unique test marker
        if body_lower.contains(&self.test_marker.to_lowercase()) {
            return true;
        }

        // Look for constructor in response
        if payload.contains("constructor") {
            if body_lower.contains("\"constructor\"") || body_lower.contains("'constructor'") {
                // Check for injected properties
                if body_lower.contains("\"name\":\"admin\"")
                    || body_lower.contains("\"isadmin\":true")
                    || (body_lower.contains("\"prototype\"")
                        && body_lower.contains("\"admin\":true"))
                {
                    return true;
                }
            }
        }

        // Look for constructor-related errors or modifications
        if body_lower.contains("constructor")
            && (body_lower.contains("modified")
                || body_lower.contains("changed")
                || body_lower.contains("updated"))
        {
            return true;
        }

        false
    }

    /// Detect privilege escalation
    fn detect_privilege_escalation(&self, body: &str, param: &str, value: &str) -> bool {
        let body_lower = body.to_lowercase();

        // First, check if this looks like a SPA/soft-404 response (HTML page returned for all routes)
        // SPA responses contain typical frontend markers and should not be considered as API responses
        if self.is_spa_response(body) {
            return false;
        }

        // Check if parameter was accepted - this is the strongest evidence
        // Must be in a JSON-like structure to be considered valid
        if body_lower.contains(&format!("\"{}\":\"{}\"", param, value))
            || body_lower.contains(&format!("{}\":{}", param, value))
            || body_lower.contains(&format!("'{}':'{}'", param, value))
        {
            return true;
        }

        // Only check for privilege indicators in structured API responses (JSON)
        // Do NOT check for these words in HTML responses as they cause false positives
        if !body.trim().starts_with("{") && !body.trim().starts_with("[") {
            // Not a JSON response, skip privilege indicator check
            return false;
        }

        // For JSON responses, check for privilege indicators in the context of the injected param
        let privilege_patterns = vec![
            format!("\"{}\":\"admin\"", param),
            format!("\"{}\":true", param),
            format!("\"{}\":1", param),
            format!("\"{}\":\"administrator\"", param),
            format!("\"{}\":\"superuser\"", param),
        ];

        for pattern in privilege_patterns {
            if body_lower.contains(&pattern.to_lowercase()) {
                return true;
            }
        }

        false
    }

    /// Check if response is a SPA/single-page-application fallback (soft-404)
    fn is_spa_response(&self, body: &str) -> bool {
        let spa_indicators = [
            "<app-root>",
            "<div id=\"root\">",
            "<div id=\"app\">",
            "__NEXT_DATA__",
            "__NUXT__",
            "ng-version=",
            "data-reactroot",
            "<script src=\"/main.",
            "<script src=\"main.",
            "polyfills.js",
            "/static/js/main.",
            "/_next/static/",
            "window.__REDUX",
            "window.__PRELOADED_STATE__",
        ];

        for indicator in &spa_indicators {
            if body.contains(indicator) {
                return true;
            }
        }

        // Check for common SPA HTML structure with no actual API content
        if body.contains("<!DOCTYPE html>") || body.contains("<!doctype html>") {
            if body.contains("<script")
                && (body.contains("angular") || body.contains("react") || body.contains("vue"))
            {
                return true;
            }
        }

        false
    }

    /// Detect price manipulation
    /// STRICT: Only trigger if we see actual price-related JSON or e-commerce context
    fn detect_price_manipulation(&self, body: &str, value: &str) -> bool {
        let body_lower = body.to_lowercase();

        // Must be a likely e-commerce response (not a generic page)
        let has_ecommerce_context = body_lower.contains("cart")
            || body_lower.contains("checkout")
            || body_lower.contains("product")
            || body_lower.contains("order")
            || body_lower.contains("\"items\"")
            || body_lower.contains("\"subtotal\"")
            || body_lower.contains("\"discount\"");

        if !has_ecommerce_context {
            return false;
        }

        // Check if price was set to manipulated value (must see actual JSON with price)
        body_lower.contains(&format!("\"price\":\"{}\"", value))
            || body_lower.contains(&format!("\"price\":{}", value))
            || body_lower.contains(&format!("\"amount\":\"{}\"", value))
            || body_lower.contains(&format!("\"total\":\"{}\"", value))
            || body_lower.contains(&format!("\"cost\":{}", value))
    }

    /// Detect hidden field manipulation
    /// STRICT: Require actual JSON response containing the param
    fn detect_hidden_field_manipulation(&self, body: &str, param: &str) -> bool {
        let body_lower = body.to_lowercase();
        let param_lower = param.to_lowercase();

        // Must be JSON or API response (not generic HTML page)
        let is_json_response = body_lower.starts_with("{")
            || body_lower.starts_with("[")
            || body_lower.contains("application/json")
            || body_lower.contains("\"success\":")
            || body_lower.contains("\"data\":");

        if !is_json_response {
            return false;
        }

        // Check if hidden parameter appears in response with our value
        body_lower.contains(&format!("\"{}\":", param_lower))
            || body_lower.contains(&format!("'{}':", param_lower))
    }

    /// Create a vulnerability record
    fn create_vulnerability(
        &self,
        url: &str,
        vuln_type: &str,
        payload: &str,
        description: &str,
        evidence: &str,
        severity: Severity,
        cwe: &str,
    ) -> Vulnerability {
        let cvss = match severity {
            Severity::Critical => 9.1,
            Severity::High => 7.5,
            Severity::Medium => 5.3,
            _ => 3.1,
        };

        Vulnerability {
            id: format!("ma_{}", uuid::Uuid::new_v4().to_string()),
            vuln_type: vuln_type.to_string(),
            severity,
            confidence: Confidence::Medium,
            category: "Business Logic".to_string(),
            url: url.to_string(),
            parameter: None,
            payload: payload.to_string(),
            description: description.to_string(),
            evidence: Some(evidence.to_string()),
            cwe: cwe.to_string(),
            cvss: cvss as f32,
            verified: true,
            false_positive: false,
            remediation: "1. Use allowlists for bindable attributes (strong parameters)\n\
                         2. Never bind user input directly to model objects\n\
                         3. Explicitly define which fields can be mass-assigned\n\
                         4. Use DTOs (Data Transfer Objects) for user input\n\
                         5. Validate all input against expected schema\n\
                         6. Mark sensitive fields as read-only or protected\n\
                         7. Implement proper authorization checks before updates\n\
                         8. Use frameworks' built-in protection (Rails strong parameters, etc.)\n\
                         9. Avoid automatic parameter binding in frameworks\n\
                         10. Implement field-level access controls\n\
                         11. Block nested object injection (validate nesting depth)\n\
                         12. Prevent prototype pollution (__proto__, constructor, prototype)\n\
                         13. Sanitize array parameter indices\n\
                         14. Use Object.create(null) for objects without prototype\n\
                         15. Implement strict JSON schema validation for deep merges"
                .to_string(),
            discovered_at: chrono::Utc::now().to_rfc3339(),
            ml_data: None,
        }
    }
}

// UUID generation helper
mod uuid {
    use rand::Rng;

    pub struct Uuid;

    impl Uuid {
        pub fn new_v4() -> Self {
            Uuid
        }

        pub fn to_string(&self) -> String {
            let mut rng = rand::rng();
            format!(
                "{:08x}{:04x}{:04x}{:04x}{:012x}",
                rng.random::<u32>(),
                rng.random::<u16>(),
                rng.random::<u16>(),
                rng.random::<u16>(),
                rng.random::<u64>() & 0xffffffffffff
            )
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::http_client::HttpClient;
    use std::sync::Arc;

    fn create_test_scanner() -> MassAssignmentScanner {
        let http_client = Arc::new(HttpClient::new(30, 3).unwrap());
        MassAssignmentScanner::new(http_client)
    }

    #[test]
    fn test_detect_privilege_escalation() {
        let scanner = create_test_scanner();

        assert!(scanner.detect_privilege_escalation(r#"{"role":"admin"}"#, "role", "admin"));
        assert!(scanner.detect_privilege_escalation("Welcome administrator", "is_admin", "true"));
    }

    #[test]
    fn test_detect_price_manipulation() {
        let scanner = create_test_scanner();

        assert!(scanner.detect_price_manipulation(r#"{"price":"0"}"#, "0"));
        assert!(scanner.detect_price_manipulation(r#"{"price":0}"#, "0"));
        assert!(scanner.detect_price_manipulation("Price updated successfully", "0.01"));
    }

    #[test]
    fn test_detect_hidden_field_manipulation() {
        let scanner = create_test_scanner();

        assert!(scanner.detect_hidden_field_manipulation(r#"{"user_id":1}"#, "user_id"));
        assert!(scanner.detect_hidden_field_manipulation("User updated", "verified"));
    }

    #[test]
    fn test_no_false_positives() {
        let scanner = create_test_scanner();

        assert!(!scanner.detect_privilege_escalation("Normal response", "role", "user"));
        assert!(!scanner.detect_price_manipulation("Invalid request", "999"));
        assert!(!scanner.detect_hidden_field_manipulation("Error", "unknown"));
    }

    #[test]
    fn test_create_vulnerability() {
        let scanner = create_test_scanner();

        let vuln = scanner.create_vulnerability(
            "http://example.com",
            "Privilege Escalation via Mass Assignment",
            "role=admin",
            "Mass assignment detected",
            "Role set to admin",
            Severity::Critical,
            "CWE-915",
        );

        assert_eq!(vuln.vuln_type, "Privilege Escalation via Mass Assignment");
        assert_eq!(vuln.severity, Severity::Critical);
        assert_eq!(vuln.cwe, "CWE-915");
        assert_eq!(vuln.cvss, 9.1);
    }

    #[test]
    fn test_detect_nested_injection() {
        let scanner = create_test_scanner();

        // Test marker-based detection (strongest evidence)
        let body_with_marker = format!(
            r#"{{"user":{{"role":{{"{}":"injected"}}}}}}"#,
            scanner.test_marker
        );
        assert!(scanner.detect_nested_injection(
            &body_with_marker,
            &format!("user[role][{}]=injected", scanner.test_marker)
        ));

        // Test nested role injection
        assert!(scanner.detect_nested_injection(r#"{"user":{"role":"admin"}}"#, "user[role]=admin"));

        // Test nested isAdmin injection
        assert!(
            scanner.detect_nested_injection(r#"{"user":{"isAdmin":true}}"#, "user[isAdmin]=true")
        );

        // Test deep nested admin property
        assert!(scanner.detect_nested_injection(
            r#"{"profile":{"permissions":{"admin":true}}}"#,
            "profile[permissions][admin]=true"
        ));

        // Test dot notation
        assert!(scanner.detect_nested_injection(r#"{"user":{"role":"admin"}}"#, "user.role=admin"));
    }

    #[test]
    fn test_detect_nested_injection_no_false_positives() {
        let scanner = create_test_scanner();

        // HTML response should not trigger
        assert!(!scanner.detect_nested_injection("<html>Welcome admin</html>", "user[role]=admin"));

        // Generic JSON without nested structure
        assert!(!scanner.detect_nested_injection(r#"{"message":"success"}"#, "user[role]=admin"));

        // Admin in wrong context
        assert!(!scanner.detect_nested_injection(r#"{"admin":"John Doe"}"#, "user[role]=admin"));
    }

    #[test]
    fn test_detect_deep_merge_injection() {
        let scanner = create_test_scanner();

        // Test with marker
        let payload_with_marker = json!({
            "user": {
                scanner.test_marker.clone(): "injected"
            }
        });
        let body_with_marker = format!(r#"{{"user":{{"{}":"injected"}}}}"#, scanner.test_marker);
        assert!(scanner.detect_deep_merge_injection(&body_with_marker, &payload_with_marker));

        // Test role injection
        let payload = json!({"user": {"role": "admin"}});
        assert!(scanner.detect_deep_merge_injection(r#"{"user":{"role":"admin"}}"#, &payload));

        // Test isAdmin injection
        let payload = json!({"user": {"isAdmin": true}});
        assert!(scanner.detect_deep_merge_injection(r#"{"user":{"isAdmin":true}}"#, &payload));

        // Test nested permissions
        let payload = json!({"profile": {"permissions": {"admin": true}}});
        assert!(scanner.detect_deep_merge_injection(
            r#"{"profile":{"permissions":{"admin":true}}}"#,
            &payload
        ));

        // Test price manipulation
        let payload = json!({"order": {"price": 0}});
        assert!(scanner.detect_deep_merge_injection(r#"{"order":{"price":0}}"#, &payload));
    }

    #[test]
    fn test_detect_prototype_pollution() {
        let scanner = create_test_scanner();

        // Test with unique marker (strongest evidence)
        let payload_with_marker = format!("__proto__[{}]=polluted", scanner.test_marker);
        let body_with_marker = format!(r#"{{"{}":"polluted"}}"#, scanner.test_marker);
        assert!(scanner.detect_prototype_pollution(&body_with_marker, &payload_with_marker));

        // Test __proto__ with isAdmin
        assert!(scanner.detect_prototype_pollution(
            r#"{"__proto__":{"isAdmin":true}}"#,
            "__proto__[isAdmin]=true"
        ));

        // Test constructor.prototype
        assert!(scanner.detect_prototype_pollution(
            r#"{"constructor":{"prototype":{"admin":true}}}"#,
            "constructor[prototype][admin]=true"
        ));

        // Test pollution error detection
        assert!(scanner.detect_prototype_pollution(
            "Error: Prototype pollution detected",
            "__proto__[test]=value"
        ));
    }

    #[test]
    fn test_detect_prototype_pollution_no_false_positives() {
        let scanner = create_test_scanner();

        // Documentation mentioning __proto__ should not trigger
        assert!(!scanner.detect_prototype_pollution(
            "Learn about __proto__ in JavaScript",
            "__proto__[test]=value"
        ));

        // HTML response
        assert!(!scanner
            .detect_prototype_pollution("<html>Admin panel</html>", "__proto__[admin]=true"));

        // Generic JSON without pollution evidence
        assert!(!scanner
            .detect_prototype_pollution(r#"{"message":"success"}"#, "__proto__[test]=value"));
    }

    #[test]
    fn test_detect_array_pollution() {
        let scanner = create_test_scanner();

        // Test with marker
        let payload_with_marker = format!("users[0][{}]=injected", scanner.test_marker);
        let body_with_marker = format!(r#"[{{"{}":"injected"}}]"#, scanner.test_marker);
        assert!(scanner.detect_array_pollution(&body_with_marker, &payload_with_marker));

        // Test array admin injection
        assert!(scanner.detect_array_pollution(r#"[{"admin":true}]"#, "users[0][admin]=true"));

        // Test array role injection
        assert!(scanner.detect_array_pollution(r#"[{"role":"admin"}]"#, "users[0][role]=admin"));

        // Test array price manipulation
        assert!(scanner.detect_array_pollution(r#"[{"price":0}]"#, "items[0][price]=0"));
    }

    #[test]
    fn test_detect_array_pollution_no_false_positives() {
        let scanner = create_test_scanner();

        // HTML response
        assert!(!scanner.detect_array_pollution("<html>Users list</html>", "users[0][admin]=true"));

        // Non-array JSON
        assert!(!scanner.detect_array_pollution(r#"{"message":"success"}"#, "users[0][admin]=true"));
    }

    #[test]
    fn test_detect_constructor_injection() {
        let scanner = create_test_scanner();

        // Test with marker
        let payload_with_marker = format!("constructor[{}]=injected", scanner.test_marker);
        let body_with_marker = format!(
            r#"{{"constructor":{{"{}":"injected"}}}}"#,
            scanner.test_marker
        );
        assert!(scanner.detect_constructor_injection(&body_with_marker, &payload_with_marker));

        // Test constructor name injection
        assert!(scanner.detect_constructor_injection(
            r#"{"constructor":{"name":"admin"}}"#,
            "constructor[name]=admin"
        ));

        // Test constructor.prototype injection
        assert!(scanner.detect_constructor_injection(
            r#"{"constructor":{"prototype":{"admin":true}}}"#,
            "constructor[prototype][admin]=true"
        ));

        // Test constructor modification message
        assert!(scanner.detect_constructor_injection(
            r#"{"constructor":"modified"}"#,
            "constructor[test]=value"
        ));
    }

    #[test]
    fn test_detect_constructor_injection_no_false_positives() {
        let scanner = create_test_scanner();

        // HTML response
        assert!(!scanner.detect_constructor_injection(
            "<html>Constructor pattern</html>",
            "constructor[test]=value"
        ));

        // Generic JSON
        assert!(!scanner
            .detect_constructor_injection(r#"{"message":"success"}"#, "constructor[test]=value"));
    }

    #[test]
    fn test_unique_test_marker() {
        let scanner1 = create_test_scanner();
        let scanner2 = create_test_scanner();

        // Each scanner should have a unique marker
        assert_ne!(scanner1.test_marker, scanner2.test_marker);
        assert!(scanner1.test_marker.starts_with("ma_"));
        assert!(scanner2.test_marker.starts_with("ma_"));
    }
}