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
//! Header anomaly types and results.
//!
//! Provides types for header-based anomaly detection:
//! - Anomaly types for different header deviations
//! - Value statistics for learning baseline patterns
//! - Per-endpoint header baselines
//!
//! ## Memory Budget
//! - HeaderBaseline: ~1-2KB per endpoint (depends on header count)
//! - ValueStats: ~48 bytes per header

use std::collections::{HashMap, HashSet};
use std::time::Instant;

use serde::{Deserialize, Serialize};

// ============================================================================
// HeaderAnomaly - Types of header anomalies
// ============================================================================

/// Type of header anomaly detected in a request.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum HeaderAnomaly {
    /// A required header (seen in >95% of baseline) is missing
    MissingRequired {
        /// The missing header name
        header: String,
    },

    /// An unexpected header not seen in baseline is present
    UnexpectedHeader {
        /// The unexpected header name
        header: String,
    },

    /// Header value is anomalous (unusual pattern)
    AnomalousValue {
        /// The header name
        header: String,
        /// The anomalous value
        value: String,
        /// Reason for flagging as anomalous
        reason: String,
    },

    /// Header value entropy is anomalous (too high or too low)
    EntropyAnomaly {
        /// The header name
        header: String,
        /// The observed entropy
        entropy: f64,
        /// The expected mean entropy
        expected_mean: f64,
    },

    /// Header value length is outside expected range
    LengthAnomaly {
        /// The header name
        header: String,
        /// The observed length
        length: usize,
        /// The expected (min, max) range
        expected_range: (usize, usize),
    },
}

impl HeaderAnomaly {
    /// Get the header name associated with this anomaly.
    pub fn header(&self) -> &str {
        match self {
            Self::MissingRequired { header } => header,
            Self::UnexpectedHeader { header } => header,
            Self::AnomalousValue { header, .. } => header,
            Self::EntropyAnomaly { header, .. } => header,
            Self::LengthAnomaly { header, .. } => header,
        }
    }

    /// Get the base risk score for this anomaly type.
    pub fn base_risk(&self) -> u16 {
        match self {
            Self::MissingRequired { .. } => 10,
            Self::UnexpectedHeader { .. } => 5,
            Self::AnomalousValue { .. } => 15,
            Self::EntropyAnomaly { .. } => 20,
            Self::LengthAnomaly { .. } => 10,
        }
    }

    /// Get a human-readable description of this anomaly.
    pub fn description(&self) -> String {
        match self {
            Self::MissingRequired { header } => {
                format!("Required header '{}' is missing", header)
            }
            Self::UnexpectedHeader { header } => {
                format!("Unexpected header '{}' not seen in baseline", header)
            }
            Self::AnomalousValue { header, reason, .. } => {
                format!("Header '{}' has anomalous value: {}", header, reason)
            }
            Self::EntropyAnomaly {
                header,
                entropy,
                expected_mean,
            } => {
                format!(
                    "Header '{}' entropy {:.2} deviates from expected {:.2}",
                    header, entropy, expected_mean
                )
            }
            Self::LengthAnomaly {
                header,
                length,
                expected_range,
            } => {
                format!(
                    "Header '{}' length {} outside expected range [{}, {}]",
                    header, length, expected_range.0, expected_range.1
                )
            }
        }
    }
}

// ============================================================================
// HeaderAnomalyResult - Aggregated anomaly detection result
// ============================================================================

/// Result of header anomaly analysis for a request.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct HeaderAnomalyResult {
    /// List of detected anomalies
    pub anomalies: Vec<HeaderAnomaly>,

    /// Total additional risk score contribution (0-50)
    pub risk_contribution: u16,
}

impl HeaderAnomalyResult {
    /// Create an empty result (no anomalies).
    #[inline]
    pub fn none() -> Self {
        Self {
            anomalies: Vec::new(),
            risk_contribution: 0,
        }
    }

    /// Create a new result with pre-allocated capacity.
    #[inline]
    pub fn new() -> Self {
        Self {
            anomalies: Vec::with_capacity(4),
            risk_contribution: 0,
        }
    }

    /// Add an anomaly and update risk score.
    #[inline]
    pub fn add(&mut self, anomaly: HeaderAnomaly) {
        self.risk_contribution = self
            .risk_contribution
            .saturating_add(anomaly.base_risk())
            .min(50);
        self.anomalies.push(anomaly);
    }

    /// Check if any anomalies were detected.
    #[inline]
    pub fn has_anomalies(&self) -> bool {
        !self.anomalies.is_empty()
    }

    /// Get the number of anomalies.
    #[inline]
    pub fn count(&self) -> usize {
        self.anomalies.len()
    }

