synapse-waf 0.9.1

High-performance WAF and reverse proxy with embedded intelligence — built on Cloudflare Pingora
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
//! Shared Fingerprint Detection Strategy
//!
//! This detector identifies campaigns where multiple IP addresses share the same
//! JA4 TLS fingerprint or combined fingerprint hash (JA4+JA4H). Shared fingerprints
//! are a strong indicator of coordinated attack tooling.
//!
//! # Detection Logic
//!
//! When 3+ IPs share an identical fingerprint, this strongly suggests:
//! - Same attack tooling (bot framework, scanner, etc.)
//! - Coordinated campaign from distributed infrastructure
//! - Potential botnet or proxy rotation
//!
//! # Example
//!
//! ```rust
//! use synapse_pingora::correlation::FingerprintIndex;
//! use synapse_pingora::correlation::detectors::{SharedFingerprintDetector, Detector};
//!
//! let index = FingerprintIndex::new();
//!
//! // Simulate 3 IPs sharing same fingerprint
//! index.update_entity("192.168.1.1", Some("t13d1516h2_abc123"), None);
//! index.update_entity("192.168.1.2", Some("t13d1516h2_abc123"), None);
//! index.update_entity("192.168.1.3", Some("t13d1516h2_abc123"), None);
//!
//! let detector = SharedFingerprintDetector::new(3);
//! let updates = detector.analyze(&index).unwrap();
//!
//! // Should detect a campaign with these 3 IPs
//! assert!(!updates.is_empty());
//! ```

use parking_lot::RwLock;
use std::collections::HashSet;
use std::net::IpAddr;

use crate::correlation::{
    Campaign, CampaignUpdate, CorrelationReason, CorrelationType, FingerprintGroup,
    FingerprintIndex, FingerprintType,
};

use super::{Detector, DetectorResult};

/// Configuration for shared fingerprint detection
#[derive(Debug, Clone)]
pub struct SharedFingerprintConfig {
    /// Minimum number of IPs required to form a campaign
    pub threshold: usize,
    /// Base confidence score for detections (0.0-1.0)
    pub base_confidence: f64,
    /// Confidence bonus for combined fingerprints (JA4+JA4H)
    pub combined_type_bonus: f64,
    /// Confidence bonus per IP above threshold
    pub size_bonus_per_ip: f64,
    /// Maximum size bonus
    pub max_size_bonus: f64,
    /// Scan interval in milliseconds
    pub scan_interval_ms: u64,
}

impl Default for SharedFingerprintConfig {
    fn default() -> Self {
        Self {
            threshold: 3,
            base_confidence: 0.85,
            combined_type_bonus: 0.1,
            size_bonus_per_ip: 0.02,
            max_size_bonus: 0.05,
            scan_interval_ms: 5000,
        }
    }
}

/// Detector for campaigns based on shared fingerprints.
///
/// Identifies groups of IPs that share the same JA4 or combined fingerprint,
/// which indicates coordinated attack tooling.
pub struct SharedFingerprintDetector {
    /// Configuration for detection
    config: SharedFingerprintConfig,

    /// Fingerprints that have already been processed into campaigns
    /// Prevents duplicate campaign creation for the same fingerprint group
    processed_fingerprints: RwLock<HashSet<String>>,
}

impl SharedFingerprintDetector {
    /// Create a new detector with the specified threshold.
    ///
    /// # Arguments
    /// * `threshold` - Minimum IPs sharing a fingerprint to trigger campaign creation
    ///
    /// # Panics
    /// Panics if threshold is less than 2 (correlation requires at least 2 IPs).
    pub fn new(threshold: usize) -> Self {
        assert!(
            threshold >= 2,
            "Threshold must be at least 2 for correlation"
        );
        Self {
            config: SharedFingerprintConfig {
                threshold,
                ..Default::default()
            },
            processed_fingerprints: RwLock::new(HashSet::new()),
        }
    }

