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
// Copyright (c) 2026 Bountyy Oy. All rights reserved.
// This software is proprietary and confidential.

/**
 * Bountyy Oy - WebSocket Security Scanner
 * Tests for WebSocket vulnerabilities and misconfigurations
 *
 * @copyright 2026 Bountyy Oy
 * @license Proprietary - Enterprise Edition
 */
use crate::detection_helpers::AppCharacteristics;
use crate::http_client::HttpClient;
use crate::types::{Confidence, ScanConfig, Severity, Vulnerability};
use anyhow::Result;
use futures_util::{SinkExt, StreamExt};
use std::sync::Arc;
use std::time::Duration;
use tokio::time::timeout;
use tokio_tungstenite::{connect_async, tungstenite::Message};
use tracing::{debug, info};

pub struct WebSocketScanner {
    http_client: Arc<HttpClient>,
}

impl WebSocketScanner {
    pub fn new(http_client: Arc<HttpClient>) -> Self {
        Self { http_client }
    }

    /// Scan URL for WebSocket vulnerabilities
    pub async fn scan(
        &self,
        url: &str,
        _config: &ScanConfig,
    ) -> Result<(Vec<Vulnerability>, usize)> {
        info!("[WebSocket] Scanning: {}", url);

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

        // Test 1: Detect WebSocket endpoints (passive detection from response)
        tests_run += 1;
        let response = match self.http_client.get(url).await {
            Ok(r) => r,
            Err(_) => {
                info!("[NOTE] [WebSocket] Could not fetch URL");
                return Ok((vulnerabilities, tests_run));
            }
        };

        // Intelligent detection
        let characteristics = AppCharacteristics::from_response(&response, url);
        let is_websocket = self.detect_websocket_endpoint(&response, url);

        // Test 2: Active WebSocket endpoint discovery (probe common paths)
        tests_run += 1;
        let discovered_endpoints = self.discover_websocket_endpoints(url).await;

        // If not detected passively and no active discovery, skip WebSocket tests
        if !is_websocket && discovered_endpoints.is_empty() {
            debug!("[NOTE] [WebSocket] Not a WebSocket endpoint");
            return Ok((vulnerabilities, tests_run));
        }

        // Store characteristics for later use in tests
        let _app_chars = characteristics;

        info!("[SUCCESS] [WebSocket] WebSocket endpoint detected");

        // Report discovered endpoints
        if !discovered_endpoints.is_empty() {
            info!(
                "[WebSocket] Discovered {} WebSocket endpoints: {:?}",
                discovered_endpoints.len(),
                discovered_endpoints
            );
        }

        // Test 3: Check for missing origin validation
        tests_run += 1;
        self.check_origin_validation(&response, url, &mut vulnerabilities);

        // Test 4: Check for missing authentication
        tests_run += 1;
        self.check_authentication(&response, url, &mut vulnerabilities);

        // Test 5: Test CSWSH (Cross-Site WebSocket Hijacking)
        tests_run += 1;
        if let Ok(cswsh_response) = self.test_cswsh(url).await {
            self.check_cswsh(&cswsh_response, url, &mut vulnerabilities);
        }

        // Test 6: Check for sensitive data in WebSocket URL
        tests_run += 1;
        self.check_sensitive_data_in_url(url, &mut vulnerabilities);

        // Test 7: Check for tunnel/message injection
        tests_run += 1;
        self.check_message_injection(&response, url, &mut vulnerabilities);

        // Test 8: Check for rate limiting
        tests_run += 1;
        self.check_rate_limiting(&response, url, &mut vulnerabilities);

        // Test 9: Check WebSocket Sec headers
        tests_run += 1;
        self.check_sec_headers(&response, url, &mut vulnerabilities);

        // Try to convert HTTP(S) URL to WebSocket URL for exploitation tests
        let ws_url = self.convert_to_ws_url(url);
        if let Some(ws_url) = &ws_url {
            // Test 10: WebSocket CSRF
            tests_run += 1;
            if let Ok(csrf_vulns) = self.test_websocket_csrf(ws_url).await {
                vulnerabilities.extend(csrf_vulns);
            }

            // Test 11: WebSocket Message Injection
            tests_run += 1;
            if let Ok(injection_vulns) = self.test_message_injection(ws_url).await {
                vulnerabilities.extend(injection_vulns);
            }

            // Test 12: WebSocket Origin Bypass (exploitation)
            tests_run += 1;
            if let Ok(origin_vulns) = self.test_origin_bypass(ws_url).await {
                vulnerabilities.extend(origin_vulns);
            }

            // Test 13: WebSocket Hijacking
            tests_run += 1;
            if let Ok(hijack_vulns) = self.test_websocket_hijacking(ws_url).await {
                vulnerabilities.extend(hijack_vulns);
            }

            // Test 14: WebSocket Denial of Service (limited)
            tests_run += 1;
            if let Ok(dos_vulns) = self.test_websocket_dos(ws_url).await {
                vulnerabilities.extend(dos_vulns);
            }

            // Test 15: WebSocket Protocol Confusion
            tests_run += 1;
            if let Ok(confusion_vulns) = self.test_protocol_confusion(ws_url).await {
                vulnerabilities.extend(confusion_vulns);
            }
        }

        // Test discovered endpoints as well
        for endpoint in discovered_endpoints {
            // Test 16+: Test each discovered endpoint for CSWSH
            tests_run += 1;
            if let Ok(origin_vulns) = self.test_origin_bypass(&endpoint).await {
                vulnerabilities.extend(origin_vulns);
            }
        }

        info!(
            "[SUCCESS] [WebSocket] Completed {} tests, found {} issues",
            tests_run,
            vulnerabilities.len()
        );

        Ok((vulnerabilities, tests_run))
    }

    /// Detect WebSocket endpoint
    fn detect_websocket_endpoint(
        &self,
        response: &crate::http_client::HttpResponse,
        url: &str,
    ) -> bool {
        let body_lower = response.body.to_lowercase();

        // Check URL for ws:// or wss://
        let url_lower = url.to_lowercase();
        if url_lower.contains("ws://") || url_lower.contains("wss://") {
            return true;
        }

        // Check response body for WebSocket indicators
        body_lower.contains("websocket")
            || body_lower.contains("ws://")
            || body_lower.contains("wss://")
            || body_lower.contains("socket.io")
            || body_lower.contains("sockjs")
            || response
                .header("upgrade")
                .map_or(false, |h| h.to_lowercase() == "websocket")
    }