    /// Merge another result into this one.
    pub fn merge(&mut self, other: HeaderAnomalyResult) {
        for anomaly in other.anomalies {
            self.add(anomaly);
        }
    }
}

// ============================================================================
// ValueStats - Per-header value statistics
// ============================================================================

/// Statistics for a single header's values.
///
/// Uses Welford's algorithm for online mean/variance calculation.
/// Memory: ~48 bytes per header
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValueStats {
    /// Minimum value length seen
    pub min_length: usize,

    /// Maximum value length seen
    pub max_length: usize,

    /// Running mean of Shannon entropy
    pub entropy_mean: f64,

    /// Running variance of Shannon entropy (using Welford's algorithm)
    pub entropy_variance: f64,

    /// Running M2 for Welford's algorithm (sum of squared differences)
    entropy_m2: f64,

    /// Total samples seen for this header
    pub total_samples: u64,
}

impl ValueStats {
    /// Create new empty value statistics.
    pub fn new() -> Self {
        Self {
            min_length: usize::MAX,
            max_length: 0,
            entropy_mean: 0.0,
            entropy_variance: 0.0,
            entropy_m2: 0.0,
            total_samples: 0,
        }
    }

    /// Update statistics with a new value.
    ///
    /// # Arguments
    /// * `length` - The length of the value
    /// * `entropy` - The Shannon entropy of the value
    #[inline]
    pub fn update(&mut self, length: usize, entropy: f64) {
        // Update length bounds
        self.min_length = self.min_length.min(length);
        self.max_length = self.max_length.max(length);

        // Update entropy statistics using Welford's algorithm
        self.total_samples += 1;
        let delta = entropy - self.entropy_mean;
        self.entropy_mean += delta / self.total_samples as f64;
        let delta2 = entropy - self.entropy_mean;
        self.entropy_m2 += delta * delta2;

        // Recalculate variance
        if self.total_samples >= 2 {
            self.entropy_variance = self.entropy_m2 / self.total_samples as f64;
        }
    }

    /// Get the standard deviation of entropy.
    #[inline]
    pub fn entropy_stddev(&self) -> f64 {
        self.entropy_variance.sqrt()
    }

    /// Check if statistics have enough samples for anomaly detection.
    #[inline]
    pub fn is_mature(&self, min_samples: u64) -> bool {
        self.total_samples >= min_samples
    }

    /// Check if a length is within the expected range (with some tolerance).
    ///
    /// # Arguments
    /// * `length` - The length to check
    /// * `tolerance_factor` - How much to extend the range (e.g., 1.5 = 50% tolerance)
    #[inline]
    pub fn is_length_in_range(&self, length: usize, tolerance_factor: f64) -> bool {
        if self.total_samples == 0 {
            return true; // No baseline yet
        }

        let range = (self.max_length - self.min_length) as f64;
        let tolerance = (range * tolerance_factor).max(10.0) as usize;

        length >= self.min_length.saturating_sub(tolerance)
            && length <= self.max_length.saturating_add(tolerance)
    }

    /// Calculate z-score for an entropy value.
    #[inline]
    pub fn entropy_z_score(&self, entropy: f64) -> f64 {
        if self.entropy_variance <= 0.001 || self.total_samples < 5 {
            return 0.0;
        }
        (entropy - self.entropy_mean) / self.entropy_stddev()
    }
}

impl Default for ValueStats {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// HeaderBaseline - Per-endpoint header baseline
// ============================================================================

/// Learned baseline for headers on a specific endpoint.
///
/// Tracks which headers are required/optional and their value patterns.
/// Memory: ~1-2KB per endpoint (varies with header count)
#[derive(Debug, Clone)]
pub struct HeaderBaseline {
    /// The endpoint path/template
    pub endpoint: String,

    /// Headers seen in >95% of requests (considered required)
    pub required_headers: HashSet<String>,

    /// Headers seen in <95% of requests (considered optional)
    pub optional_headers: HashSet<String>,

    /// Value statistics per header
    pub header_value_stats: HashMap<String, ValueStats>,

    /// Total sample count for this endpoint
    pub sample_count: u64,

    /// Last time this baseline was updated
    pub last_updated: Instant,
}

impl HeaderBaseline {
    /// Create a new empty baseline for an endpoint.
    pub fn new(endpoint: String) -> Self {
        Self {
            endpoint,
            required_headers: HashSet::with_capacity(16),
            optional_headers: HashSet::with_capacity(16),
            header_value_stats: HashMap::with_capacity(16),
            sample_count: 0,
            last_updated: Instant::now(),
        }
    }