    /// Create a detector with custom configuration.
    ///
    /// # Arguments
    /// * `threshold` - Minimum IPs sharing a fingerprint
    /// * `base_confidence` - Confidence score for detections (0.0-1.0)
    /// * `scan_interval_ms` - Milliseconds between full scans
    pub fn with_config(threshold: usize, base_confidence: f64, scan_interval_ms: u64) -> Self {
        assert!(
            threshold >= 2,
            "Threshold must be at least 2 for correlation"
        );
        Self {
            config: SharedFingerprintConfig {
                threshold,
                base_confidence: base_confidence.clamp(0.0, 1.0),
                scan_interval_ms,
                ..Default::default()
            },
            processed_fingerprints: RwLock::new(HashSet::new()),
        }
    }

    /// Create a detector with full configuration.
    pub fn from_config(config: SharedFingerprintConfig) -> Self {
        assert!(
            config.threshold >= 2,
            "Threshold must be at least 2 for correlation"
        );
        Self {
            config,
            processed_fingerprints: RwLock::new(HashSet::new()),
        }
    }

    /// Check if a fingerprint has already been processed into a campaign.
    fn is_processed(&self, fingerprint: &str) -> bool {
        self.processed_fingerprints.read().contains(fingerprint)
    }

    /// Mark a fingerprint as processed.
    fn mark_processed(&self, fingerprint: &str) {
        self.processed_fingerprints
            .write()
            .insert(fingerprint.to_string());
    }

    /// Clear processed fingerprints (e.g., when resetting detector state).
    pub fn clear_processed(&self) {
        self.processed_fingerprints.write().clear();
    }

    /// Get the number of processed fingerprints.
    pub fn processed_count(&self) -> usize {
        self.processed_fingerprints.read().len()
    }

    /// Calculate confidence score based on fingerprint type and group size.
    ///
    /// Combined fingerprints (JA4+JA4H) get higher confidence than JA4 alone.
    /// Larger groups also increase confidence.
    fn calculate_confidence(&self, fp_type: FingerprintType, group_size: usize) -> f64 {
        let type_bonus = match fp_type {
            FingerprintType::Ja4 => 0.0,
            FingerprintType::Combined => self.config.combined_type_bonus,
        };

        // Size bonus: configurable per IP above threshold, capped at configurable max
        let size_bonus = ((group_size.saturating_sub(self.config.threshold)) as f64
            * self.config.size_bonus_per_ip)
            .min(self.config.max_size_bonus);

        (self.config.base_confidence + type_bonus + size_bonus).min(1.0)
    }

    /// Create a campaign update for a new fingerprint group.
    fn create_campaign_update(&self, group: &FingerprintGroup) -> CampaignUpdate {
        let confidence = self.calculate_confidence(group.fingerprint_type, group.size);

        let description = format!(
            "{} fingerprint '{}' shared by {} IPs",
            group.fingerprint_type, group.fingerprint, group.size
        );

        let reason = CorrelationReason::new(
            CorrelationType::HttpFingerprint,
            confidence,
            description,
            group.ips.clone(),
        );

        // Create a new campaign
        let campaign = Campaign::new(Campaign::generate_id(), group.ips.clone(), confidence);

        // Build update with correlation reason
        CampaignUpdate {
            campaign_id: Some(campaign.id.clone()),
            status: Some(campaign.status),
            confidence: Some(confidence),
            attack_types: None,
            add_member_ips: Some(group.ips.clone()),
            add_correlation_reason: Some(reason),
            increment_requests: None,
            increment_blocked: None,
            increment_rules: None,
            risk_score: None,
        }
    }

    /// Analyze a fingerprint group and return campaign update if new.
    fn process_group(&self, group: &FingerprintGroup) -> Option<CampaignUpdate> {
        // Skip if already processed
        if self.is_processed(&group.fingerprint) {
            return None;
        }

        // Skip if below threshold
        if group.size < self.config.threshold {
            return None;
        }

        // Mark as processed and create update
        self.mark_processed(&group.fingerprint);
        Some(self.create_campaign_update(group))
    }
}