    /// Actively discover WebSocket endpoints by probing common paths
    async fn discover_websocket_endpoints(&self, url: &str) -> Vec<String> {
        let mut discovered = Vec::new();

        // Parse base URL
        let base_url = if let Ok(parsed) = url::Url::parse(url) {
            let scheme = if parsed.scheme() == "https" {
                "wss"
            } else {
                "ws"
            };
            let host = parsed.host_str().unwrap_or("localhost");
            let port = if let Some(p) = parsed.port() {
                format!(":{}", p)
            } else {
                String::new()
            };
            format!("{}://{}{}", scheme, host, port)
        } else {
            return discovered;
        };

        // Common WebSocket endpoint paths
        let common_paths = vec![
            "/ws",
            "/wss",
            "/websocket",
            "/socket",
            "/socket.io",
            "/sockjs",
            "/api/ws",
            "/api/websocket",
            "/realtime",
            "/live",
            "/chat",
            "/stream",
            "/events",
            "/notifications",
        ];

        // Try to connect to each common path
        for path in common_paths {
            let ws_url = format!("{}{}", base_url, path);

            // Try to establish WebSocket connection with a short timeout
            match timeout(Duration::from_secs(2), connect_async(&ws_url)).await {
                Ok(Ok((ws_stream, _response))) => {
                    debug!("[WebSocket Discovery] Found endpoint: {}", ws_url);
                    discovered.push(ws_url.clone());

                    // Close the connection
                    let (mut write, _) = ws_stream.split();
                    let _ = write.send(Message::Close(None)).await;
                }
                Ok(Err(e)) => {
                    // Connection failed, but check if it's an HTTP upgrade response
                    let error_str = e.to_string().to_lowercase();
                    if error_str.contains("upgrade") || error_str.contains("101") {
                        debug!("[WebSocket Discovery] Potential endpoint: {} (upgrade-related error)", ws_url);
                        discovered.push(ws_url);
                    }
                }
                Err(_) => {
                    // Timeout - skip
                }
            }
        }

        discovered
    }

    /// Check origin validation
    fn check_origin_validation(
        &self,
        response: &crate::http_client::HttpResponse,
        url: &str,
        vulnerabilities: &mut Vec<Vulnerability>,
    ) {
        let body = &response.body;
        let body_lower = body.to_lowercase();

        // Check if code validates Origin header
        let checks_origin = body_lower.contains("origin")
            && (body_lower.contains("check")
                || body_lower.contains("validate")
                || body_lower.contains("verify")
                || body_lower.contains("allowed")
                || body_lower.contains("whitelist"));

        // Check for WebSocket upgrade without origin validation
        if (body_lower.contains("websocket") || body_lower.contains("upgrade")) && !checks_origin {
            vulnerabilities.push(self.create_vulnerability(
                "Missing WebSocket Origin Validation",
                url,
                Severity::High,
                Confidence::Medium,
                "WebSocket endpoint does not validate Origin header - vulnerable to CSWSH",
                "No Origin header validation detected in WebSocket code".to_string(),
                7.4,
            ));
        }
    }

    /// Check authentication
    fn check_authentication(
        &self,
        response: &crate::http_client::HttpResponse,
        url: &str,
        vulnerabilities: &mut Vec<Vulnerability>,
    ) {
        let body_lower = response.body.to_lowercase();

        // Check if authentication is implemented
        let has_auth = body_lower.contains("authenticate")
            || body_lower.contains("authorization")
            || body_lower.contains("token")
            || body_lower.contains("session");

        // If WebSocket but no auth indicators
        if (body_lower.contains("websocket") || body_lower.contains("socket.io")) && !has_auth {
            vulnerabilities.push(self.create_vulnerability(
                "WebSocket Missing Authentication",
                url,
                Severity::Medium,
                Confidence::Low,
                "WebSocket endpoint may lack authentication - verify manually",
                "No authentication mechanisms detected in WebSocket code".to_string(),
                5.3,
            ));
        }
    }

    /// Test CSWSH (Cross-Site WebSocket Hijacking)
    async fn test_cswsh(&self, url: &str) -> Result<crate::http_client::HttpResponse> {
        // Try to upgrade to WebSocket with malicious origin
        // Note: This is simplified since we're using HTTP client, not WebSocket client
        self.http_client.get(url).await
    }

    /// Check CSWSH vulnerability
    fn check_cswsh(
        &self,
        response: &crate::http_client::HttpResponse,
        url: &str,
        vulnerabilities: &mut Vec<Vulnerability>,
    ) {
        // Check if WebSocket upgrade succeeded without proper origin check
        if response.status_code == 101
            || response.header("upgrade").is_some()
            || response.header("sec-websocket-accept").is_some()
        {
            vulnerabilities.push(self.create_vulnerability(
                "Cross-Site WebSocket Hijacking (CSWSH)",
                url,
                Severity::High,
                Confidence::Medium,
                "WebSocket accepts connections without proper origin validation",
                "WebSocket upgrade accepted without origin validation".to_string(),
                7.5,
            ));
        }
    }

    /// Check for sensitive data in WebSocket URL
    fn check_sensitive_data_in_url(&self, url: &str, vulnerabilities: &mut Vec<Vulnerability>) {
        let url_lower = url.to_lowercase();

        // Check for sensitive parameters in URL
        let sensitive_params = vec![
            "token=",
            "api_key=",
            "apikey=",
            "secret=",
            "password=",
            "session=",
            "auth=",
            "key=",
            "access_token=",
        ];

        for param in &sensitive_params {
            if url_lower.contains(param) {
                vulnerabilities.push(self.create_vulnerability(
                    "Sensitive Data in WebSocket URL",
                    url,
                    Severity::Medium,
                    Confidence::High,
                    "WebSocket URL contains sensitive data - vulnerable to leakage via logs/referrer",
                    format!("Sensitive parameter '{}' found in WebSocket URL", param),
                    6.5,
                ));
                break;
            }
        }

        // Check for insecure ws:// instead of wss://
        if url.starts_with("ws://") && !url.contains("localhost") && !url.contains("127.0.0.1") {
            vulnerabilities.push(self.create_vulnerability(
                "WebSocket Using Insecure Protocol",
                url,
                Severity::Medium,
                Confidence::High,
                "WebSocket uses unencrypted ws:// protocol - data transmitted in plaintext",
                "WebSocket URL uses ws:// instead of wss://".to_string(),
                5.9,
            ));
        }
    }