    /// Check if baseline has enough samples for anomaly detection.
    #[inline]
    pub fn is_mature(&self, min_samples: u64) -> bool {
        self.sample_count >= min_samples
    }

    /// Get value stats for a header, if available.
    #[inline]
    pub fn get_stats(&self, header: &str) -> Option<&ValueStats> {
        self.header_value_stats.get(&header.to_lowercase())
    }

    /// Check if a header is considered required (>95% frequency).
    #[inline]
    pub fn is_required(&self, header: &str) -> bool {
        self.required_headers.contains(&header.to_lowercase())
    }

    /// Check if a header has been seen before (required or optional).
    #[inline]
    pub fn is_known(&self, header: &str) -> bool {
        let h = header.to_lowercase();
        self.required_headers.contains(&h) || self.optional_headers.contains(&h)
    }

    /// Get the frequency of a header (0.0 to 1.0).
    pub fn header_frequency(&self, header: &str) -> f64 {
        if self.sample_count == 0 {
            return 0.0;
        }

        self.header_value_stats
            .get(&header.to_lowercase())
            .map(|stats| stats.total_samples as f64 / self.sample_count as f64)
            .unwrap_or(0.0)
    }
}

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

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

    // ------------------------------------------------------------------------
    // HeaderAnomaly tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_header_anomaly_header() {
        let missing = HeaderAnomaly::MissingRequired {
            header: "Authorization".to_string(),
        };
        assert_eq!(missing.header(), "Authorization");