impl Detector for SharedFingerprintDetector {
    fn name(&self) -> &'static str {
        "shared_fingerprint"
    }

    fn analyze(&self, index: &FingerprintIndex) -> DetectorResult<Vec<CampaignUpdate>> {
        // Get all groups above threshold
        let groups = index.get_groups_above_threshold(self.config.threshold);

        if groups.is_empty() {
            return Ok(Vec::new());
        }

        // Process each group
        let updates: Vec<CampaignUpdate> = groups
            .iter()
            .filter_map(|group| self.process_group(group))
            .collect();

        Ok(updates)
    }

    fn should_trigger(&self, ip: &IpAddr, index: &FingerprintIndex) -> bool {
        let ip_str = ip.to_string();

        // Get fingerprints for this IP
        let fingerprints = match index.get_ip_fingerprints(&ip_str) {
            Some(fps) => fps,
            None => return false,
        };

        // Check if JA4 fingerprint group is at threshold
        if let Some(ref ja4) = fingerprints.0 {
            if !self.is_processed(ja4) {
                let count = index.count_ips_by_ja4(ja4);
                if count >= self.config.threshold {
                    return true;
                }
            }
        }

        // Check if combined fingerprint group is at threshold
        if let Some(ref combined) = fingerprints.1 {
            if !self.is_processed(combined) {
                let count = index.count_ips_by_combined(combined);
                if count >= self.config.threshold {
                    return true;
                }
            }
        }

        false
    }

    fn scan_interval_ms(&self) -> u64 {
        self.config.scan_interval_ms
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    // ========================================================================
    // Test Helpers
    // ========================================================================

    /// Create a test index with IPs sharing a JA4 fingerprint.
    fn create_test_index_ja4(fingerprint: &str, ip_count: usize) -> FingerprintIndex {
        let index = FingerprintIndex::new();
        for i in 0..ip_count {
            let ip = format!("192.168.1.{}", i + 1);
            index.update_entity(&ip, Some(fingerprint), None);
        }
        index
    }

    /// Create a test index with IPs sharing a combined fingerprint.
    fn create_test_index_combined(fingerprint: &str, ip_count: usize) -> FingerprintIndex {
        let index = FingerprintIndex::new();
        for i in 0..ip_count {
            let ip = format!("10.0.0.{}", i + 1);
            index.update_entity(&ip, None, Some(fingerprint));
        }
        index
    }

    /// Create a test index with mixed fingerprint types.
    fn create_mixed_test_index() -> FingerprintIndex {
        let index = FingerprintIndex::new();

        // JA4 group of 4 IPs
        for i in 0..4 {
            index.update_entity(&format!("192.168.1.{}", i + 1), Some("ja4_shared"), None);
        }

        // Combined group of 3 IPs
        for i in 0..3 {
            index.update_entity(&format!("10.0.0.{}", i + 1), None, Some("combined_shared"));
        }

        // Single IP with unique fingerprint
        index.update_entity("172.16.0.1", Some("ja4_unique"), Some("combined_unique"));

        index
    }

    // ========================================================================
    // Constructor Tests
    // ========================================================================

    #[test]
    fn test_new_detector() {
        let detector = SharedFingerprintDetector::new(3);
        assert_eq!(detector.config.threshold, 3);
        assert!((detector.config.base_confidence - 0.85).abs() < 0.001);
        assert_eq!(detector.config.scan_interval_ms, 5000);
        assert_eq!(detector.processed_count(), 0);
    }

    #[test]
    fn test_detector_with_config() {
        let detector = SharedFingerprintDetector::with_config(5, 0.9, 10000);
        assert_eq!(detector.config.threshold, 5);
        assert!((detector.config.base_confidence - 0.9).abs() < 0.001);
        assert_eq!(detector.config.scan_interval_ms, 10000);
    }

    #[test]
    fn test_confidence_clamping() {
        let detector = SharedFingerprintDetector::with_config(2, 1.5, 1000);
        assert!((detector.config.base_confidence - 1.0).abs() < 0.001);

        let detector = SharedFingerprintDetector::with_config(2, -0.5, 1000);
        assert!(detector.config.base_confidence >= 0.0);
    }

    #[test]
    #[should_panic(expected = "Threshold must be at least 2")]
    fn test_threshold_too_low() {
        SharedFingerprintDetector::new(1);
    }

    // ========================================================================
    // Detection Tests - JA4 Fingerprints
    // ========================================================================

    #[test]
    fn test_detect_ja4_group_at_threshold() {
        let index = create_test_index_ja4("shared_ja4", 3);
        let detector = SharedFingerprintDetector::new(3);

        let updates = detector.analyze(&index).unwrap();

        assert_eq!(updates.len(), 1);
        assert!(updates[0].add_correlation_reason.is_some());

        let reason = updates[0].add_correlation_reason.as_ref().unwrap();
        assert_eq!(reason.correlation_type, CorrelationType::HttpFingerprint);
        assert_eq!(reason.evidence.len(), 3);
    }

    #[test]
    fn test_detect_ja4_group_above_threshold() {
        let index = create_test_index_ja4("large_group", 10);
        let detector = SharedFingerprintDetector::new(3);

        let updates = detector.analyze(&index).unwrap();

        assert_eq!(updates.len(), 1);

        let reason = updates[0].add_correlation_reason.as_ref().unwrap();
        assert_eq!(reason.evidence.len(), 10);
        // Larger group should have higher confidence
        assert!(reason.confidence > 0.85);
    }

    #[test]
    fn test_no_detection_below_threshold() {
        let index = create_test_index_ja4("small_group", 2);
        let detector = SharedFingerprintDetector::new(3);

        let updates = detector.analyze(&index).unwrap();

        assert!(updates.is_empty());
    }

    // ========================================================================
    // Detection Tests - Combined Fingerprints
    // ========================================================================

    #[test]
    fn test_detect_combined_group() {
        let index = create_test_index_combined("combined_fp", 4);
        let detector = SharedFingerprintDetector::new(3);

        let updates = detector.analyze(&index).unwrap();

        assert_eq!(updates.len(), 1);

        let reason = updates[0].add_correlation_reason.as_ref().unwrap();
        // Combined fingerprints should have higher confidence than JA4 alone
        assert!(reason.confidence > 0.9);
    }

    #[test]
    fn test_detect_mixed_groups() {
        let index = create_mixed_test_index();
        let detector = SharedFingerprintDetector::new(3);

        let updates = detector.analyze(&index).unwrap();

        // Should detect both JA4 group (4 IPs) and combined group (3 IPs)
        assert_eq!(updates.len(), 2);
    }

    // ========================================================================
    // Duplicate Prevention Tests
    // ========================================================================

    #[test]
    fn test_no_duplicate_campaigns() {
        let index = create_test_index_ja4("repeated_fp", 5);
        let detector = SharedFingerprintDetector::new(3);

        // First analysis should detect
        let updates1 = detector.analyze(&index).unwrap();
        assert_eq!(updates1.len(), 1);

        // Second analysis should not duplicate
        let updates2 = detector.analyze(&index).unwrap();
        assert!(updates2.is_empty());

        assert_eq!(detector.processed_count(), 1);
    }

    #[test]
    fn test_clear_processed() {
        let index = create_test_index_ja4("clearable_fp", 3);
        let detector = SharedFingerprintDetector::new(3);

        // First detection
        let updates1 = detector.analyze(&index).unwrap();
        assert_eq!(updates1.len(), 1);

        // Clear processed
        detector.clear_processed();
        assert_eq!(detector.processed_count(), 0);

        // Should detect again
        let updates2 = detector.analyze(&index).unwrap();
        assert_eq!(updates2.len(), 1);
    }

    // ========================================================================
    // Trigger Tests
    // ========================================================================

    #[test]
    fn test_should_trigger_at_threshold() {
        let index = create_test_index_ja4("trigger_fp", 3);
        let detector = SharedFingerprintDetector::new(3);

        let ip: IpAddr = "192.168.1.1".parse().unwrap();

        // Should trigger - group has reached threshold
        assert!(detector.should_trigger(&ip, &index));
    }

    #[test]
    fn test_should_not_trigger_below_threshold() {
        let index = create_test_index_ja4("small_fp", 2);
        let detector = SharedFingerprintDetector::new(3);

        let ip: IpAddr = "192.168.1.1".parse().unwrap();

        // Should not trigger - below threshold
        assert!(!detector.should_trigger(&ip, &index));
    }

    #[test]
    fn test_should_not_trigger_already_processed() {
        let index = create_test_index_ja4("processed_fp", 5);
        let detector = SharedFingerprintDetector::new(3);

        // Process first
        detector.analyze(&index).unwrap();

        let ip: IpAddr = "192.168.1.1".parse().unwrap();

        // Should not trigger - already processed
        assert!(!detector.should_trigger(&ip, &index));
    }

    #[test]
    fn test_should_not_trigger_unknown_ip() {
        let index = create_test_index_ja4("known_fp", 3);
        let detector = SharedFingerprintDetector::new(3);

        // IP not in the index
        let ip: IpAddr = "10.10.10.10".parse().unwrap();

        assert!(!detector.should_trigger(&ip, &index));
    }

    // ========================================================================
    // Confidence Calculation Tests
    // ========================================================================

    #[test]
    fn test_confidence_ja4_only() {
        let detector = SharedFingerprintDetector::new(3);
        let confidence = detector.calculate_confidence(FingerprintType::Ja4, 3);

        // Base confidence for JA4
        assert!((confidence - 0.85).abs() < 0.001);
    }

    #[test]
    fn test_confidence_combined_higher() {
        let detector = SharedFingerprintDetector::new(3);

        let ja4_conf = detector.calculate_confidence(FingerprintType::Ja4, 3);
        let combined_conf = detector.calculate_confidence(FingerprintType::Combined, 3);

        // Combined should have higher confidence
        assert!(combined_conf > ja4_conf);
        assert!((combined_conf - 0.95).abs() < 0.001);
    }

    #[test]
    fn test_confidence_increases_with_size() {
        let detector = SharedFingerprintDetector::new(3);

        let conf_3 = detector.calculate_confidence(FingerprintType::Ja4, 3);
        let conf_5 = detector.calculate_confidence(FingerprintType::Ja4, 5);
        let conf_10 = detector.calculate_confidence(FingerprintType::Ja4, 10);

        // Confidence should increase with group size
        assert!(conf_5 > conf_3);
        assert!(conf_10 > conf_5);
    }

    #[test]
    fn test_confidence_capped_at_one() {
        let detector = SharedFingerprintDetector::with_config(3, 0.99, 1000);
        let confidence = detector.calculate_confidence(FingerprintType::Combined, 100);

        // Should be capped at 1.0
        assert!((confidence - 1.0).abs() < 0.001);
    }

    // ========================================================================
    // Trait Implementation Tests
    // ========================================================================

    #[test]
    fn test_detector_name() {
        let detector = SharedFingerprintDetector::new(3);
        assert_eq!(detector.name(), "shared_fingerprint");
    }

    #[test]
    fn test_detector_scan_interval() {
        let detector = SharedFingerprintDetector::with_config(3, 0.9, 7500);
        assert_eq!(detector.scan_interval_ms(), 7500);
    }

    // ========================================================================
    // Edge Cases
    // ========================================================================

    #[test]
    fn test_empty_index() {
        let index = FingerprintIndex::new();
        let detector = SharedFingerprintDetector::new(3);

        let updates = detector.analyze(&index).unwrap();
        assert!(updates.is_empty());
    }

    #[test]
    fn test_all_unique_fingerprints() {
        let index = FingerprintIndex::new();

        // Each IP has unique fingerprint
        for i in 0..10 {
            index.update_entity(
                &format!("10.0.0.{}", i),
                Some(&format!("unique_fp_{}", i)),
                None,
            );
        }

        let detector = SharedFingerprintDetector::new(3);
        let updates = detector.analyze(&index).unwrap();

        assert!(updates.is_empty());
    }

    #[test]
    fn test_multiple_groups_different_sizes() {
        let index = FingerprintIndex::new();

        // Large group (5 IPs)
        for i in 0..5 {
            index.update_entity(&format!("192.168.1.{}", i), Some("large_group"), None);
        }

        // Medium group (3 IPs)
        for i in 0..3 {
            index.update_entity(&format!("10.0.0.{}", i), Some("medium_group"), None);
        }

        // Small group (2 IPs) - below threshold
        index.update_entity("172.16.0.1", Some("small_group"), None);
        index.update_entity("172.16.0.2", Some("small_group"), None);

        let detector = SharedFingerprintDetector::new(3);
        let updates = detector.analyze(&index).unwrap();

        // Should detect large and medium, but not small
        assert_eq!(updates.len(), 2);
    }

    #[test]
    fn test_ipv6_addresses() {
        let index = FingerprintIndex::new();

        index.update_entity("2001:db8::1", Some("ipv6_shared"), None);
        index.update_entity("2001:db8::2", Some("ipv6_shared"), None);
        index.update_entity("2001:db8::3", Some("ipv6_shared"), None);

        let detector = SharedFingerprintDetector::new(3);
        let updates = detector.analyze(&index).unwrap();

        assert_eq!(updates.len(), 1);

        let reason = updates[0].add_correlation_reason.as_ref().unwrap();
        assert!(reason.evidence.contains(&"2001:db8::1".to_string()));
    }

    // ========================================================================
    // Thread Safety Tests
    // ========================================================================

    #[test]
    fn test_concurrent_analysis() {
        use std::sync::Arc;
        use std::thread;

        let index = Arc::new(create_test_index_ja4("concurrent_fp", 5));
        let detector = Arc::new(SharedFingerprintDetector::new(3));

        let mut handles = vec![];

        // Spawn multiple threads analyzing the same index
        for _ in 0..5 {
            let index = Arc::clone(&index);
            let detector = Arc::clone(&detector);

            handles.push(thread::spawn(move || detector.analyze(&index).unwrap()));
        }

        let mut total_updates = 0;
        for handle in handles {
            let updates = handle.join().unwrap();
            total_updates += updates.len();
        }

        // Only one thread should create the campaign
        assert_eq!(total_updates, 1);
        assert_eq!(detector.processed_count(), 1);
    }

    #[test]
    fn test_concurrent_trigger_checks() {
        use std::sync::Arc;
        use std::thread;

        let index = Arc::new(create_test_index_ja4("trigger_concurrent", 5));
        let detector = Arc::new(SharedFingerprintDetector::new(3));

        let mut handles = vec![];

        // Multiple threads checking should_trigger
        for i in 0..5 {
            let index = Arc::clone(&index);
            let detector = Arc::clone(&detector);
            let ip: IpAddr = format!("192.168.1.{}", i + 1).parse().unwrap();

            handles.push(thread::spawn(move || detector.should_trigger(&ip, &index)));
        }

        let mut triggered_count = 0;
        for handle in handles {
            if handle.join().unwrap() {
                triggered_count += 1;
            }
        }

        // All should return true since nothing is processed yet
        assert!(triggered_count > 0);
    }
}