    /// Check message injection vulnerabilities
    fn check_message_injection(
        &self,
        response: &crate::http_client::HttpResponse,
        url: &str,
        vulnerabilities: &mut Vec<Vulnerability>,
    ) {
        let body = &response.body;

        // Check if messages are processed without validation
        let validates_messages = body.contains("validate")
            || body.contains("sanitize")
            || body.contains("escape")
            || body.contains("filter");

        let processes_messages = body.contains("onmessage")
            || body.contains("on('message")
            || body.contains("addEventListener('message");

        if processes_messages && !validates_messages {
            vulnerabilities.push(self.create_vulnerability(
                "WebSocket Message Injection Risk",
                url,
                Severity::Medium,
                Confidence::Low,
                "WebSocket messages may not be validated - potential injection attacks",
                "Message processing without apparent validation detected".to_string(),
                5.0,
            ));
        }
    }

    /// Check rate limiting
    fn check_rate_limiting(
        &self,
        response: &crate::http_client::HttpResponse,
        url: &str,
        vulnerabilities: &mut Vec<Vulnerability>,
    ) {
        let body_lower = response.body.to_lowercase();

        // Check for rate limiting implementation
        let has_rate_limit = body_lower.contains("rate")
            || body_lower.contains("throttle")
            || body_lower.contains("limit")
            || body_lower.contains("quota");

        if (body_lower.contains("websocket") || body_lower.contains("socket")) && !has_rate_limit {
            vulnerabilities.push(self.create_vulnerability(
                "WebSocket Missing Rate Limiting",
                url,
                Severity::Low,
                Confidence::Low,
                "WebSocket endpoint may lack rate limiting - vulnerable to DoS",
                "No rate limiting implementation detected".to_string(),
                4.3,
            ));
        }
    }

    /// Check WebSocket Sec headers
    fn check_sec_headers(
        &self,
        response: &crate::http_client::HttpResponse,
        url: &str,
        vulnerabilities: &mut Vec<Vulnerability>,
    ) {
        // Check for proper Sec-WebSocket headers in upgrade response
        if response.header("upgrade").is_some() {
            if response.header("sec-websocket-accept").is_none() {
                vulnerabilities.push(self.create_vulnerability(
                    "Missing Sec-WebSocket-Accept Header",
                    url,
                    Severity::Low,
                    Confidence::High,
                    "WebSocket upgrade response missing Sec-WebSocket-Accept header",
                    "Required security header not present in upgrade response".to_string(),
                    3.7,
                ));
            }
        }
    }

    /// Convert HTTP(S) URL to WebSocket URL
    fn convert_to_ws_url(&self, url: &str) -> Option<String> {
        if url.starts_with("ws://") || url.starts_with("wss://") {
            return Some(url.to_string());
        }

        // Try common WebSocket endpoint patterns
        let base = if url.starts_with("https://") {
            url.replace("https://", "wss://")
        } else if url.starts_with("http://") {
            url.replace("http://", "ws://")
        } else {
            return None;
        };

        // Try common WebSocket paths
        let paths = vec![
            "/ws",
            "/wss",
            "/websocket",
            "/socket.io",
            "/sockjs",
            "/socket",
            "/chat",
            "/realtime",
            "/live",
            "/api/ws",
            "/api/websocket",
        ];

        // If URL already has a path that looks like WebSocket, use it
        for ws_path in &paths {
            if url.contains(ws_path) {
                return Some(base);
            }
        }

        // Otherwise, try appending common paths
        Some(format!("{}/ws", base.trim_end_matches('/')))
    }