        let unexpected = HeaderAnomaly::UnexpectedHeader {
            header: "X-Evil".to_string(),
        };
        assert_eq!(unexpected.header(), "X-Evil");
    }

    #[test]
    fn test_header_anomaly_base_risk() {
        assert_eq!(
            HeaderAnomaly::MissingRequired {
                header: "Auth".to_string()
            }
            .base_risk(),
            10
        );
        assert_eq!(
            HeaderAnomaly::UnexpectedHeader {
                header: "X".to_string()
            }
            .base_risk(),
            5
        );
        assert_eq!(
            HeaderAnomaly::AnomalousValue {
                header: "X".to_string(),
                value: "bad".to_string(),
                reason: "test".to_string()
            }
            .base_risk(),
            15
        );
        assert_eq!(
            HeaderAnomaly::EntropyAnomaly {
                header: "X".to_string(),
                entropy: 7.5,
                expected_mean: 4.0
            }
            .base_risk(),
            20
        );
        assert_eq!(
            HeaderAnomaly::LengthAnomaly {
                header: "X".to_string(),
                length: 1000,
                expected_range: (10, 50)
            }
            .base_risk(),
            10
        );
    }

    #[test]
    fn test_header_anomaly_description() {
        let anomaly = HeaderAnomaly::MissingRequired {
            header: "Content-Type".to_string(),
        };
        let desc = anomaly.description();
        assert!(desc.contains("Content-Type"));
        assert!(desc.contains("missing"));
    }

    // ------------------------------------------------------------------------
    // HeaderAnomalyResult tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_anomaly_result_empty() {
        let result = HeaderAnomalyResult::none();
        assert!(!result.has_anomalies());
        assert_eq!(result.count(), 0);
        assert_eq!(result.risk_contribution, 0);
    }

    #[test]
    fn test_anomaly_result_add() {
        let mut result = HeaderAnomalyResult::new();

        result.add(HeaderAnomaly::MissingRequired {
            header: "Auth".to_string(),
        });
        assert_eq!(result.count(), 1);
        assert_eq!(result.risk_contribution, 10);

        result.add(HeaderAnomaly::EntropyAnomaly {
            header: "Token".to_string(),
            entropy: 7.5,
            expected_mean: 4.0,
        });
        assert_eq!(result.count(), 2);
        assert_eq!(result.risk_contribution, 30);
    }

    #[test]
    fn test_anomaly_result_risk_capped() {
        let mut result = HeaderAnomalyResult::new();

        // Add many high-risk anomalies
        for i in 0..10 {
            result.add(HeaderAnomaly::EntropyAnomaly {
                header: format!("Header-{}", i),
                entropy: 7.5,
                expected_mean: 4.0,
            });
        }

        // Risk should be capped at 50
        assert_eq!(result.risk_contribution, 50);
    }

    #[test]
    fn test_anomaly_result_merge() {
        let mut result1 = HeaderAnomalyResult::new();
        result1.add(HeaderAnomaly::MissingRequired {
            header: "A".to_string(),
        });

        let mut result2 = HeaderAnomalyResult::new();
        result2.add(HeaderAnomaly::UnexpectedHeader {
            header: "B".to_string(),
        });

        result1.merge(result2);
        assert_eq!(result1.count(), 2);
        assert_eq!(result1.risk_contribution, 15); // 10 + 5
    }

    // ------------------------------------------------------------------------
    // ValueStats tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_value_stats_new() {
        let stats = ValueStats::new();
        assert_eq!(stats.min_length, usize::MAX);
        assert_eq!(stats.max_length, 0);
        assert_eq!(stats.entropy_mean, 0.0);
        assert_eq!(stats.total_samples, 0);
    }

    #[test]
    fn test_value_stats_update() {
        let mut stats = ValueStats::new();

        stats.update(10, 4.0);
        assert_eq!(stats.min_length, 10);
        assert_eq!(stats.max_length, 10);
        assert_eq!(stats.total_samples, 1);
        assert!((stats.entropy_mean - 4.0).abs() < 0.001);

        stats.update(20, 5.0);
        assert_eq!(stats.min_length, 10);
        assert_eq!(stats.max_length, 20);
        assert_eq!(stats.total_samples, 2);
        assert!((stats.entropy_mean - 4.5).abs() < 0.001);
    }

    #[test]
    fn test_value_stats_is_mature() {
        let mut stats = ValueStats::new();
        assert!(!stats.is_mature(10));

        for _ in 0..10 {
            stats.update(10, 4.0);
        }
        assert!(stats.is_mature(10));
        assert!(!stats.is_mature(20));
    }

    #[test]
    fn test_value_stats_is_length_in_range() {
        let mut stats = ValueStats::new();

        // No samples yet - everything is in range
        assert!(stats.is_length_in_range(100, 1.5));

        // Add samples with lengths 10-50
        for len in [10, 20, 30, 40, 50] {
            stats.update(len, 4.0);
        }

        // Within range
        assert!(stats.is_length_in_range(30, 1.5));

        // Within tolerance
        assert!(stats.is_length_in_range(5, 1.5));
        assert!(stats.is_length_in_range(60, 1.5));

        // Outside tolerance (assuming tolerance is ~30 with 1.5 factor)
        // Actually with max 10 tolerance, 0 might still be out
    }

    #[test]
    fn test_value_stats_entropy_z_score() {
        let mut stats = ValueStats::new();

        // Not enough samples
        assert_eq!(stats.entropy_z_score(7.0), 0.0);

        // Add samples with varying entropy
        for entropy in [3.5, 4.0, 4.5, 4.0, 4.0] {
            stats.update(10, entropy);
        }

        // At mean should be ~0
        let z = stats.entropy_z_score(stats.entropy_mean);
        assert!(z.abs() < 0.1);

        // Above mean should be positive
        let z = stats.entropy_z_score(stats.entropy_mean + stats.entropy_stddev());
        assert!(z > 0.9 && z < 1.1);
    }

    // ------------------------------------------------------------------------
    // HeaderBaseline tests
    // ------------------------------------------------------------------------

    #[test]
    fn test_header_baseline_new() {
        let baseline = HeaderBaseline::new("/api/users".to_string());
        assert_eq!(baseline.endpoint, "/api/users");
        assert_eq!(baseline.sample_count, 0);
        assert!(baseline.required_headers.is_empty());
        assert!(baseline.optional_headers.is_empty());
    }

    #[test]
    fn test_header_baseline_is_mature() {
        let mut baseline = HeaderBaseline::new("/test".to_string());
        assert!(!baseline.is_mature(10));

        baseline.sample_count = 10;
        assert!(baseline.is_mature(10));
    }

    #[test]
    fn test_header_baseline_is_known() {
        let mut baseline = HeaderBaseline::new("/test".to_string());

        baseline.required_headers.insert("Content-Type".to_string());
        baseline.optional_headers.insert("X-Request-ID".to_string());

        assert!(baseline.is_required("Content-Type"));
        assert!(!baseline.is_required("X-Request-ID"));

        assert!(baseline.is_known("Content-Type"));
        assert!(baseline.is_known("X-Request-ID"));
        assert!(!baseline.is_known("X-Unknown"));
    }

    #[test]
    fn test_header_baseline_header_frequency() {
        let mut baseline = HeaderBaseline::new("/test".to_string());
        baseline.sample_count = 100;

        let mut stats = ValueStats::new();
        for _ in 0..95 {
            stats.update(10, 4.0);
        }
        baseline
            .header_value_stats
            .insert("Content-Type".to_string(), stats);

        let freq = baseline.header_frequency("Content-Type");
        assert!((freq - 0.95).abs() < 0.01);

        // Unknown header
        assert_eq!(baseline.header_frequency("Unknown"), 0.0);
    }
}