    /// Test WebSocket CSRF - sensitive commands without authentication
    async fn test_websocket_csrf(&self, ws_url: &str) -> Result<Vec<Vulnerability>> {
        let mut vulnerabilities = Vec::new();
        let marker = uuid::Uuid::new_v4().to_string();

        info!("[WebSocket CSRF] Testing: {}", ws_url);

        // Try to connect WITHOUT cookies/auth
        let connect_result = timeout(Duration::from_secs(5), connect_async(ws_url)).await;

        let ws_stream = match connect_result {
            Ok(Ok((stream, response))) => {
                debug!(
                    "[WebSocket CSRF] Connected successfully: {:?}",
                    response.status()
                );

                // If connection succeeds without authentication, that's already a vulnerability
                if response.status().is_success() || response.status().as_u16() == 101 {
                    vulnerabilities.push(self.create_vulnerability(
                        "WebSocket Connection Without Authentication",
                        ws_url,
                        Severity::High,
                        Confidence::High,
                        "WebSocket accepts connections without authentication tokens or cookies",
                        format!(
                            "Connected to WebSocket without credentials. Status: {}",
                            response.status()
                        ),
                        7.5,
                    ));
                }

                stream
            }
            Ok(Err(e)) => {
                debug!("[WebSocket CSRF] Connection failed: {}", e);
                return Ok(vulnerabilities);
            }
            Err(_) => {
                debug!("[WebSocket CSRF] Connection timeout");
                return Ok(vulnerabilities);
            }
        };

        let (mut write, mut read) = ws_stream.split();

        // Test privileged commands that should require authentication
        let test_payloads = vec![
            format!(r#"{{"action":"deleteUser","userId":"1","marker":"ws_{marker}"}}"#),
            format!(
                r#"{{"action":"updateProfile","email":"attacker@evil.com","marker":"ws_{marker}"}}"#
            ),
            format!(r#"{{"action":"transferFunds","amount":1000,"marker":"ws_{marker}"}}"#),
            format!(r#"{{"command":"admin","operation":"delete","marker":"ws_{marker}"}}"#),
            format!(r#"{{"type":"privileged","action":"execute","marker":"ws_{marker}"}}"#),
        ];

        for payload in &test_payloads {
            // Send privileged command
            if let Err(e) = write.send(Message::Text(payload.clone().into())).await {
                debug!("[WebSocket CSRF] Failed to send: {}", e);
                break;
            }

            // Try to read response
            if let Ok(Some(msg_result)) = timeout(Duration::from_millis(500), read.next()).await {
                if let Ok(msg) = msg_result {
                    let response_text = msg.to_text().unwrap_or("");

                    // Check if command was executed (look for success indicators)
                    if (response_text.contains("success")
                        || response_text.contains("executed")
                        || response_text.contains("completed")
                        || response_text.contains(&marker))
                        && !response_text.contains("unauthorized")
                        && !response_text.contains("forbidden")
                        && !response_text.contains("error")
                    {
                        vulnerabilities.push(self.create_vulnerability(
                            "WebSocket CSRF - Unauthenticated Command Execution",
                            ws_url,
                            Severity::Critical,
                            Confidence::High,
                            "WebSocket accepts privileged commands without authentication",
                            format!("Payload: {}\nResponse: {}", payload, response_text),
                            9.1,
                        ));
                        break; // Found vulnerability, no need to test more
                    }
                }
            }
        }

        // Close connection
        let _ = write.send(Message::Close(None)).await;

        Ok(vulnerabilities)
    }

    /// Test WebSocket Message Injection
    async fn test_message_injection(&self, ws_url: &str) -> Result<Vec<Vulnerability>> {
        let mut vulnerabilities = Vec::new();
        let marker = uuid::Uuid::new_v4().to_string();

        info!("[WebSocket Injection] Testing: {}", ws_url);

        let connect_result = timeout(Duration::from_secs(5), connect_async(ws_url)).await;

        let (ws_stream, _) = match connect_result {
            Ok(Ok((stream, response))) => (stream, response),
            _ => return Ok(vulnerabilities),
        };

        let (mut write, mut read) = ws_stream.split();

        // Test XSS in WebSocket messages
        let xss_payloads = vec![
            format!(r#"{{"message":"<script>alert('ws_{marker}')</script>"}}"#),
            format!(r#"{{"text":"<img src=x onerror=alert('ws_{marker}')>"}}"#),
            format!(r#"{{"content":"<svg onload=alert('ws_{marker}')>"}}"#),
            format!(r#"{{"data":"javascript:alert('ws_{marker}')"}}"#),
        ];

        for payload in &xss_payloads {
            if let Ok(_) = write.send(Message::Text(payload.clone().into())).await {
                if let Ok(Some(Ok(msg))) = timeout(Duration::from_millis(500), read.next()).await {
                    let response = msg.to_text().unwrap_or("");

                    // Check if payload is reflected without sanitization
                    if response.contains("<script>")
                        || response.contains("onerror=")
                        || response.contains("onload=")
                        || response.contains("javascript:")
                    {
                        vulnerabilities.push(self.create_vulnerability(
                            "WebSocket XSS - Unsanitized Message Reflection",
                            ws_url,
                            Severity::High,
                            Confidence::High,
                            "WebSocket reflects messages without sanitization - XSS possible",
                            format!("Payload: {}\nReflected: {}", payload, response),
                            7.8,
                        ));
                        break;
                    }
                }
            }
        }

        // Test SQL Injection
        let sql_payloads = vec![
            format!(r#"{{"search":"' OR 1=1-- ws_{marker}"}}"#),
            format!(r#"{{"query":"' UNION SELECT NULL-- ws_{marker}"}}"#),
            format!(r#"{{"filter":"1'; DROP TABLE users-- ws_{marker}"}}"#),
        ];

        for payload in &sql_payloads {
            if let Ok(_) = write.send(Message::Text(payload.clone().into())).await {
                if let Ok(Some(Ok(msg))) = timeout(Duration::from_millis(500), read.next()).await {
                    let response = msg.to_text().unwrap_or("");

                    // Check for SQL error messages
                    if response.contains("SQL")
                        || response.contains("syntax error")
                        || response.contains("mysql")
                        || response.contains("postgresql")
                        || response.contains("sqlite")
                        || response.contains("ORA-")
                        || response.contains("syntax")
                    {
                        vulnerabilities.push(self.create_vulnerability(
                            "WebSocket SQL Injection",
                            ws_url,
                            Severity::Critical,
                            Confidence::High,
                            "WebSocket message processing vulnerable to SQL injection",
                            format!("Payload: {}\nError: {}", payload, response),
                            9.8,
                        ));
                        break;
                    }
                }
            }
        }

        // Test Command Injection
        let cmd_payloads = vec![
            format!(r#"{{"ping":"127.0.0.1; whoami ws_{marker}"}}"#),
            format!(r#"{{"host":"localhost && id ws_{marker}"}}"#),
            format!(r#"{{"cmd":"test | ls ws_{marker}"}}"#),
        ];

        for payload in &cmd_payloads {
            if let Ok(_) = write.send(Message::Text(payload.clone().into())).await {
                if let Ok(Some(Ok(msg))) = timeout(Duration::from_millis(500), read.next()).await {
                    let response = msg.to_text().unwrap_or("");

                    // Check for command execution indicators
                    if response.contains("uid=")
                        || response.contains("gid=")
                        || response.contains("root")
                        || response.contains("/bin/")
                        || response.contains("total ")
                    {
                        vulnerabilities.push(self.create_vulnerability(
                            "WebSocket Command Injection",
                            ws_url,
                            Severity::Critical,
                            Confidence::High,
                            "WebSocket message processing vulnerable to command injection",
                            format!("Payload: {}\nOutput: {}", payload, response),
                            9.8,
                        ));
                        break;
                    }
                }
            }
        }

        let _ = write.send(Message::Close(None)).await;
        Ok(vulnerabilities)
    }

    /// Test WebSocket Origin Bypass (Cross-Site WebSocket Hijacking)
    async fn test_origin_bypass(&self, ws_url: &str) -> Result<Vec<Vulnerability>> {
        let mut vulnerabilities = Vec::new();

        info!("[WebSocket Origin] Testing: {}", ws_url);

        // Test with malicious origins - comprehensive list
        let evil_origins = vec![
            "https://evil.com",
            "http://attacker.evil",
            "https://attacker.com",
            "http://malicious.net",
            "null",
            "",
            "file://",
            "data://",
            "javascript://evil.com",
        ];

        for origin in &evil_origins {
            // Create custom request with Origin header
            let request = match url::Url::parse(ws_url) {
                Ok(parsed_url) => {
                    let host = parsed_url.host_str().unwrap_or("localhost");
                    let _path = parsed_url.path();

                    let mut request = tungstenite::handshake::client::Request::builder()
                        .uri(ws_url)
                        .header("Host", host)
                        .header("Connection", "Upgrade")
                        .header("Upgrade", "websocket")
                        .header("Sec-WebSocket-Version", "13")
                        .header(
                            "Sec-WebSocket-Key",
                            tungstenite::handshake::client::generate_key(),
                        );

                    if !origin.is_empty() {
                        request = request.header("Origin", *origin);
                    }

                    match request.body(()) {
                        Ok(req) => req,
                        Err(_) => continue,
                    }
                }
                Err(_) => continue,
            };

            // Try to connect with malicious origin
            let connect_result = timeout(Duration::from_secs(5), connect_async(request)).await;

            if let Ok(Ok((ws_stream, response))) = connect_result {
                // Connection accepted with malicious origin
                let origin_display = if origin.is_empty() {
                    "no origin"
                } else {
                    origin
                };

                vulnerabilities.push(self.create_vulnerability(
                    "WebSocket Origin Validation Bypass",
                    ws_url,
                    Severity::High,
                    Confidence::High,
                    &format!(
                        "WebSocket accepts connections from malicious origin: {}",
                        origin_display
                    ),
                    format!(
                        "Connected with Origin: {}\nStatus: {}",
                        origin_display,
                        response.status()
                    ),
                    7.5,
                ));

                // Close the connection
                let (mut write, _) = ws_stream.split();
                let _ = write.send(Message::Close(None)).await;

                break; // Found vulnerability
            }
        }

        Ok(vulnerabilities)
    }

    /// Test WebSocket Hijacking
    async fn test_websocket_hijacking(&self, ws_url: &str) -> Result<Vec<Vulnerability>> {
        let mut vulnerabilities = Vec::new();

        info!("[WebSocket Hijacking] Testing: {}", ws_url);

        // Check if WebSocket URL contains tokens (security issue)
        let url_lower = ws_url.to_lowercase();
        let sensitive_params = vec!["token=", "session=", "key=", "auth=", "api_key="];

        for param in &sensitive_params {
            if url_lower.contains(param) {
                vulnerabilities.push(self.create_vulnerability(
                    "WebSocket Session Token in URL",
                    ws_url,
                    Severity::High,
                    Confidence::High,
                    "WebSocket URL contains authentication token - vulnerable to hijacking via logs",
                    format!("Sensitive parameter '{}' found in URL", param),
                    7.5,
                ));
            }
        }

        // Test if WebSocket uses predictable connection IDs
        let connect_result = timeout(Duration::from_secs(5), connect_async(ws_url)).await;

        if let Ok(Ok((ws_stream, _))) = connect_result {
            let (mut write, mut read) = ws_stream.split();

            // Send a test message to see if we get a connection ID
            let test_msg = r#"{"type":"test","action":"getId"}"#;
            if let Ok(_) = write.send(Message::Text(test_msg.to_string().into())).await {
                if let Ok(Some(Ok(msg))) = timeout(Duration::from_millis(500), read.next()).await {
                    let response = msg.to_text().unwrap_or("");

                    // Check for sequential or predictable IDs
                    if response.contains("\"id\":")
                        || response.contains("\"connectionId\":")
                        || response.contains("\"sessId\":")
                    {
                        // Try to extract the ID pattern
                        if response.contains("\"id\":1")
                            || response.contains("\"id\":\"1\"")
                            || response.contains("\"id\":12")
                            || response.contains("\"id\":123")
                        {
                            vulnerabilities.push(self.create_vulnerability(
                                "WebSocket Predictable Connection IDs",
                                ws_url,
                                Severity::Medium,
                                Confidence::Medium,
                                "WebSocket uses predictable connection IDs - vulnerable to session hijacking",
                                format!("Response contains predictable ID: {}", response),
                                6.5,
                            ));
                        }
                    }
                }
            }

            let _ = write.send(Message::Close(None)).await;
        }

        Ok(vulnerabilities)
    }

    /// Test WebSocket Denial of Service (limited to prevent production impact)
    async fn test_websocket_dos(&self, ws_url: &str) -> Result<Vec<Vulnerability>> {
        let mut vulnerabilities = Vec::new();

        info!("[WebSocket DoS] Testing: {}", ws_url);

        let connect_result = timeout(Duration::from_secs(5), connect_async(ws_url)).await;

        let (ws_stream, _) = match connect_result {
            Ok(Ok((stream, response))) => (stream, response),
            _ => return Ok(vulnerabilities),
        };

        let (mut write, mut read) = ws_stream.split();

        // Test 1: Large message (1MB, not 10MB to be safe)
        let large_payload = "A".repeat(1024 * 1024); // 1MB
        let large_msg = format!(r#"{{"data":"{}"}}"#, large_payload);

        let start = std::time::Instant::now();
        if let Ok(_) = write.send(Message::Text(large_msg.clone().into())).await {
            if let Ok(Some(Ok(msg))) = timeout(Duration::from_secs(3), read.next()).await {
                let duration = start.elapsed();
                let response = msg.to_text().unwrap_or("");

                // If server accepts and processes large message without error
                if !response.contains("error")
                    && !response.contains("too large")
                    && !response.contains("limit exceeded")
                {
                    vulnerabilities.push(self.create_vulnerability(
                        "WebSocket Large Message DoS",
                        ws_url,
                        Severity::Medium,
                        Confidence::High,
                        "WebSocket accepts extremely large messages without size limits",
                        format!("Sent 1MB message, processed in {:?}", duration),
                        5.3,
                    ));
                }
            }
        }

        // Test 2: Rapid small messages (limit to 50 to be safe, not 1000)
        let mut rapid_success = 0;
        let start = std::time::Instant::now();

        for i in 0..50 {
            let msg = format!(r#"{{"seq":{}}}"#, i);
            if write.send(Message::Text(msg.into())).await.is_ok() {
                rapid_success += 1;
            } else {
                break;
            }
        }

        let duration = start.elapsed();

        if rapid_success >= 45 && duration.as_millis() < 1000 {
            vulnerabilities.push(self.create_vulnerability(
                "WebSocket Rate Limiting Missing",
                ws_url,
                Severity::Medium,
                Confidence::High,
                "WebSocket accepts rapid messages without rate limiting - DoS possible",
                format!(
                    "Sent {} messages in {:?} without rejection",
                    rapid_success, duration
                ),
                6.5,
            ));
        }

        // Test 3: Malformed frames
        let malformed_payloads = vec![
            "{invalid json",
            "not json at all",
            "[]]]",
            r#"{"unclosed": "#,
        ];

        for payload in &malformed_payloads {
            if let Ok(_) = write.send(Message::Text(payload.to_string().into())).await {
                if let Ok(Some(Ok(msg))) = timeout(Duration::from_millis(500), read.next()).await {
                    let response = msg.to_text().unwrap_or("");

                    // Check for crash indicators or verbose errors
                    if response.contains("panic")
                        || response.contains("exception")
                        || response.contains("stack trace")
                        || response.contains("internal error")
                    {
                        vulnerabilities.push(self.create_vulnerability(
                            "WebSocket Malformed Message Crash",
                            ws_url,
                            Severity::Medium,
                            Confidence::High,
                            "WebSocket crashes or leaks internal errors on malformed messages",
                            format!("Payload: {}\nError: {}", payload, response),
                            5.0,
                        ));
                        break;
                    }
                }
            }
        }

        let _ = write.send(Message::Close(None)).await;
        Ok(vulnerabilities)
    }

    /// Test WebSocket Protocol Confusion
    async fn test_protocol_confusion(&self, ws_url: &str) -> Result<Vec<Vulnerability>> {
        let mut vulnerabilities = Vec::new();

        info!("[WebSocket Protocol] Testing: {}", ws_url);

        let connect_result = timeout(Duration::from_secs(5), connect_async(ws_url)).await;

        let (ws_stream, _) = match connect_result {
            Ok(Ok((stream, response))) => (stream, response),
            _ => return Ok(vulnerabilities),
        };

        let (mut write, mut read) = ws_stream.split();

        // Test 1: Send HTTP-like request over WebSocket
        let http_payloads = vec![
            "GET / HTTP/1.1\r\nHost: evil.com\r\n\r\n",
            "POST /api HTTP/1.1\r\nContent-Length: 0\r\n\r\n",
        ];

        for payload in &http_payloads {
            if let Ok(_) = write.send(Message::Text(payload.to_string().into())).await {
                if let Ok(Some(Ok(msg))) = timeout(Duration::from_millis(500), read.next()).await {
                    let response = msg.to_text().unwrap_or("");

                    // Check if HTTP request was processed
                    if response.contains("HTTP/")
                        || response.contains("200 OK")
                        || response.contains("Content-Type")
                    {
                        vulnerabilities.push(self.create_vulnerability(
                            "WebSocket Protocol Confusion - HTTP Processing",
                            ws_url,
                            Severity::Medium,
                            Confidence::High,
                            "WebSocket processes HTTP requests - protocol confusion possible",
                            format!("Sent: {}\nResponse: {}", payload, response),
                            6.0,
                        ));
                        break;
                    }
                }
            }
        }

        // Test 2: Non-JSON data when JSON expected
        let non_json_payloads = vec![
            "plain text message",
            "<xml><tag>value</tag></xml>",
            "random binary data \x00\x01\x02",
        ];

        for payload in &non_json_payloads {
            if let Ok(_) = write.send(Message::Text(payload.to_string().into())).await {
                if let Ok(Some(Ok(msg))) = timeout(Duration::from_millis(500), read.next()).await {
                    let response = msg.to_text().unwrap_or("");

                    // Check for verbose error disclosure
                    if response.contains("JSON.parse")
                        || response.contains("unexpected token")
                        || response.contains("SyntaxError")
                        || response.contains("at position")
                        || response.contains("line ")
                        || response.contains("column ")
                    {
                        vulnerabilities.push(self.create_vulnerability(
                            "WebSocket Verbose Error Disclosure",
                            ws_url,
                            Severity::Low,
                            Confidence::High,
                            "WebSocket returns verbose errors that may leak implementation details",
                            format!("Payload: {}\nError: {}", payload, response),
                            4.3,
                        ));
                        break;
                    }
                }
            }
        }

        // Test 3: Binary frames when text expected
        if let Ok(_) = write
            .send(Message::Binary(vec![0x00, 0x01, 0x02, 0xFF].into()))
            .await
        {
            if let Ok(Some(Ok(msg))) = timeout(Duration::from_millis(500), read.next()).await {
                let response_str = format!("{:?}", msg);

                // Check if binary data is processed without validation
                if !response_str.contains("error") && !response_str.contains("invalid") {
                    vulnerabilities.push(self.create_vulnerability(
                        "WebSocket Binary Frame Processing",
                        ws_url,
                        Severity::Low,
                        Confidence::Medium,
                        "WebSocket processes binary frames without proper validation",
                        format!("Binary frame accepted: {:?}", msg),
                        3.7,
                    ));
                }
            }
        }

        let _ = write.send(Message::Close(None)).await;
        Ok(vulnerabilities)
    }

    /// Create vulnerability record
    fn create_vulnerability(
        &self,
        title: &str,
        url: &str,
        severity: Severity,
        confidence: Confidence,
        description: &str,
        evidence: String,
        cvss: f32,
    ) -> Vulnerability {
        Vulnerability {
            id: format!("websocket_{}", uuid::Uuid::new_v4().to_string()),
            vuln_type: format!("WebSocket Vulnerability - {}", title),
            severity,
            confidence,
            category: "WebSocket Security".to_string(),
            url: url.to_string(),
            parameter: None,
            payload: String::new(),
            description: description.to_string(),
            evidence: Some(evidence),
            cwe: "CWE-346".to_string(), // Origin Validation Error
            cvss,
            verified: true,
            false_positive: false,
            remediation: r#"IMMEDIATE ACTION REQUIRED:

1. **Implement Strict Origin Validation**
   ```javascript
   // Node.js WebSocket server
   const WebSocket = require('ws');
   const wss = new WebSocket.Server({ noServer: true });

   const ALLOWED_ORIGINS = [
     'https://app.example.com',
     'https://www.example.com'
   ];

   server.on('upgrade', (request, socket, head) => {
     const origin = request.headers.origin;

     // Validate origin
     if (!ALLOWED_ORIGINS.includes(origin)) {
       socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
       socket.destroy();
       return;
     }

     wss.handleUpgrade(request, socket, head, (ws) => {
       wss.emit('connection', ws, request);
     });
   });
   ```

2. **Implement Authentication & Authorization**
   ```javascript
   // Authenticate during handshake
   server.on('upgrade', async (request, socket, head) => {
     const token = new URLSearchParams(request.url.split('?')[1]).get('token');

     // Validate token
     const user = await validateToken(token);
     if (!user) {
       socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
       socket.destroy();
       return;
     }

     wss.handleUpgrade(request, socket, head, (ws) => {
       ws.user = user;  // Attach user to connection
       wss.emit('connection', ws, request);
     });
   });

   // Authorize individual messages
   ws.on('message', (message) => {
     const data = JSON.parse(message);

     if (!canPerformAction(ws.user, data.action)) {
       ws.send(JSON.stringify({ error: 'Unauthorized' }));
       return;
     }

     // Process message
   });
   ```

3. **Use Secure WebSocket (wss://)**
   ```javascript
   // Use TLS for WebSocket connections
   const https = require('https');
   const fs = require('fs');

   const server = https.createServer({
     cert: fs.readFileSync('cert.pem'),
     key: fs.readFileSync('key.pem')
   });

   const wss = new WebSocket.Server({ server });

   // Client connects with wss://
   const ws = new WebSocket('wss://example.com/socket');
   ```

4. **Validate and Sanitize All Messages**
   ```javascript
   // Input validation
   const Joi = require('joi');

   const messageSchema = Joi.object({
     type: Joi.string().valid('chat', 'notification').required(),
     content: Joi.string().max(1000).required(),
     metadata: Joi.object()
   });

   ws.on('message', (message) => {
     let data;
     try {
       data = JSON.parse(message);
     } catch (e) {
       ws.send(JSON.stringify({ error: 'Invalid JSON' }));
       return;
     }

     // Validate schema
     const { error } = messageSchema.validate(data);
     if (error) {
       ws.send(JSON.stringify({ error: error.message }));
       return;
     }

     // Sanitize content
     data.content = sanitizeHtml(data.content);

     // Process validated message
     processMessage(ws, data);
   });
   ```

5. **Implement Rate Limiting**
   ```javascript
   const rateLimit = require('ws-rate-limit');

   // Limit to 100 messages per minute per connection
   const limiter = rateLimit('100 per minute');

   ws.on('message', async (message) => {
     try {
       await limiter.check(ws.id);
     } catch (error) {
       ws.send(JSON.stringify({ error: 'Rate limit exceeded' }));
       return;
     }

     // Process message
   });
   ```

6. **Prevent Token/Credential Leakage**
   ```javascript
   // WRONG - credentials in URL
   const ws = new WebSocket('wss://example.com?token=secret123');

   // CORRECT - use headers or post-connection auth
   const ws = new WebSocket('wss://example.com');
   ws.onopen = () => {
     ws.send(JSON.stringify({
       type: 'auth',
       token: getTokenFromSecureStorage()
     }));
   };
   ```

7. **Implement Connection Limits**
   ```javascript
   const connectedClients = new Map();
   const MAX_CONNECTIONS_PER_USER = 5;

   wss.on('connection', (ws, request) => {
     const userId = ws.user.id;

     // Check connection limit
     const userConnections = connectedClients.get(userId) || [];
     if (userConnections.length >= MAX_CONNECTIONS_PER_USER) {
       ws.close(1008, 'Too many connections');
       return;
     }

     // Track connection
     userConnections.push(ws);
     connectedClients.set(userId, userConnections);

     ws.on('close', () => {
       // Remove from tracking
       const connections = connectedClients.get(userId);
       const index = connections.indexOf(ws);
       if (index > -1) {
         connections.splice(index, 1);
       }
     });
   });
   ```

8. **Use CSRF Tokens for WebSocket Handshake**
   ```javascript
   // Generate CSRF token on page load
   const csrfToken = generateCSRFToken();

   // Include in WebSocket connection
   const ws = new WebSocket(`wss://example.com?csrf=${csrfToken}`);

   // Server validates
   server.on('upgrade', (request, socket, head) => {
     const csrf = new URLSearchParams(request.url.split('?')[1]).get('csrf');

     if (!validateCSRF(csrf, request.headers.cookie)) {
       socket.destroy();
       return;
     }

     // Continue with upgrade
   });
   ```

9. **Implement Heartbeat/Ping-Pong**
   ```javascript
   // Detect and close dead connections
   function heartbeat() {
     this.isAlive = true;
   }

   wss.on('connection', (ws) => {
     ws.isAlive = true;
     ws.on('pong', heartbeat);
   });

   const interval = setInterval(() => {
     wss.clients.forEach((ws) => {
       if (ws.isAlive === false) {
         return ws.terminate();
       }
       ws.isAlive = false;
       ws.ping();
     });
   }, 30000);
   ```

10. **Security Checklist**
    - [ ] Origin header validated against whitelist
    - [ ] Authentication required for connections
    - [ ] Authorization checked per message
    - [ ] All connections use wss:// (TLS)
    - [ ] No credentials in WebSocket URL
    - [ ] All messages validated and sanitized
    - [ ] Rate limiting implemented
    - [ ] Connection limits per user
    - [ ] CSRF protection for handshake
    - [ ] Dead connection cleanup (heartbeat)
    - [ ] Error messages don't leak info
    - [ ] Comprehensive logging enabled

11. **Testing & Monitoring**
    - Test with tools like wsrepl, wsdump
    - Monitor for unauthorized connection attempts
    - Log all authentication failures
    - Alert on rate limit violations
    - Track connection patterns

References:
- OWASP WebSocket Security: https://owasp.org/www-community/vulnerabilities/WebSocket_security
- WebSocket RFC 6455: https://datatracker.ietf.org/doc/html/rfc6455
- PortSwigger WebSocket Security: https://portswigger.net/web-security/websockets
"#
            .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 {
            Self
        }

        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 std::collections::HashMap;

    #[test]
    fn test_websocket_detection() {
        let scanner = WebSocketScanner::new(Arc::new(HttpClient::new(5, 2).unwrap()));

        let response = crate::http_client::HttpResponse {
            status_code: 200,
            body: r#"const ws = new WebSocket('wss://example.com/socket');"#.to_string(),
            headers: HashMap::new(),
            duration_ms: 100,
        };

        assert!(scanner.detect_websocket_endpoint(&response, "https://example.com"));
    }

    #[test]
    fn test_missing_origin_validation() {
        let scanner = WebSocketScanner::new(Arc::new(HttpClient::new(5, 2).unwrap()));

        let response = crate::http_client::HttpResponse {
            status_code: 200,
            body: r#"const wss = new WebSocket.Server({ server });"#.to_string(),
            headers: HashMap::new(),
            duration_ms: 100,
        };

        let mut vulns = Vec::new();
        scanner.check_origin_validation(&response, "https://example.com", &mut vulns);

        assert_eq!(vulns.len(), 1, "Should detect missing origin validation");
        assert_eq!(vulns[0].severity, Severity::High);
    }

    #[test]
    fn test_insecure_websocket_protocol() {
        let scanner = WebSocketScanner::new(Arc::new(HttpClient::new(5, 2).unwrap()));

        let mut vulns = Vec::new();
        scanner.check_sensitive_data_in_url("ws://example.com/socket", &mut vulns);

        assert!(vulns.len() > 0, "Should detect insecure ws:// protocol");
        assert_eq!(vulns[0].severity, Severity::Medium);
    }

    #[test]
    fn test_sensitive_data_in_url() {
        let scanner = WebSocketScanner::new(Arc::new(HttpClient::new(5, 2).unwrap()));

        let mut vulns = Vec::new();
        scanner.check_sensitive_data_in_url("wss://example.com/socket?token=abc123", &mut vulns);

        assert_eq!(vulns.len(), 1, "Should detect token in URL");
        assert_eq!(vulns[0].severity, Severity::Medium);
    }

    #[test]
    fn test_no_false_positive() {
        let scanner = WebSocketScanner::new(Arc::new(HttpClient::new(5, 2).unwrap()));

        let response = crate::http_client::HttpResponse {
            status_code: 200,
            body: "<html><body>Normal page</body></html>".to_string(),
            headers: HashMap::new(),
            duration_ms: 100,
        };

        assert!(!scanner.detect_websocket_endpoint(&response, "https://example.com"));
    }

    #[test]
    fn test_websocket_url_detection() {
        let scanner = WebSocketScanner::new(Arc::new(HttpClient::new(5, 2).unwrap()));

        let response = crate::http_client::HttpResponse {
            status_code: 200,
            body: String::new(),
            headers: HashMap::new(),
            duration_ms: 100,
        };

        // Should detect ws:// URL
        assert!(scanner.detect_websocket_endpoint(&response, "ws://example.com/socket"));

        // Should detect wss:// URL
        assert!(scanner.detect_websocket_endpoint(&response, "wss://example.com/socket"));
    }

    #[test]
    fn test_convert_to_ws_url() {
        let scanner = WebSocketScanner::new(Arc::new(HttpClient::new(5, 2).unwrap()));

        // HTTPS should convert to WSS
        let ws_url = scanner.convert_to_ws_url("https://example.com/api/ws");
        assert!(ws_url.is_some());
        assert!(ws_url.unwrap().starts_with("wss://"));

        // HTTP should convert to WS
        let ws_url = scanner.convert_to_ws_url("http://example.com/socket");
        assert!(ws_url.is_some());
        assert!(ws_url.unwrap().starts_with("ws://"));

        // Already WebSocket URL should be preserved
        let ws_url = scanner.convert_to_ws_url("wss://example.com/chat");
        assert_eq!(ws_url, Some("wss://example.com/chat".to_string()));
    }

    #[test]
    fn test_sockjs_detection() {
        let scanner = WebSocketScanner::new(Arc::new(HttpClient::new(5, 2).unwrap()));

        let response = crate::http_client::HttpResponse {
            status_code: 200,
            body: r#"const socket = new SockJS('/sockjs');"#.to_string(),
            headers: HashMap::new(),
            duration_ms: 100,
        };

        assert!(scanner.detect_websocket_endpoint(&response, "https://example.com"));
    }

    #[test]
    fn test_socket_io_detection() {
        let scanner = WebSocketScanner::new(Arc::new(HttpClient::new(5, 2).unwrap()));

        let response = crate::http_client::HttpResponse {
            status_code: 200,
            body: r#"const socket = io.connect('http://localhost:3000');"#.to_string(),
            headers: HashMap::new(),
            duration_ms: 100,
        };

        assert!(scanner.detect_websocket_endpoint(&response, "http://localhost:3000"));
    }

    #[test]
    fn test_upgrade_header_detection() {
        let scanner = WebSocketScanner::new(Arc::new(HttpClient::new(5, 2).unwrap()));

        let mut headers = HashMap::new();
        headers.insert("upgrade".to_string(), "websocket".to_string());

        let response = crate::http_client::HttpResponse {
            status_code: 101,
            body: String::new(),
            headers,
            duration_ms: 100,
        };

        assert!(scanner.detect_websocket_endpoint(&response, "https://example.com"));
    }
}